1
0
mirror of https://github.com/octoleo/plantuml.git synced 2024-06-02 00:20:49 +00:00

Extract TextBlockUtils.createTextLayout() methods

This commit is contained in:
matthew16550 2021-09-19 20:22:54 +10:00
parent e1fe53457a
commit c6f40e32b1
10 changed files with 35 additions and 41 deletions

View File

@ -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() {

View File

@ -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);

View File

@ -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<EpsGraphics> {
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));

View File

@ -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<EpsGraphics> {
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<EpsGraphics> {
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;

View File

@ -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<Graphics2D> {
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<Graphics2D> {
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);

View File

@ -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<Graphics2D> 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));
}

View File

@ -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<SvgGraphics> {
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));

View File

@ -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<SvgGraphics> {
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<SvgGraphics> {
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));
}

View File

@ -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<SvgGraphics> 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));
}

View File

@ -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<TikzGraphics> {
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));