1
0
mirror of https://github.com/octoleo/plantuml.git synced 2024-11-13 00:46:29 +00:00

Merge pull request #507 from matthew16550/simplify-image-builder

ImageBuilder simplifications
This commit is contained in:
arnaudroques 2021-03-25 08:34:14 +01:00 committed by GitHub
commit d9e7c134d7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 70 additions and 87 deletions

View File

@ -44,10 +44,11 @@ public class ImageDataComplex extends ImageDataAbstract {
private final CMapData cmap; private final CMapData cmap;
private final String warningOrError; private final String warningOrError;
public ImageDataComplex(Dimension2D info, CMapData cmap, String warningOrError) { public ImageDataComplex(Dimension2D info, CMapData cmap, String warningOrError, int status) {
super(info); super(info);
this.cmap = cmap; this.cmap = cmap;
this.warningOrError = warningOrError; this.warningOrError = warningOrError;
setStatus(status);
} }
public boolean containsCMapData() { public boolean containsCMapData() {

View File

@ -49,6 +49,11 @@ public class ImageDataSimple extends ImageDataAbstract {
super(dim); super(dim);
} }
public ImageDataSimple(Dimension2D dim, int status) {
super(dim);
setStatus(status);
}
private ImageDataSimple() { private ImageDataSimple() {
this(0, 0); this(0, 0);
} }

View File

@ -68,7 +68,7 @@ public class UGraphicBraille extends AbstractUGraphic<BrailleGrid> implements Cl
return new UGraphicBraille(this); return new UGraphicBraille(this);
} }
public UGraphicBraille(ColorMapper colorMapper, FileFormat fileFormat) { public UGraphicBraille(ColorMapper colorMapper) {
this(colorMapper, new BrailleGrid(QUANTA)); this(colorMapper, new BrailleGrid(QUANTA));
} }

View File

@ -69,7 +69,6 @@ import net.sourceforge.plantuml.Url;
import net.sourceforge.plantuml.UseStyle; import net.sourceforge.plantuml.UseStyle;
import net.sourceforge.plantuml.anim.AffineTransformation; import net.sourceforge.plantuml.anim.AffineTransformation;
import net.sourceforge.plantuml.anim.Animation; import net.sourceforge.plantuml.anim.Animation;
import net.sourceforge.plantuml.api.ImageDataAbstract;
import net.sourceforge.plantuml.api.ImageDataComplex; import net.sourceforge.plantuml.api.ImageDataComplex;
import net.sourceforge.plantuml.api.ImageDataSimple; import net.sourceforge.plantuml.api.ImageDataSimple;
import net.sourceforge.plantuml.braille.UGraphicBraille; import net.sourceforge.plantuml.braille.UGraphicBraille;
@ -108,11 +107,6 @@ import net.sourceforge.plantuml.ugraphic.visio.UGraphicVdx;
public class ImageBuilder { public class ImageBuilder {
private double top;
private double right;
private double bottom;
private double left;
private Animation animation; private Animation animation;
private boolean annotations; private boolean annotations;
private HColor backcolor = HColorUtils.WHITE; private HColor backcolor = HColorUtils.WHITE;
@ -121,17 +115,18 @@ public class ImageBuilder {
private UStroke borderStroke; private UStroke borderStroke;
private ColorMapper colorMapper = new ColorMapperIdentity(); private ColorMapper colorMapper = new ColorMapperIdentity();
private int dpi = 96; private int dpi = 96;
private boolean handwritten;
private LengthAdjust lengthAdjust = LengthAdjust.defaultValue();;
private TitledDiagram titledDiagram;
private UDrawable udrawable;
private final FileFormatOption fileFormatOption; private final FileFormatOption fileFormatOption;
private boolean handwritten;
private LengthAdjust lengthAdjust = LengthAdjust.defaultValue();
private UDrawable udrawable;
private ClockwiseTopRightBottomLeft margin = ClockwiseTopRightBottomLeft.none();
private String metadata;
private Scale scale; private Scale scale;
private long seed = 42; private long seed = 42;
private int status = 0; private int status = 0;
private SvgCharSizeHack svgCharSizeHack = SvgCharSizeHack.NO_HACK; private SvgCharSizeHack svgCharSizeHack = SvgCharSizeHack.NO_HACK;
private boolean svgDimensionStyle = true; private boolean svgDimensionStyle = true;
private String metadata; private TitledDiagram titledDiagram;
private boolean randomPixel; private boolean randomPixel;
private String warningOrError; private String warningOrError;
@ -174,10 +169,7 @@ public class ImageBuilder {
} }
public ImageBuilder margin(ClockwiseTopRightBottomLeft margin) { public ImageBuilder margin(ClockwiseTopRightBottomLeft margin) {
this.top = margin.getTop(); this.margin = margin;
this.right = margin.getRight();
this.bottom = margin.getBottom();
this.left = margin.getLeft();
return this; return this;
} }
@ -218,7 +210,7 @@ public class ImageBuilder {
dpi = skinParam.getDpi(); dpi = skinParam.getDpi();
handwritten = skinParam.handwritten(); handwritten = skinParam.handwritten();
lengthAdjust = skinParam.getlengthAdjust(); lengthAdjust = skinParam.getlengthAdjust();
margin(calculateDiagramMargin(diagram)); margin = calculateMargin(diagram);
metadata = fileFormatOption.isWithMetadata() ? diagram.getMetadata() : null; metadata = fileFormatOption.isWithMetadata() ? diagram.getMetadata() : null;
scale = diagram.getScale(); scale = diagram.getScale();
seed = diagram.seed(); seed = diagram.seed();
@ -237,9 +229,15 @@ public class ImageBuilder {
final AnnotatedWorker annotatedWorker = new AnnotatedWorker(titledDiagram, skinParam, stringBounder); final AnnotatedWorker annotatedWorker = new AnnotatedWorker(titledDiagram, skinParam, stringBounder);
udrawable = annotatedWorker.addAdd((TextBlock) udrawable); udrawable = annotatedWorker.addAdd((TextBlock) udrawable);
} }
final ImageData imageData = writeImageTOBEMOVED(fileFormatOption, os);
((ImageDataAbstract) imageData).setStatus(status); switch (fileFormatOption.getFileFormat()) {
return imageData; case MJPEG:
return writeImageMjpeg(os);
case ANIMATED_GIF:
return writeImageAnimatedGif(os);
default:
return writeImageInternal(fileFormatOption, os, animation);
}
} }
public byte[] writeByteArray() throws IOException { public byte[] writeByteArray() throws IOException {
@ -249,21 +247,6 @@ public class ImageBuilder {
} }
} }
public ImageData writeImageTOBEMOVED(OutputStream os) throws IOException {
return writeImageTOBEMOVED(fileFormatOption, os);
}
public ImageData writeImageTOBEMOVED(FileFormatOption fileFormatOption, OutputStream os)
throws IOException {
final FileFormat fileFormat = fileFormatOption.getFileFormat();
if (fileFormat == FileFormat.MJPEG) {
return writeImageMjpeg(os, fileFormatOption.getDefaultStringBounder(svgCharSizeHack));
} else if (fileFormat == FileFormat.ANIMATED_GIF) {
return writeImageAnimatedGif(os, fileFormatOption.getDefaultStringBounder(svgCharSizeHack));
}
return writeImageInternal(fileFormatOption, os, animation);
}
private ImageData writeImageInternal(FileFormatOption fileFormatOption, OutputStream os, private ImageData writeImageInternal(FileFormatOption fileFormatOption, OutputStream os,
Animation animationArg) throws IOException { Animation animationArg) throws IOException {
Dimension2D dim = getFinalDimension(fileFormatOption.getDefaultStringBounder(svgCharSizeHack)); Dimension2D dim = getFinalDimension(fileFormatOption.getDefaultStringBounder(svgCharSizeHack));
@ -290,10 +273,9 @@ public class ImageBuilder {
if (randomPixel) { if (randomPixel) {
drawRandomPoint(ug2); drawRandomPoint(ug2);
} }
ug2 = ug2.apply(new UTranslate(left, top)); ug2 = handwritten(ug2.apply(new UTranslate(margin.getLeft(), margin.getTop())));
final UGraphic ugDecored = handwritten(ug2); udrawable.drawU(ug2);
udrawable.drawU(ugDecored); ug2.flushUg();
ugDecored.flushUg();
ug.writeImageTOBEMOVED(os, metadata, 96); ug.writeImageTOBEMOVED(os, metadata, 96);
os.flush(); os.flush();
@ -301,10 +283,10 @@ public class ImageBuilder {
final Set<Url> urls = ((UGraphicG2d) ug).getAllUrlsEncountered(); final Set<Url> urls = ((UGraphicG2d) ug).getAllUrlsEncountered();
if (urls.size() > 0) { if (urls.size() > 0) {
final CMapData cmap = CMapData.cmapString(urls, dpi); final CMapData cmap = CMapData.cmapString(urls, dpi);
return new ImageDataComplex(dim, cmap, warningOrError); return new ImageDataComplex(dim, cmap, warningOrError, status);
} }
} }
return new ImageDataSimple(dim); return createImageData(dim);
} }
private void drawRandomPoint(UGraphic ug2) { private void drawRandomPoint(UGraphic ug2) {
@ -315,17 +297,17 @@ public class ImageBuilder {
final Color c = new Color(red, green, blue); final Color c = new Color(red, green, blue);
final HColor color = new HColorSimple(c, false); final HColor color = new HColorSimple(c, false);
ug2.apply(color).apply(color.bg()).draw(new URectangle(1, 1)); ug2.apply(color).apply(color.bg()).draw(new URectangle(1, 1));
} }
public Dimension2D getFinalDimension(StringBounder stringBounder) { private Dimension2D getFinalDimension(StringBounder stringBounder) {
final Dimension2D dim;
final LimitFinder limitFinder = new LimitFinder(stringBounder, true); final LimitFinder limitFinder = new LimitFinder(stringBounder, true);
udrawable.drawU(limitFinder); udrawable.drawU(limitFinder);
dim = new Dimension2DDouble(limitFinder.getMaxX(), limitFinder.getMaxY()); return new Dimension2DDouble(limitFinder.getMaxX() + 1 + margin.getLeft() + margin.getRight(),
limitFinder.getMaxY() + 1 + margin.getTop() + margin.getBottom());
}
return new Dimension2DDouble(dim.getWidth() + 1 + left + right, dim.getHeight() + 1 + top + bottom); private Dimension2D getFinalDimension() {
return getFinalDimension(fileFormatOption.getDefaultStringBounder(svgCharSizeHack));
} }
private UGraphic handwritten(UGraphic ug) { private UGraphic handwritten(UGraphic ug) {
@ -339,12 +321,9 @@ public class ImageBuilder {
// } // }
} }
private ImageData writeImageMjpeg(OutputStream os, StringBounder stringBounder) throws IOException { private ImageData writeImageMjpeg(OutputStream os) throws IOException {
final LimitFinder limitFinder = new LimitFinder(stringBounder, true); final Dimension2D dim = getFinalDimension();
udrawable.drawU(limitFinder);
final Dimension2D dim = new Dimension2DDouble(limitFinder.getMaxX() + 1 + left + right,
limitFinder.getMaxY() + 1 + top + bottom);
final SFile f = new SFile("c:/tmp.avi"); final SFile f = new SFile("c:/tmp.avi");
@ -366,16 +345,12 @@ public class ImageBuilder {
FileUtils.copyToStream(f, os); FileUtils.copyToStream(f, os);
return new ImageDataSimple(dim); return createImageData(dim);
} }
private ImageData writeImageAnimatedGif(OutputStream os, StringBounder stringBounder) throws IOException { private ImageData writeImageAnimatedGif(OutputStream os) throws IOException {
final LimitFinder limitFinder = new LimitFinder(stringBounder, true); final Dimension2D dim = getFinalDimension();
udrawable.drawU(limitFinder);
final Dimension2D dim = new Dimension2DDouble(limitFinder.getMaxX() + 1 + left + right,
limitFinder.getMaxY() + 1 + top + bottom);
final MinMax minmax = animation.getMinMax(dim); final MinMax minmax = animation.getMinMax(dim);
@ -393,8 +368,7 @@ public class ImageBuilder {
e.addFrame((BufferedImage) ii.getImage()); e.addFrame((BufferedImage) ii.getImage());
} }
e.finish(); e.finish();
return new ImageDataSimple(dim); return createImageData(dim);
} }
private Image getAviImage(AffineTransformation affineTransform) throws IOException { private Image getAviImage(AffineTransformation affineTransform) throws IOException {
@ -411,14 +385,13 @@ public class ImageBuilder {
private UGraphic2 createUGraphic(FileFormatOption option, final Dimension2D dim, Animation animationArg, private UGraphic2 createUGraphic(FileFormatOption option, final Dimension2D dim, Animation animationArg,
double dx, double dy) { double dx, double dy) {
final double scaleFactor = (scale == null ? 1 : scale.getScale(dim.getWidth(), dim.getHeight())) * dpi / 96.0; final double scaleFactor = (scale == null ? 1 : scale.getScale(dim.getWidth(), dim.getHeight())) * dpi / 96.0;
final FileFormat fileFormat = option.getFileFormat(); switch (option.getFileFormat()) {
switch (fileFormat) {
case PNG: case PNG:
return createUGraphicPNG(colorMapper, scaleFactor, dim, backcolor, animationArg, dx, dy, return createUGraphicPNG(scaleFactor, dim, animationArg, dx, dy,
option.getWatermark()); option.getWatermark());
case SVG: case SVG:
return createUGraphicSVG(colorMapper, scaleFactor, dim, backcolor, option.getSvgLinkTarget(), return createUGraphicSVG(scaleFactor, dim, option.getSvgLinkTarget(),
option.getHoverColor(), option.getPreserveAspectRatio(), lengthAdjust); option.getHoverColor(), option.getPreserveAspectRatio());
case EPS: case EPS:
return new UGraphicEps(colorMapper, EpsStrategy.getDefault2()); return new UGraphicEps(colorMapper, EpsStrategy.getDefault2());
case EPS_TEXT: case EPS_TEXT:
@ -432,27 +405,26 @@ public class ImageBuilder {
case LATEX_NO_PREAMBLE: case LATEX_NO_PREAMBLE:
return new UGraphicTikz(colorMapper, scaleFactor, false, option.getTikzFontDistortion()); return new UGraphicTikz(colorMapper, scaleFactor, false, option.getTikzFontDistortion());
case BRAILLE_PNG: case BRAILLE_PNG:
return new UGraphicBraille(colorMapper, fileFormat); return new UGraphicBraille(colorMapper);
case UTXT: case UTXT:
case ATXT: case ATXT:
return new UGraphicTxt(); return new UGraphicTxt();
case DEBUG: case DEBUG:
return new UGraphicDebug(); return new UGraphicDebug();
default: default:
throw new UnsupportedOperationException(fileFormat.toString()); throw new UnsupportedOperationException(option.getFileFormat().toString());
} }
} }
private UGraphic2 createUGraphicSVG(ColorMapper colorMapper, double scaleFactor, Dimension2D dim, private UGraphic2 createUGraphicSVG(double scaleFactor, Dimension2D dim,
final HColor suggested, String svgLinkTarget, String hover, String preserveAspectRatio, String svgLinkTarget, String hover, String preserveAspectRatio) {
LengthAdjust lengthAdjust) { HColor backColor = HColorUtils.WHITE; // TODO simplify backcolor some more in a future PR
HColor backColor = HColorUtils.WHITE; if (this.backcolor instanceof HColorSimple) {
if (suggested instanceof HColorSimple) { backColor = this.backcolor;
backColor = suggested;
} }
final UGraphicSvg ug; final UGraphicSvg ug;
if (suggested instanceof HColorGradient) { if (this.backcolor instanceof HColorGradient) {
ug = new UGraphicSvg(svgDimensionStyle, dim, colorMapper, (HColorGradient) suggested, false, scaleFactor, ug = new UGraphicSvg(svgDimensionStyle, dim, colorMapper, (HColorGradient) this.backcolor, false, scaleFactor,
svgLinkTarget, hover, seed, preserveAspectRatio, svgCharSizeHack, lengthAdjust); svgLinkTarget, hover, seed, preserveAspectRatio, svgCharSizeHack, lengthAdjust);
} else if (backColor == null || colorMapper.toColor(backColor).equals(Color.WHITE)) { } else if (backColor == null || colorMapper.toColor(backColor).equals(Color.WHITE)) {
ug = new UGraphicSvg(svgDimensionStyle, dim, colorMapper, false, scaleFactor, svgLinkTarget, hover, seed, ug = new UGraphicSvg(svgDimensionStyle, dim, colorMapper, false, scaleFactor, svgLinkTarget, hover, seed,
@ -466,12 +438,12 @@ public class ImageBuilder {
} }
private UGraphic2 createUGraphicPNG(ColorMapper colorMapper, double scaleFactor, final Dimension2D dim, private UGraphic2 createUGraphicPNG(double scaleFactor, final Dimension2D dim,
HColor mybackcolor, Animation affineTransforms, double dx, double dy, String watermark) { Animation affineTransforms, double dx, double dy, String watermark) {
Color backColor = Color.WHITE; Color backColor = Color.WHITE; // TODO simplify backcolor some more in a future PR
if (mybackcolor instanceof HColorSimple) { if (this.backcolor instanceof HColorSimple) {
backColor = colorMapper.toColor(mybackcolor); backColor = colorMapper.toColor(this.backcolor);
} else if (mybackcolor instanceof HColorBackground) { } else if (this.backcolor instanceof HColorBackground) {
backColor = null; backColor = null;
} }
@ -482,9 +454,9 @@ public class ImageBuilder {
final UGraphicG2d ug = new UGraphicG2d(colorMapper, graphics2D, scaleFactor, final UGraphicG2d ug = new UGraphicG2d(colorMapper, 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 = ((UGraphicG2d) ug).getBufferedImage(); final BufferedImage im = ug.getBufferedImage();
if (mybackcolor instanceof HColorGradient) { if (this.backcolor instanceof HColorGradient) {
ug.apply(mybackcolor.bg()).draw(new URectangle(im.getWidth() / scaleFactor, im.getHeight() / scaleFactor)); ug.apply(this.backcolor.bg()).draw(new URectangle(im.getWidth() / scaleFactor, im.getHeight() / scaleFactor));
} }
return ug; return ug;
@ -512,7 +484,7 @@ public class ImageBuilder {
return (thickness == null && borderColor != null) ? new UStroke() : thickness; return (thickness == null && borderColor != null) ? new UStroke() : thickness;
} }
private static ClockwiseTopRightBottomLeft calculateDiagramMargin(TitledDiagram diagram) { private static ClockwiseTopRightBottomLeft calculateMargin(TitledDiagram diagram) {
if (UseStyle.useBetaStyle()) { if (UseStyle.useBetaStyle()) {
final Style style = StyleSignature.of(SName.root, SName.document) final Style style = StyleSignature.of(SName.root, SName.document)
.getMergedStyle(diagram.getSkinParam().getCurrentStyleBuilder()); .getMergedStyle(diagram.getSkinParam().getCurrentStyleBuilder());
@ -522,4 +494,9 @@ public class ImageBuilder {
} }
return diagram.getDefaultMargins(); return diagram.getDefaultMargins();
} }
private ImageDataSimple createImageData(Dimension2D dim) {
return new ImageDataSimple(dim, status);
}
} }