1
0
mirror of https://github.com/octoleo/plantuml.git synced 2025-01-03 15:17:23 +00:00

Pass a StringBounder into UGraphicG2d & EmptyImageBuilder

This commit is contained in:
matthew16550 2021-10-03 05:12:56 +11:00
parent d3a4fc6fe0
commit 102a7ccf64
6 changed files with 31 additions and 19 deletions

View File

@ -46,6 +46,7 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import net.sourceforge.plantuml.cucadiagram.dot.GraphvizUtils; import net.sourceforge.plantuml.cucadiagram.dot.GraphvizUtils;
import net.sourceforge.plantuml.graphic.StringBounder;
import net.sourceforge.plantuml.ugraphic.UAntiAliasing; import net.sourceforge.plantuml.ugraphic.UAntiAliasing;
import net.sourceforge.plantuml.ugraphic.color.ColorMapperIdentity; import net.sourceforge.plantuml.ugraphic.color.ColorMapperIdentity;
import net.sourceforge.plantuml.ugraphic.color.HColor; import net.sourceforge.plantuml.ugraphic.color.HColor;
@ -57,12 +58,13 @@ public class EmptyImageBuilder {
private final BufferedImage im; private final BufferedImage im;
private final Graphics2D g2d; private final Graphics2D g2d;
private final Color background; private final Color background;
private final StringBounder stringBounder;
public EmptyImageBuilder(String watermark, double width, double height, Color background) { public EmptyImageBuilder(String watermark, double width, double height, Color background, StringBounder stringBounder) {
this(watermark, (int) width, (int) height, background); this(watermark, (int) width, (int) height, background, stringBounder);
} }
public EmptyImageBuilder(String watermark, int width, int height, Color background) { public EmptyImageBuilder(String watermark, int width, int height, Color background, StringBounder stringBounder) {
if (width > GraphvizUtils.getenvImageLimit()) { if (width > GraphvizUtils.getenvImageLimit()) {
Log.info("Width too large " + width + ". You should set PLANTUML_LIMIT_SIZE"); Log.info("Width too large " + width + ". You should set PLANTUML_LIMIT_SIZE");
width = GraphvizUtils.getenvImageLimit(); width = GraphvizUtils.getenvImageLimit();
@ -72,6 +74,7 @@ public class EmptyImageBuilder {
height = GraphvizUtils.getenvImageLimit(); height = GraphvizUtils.getenvImageLimit();
} }
this.background = background; this.background = background;
this.stringBounder = stringBounder;
Log.info("Creating image " + width + "x" + height); Log.info("Creating image " + width + "x" + height);
im = new BufferedImage(width, height, getType(background)); im = new BufferedImage(width, height, getType(background));
g2d = im.createGraphics(); g2d = im.createGraphics();
@ -149,8 +152,8 @@ public class EmptyImageBuilder {
return result; return result;
} }
public EmptyImageBuilder(String watermark, int width, int height, Color background, double dpiFactor) { public EmptyImageBuilder(String watermark, int width, int height, Color background, StringBounder stringBounder, double dpiFactor) {
this(watermark, width * dpiFactor, height * dpiFactor, background); this(watermark, width * dpiFactor, height * dpiFactor, background, stringBounder);
if (dpiFactor != 1.0) { if (dpiFactor != 1.0) {
g2d.setTransform(AffineTransform.getScaleInstance(dpiFactor, dpiFactor)); g2d.setTransform(AffineTransform.getScaleInstance(dpiFactor, dpiFactor));
} }
@ -166,7 +169,7 @@ public class EmptyImageBuilder {
public UGraphicG2d getUGraphicG2d() { public UGraphicG2d getUGraphicG2d() {
final HColor back = new HColorSimple(background, false); final HColor back = new HColorSimple(background, false);
final UGraphicG2d result = new UGraphicG2d(back, new ColorMapperIdentity(), g2d, 1.0); final UGraphicG2d result = new UGraphicG2d(back, new ColorMapperIdentity(), stringBounder, g2d, 1.0);
result.setBufferedImage(im); result.setBufferedImage(im);
return result; return result;
} }

View File

@ -42,8 +42,10 @@ import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
import net.sourceforge.plantuml.EmptyImageBuilder; import net.sourceforge.plantuml.EmptyImageBuilder;
import net.sourceforge.plantuml.FileFormat;
import net.sourceforge.plantuml.api.ImageDataSimple; import net.sourceforge.plantuml.api.ImageDataSimple;
import net.sourceforge.plantuml.core.ImageData; import net.sourceforge.plantuml.core.ImageData;
import net.sourceforge.plantuml.graphic.StringBounder;
import net.sourceforge.plantuml.png.PngIO; import net.sourceforge.plantuml.png.PngIO;
import net.sourceforge.plantuml.ugraphic.UChange; import net.sourceforge.plantuml.ugraphic.UChange;
import net.sourceforge.plantuml.ugraphic.UMotif; import net.sourceforge.plantuml.ugraphic.UMotif;
@ -68,11 +70,12 @@ public class GraphicsPath {
} }
private BufferedImage createImage() { private BufferedImage createImage() {
final EmptyImageBuilder builder = new EmptyImageBuilder(null, 50, 50, Color.WHITE); final StringBounder stringBounder = FileFormat.PNG.getDefaultStringBounder();
final EmptyImageBuilder builder = new EmptyImageBuilder(null, 50, 50, Color.WHITE, stringBounder);
final BufferedImage im = builder.getBufferedImage(); final BufferedImage im = builder.getBufferedImage();
final Graphics2D g2d = builder.getGraphics2D(); final Graphics2D g2d = builder.getGraphics2D();
final UGraphicG2d ug = new UGraphicG2d(HColorUtils.WHITE, colorMapper, g2d, 1.0); final UGraphicG2d ug = new UGraphicG2d(HColorUtils.WHITE, colorMapper, stringBounder, g2d, 1.0);
ug.setBufferedImage(im); ug.setBufferedImage(im);
final UMotif motif = new UMotif(path); final UMotif motif = new UMotif(path);
motif.drawHorizontal(ug.apply((UChange) HColorUtils.BLACK), 20, 20, 1); motif.drawHorizontal(ug.apply((UChange) HColorUtils.BLACK), 20, 20, 1);

View File

@ -44,11 +44,13 @@ import java.util.List;
import net.sourceforge.plantuml.AbstractPSystem; import net.sourceforge.plantuml.AbstractPSystem;
import net.sourceforge.plantuml.EmptyImageBuilder; import net.sourceforge.plantuml.EmptyImageBuilder;
import net.sourceforge.plantuml.FileFormat;
import net.sourceforge.plantuml.FileFormatOption; import net.sourceforge.plantuml.FileFormatOption;
import net.sourceforge.plantuml.api.ImageDataSimple; import net.sourceforge.plantuml.api.ImageDataSimple;
import net.sourceforge.plantuml.core.DiagramDescription; import net.sourceforge.plantuml.core.DiagramDescription;
import net.sourceforge.plantuml.core.ImageData; import net.sourceforge.plantuml.core.ImageData;
import net.sourceforge.plantuml.core.UmlSource; import net.sourceforge.plantuml.core.UmlSource;
import net.sourceforge.plantuml.graphic.StringBounder;
import net.sourceforge.plantuml.png.PngIO; import net.sourceforge.plantuml.png.PngIO;
import net.sourceforge.plantuml.ugraphic.UGraphic; import net.sourceforge.plantuml.ugraphic.UGraphic;
import net.sourceforge.plantuml.ugraphic.color.ColorMapperIdentity; import net.sourceforge.plantuml.ugraphic.color.ColorMapperIdentity;
@ -68,9 +70,10 @@ public class PSystemLogo extends AbstractPSystem {
throws IOException { throws IOException {
final int width = 640; final int width = 640;
final int height = 480; final int height = 480;
final EmptyImageBuilder builder = new EmptyImageBuilder(fileFormat.getWatermark(), width, height, Color.WHITE); final StringBounder stringBounder = FileFormat.PNG.getDefaultStringBounder();
final EmptyImageBuilder builder = new EmptyImageBuilder(fileFormat.getWatermark(), width, height, Color.WHITE, stringBounder);
final BufferedImage im = builder.getBufferedImage(); final BufferedImage im = builder.getBufferedImage();
final UGraphic ug = new UGraphicG2d(HColorUtils.WHITE, new ColorMapperIdentity(), builder.getGraphics2D(), 1.0); final UGraphic ug = new UGraphicG2d(HColorUtils.WHITE, new ColorMapperIdentity(), stringBounder, builder.getGraphics2D(), 1.0);
((UGraphicG2d) ug).setBufferedImage(im); ((UGraphicG2d) ug).setBufferedImage(im);
final TurtleGraphicsPane turtleGraphicsPane = new TurtleGraphicsPane(width, height); final TurtleGraphicsPane turtleGraphicsPane = new TurtleGraphicsPane(width, height);

View File

@ -53,6 +53,7 @@ import net.sourceforge.plantuml.cucadiagram.Display;
import net.sourceforge.plantuml.eps.EpsStrategy; import net.sourceforge.plantuml.eps.EpsStrategy;
import net.sourceforge.plantuml.graphic.FontConfiguration; import net.sourceforge.plantuml.graphic.FontConfiguration;
import net.sourceforge.plantuml.graphic.HorizontalAlignment; import net.sourceforge.plantuml.graphic.HorizontalAlignment;
import net.sourceforge.plantuml.graphic.StringBounder;
import net.sourceforge.plantuml.graphic.TextBlock; import net.sourceforge.plantuml.graphic.TextBlock;
import net.sourceforge.plantuml.png.PngIO; import net.sourceforge.plantuml.png.PngIO;
import net.sourceforge.plantuml.svg.LengthAdjust; import net.sourceforge.plantuml.svg.LengthAdjust;
@ -104,12 +105,13 @@ public class GraphicsSudoku {
} }
public ImageData writeImagePng(OutputStream os) throws IOException { public ImageData writeImagePng(OutputStream os) throws IOException {
final StringBounder stringBounder = FileFormat.PNG.getDefaultStringBounder();
final EmptyImageBuilder builder = new EmptyImageBuilder(null, sudoWidth, sudoHeight + textTotalHeight, final EmptyImageBuilder builder = new EmptyImageBuilder(null, sudoWidth, sudoHeight + textTotalHeight,
Color.WHITE); Color.WHITE, stringBounder);
final BufferedImage im = builder.getBufferedImage(); final BufferedImage im = builder.getBufferedImage();
final Graphics2D g3d = builder.getGraphics2D(); final Graphics2D g3d = builder.getGraphics2D();
final UGraphic ug = new UGraphicG2d(HColorUtils.WHITE, new ColorMapperIdentity(), g3d, 1.0); final UGraphic ug = new UGraphicG2d(HColorUtils.WHITE, new ColorMapperIdentity(), stringBounder, g3d, 1.0);
drawInternal(ug); drawInternal(ug);
g3d.dispose(); g3d.dispose();

View File

@ -461,11 +461,12 @@ public class ImageBuilder {
backColor = new Color(0, 0, 0, 0); backColor = new Color(0, 0, 0, 0);
} }
final StringBounder stringBounder = FileFormat.PNG.getDefaultStringBounder();
final EmptyImageBuilder builder = new EmptyImageBuilder(watermark, (int) (dim.getWidth() * scaleFactor), final EmptyImageBuilder builder = new EmptyImageBuilder(watermark, (int) (dim.getWidth() * scaleFactor),
(int) (dim.getHeight() * scaleFactor), backColor); (int) (dim.getHeight() * scaleFactor), backColor, stringBounder);
final Graphics2D graphics2D = builder.getGraphics2D(); final Graphics2D graphics2D = builder.getGraphics2D();
final UGraphicG2d ug = new UGraphicG2d(backcolor, colorMapper, graphics2D, scaleFactor, final UGraphicG2d ug = new UGraphicG2d(backcolor, colorMapper, stringBounder, graphics2D, scaleFactor,
affineTransforms == null ? null : affineTransforms.getFirst(), dx, dy); affineTransforms == null ? null : affineTransforms.getFirst(), dx, dy);
ug.setBufferedImage(builder.getBufferedImage()); ug.setBufferedImage(builder.getBufferedImage());
final BufferedImage im = ug.getBufferedImage(); final BufferedImage im = ug.getBufferedImage();

View File

@ -50,9 +50,9 @@ import java.util.Objects;
import java.util.Set; import java.util.Set;
import net.sourceforge.plantuml.EnsureVisible; import net.sourceforge.plantuml.EnsureVisible;
import net.sourceforge.plantuml.FileFormat;
import net.sourceforge.plantuml.Url; import net.sourceforge.plantuml.Url;
import net.sourceforge.plantuml.anim.AffineTransformation; import net.sourceforge.plantuml.anim.AffineTransformation;
import net.sourceforge.plantuml.graphic.StringBounder;
import net.sourceforge.plantuml.png.PngIO; import net.sourceforge.plantuml.png.PngIO;
import net.sourceforge.plantuml.posimo.DotPath; import net.sourceforge.plantuml.posimo.DotPath;
import net.sourceforge.plantuml.ugraphic.AbstractCommonUGraphic; import net.sourceforge.plantuml.ugraphic.AbstractCommonUGraphic;
@ -116,13 +116,13 @@ public class UGraphicG2d extends AbstractUGraphic<Graphics2D> implements EnsureV
register(dpiFactor); register(dpiFactor);
} }
public UGraphicG2d(HColor defaultBackground, ColorMapper colorMapper, Graphics2D g2d, double dpiFactor) { public UGraphicG2d(HColor defaultBackground, ColorMapper colorMapper, StringBounder stringBounder, Graphics2D g2d, double dpiFactor) {
this(defaultBackground, colorMapper, g2d, dpiFactor, null, 0, 0); this(defaultBackground, colorMapper, stringBounder, g2d, dpiFactor, null, 0, 0);
} }
public UGraphicG2d(HColor defaultBackground, ColorMapper colorMapper, Graphics2D g2d, double dpiFactor, public UGraphicG2d(HColor defaultBackground, ColorMapper colorMapper, StringBounder stringBounder, Graphics2D g2d, double dpiFactor,
AffineTransformation affineTransform, double dx, double dy) { AffineTransformation affineTransform, double dx, double dy) {
super(defaultBackground, colorMapper, FileFormat.PNG.getDefaultStringBounder(), g2d); super(defaultBackground, colorMapper, stringBounder, g2d);
this.hasAffineTransform = affineTransform != null; this.hasAffineTransform = affineTransform != null;
this.dpiFactor = dpiFactor; this.dpiFactor = dpiFactor;
if (dpiFactor != 1.0) { if (dpiFactor != 1.0) {