From 0fc407774421baca9f9000b4c51b6909909d6194 Mon Sep 17 00:00:00 2001 From: Tuukka Lehtonen Date: Tue, 19 Sep 2017 09:50:05 +0300 Subject: [PATCH] Added cleaner rendering hint to decide text rendering mode: text/paths refs #7491 Change-Id: I0c22413f149b25fcfc16cde2c2f7279a9e0eaa55 --- .../simantics/diagram/elements/TextNode.java | 14 ++++++----- .../scenegraph/g2d/G2DPDFRenderingHints.java | 25 ++++++++++++++++++- .../scenegraph/g2d/nodes/FlagNode.java | 8 ++++-- 3 files changed, 38 insertions(+), 9 deletions(-) diff --git a/bundles/org.simantics.diagram/src/org/simantics/diagram/elements/TextNode.java b/bundles/org.simantics.diagram/src/org/simantics/diagram/elements/TextNode.java index 4554decee..9d39bbfde 100644 --- a/bundles/org.simantics.diagram/src/org/simantics/diagram/elements/TextNode.java +++ b/bundles/org.simantics.diagram/src/org/simantics/diagram/elements/TextNode.java @@ -51,6 +51,7 @@ import org.simantics.scenegraph.LoaderNode; import org.simantics.scenegraph.ScenegraphUtils; import org.simantics.scenegraph.g2d.G2DNode; import org.simantics.scenegraph.g2d.G2DPDFRenderingHints; +import org.simantics.scenegraph.g2d.G2DPDFRenderingHints.TextRenderingMode; import org.simantics.scenegraph.g2d.events.Event; import org.simantics.scenegraph.g2d.events.EventTypes; import org.simantics.scenegraph.g2d.events.KeyEvent.KeyPressedEvent; @@ -710,7 +711,8 @@ public class TextNode extends G2DNode implements IDynamicSelectionPainterNode, L // PDF PdfWriter writer = (PdfWriter) g.getRenderingHint(G2DPDFRenderingHints.KEY_PDF_WRITER); - boolean isRenderingPdf = writer != null; + TextRenderingMode renderingMode = (TextRenderingMode) g.getRenderingHint(G2DPDFRenderingHints.KEY_TEXT_RENDERING_MODE); + boolean renderAsText = writer != null || renderingMode == TextRenderingMode.AS_TEXT; /// PDF Color backgroundColor = hasState(STATE_VALID) ? this.backgroundColor : Color.red; @@ -730,7 +732,7 @@ public class TextNode extends G2DNode implements IDynamicSelectionPainterNode, L // Base text g.setColor(color); - renderText(g, xOffset, isRenderingPdf); + renderText(g, xOffset, renderAsText); Shape clip = g.getClip(); @@ -744,7 +746,7 @@ public class TextNode extends G2DNode implements IDynamicSelectionPainterNode, L g.fill(selShape); g.setColor(Color.WHITE); // #6459: render as text in PDF and paths on screen - if (isRenderingPdf) + if (renderAsText) g.drawString(line.getText(), 0, 0); else line.layout.draw(g, 0, 0); @@ -760,7 +762,7 @@ public class TextNode extends G2DNode implements IDynamicSelectionPainterNode, L if (renderText) { g.setColor(color); - renderText(g, 0, isRenderingPdf); + renderText(g, 0, renderAsText); } } @@ -823,11 +825,11 @@ public class TextNode extends G2DNode implements IDynamicSelectionPainterNode, L } } } - private void renderText(Graphics2D g, float xOffset, boolean isRenderingPdf) { + private void renderText(Graphics2D g, float xOffset, boolean renderAsText) { //g.draw(tightBoundsCache); // DEBUG for (Line line : lines) { // #6459: render as text in PDF and paths on screen - if (isRenderingPdf) + if (renderAsText) g.drawString(line.getText(), line.alignedPosX + xOffset, line.alignedPosY); else line.layout.draw(g, line.alignedPosX + xOffset, line.alignedPosY); diff --git a/bundles/org.simantics.scenegraph/src/org/simantics/scenegraph/g2d/G2DPDFRenderingHints.java b/bundles/org.simantics.scenegraph/src/org/simantics/scenegraph/g2d/G2DPDFRenderingHints.java index ffdcc32ab..0b9a57474 100644 --- a/bundles/org.simantics.scenegraph/src/org/simantics/scenegraph/g2d/G2DPDFRenderingHints.java +++ b/bundles/org.simantics.scenegraph/src/org/simantics/scenegraph/g2d/G2DPDFRenderingHints.java @@ -62,6 +62,29 @@ public final class G2DPDFRenderingHints { return val instanceof FontMapper; } }; - + + /** + * If this hint is not specified, the default interpretation should be + * {@value #AS_PATHS}. + * + * @since 1.31.0 + */ + public static enum TextRenderingMode { + AS_PATHS, + AS_TEXT + } + + /** + * A rendering hint for telling text rendering Simantics G2D scene graph node + * implementations how to render the text: as text or paths. + * + * @since 1.31.0 + */ + public static final Key KEY_TEXT_RENDERING_MODE = new Key(2004) { + @Override + public boolean isCompatibleValue(Object val) { + return val instanceof TextRenderingMode; + } + }; } \ No newline at end of file diff --git a/bundles/org.simantics.scenegraph/src/org/simantics/scenegraph/g2d/nodes/FlagNode.java b/bundles/org.simantics.scenegraph/src/org/simantics/scenegraph/g2d/nodes/FlagNode.java index 598e13009..756804f7d 100644 --- a/bundles/org.simantics.scenegraph/src/org/simantics/scenegraph/g2d/nodes/FlagNode.java +++ b/bundles/org.simantics.scenegraph/src/org/simantics/scenegraph/g2d/nodes/FlagNode.java @@ -28,8 +28,11 @@ import java.util.Arrays; import org.simantics.scenegraph.g2d.G2DNode; import org.simantics.scenegraph.g2d.G2DPDFRenderingHints; +import org.simantics.scenegraph.g2d.G2DPDFRenderingHints.TextRenderingMode; import org.simantics.scenegraph.utils.GeometryUtils; +import com.lowagie.text.pdf.PdfWriter; + public class FlagNode extends G2DNode { private static final long serialVersionUID = -1716729504104107151L; @@ -310,7 +313,8 @@ public class FlagNode extends G2DNode { lineDist *= gScaleRecip; double y = startY; double textAreaWidth = textArea.getWidth() * gScaleRecip; - boolean isRenderingPdf = g.getRenderingHint(G2DPDFRenderingHints.KEY_PDF_WRITER) != null; + boolean renderAsText = g.getRenderingHint(G2DPDFRenderingHints.KEY_PDF_WRITER) != null + || g.getRenderingHint(G2DPDFRenderingHints.KEY_TEXT_RENDERING_MODE) == TextRenderingMode.AS_TEXT; for (int i = 0; i < flagText.length; ++i) { //String line = flagText[i]; @@ -343,7 +347,7 @@ public class FlagNode extends G2DNode { System.out.println(" DRAW: '" + flagText[i] + "' with " + g.getTransform()); // #6459: render as text in PDF and paths on screen - if (isRenderingPdf) + if (renderAsText) g.drawString(flagText[i], (float) x, (float) y); else textLayout[i].draw(g, (float) x, (float) y); -- 2.47.1