Skip to content

Commit 1650542

Browse files
committed
Better handling of rendering labels for salts where we have a large component and an abbreviated salt.
1 parent 04f9914 commit 1650542

File tree

4 files changed

+68
-15
lines changed

4 files changed

+68
-15
lines changed

base/standard/src/main/java/org/openscience/cdk/geometry/GeometryUtil.java

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -333,26 +333,23 @@ public static double[] get2DDimension(IAtomContainer atomCon) {
333333
}
334334

335335
/**
336-
* Returns the minimum and maximum X and Y coordinates of the atoms in the
337-
* AtomContainer. The output is returned as: <pre>
336+
* Returns the minimum and maximum X and Y coordinates of the atoms.
337+
* The output is returned as: <pre>
338338
* minmax[0] = minX;
339339
* minmax[1] = minY;
340340
* minmax[2] = maxX;
341341
* minmax[3] = maxY;
342342
* </pre>
343-
* See comment for center(IAtomContainer atomCon, Dimension areaDim, HashMap
344-
* renderingCoordinates) for details on coordinate sets
345343
*
346-
* @param container Description of the Parameter
344+
* @param atoms the atoms.
347345
* @return An four int array as defined above.
348346
*/
349-
public static double[] getMinMax(IAtomContainer container) {
347+
public static double[] getMinMax(Iterable<IAtom> atoms) {
350348
double maxX = -Double.MAX_VALUE;
351349
double maxY = -Double.MAX_VALUE;
352350
double minX = Double.MAX_VALUE;
353351
double minY = Double.MAX_VALUE;
354-
for (int i = 0; i < container.getAtomCount(); i++) {
355-
IAtom atom = container.getAtom(i);
352+
for (IAtom atom : atoms) {
356353
if (atom.getPoint2d() != null) {
357354
if (atom.getPoint2d().x > maxX) {
358355
maxX = atom.getPoint2d().x;
@@ -376,6 +373,24 @@ public static double[] getMinMax(IAtomContainer container) {
376373
return minmax;
377374
}
378375

376+
/**
377+
* Returns the minimum and maximum X and Y coordinates of the atoms in the
378+
* AtomContainer. The output is returned as: <pre>
379+
* minmax[0] = minX;
380+
* minmax[1] = minY;
381+
* minmax[2] = maxX;
382+
* minmax[3] = maxY;
383+
* </pre>
384+
* See comment for center(IAtomContainer atomCon, Dimension areaDim, HashMap
385+
* renderingCoordinates) for details on coordinate sets
386+
*
387+
* @param container Description of the Parameter
388+
* @return An four int array as defined above.
389+
*/
390+
public static double[] getMinMax(IAtomContainer container) {
391+
return getMinMax(container.atoms());
392+
}
393+
379394
/**
380395
* Translates a molecule from the origin to a new point denoted by a vector. See comment for
381396
* center(IAtomContainer atomCon, Dimension areaDim, HashMap renderingCoordinates) for details

display/renderbasic/src/main/java/org/openscience/cdk/renderer/generators/standard/AbbreviationLabel.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,8 @@ static boolean parse(String label, List<String> tokens) {
191191
continue;
192192
}
193193

194-
if (c == '/' || c == '·') {
194+
// separators
195+
if (c == '/' || c == '·' || c == '.' || c == '•') {
195196
tokens.add(Character.toString(c));
196197
i++;
197198
continue;

display/renderbasic/src/main/java/org/openscience/cdk/renderer/generators/standard/StandardSgroupGenerator.java

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@
6161
*/
6262
final class StandardSgroupGenerator {
6363

64-
public static final double EQUIV_THRESHOLD = 0.1;
64+
public static final double EQUIV_THRESHOLD = 0.1;
65+
public static final char BULLET = '•';
6566
private final double stroke;
6667
private final double scale;
6768
private final double bracketDepth;
@@ -358,11 +359,42 @@ private IRenderingElement generateAbbreviationSgroup(IAtomContainer mol, Sgroup
358359
final StandardGenerator.HighlightStyle style = parameters.get(StandardGenerator.Highlighting.class);
359360
final double glowWidth = parameters.get(StandardGenerator.OuterGlowWidth.class);
360361

361-
final Point2d labelCoords = GeometryUtil.get2DCenter(sgroupAtoms);
362+
final Point2d labelLocation;
363+
if (mol.getAtomCount() == sgroup.getAtoms().size()) {
364+
labelLocation = GeometryUtil.get2DCenter(sgroupAtoms);
365+
} else {
366+
// contraction of part of a fragment, e.g. SALT
367+
// here we work out the point we want to place the contract relative
368+
// to the SGroup Atoms
369+
labelLocation = new Point2d();
370+
final Point2d sgrpCenter = GeometryUtil.get2DCenter(sgroupAtoms);
371+
final Point2d molCenter = GeometryUtil.get2DCenter(mol);
372+
final double[] minMax = GeometryUtil.getMinMax(sgroupAtoms);
373+
double xDiff = sgrpCenter.x - molCenter.x;
374+
double yDiff = sgrpCenter.y - molCenter.y;
375+
if (xDiff > 0.1) {
376+
labelLocation.x = minMax[0]; // min x
377+
label = BULLET + label;
378+
}
379+
else if (xDiff < -0.1) {
380+
labelLocation.x = minMax[2]; // max x
381+
label = label + BULLET;
382+
}
383+
else {
384+
labelLocation.x = sgrpCenter.x;
385+
label = BULLET + label;
386+
}
387+
if (yDiff > 0.1)
388+
labelLocation.y = minMax[1]; // min y
389+
else if (yDiff < -0.1)
390+
labelLocation.y = minMax[3]; // max y
391+
else
392+
labelLocation.y = sgrpCenter.y;
393+
}
362394

363395
ElementGroup labelgroup = new ElementGroup();
364396
for (Shape outline : atomGenerator.generateAbbreviatedSymbol(label, HydrogenPosition.Right)
365-
.center(labelCoords.x, labelCoords.y)
397+
.center(labelLocation.x, labelLocation.y)
366398
.resize(1 / scale, 1 / -scale)
367399
.getOutlines()) {
368400
if (highlight != null && style == StandardGenerator.HighlightStyle.Colored) {

display/renderbasic/src/test/java/org/openscience/cdk/renderer/generators/standard/AbbreviationLabelTest.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,7 @@
3232

3333
import static org.hamcrest.CoreMatchers.hasItems;
3434
import static org.hamcrest.CoreMatchers.is;
35-
import static org.junit.Assert.assertFalse;
36-
import static org.junit.Assert.assertThat;
37-
import static org.junit.Assert.assertTrue;
35+
import static org.junit.Assert.*;
3836

3937
public class AbbreviationLabelTest {
4038

@@ -234,6 +232,13 @@ public void formatOPO3H2() {
234232
assertThat(texts.get(3).style, is(-1));
235233
}
236234

235+
@Test
236+
public void hydrate() {
237+
List<String> tokens = new ArrayList<>();
238+
AbbreviationLabel.parse("•H2O", tokens);
239+
assertThat(tokens, is(Arrays.asList("•", "H2", "O")));
240+
}
241+
237242
@Test
238243
public void het() {
239244
// 'Het' not 'He'lium and 't'erts

0 commit comments

Comments
 (0)