Skip to content

Commit 89f67fe

Browse files
committed
Allow the font style of pseudo-atoms to be configured.
1 parent 2e8fd8f commit 89f67fe

File tree

3 files changed

+52
-30
lines changed

3 files changed

+52
-30
lines changed

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

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import org.openscience.cdk.renderer.RendererModel;
3434
import org.openscience.cdk.renderer.generators.standard.AbbreviationLabel.FormattedText;
3535
import org.openscience.cdk.renderer.generators.standard.StandardGenerator.DeuteriumSymbol;
36+
import org.openscience.cdk.renderer.generators.standard.StandardGenerator.PseudoFontStyle;
3637

3738
import java.awt.Font;
3839
import java.awt.geom.Rectangle2D;
@@ -125,13 +126,11 @@ AtomSymbol generateSymbol(IAtomContainer container, IAtom atom, HydrogenPosition
125126
int charge = unboxSafely(pAtom.getFormalCharge(), 0);
126127
int hcnt = unboxSafely(pAtom.getImplicitHydrogenCount(), 0);
127128
int nrad = container.getConnectedSingleElectronsCount(atom);
128-
if (mass != 0 || charge != 0 || hcnt != 0) {
129-
return generatePeriodicSymbol(0, hcnt,
130-
mass, charge,
131-
nrad, position, model);
132-
}
129+
return generatePeriodicSymbol(0, hcnt,
130+
mass, charge,
131+
nrad, position, model);
133132
}
134-
return generatePseudoSymbol(accessPseudoLabel(pAtom, "?"), position);
133+
return generatePseudoSymbol(accessPseudoLabel(pAtom, "?"), position, model);
135134
}
136135
else
137136
return null; // attach point drawn in bond generator
@@ -159,10 +158,16 @@ AtomSymbol generateSymbol(IAtomContainer container, IAtom atom, HydrogenPosition
159158
*
160159
* @return the atom symbol
161160
*/
162-
AtomSymbol generatePseudoSymbol(String label, HydrogenPosition position) {
161+
AtomSymbol generatePseudoSymbol(String label, HydrogenPosition position, RendererModel model) {
162+
163+
final Font stylisedFont;
164+
if (model != null && model.get(PseudoFontStyle.class) != 0) {
165+
Integer style = model.get(PseudoFontStyle.class);
166+
stylisedFont = font.deriveFont(style);
167+
} else {
168+
stylisedFont = font;
169+
}
163170

164-
final Font italicFont = font.deriveFont(Font.BOLD)
165-
.deriveFont(Font.ITALIC);
166171
List<TextOutline> outlines = new ArrayList<>(3);
167172

168173
int beg = 0;
@@ -177,7 +182,7 @@ AtomSymbol generatePseudoSymbol(String label, HydrogenPosition position) {
177182
pos++;
178183

179184
if (pos > beg) {
180-
outlines.add(new TextOutline(label.substring(beg, pos), italicFont));
185+
outlines.add(new TextOutline(label.substring(beg, pos), stylisedFont));
181186
beg = pos;
182187
// 2a etc.
183188
while (pos < len && isDigit(label.charAt(pos)))
@@ -186,7 +191,7 @@ AtomSymbol generatePseudoSymbol(String label, HydrogenPosition position) {
186191
pos++;
187192

188193
if (pos > beg) {
189-
TextOutline outline = new TextOutline(label.substring(beg, pos), italicFont);
194+
TextOutline outline = new TextOutline(label.substring(beg, pos), stylisedFont);
190195
outline = outline.resize(scriptSize, scriptSize);
191196
outline = positionSuperscript(outlines.get(0), outline);
192197
outlines.add(outline);
@@ -221,7 +226,7 @@ AtomSymbol generatePseudoSymbol(String label, HydrogenPosition position) {
221226
}
222227

223228
if (pos < len) {
224-
return new AtomSymbol(new TextOutline(label, italicFont), Collections.<TextOutline>emptyList());
229+
return new AtomSymbol(new TextOutline(label, stylisedFont), Collections.<TextOutline>emptyList());
225230
} else {
226231
TextOutline outline = null;
227232
TextOutline ref = outlines.get(outlines.size()-1);
@@ -255,7 +260,7 @@ AtomSymbol generatePseudoSymbol(String label, HydrogenPosition position) {
255260
return new AtomSymbol(outlines.get(0),
256261
outlines.subList(1, outlines.size()));
257262
} else {
258-
return new AtomSymbol(new TextOutline(label, italicFont), Collections.<TextOutline>emptyList());
263+
return new AtomSymbol(new TextOutline(label, stylisedFont), Collections.<TextOutline>emptyList());
259264
}
260265
}
261266

@@ -376,10 +381,12 @@ AtomSymbol generatePeriodicSymbol(final int number, final int hydrogens, int mas
376381
final int unpaired, HydrogenPosition position,
377382
RendererModel opts) {
378383

384+
Font myfont = font;
379385
final String label;
380386
switch (number) {
381387
case 0:
382388
label = "*";
389+
myfont = font.deriveFont(Font.BOLD);
383390
break;
384391
case 1:
385392
if (mass == 2 && opts.get(DeuteriumSymbol.class)) {
@@ -393,7 +400,7 @@ AtomSymbol generatePeriodicSymbol(final int number, final int hydrogens, int mas
393400
label = Elements.ofNumber(number).symbol();
394401
break;
395402
}
396-
TextOutline element = new TextOutline(label, font);
403+
TextOutline element = new TextOutline(label, myfont);
397404
TextOutline hydrogenAdjunct = defaultHydrogenLabel;
398405

399406
// the hydrogen count, charge, and mass adjuncts are script size

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

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -166,15 +166,15 @@ public static enum HighlightStyle {
166166
}
167167

168168
private final IGeneratorParameter<?> atomColor = new AtomColor(), visibility = new Visibility(),
169-
strokeRatio = new StrokeRatio(), separationRatio = new BondSeparation(), wedgeRatio = new WedgeRatio(),
170-
marginRatio = new SymbolMarginRatio(), hatchSections = new HashSpacing(), dashSections = new DashSection(),
171-
waveSections = new WaveSpacing(), fancyBoldWedges = new FancyBoldWedges(),
172-
fancyHashedWedges = new FancyHashedWedges(), highlighting = new Highlighting(),
173-
glowWidth = new OuterGlowWidth(), annCol = new AnnotationColor(), annDist = new AnnotationDistance(),
174-
annFontSize = new AnnotationFontScale(), sgroupBracketDepth = new SgroupBracketDepth(),
175-
sgroupFontScale = new SgroupFontScale(), omitMajorIsotopes = new OmitMajorIsotopes(),
176-
forceDelocalised = new ForceDelocalisedBondDisplay(), delocaliseDonuts = new DelocalisedDonutsBondDisplay(),
177-
deuteriumSymbol = new DeuteriumSymbol();
169+
strokeRatio = new StrokeRatio(), separationRatio = new BondSeparation(), wedgeRatio = new WedgeRatio(),
170+
marginRatio = new SymbolMarginRatio(), hatchSections = new HashSpacing(), dashSections = new DashSection(),
171+
waveSections = new WaveSpacing(), fancyBoldWedges = new FancyBoldWedges(),
172+
fancyHashedWedges = new FancyHashedWedges(), highlighting = new Highlighting(),
173+
glowWidth = new OuterGlowWidth(), annCol = new AnnotationColor(), annDist = new AnnotationDistance(),
174+
annFontSize = new AnnotationFontScale(), sgroupBracketDepth = new SgroupBracketDepth(),
175+
sgroupFontScale = new SgroupFontScale(), omitMajorIsotopes = new OmitMajorIsotopes(),
176+
forceDelocalised = new ForceDelocalisedBondDisplay(), delocaliseDonuts = new DelocalisedDonutsBondDisplay(),
177+
deuteriumSymbol = new DeuteriumSymbol(), pseudoFontStyle = new PseudoFontStyle();
178178

179179
/**
180180
* Create a new standard generator that utilises the specified font to display atom symbols.
@@ -626,7 +626,7 @@ public List<IGeneratorParameter<?>> getParameters() {
626626
return Arrays.asList(atomColor, visibility, strokeRatio, separationRatio, wedgeRatio, marginRatio,
627627
hatchSections, dashSections, waveSections, fancyBoldWedges, fancyHashedWedges, highlighting, glowWidth,
628628
annCol, annDist, annFontSize, sgroupBracketDepth, sgroupFontScale, omitMajorIsotopes, forceDelocalised, delocaliseDonuts,
629-
deuteriumSymbol);
629+
deuteriumSymbol, pseudoFontStyle);
630630
}
631631

632632
static String getAnnotationLabel(IChemObject chemObject) {
@@ -1203,4 +1203,19 @@ public Boolean getDefault() {
12031203
return true;
12041204
}
12051205
}
1206+
1207+
/**
1208+
* The default font style for pseudo-atoms, is Bold and Italic. This
1209+
* allows one to distinguish a 'Y/W' for an R group 'Y' Yttrium etc. To
1210+
* render symbols the same as other atoms set this to '0'.
1211+
*/
1212+
public static final class PseudoFontStyle
1213+
extends AbstractGeneratorParameter<Integer> {
1214+
1215+
/**{@inheritDoc} */
1216+
@Override
1217+
public Integer getDefault() {
1218+
return 3; // Font.BOLD|Font.ITALIC;
1219+
}
1220+
}
12061221
}

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -356,42 +356,42 @@ public void accessRgroupPseudoLabel() throws Exception {
356356

357357
@Test
358358
public void numberedRgroupSymbol() {
359-
AtomSymbol atomSymbol = atomGenerator.generatePseudoSymbol("R1", HydrogenPosition.Right);
359+
AtomSymbol atomSymbol = atomGenerator.generatePseudoSymbol("R1", HydrogenPosition.Right, null);
360360
List<Shape> shapes = atomSymbol.getOutlines();
361361
assertThat(shapes.size(), is(2));
362362
}
363363

364364
@Test
365365
public void RgroupSymbol2A() {
366-
AtomSymbol atomSymbol = atomGenerator.generatePseudoSymbol("R2a", HydrogenPosition.Right);
366+
AtomSymbol atomSymbol = atomGenerator.generatePseudoSymbol("R2a", HydrogenPosition.Right, null);
367367
List<Shape> shapes = atomSymbol.getOutlines();
368368
assertThat(shapes.size(), is(2));
369369
}
370370

371371
@Test
372372
public void RgroupSymbolY() {
373-
AtomSymbol atomSymbol = atomGenerator.generatePseudoSymbol("Y1a2", HydrogenPosition.Right);
373+
AtomSymbol atomSymbol = atomGenerator.generatePseudoSymbol("Y1a2", HydrogenPosition.Right, null);
374374
List<Shape> shapes = atomSymbol.getOutlines();
375375
assertThat(shapes.size(), is(1));
376376
}
377377

378378
@Test
379379
public void RgroupSymbolPrime() {
380-
AtomSymbol atomSymbol = atomGenerator.generatePseudoSymbol("R'", HydrogenPosition.Right);
380+
AtomSymbol atomSymbol = atomGenerator.generatePseudoSymbol("R'", HydrogenPosition.Right, null);
381381
List<Shape> shapes = atomSymbol.getOutlines();
382382
assertThat(shapes.size(), is(2));
383383
}
384384

385385
@Test
386386
public void RgroupSymbolNumberedPrime() {
387-
AtomSymbol atomSymbol = atomGenerator.generatePseudoSymbol("R2'", HydrogenPosition.Right);
387+
AtomSymbol atomSymbol = atomGenerator.generatePseudoSymbol("R2'", HydrogenPosition.Right, null);
388388
List<Shape> shapes = atomSymbol.getOutlines();
389389
assertThat(shapes.size(), is(3));
390390
}
391391

392392
@Test
393393
public void pseudoSymbol() {
394-
AtomSymbol atomSymbol = atomGenerator.generatePseudoSymbol("Protein", HydrogenPosition.Right);
394+
AtomSymbol atomSymbol = atomGenerator.generatePseudoSymbol("Protein", HydrogenPosition.Right, null);
395395
List<Shape> shapes = atomSymbol.getOutlines();
396396
assertThat(shapes.size(), is(1));
397397
}

0 commit comments

Comments
 (0)