From c6f40e32b1d9e05d163fb14a738bddf4f1162150 Mon Sep 17 00:00:00 2001 From: matthew16550 Date: Sun, 19 Sep 2021 20:22:54 +1000 Subject: [PATCH] Extract TextBlockUtils.createTextLayout() methods --- .../plantuml/graphic/TextBlockUtils.java | 13 ++++++++++--- .../plantuml/ugraphic/FontChecker.java | 6 +++--- .../ugraphic/eps/DriverCenteredCharacterEps.java | 5 +++-- .../plantuml/ugraphic/eps/DriverTextEps.java | 12 +++--------- .../ugraphic/g2d/DriverTextAsPathG2d.java | 9 ++++----- .../plantuml/ugraphic/g2d/UGraphicG2d.java | 3 +-- .../ugraphic/svg/DriverCenteredCharacterSvg.java | 5 +++-- .../ugraphic/svg/DriverTextAsPathSvg.java | 15 ++++----------- .../plantuml/ugraphic/svg/UGraphicSvg.java | 3 +-- .../tikz/DriverCenteredCharacterTikz.java | 5 +++-- 10 files changed, 35 insertions(+), 41 deletions(-) diff --git a/src/net/sourceforge/plantuml/graphic/TextBlockUtils.java b/src/net/sourceforge/plantuml/graphic/TextBlockUtils.java index bfd1c5800..09128e3fb 100644 --- a/src/net/sourceforge/plantuml/graphic/TextBlockUtils.java +++ b/src/net/sourceforge/plantuml/graphic/TextBlockUtils.java @@ -35,10 +35,8 @@ */ package net.sourceforge.plantuml.graphic; -import java.awt.Font; -import java.awt.FontMetrics; import java.awt.font.FontRenderContext; -import java.awt.font.LineMetrics; +import java.awt.font.TextLayout; import java.awt.geom.Dimension2D; import java.awt.geom.Point2D; import java.awt.geom.Rectangle2D; @@ -62,6 +60,7 @@ import net.sourceforge.plantuml.ugraphic.UFont; import net.sourceforge.plantuml.ugraphic.UGraphic; import net.sourceforge.plantuml.ugraphic.UImage; import net.sourceforge.plantuml.ugraphic.UStroke; +import net.sourceforge.plantuml.ugraphic.UText; import net.sourceforge.plantuml.ugraphic.color.HColor; import net.sourceforge.plantuml.ugraphic.color.HColorUtils; @@ -202,6 +201,14 @@ public class TextBlockUtils { return FileFormat.gg.getFontRenderContext(); } + public static TextLayout createTextLayout(UText shape) { + return createTextLayout(shape.getFontConfiguration().getFont(), shape.getText()); + } + + public static TextLayout createTextLayout(UFont font, String string) { + return new TextLayout(string, font.getUnderlayingFont(), getFontRenderContext()); + } + public static TextBlock fullInnerPosition(final TextBlock bloc, final String display) { return new TextBlock() { diff --git a/src/net/sourceforge/plantuml/ugraphic/FontChecker.java b/src/net/sourceforge/plantuml/ugraphic/FontChecker.java index c30d4473d..3c38fa092 100644 --- a/src/net/sourceforge/plantuml/ugraphic/FontChecker.java +++ b/src/net/sourceforge/plantuml/ugraphic/FontChecker.java @@ -35,6 +35,7 @@ */ package net.sourceforge.plantuml.ugraphic; +import static net.sourceforge.plantuml.graphic.TextBlockUtils.createTextLayout; import static net.sourceforge.plantuml.ugraphic.ImageBuilder.plainPngBuilder; import java.awt.Font; @@ -54,7 +55,6 @@ import javax.xml.transform.TransformerException; import net.sourceforge.plantuml.Dimension2DDouble; import net.sourceforge.plantuml.graphic.FontConfiguration; -import net.sourceforge.plantuml.graphic.TextBlockUtils; import net.sourceforge.plantuml.graphic.UDrawable; import net.sourceforge.plantuml.security.ImageIO; import net.sourceforge.plantuml.security.SFile; @@ -102,7 +102,7 @@ public class FontChecker { } public String getCharDesc(char c) { - final TextLayout t = new TextLayout("" + c, font.getUnderlayingFont(), TextBlockUtils.getFontRenderContext()); + final TextLayout t = createTextLayout(font, "" + c); final Shape sh = t.getOutline(null); final double current[] = new double[6]; final PathIterator it = sh.getPathIterator(null); @@ -120,7 +120,7 @@ public class FontChecker { } public String getCharDescVerbose(char c) { - final TextLayout t = new TextLayout("" + c, font.getUnderlayingFont(), TextBlockUtils.getFontRenderContext()); + final TextLayout t = createTextLayout(font, "" + c); final Shape sh = t.getOutline(null); final double current[] = new double[6]; final PathIterator it = sh.getPathIterator(null); diff --git a/src/net/sourceforge/plantuml/ugraphic/eps/DriverCenteredCharacterEps.java b/src/net/sourceforge/plantuml/ugraphic/eps/DriverCenteredCharacterEps.java index 0d27f3b50..c7e774f14 100644 --- a/src/net/sourceforge/plantuml/ugraphic/eps/DriverCenteredCharacterEps.java +++ b/src/net/sourceforge/plantuml/ugraphic/eps/DriverCenteredCharacterEps.java @@ -34,10 +34,11 @@ */ package net.sourceforge.plantuml.ugraphic.eps; +import static net.sourceforge.plantuml.graphic.TextBlockUtils.createTextLayout; + import java.awt.font.TextLayout; import net.sourceforge.plantuml.eps.EpsGraphics; -import net.sourceforge.plantuml.graphic.TextBlockUtils; import net.sourceforge.plantuml.graphic.UnusedSpace; import net.sourceforge.plantuml.ugraphic.UCenteredCharacter; import net.sourceforge.plantuml.ugraphic.UDriver; @@ -57,7 +58,7 @@ public class DriverCenteredCharacterEps implements UDriver { final double xpos = x - unusedSpace.getCenterX() - 0.5; final double ypos = y - unusedSpace.getCenterY() - 0.5; - final TextLayout t = new TextLayout("" + c, font.getUnderlayingFont(), TextBlockUtils.getFontRenderContext()); + final TextLayout t = createTextLayout(font, "" + c); eps.setStrokeColor(mapper.toColor(param.getColor())); DriverTextEps.drawPathIterator(eps, xpos, ypos, t.getOutline(null)); diff --git a/src/net/sourceforge/plantuml/ugraphic/eps/DriverTextEps.java b/src/net/sourceforge/plantuml/ugraphic/eps/DriverTextEps.java index 1da5d2ddb..6a538e9d9 100644 --- a/src/net/sourceforge/plantuml/ugraphic/eps/DriverTextEps.java +++ b/src/net/sourceforge/plantuml/ugraphic/eps/DriverTextEps.java @@ -34,9 +34,10 @@ */ package net.sourceforge.plantuml.ugraphic.eps; +import static net.sourceforge.plantuml.graphic.TextBlockUtils.createTextLayout; + import java.awt.Color; import java.awt.Shape; -import java.awt.font.FontRenderContext; import java.awt.font.TextLayout; import java.awt.geom.PathIterator; import java.util.ArrayList; @@ -49,12 +50,10 @@ import net.sourceforge.plantuml.eps.EpsStrategy; import net.sourceforge.plantuml.graphic.FontConfiguration; import net.sourceforge.plantuml.graphic.FontStyle; import net.sourceforge.plantuml.graphic.StringBounder; -import net.sourceforge.plantuml.graphic.TextBlockUtils; import net.sourceforge.plantuml.ugraphic.ClipContainer; import net.sourceforge.plantuml.ugraphic.MinMax; import net.sourceforge.plantuml.ugraphic.UClip; import net.sourceforge.plantuml.ugraphic.UDriver; -import net.sourceforge.plantuml.ugraphic.UFont; import net.sourceforge.plantuml.ugraphic.UParam; import net.sourceforge.plantuml.ugraphic.UShape; import net.sourceforge.plantuml.ugraphic.UText; @@ -66,13 +65,11 @@ public class DriverTextEps implements UDriver { private final StringBounder stringBounder; private final ClipContainer clipContainer; - private final FontRenderContext fontRenderContext; private final EpsStrategy strategy; public DriverTextEps(ClipContainer clipContainer, EpsStrategy strategy) { this.stringBounder = FileFormat.PNG.getDefaultStringBounder(); this.clipContainer = clipContainer; - this.fontRenderContext = TextBlockUtils.getFontRenderContext(); this.strategy = strategy; } @@ -95,10 +92,7 @@ public class DriverTextEps implements UDriver { return; } - final UFont font = fontConfiguration.getFont(); - - final TextLayout textLayout = new TextLayout(shape.getText(), font.getUnderlayingFont(), fontRenderContext); - // System.err.println("text=" + shape.getText()); + final TextLayout textLayout = createTextLayout(shape); MinMax dim = null; diff --git a/src/net/sourceforge/plantuml/ugraphic/g2d/DriverTextAsPathG2d.java b/src/net/sourceforge/plantuml/ugraphic/g2d/DriverTextAsPathG2d.java index ed5a1eadb..d9aacf87c 100644 --- a/src/net/sourceforge/plantuml/ugraphic/g2d/DriverTextAsPathG2d.java +++ b/src/net/sourceforge/plantuml/ugraphic/g2d/DriverTextAsPathG2d.java @@ -35,12 +35,13 @@ */ package net.sourceforge.plantuml.ugraphic.g2d; +import static net.sourceforge.plantuml.graphic.TextBlockUtils.createTextLayout; + import java.awt.BasicStroke; import java.awt.Color; import java.awt.FontMetrics; import java.awt.Graphics2D; import java.awt.GraphicsEnvironment; -import java.awt.font.FontRenderContext; import java.awt.font.TextLayout; import java.awt.geom.Dimension2D; import java.awt.geom.Rectangle2D; @@ -63,11 +64,9 @@ import net.sourceforge.plantuml.ugraphic.color.HColor; public class DriverTextAsPathG2d implements UDriver { private final EnsureVisible visible; - private final FontRenderContext fontRenderContext; - public DriverTextAsPathG2d(EnsureVisible visible, FontRenderContext fontRenderContext) { + public DriverTextAsPathG2d(EnsureVisible visible) { this.visible = visible; - this.fontRenderContext = fontRenderContext; } private static void printFont() { @@ -99,7 +98,7 @@ public class DriverTextAsPathG2d implements UDriver { g2d.setFont(font.getUnderlayingFont()); g2d.setColor(mapper.toColor(fontConfiguration.getColor())); - final TextLayout t = new TextLayout(shape.getText(), font.getUnderlayingFont(), fontRenderContext); + final TextLayout t = createTextLayout(font, shape.getText()); g2d.translate(x, y); g2d.fill(t.getOutline(null)); g2d.translate(-x, -y); diff --git a/src/net/sourceforge/plantuml/ugraphic/g2d/UGraphicG2d.java b/src/net/sourceforge/plantuml/ugraphic/g2d/UGraphicG2d.java index 77f3c6ccb..c7174df16 100644 --- a/src/net/sourceforge/plantuml/ugraphic/g2d/UGraphicG2d.java +++ b/src/net/sourceforge/plantuml/ugraphic/g2d/UGraphicG2d.java @@ -54,7 +54,6 @@ import net.sourceforge.plantuml.FileFormat; import net.sourceforge.plantuml.Url; import net.sourceforge.plantuml.anim.AffineTransformation; import net.sourceforge.plantuml.graphic.StringBounder; -import net.sourceforge.plantuml.graphic.TextBlockUtils; import net.sourceforge.plantuml.png.PngIO; import net.sourceforge.plantuml.posimo.DotPath; import net.sourceforge.plantuml.ugraphic.AbstractCommonUGraphic; @@ -143,7 +142,7 @@ public class UGraphicG2d extends AbstractUGraphic implements EnsureV private void register(double dpiFactor) { registerDriver(URectangle.class, new DriverRectangleG2d(dpiFactor, this)); if (this.hasAffineTransform || dpiFactor != 1.0) { - registerDriver(UText.class, new DriverTextAsPathG2d(this, TextBlockUtils.getFontRenderContext())); + registerDriver(UText.class, new DriverTextAsPathG2d(this)); } else { registerDriver(UText.class, new DriverTextG2d(this)); } diff --git a/src/net/sourceforge/plantuml/ugraphic/svg/DriverCenteredCharacterSvg.java b/src/net/sourceforge/plantuml/ugraphic/svg/DriverCenteredCharacterSvg.java index dac0922eb..555817ad7 100644 --- a/src/net/sourceforge/plantuml/ugraphic/svg/DriverCenteredCharacterSvg.java +++ b/src/net/sourceforge/plantuml/ugraphic/svg/DriverCenteredCharacterSvg.java @@ -34,9 +34,10 @@ */ package net.sourceforge.plantuml.ugraphic.svg; +import static net.sourceforge.plantuml.graphic.TextBlockUtils.createTextLayout; + import java.awt.font.TextLayout; -import net.sourceforge.plantuml.graphic.TextBlockUtils; import net.sourceforge.plantuml.graphic.UnusedSpace; import net.sourceforge.plantuml.svg.SvgGraphics; import net.sourceforge.plantuml.ugraphic.UCenteredCharacter; @@ -57,7 +58,7 @@ public class DriverCenteredCharacterSvg implements UDriver { final double xpos = x - unusedSpace.getCenterX() - 0.5; final double ypos = y - unusedSpace.getCenterY() - 0.5; - final TextLayout t = new TextLayout("" + c, font.getUnderlayingFont(), TextBlockUtils.getFontRenderContext()); + final TextLayout t = createTextLayout(font, "" + c); svg.setFillColor(mapper.toRGB(param.getColor())); svg.drawPathIterator(xpos, ypos, t.getOutline(null).getPathIterator(null)); diff --git a/src/net/sourceforge/plantuml/ugraphic/svg/DriverTextAsPathSvg.java b/src/net/sourceforge/plantuml/ugraphic/svg/DriverTextAsPathSvg.java index 22737467a..34b721fd6 100644 --- a/src/net/sourceforge/plantuml/ugraphic/svg/DriverTextAsPathSvg.java +++ b/src/net/sourceforge/plantuml/ugraphic/svg/DriverTextAsPathSvg.java @@ -34,15 +34,14 @@ */ package net.sourceforge.plantuml.ugraphic.svg; -import java.awt.font.FontRenderContext; +import static net.sourceforge.plantuml.graphic.TextBlockUtils.createTextLayout; + import java.awt.font.TextLayout; -import net.sourceforge.plantuml.graphic.FontConfiguration; import net.sourceforge.plantuml.svg.SvgGraphics; import net.sourceforge.plantuml.ugraphic.ClipContainer; import net.sourceforge.plantuml.ugraphic.UClip; import net.sourceforge.plantuml.ugraphic.UDriver; -import net.sourceforge.plantuml.ugraphic.UFont; import net.sourceforge.plantuml.ugraphic.UParam; import net.sourceforge.plantuml.ugraphic.UShape; import net.sourceforge.plantuml.ugraphic.UText; @@ -50,11 +49,9 @@ import net.sourceforge.plantuml.ugraphic.color.ColorMapper; public class DriverTextAsPathSvg implements UDriver { - private final FontRenderContext fontRenderContext; private final ClipContainer clipContainer; - public DriverTextAsPathSvg(FontRenderContext fontRenderContext, ClipContainer clipContainer) { - this.fontRenderContext = fontRenderContext; + public DriverTextAsPathSvg(ClipContainer clipContainer) { this.clipContainer = clipContainer; } @@ -65,11 +62,7 @@ public class DriverTextAsPathSvg implements UDriver { return; } - final UText shape = (UText) ushape; - final FontConfiguration fontConfiguration = shape.getFontConfiguration(); - final UFont font = fontConfiguration.getFont(); - - final TextLayout t = new TextLayout(shape.getText(), font.getUnderlayingFont(), fontRenderContext); + final TextLayout t = createTextLayout((UText) ushape); svg.drawPathIterator(x, y, t.getOutline(null).getPathIterator(null)); } diff --git a/src/net/sourceforge/plantuml/ugraphic/svg/UGraphicSvg.java b/src/net/sourceforge/plantuml/ugraphic/svg/UGraphicSvg.java index 7c3b49c21..a65aff8ef 100644 --- a/src/net/sourceforge/plantuml/ugraphic/svg/UGraphicSvg.java +++ b/src/net/sourceforge/plantuml/ugraphic/svg/UGraphicSvg.java @@ -45,7 +45,6 @@ import net.sourceforge.plantuml.SvgCharSizeHack; import net.sourceforge.plantuml.TikzFontDistortion; import net.sourceforge.plantuml.Url; import net.sourceforge.plantuml.graphic.StringBounder; -import net.sourceforge.plantuml.graphic.TextBlockUtils; import net.sourceforge.plantuml.posimo.DotPath; import net.sourceforge.plantuml.svg.LengthAdjust; import net.sourceforge.plantuml.svg.SvgGraphics; @@ -131,7 +130,7 @@ public class UGraphicSvg extends AbstractUGraphic implements ClipCo private void register() { registerDriver(URectangle.class, new DriverRectangleSvg(this)); if (textAsPath2) { - registerDriver(UText.class, new DriverTextAsPathSvg(TextBlockUtils.getFontRenderContext(), this)); + registerDriver(UText.class, new DriverTextAsPathSvg(this)); } else { registerDriver(UText.class, new DriverTextSvg(getStringBounder(), this)); } diff --git a/src/net/sourceforge/plantuml/ugraphic/tikz/DriverCenteredCharacterTikz.java b/src/net/sourceforge/plantuml/ugraphic/tikz/DriverCenteredCharacterTikz.java index ead63b9af..5ce35a0d5 100644 --- a/src/net/sourceforge/plantuml/ugraphic/tikz/DriverCenteredCharacterTikz.java +++ b/src/net/sourceforge/plantuml/ugraphic/tikz/DriverCenteredCharacterTikz.java @@ -34,9 +34,10 @@ */ package net.sourceforge.plantuml.ugraphic.tikz; +import static net.sourceforge.plantuml.graphic.TextBlockUtils.createTextLayout; + import java.awt.font.TextLayout; -import net.sourceforge.plantuml.graphic.TextBlockUtils; import net.sourceforge.plantuml.graphic.UnusedSpace; import net.sourceforge.plantuml.tikz.TikzGraphics; import net.sourceforge.plantuml.ugraphic.UCenteredCharacter; @@ -57,7 +58,7 @@ public class DriverCenteredCharacterTikz implements UDriver { final double xpos = x - unusedSpace.getCenterX() - 0.5; final double ypos = y - unusedSpace.getCenterY() - 0.5; - final TextLayout t = new TextLayout("" + c, font.getUnderlayingFont(), TextBlockUtils.getFontRenderContext()); + final TextLayout t = createTextLayout(font, "" + c); tikz.setStrokeColor(mapper.toColor(param.getColor())); tikz.drawPathIterator(xpos, ypos, t.getOutline(null).getPathIterator(null));