Skip to content

Commit

Permalink
feat: when generating SVG check that coordinates are not all 0 (#174)
Browse files Browse the repository at this point in the history
* feat: when generating SVG check that coordinates are not all 0

* revert: openchemlib change
  • Loading branch information
lpatiny authored Oct 6, 2023
1 parent 73ab2c0 commit eded253
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
16 changes: 16 additions & 0 deletions __tests__/molecule.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,22 @@ describe('Molecule', () => {
expect(svg).toContain('stroke-width="2"');
});

it('toSVG wrong coordinates (all 0)', () => {
const mol = Molecule.fromSmiles('CCOCCO');
for (let i = 0; i < mol.getAllAtoms(); i++) {
mol.setAtomX(i, 0);
mol.setAtomY(i, 0);
mol.setAtomZ(i, 0);
}
let svg = mol.toSVG(300, 150, 'myId');
for (let i = 0; i < mol.getAllAtoms(); i++) {
expect(mol.getAtomX(i)).toBe(0);
expect(mol.getAtomY(i)).toBe(0);
expect(mol.getAtomZ(i)).toBe(0);
}
expect(svg).toContain('width="300px" height="150px"');
});

it('toSVG with autoCrop', () => {
const mol = Molecule.fromSmiles('CCOCCO');
let svg = mol.toSVG(300, 150, 'myId', { autoCrop: true });
Expand Down
17 changes: 16 additions & 1 deletion src/com/actelion/research/gwt/minimal/JSMolecule.java
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,23 @@ public native String toSVG(int width, int height, String id, JavaScriptObject op

private String getSVG(int width, int height, float factorTextSize, boolean autoCrop,
int autoCropMargin, String id, JavaScriptObject options) {

boolean degenerated = true;
for (int i = 0; i < oclMolecule.getAllAtoms() - 1; i++) {
if ((oclMolecule.getAtomX(i) != oclMolecule.getAtomX(i + 1))
|| (oclMolecule.getAtomY(i) != oclMolecule.getAtomY(i + 1))) {
degenerated = false;
break;
}
}

StereoMolecule mol = degenerated ? oclMolecule.getCompactCopy() : oclMolecule;
if (degenerated) {
new CoordinateInventor(0).invent(mol);
}

int mode = Util.getDisplayMode(options);
SVGDepictor d = new SVGDepictor(oclMolecule, mode, id);
SVGDepictor d = new SVGDepictor(mol, mode, id);
d.setFactorTextSize(factorTextSize);
d.validateView(null, new GenericRectangle(0, 0, width, height),
AbstractDepictor.cModeInflateToMaxAVBL);
Expand Down

0 comments on commit eded253

Please sign in to comment.