1
0
mirror of https://github.com/octoleo/plantuml.git synced 2024-11-22 21:15:09 +00:00

* Extract common functionality to UGraphicNo class

* Simplify subclass constructors
* Remove dpiFactor() methods which became redundant a while ago
This commit is contained in:
matthew16550 2021-09-19 22:33:09 +10:00
parent e1fe53457a
commit 8ec5a84040
8 changed files with 205 additions and 297 deletions

View File

@ -35,6 +35,8 @@
*/
package net.sourceforge.plantuml.activitydiagram3.ftile;
import static net.sourceforge.plantuml.utils.ObjectUtils.instanceOfAny;
import java.awt.geom.Line2D;
import java.util.ArrayList;
import java.util.List;
@ -46,36 +48,41 @@ import net.sourceforge.plantuml.ugraphic.UChange;
import net.sourceforge.plantuml.ugraphic.UGraphic;
import net.sourceforge.plantuml.ugraphic.UGraphicNo;
import net.sourceforge.plantuml.ugraphic.ULine;
import net.sourceforge.plantuml.ugraphic.UParam;
import net.sourceforge.plantuml.ugraphic.UParamNull;
import net.sourceforge.plantuml.ugraphic.UPolygon;
import net.sourceforge.plantuml.ugraphic.URectangle;
import net.sourceforge.plantuml.ugraphic.UShape;
import net.sourceforge.plantuml.ugraphic.UStroke;
import net.sourceforge.plantuml.ugraphic.UTranslate;
import net.sourceforge.plantuml.ugraphic.color.ColorMapper;
import net.sourceforge.plantuml.ugraphic.color.HColor;
import net.sourceforge.plantuml.ugraphic.color.HColorUtils;
public class CollisionDetector extends UGraphicNo implements UGraphic {
public class CollisionDetector extends UGraphicNo {
@Override
public UGraphic apply(UChange change) {
if (change instanceof UTranslate) {
return new CollisionDetector(stringBounder, translate.compose((UTranslate) change), this.context);
} else if (change instanceof UStroke) {
return new CollisionDetector(this);
} else if (change instanceof UBackground) {
return new CollisionDetector(this);
} else if (change instanceof HColor) {
return new CollisionDetector(this);
}
throw new UnsupportedOperationException();
return new CollisionDetector(this, change);
}
private final StringBounder stringBounder;
private final UTranslate translate;
private final Context context;
public CollisionDetector(StringBounder stringBounder) {
super(stringBounder);
this.context = new Context();
}
private CollisionDetector(CollisionDetector other, UChange change) {
super(other, change);
if (!instanceOfAny(change,
UBackground.class,
HColor.class,
UStroke.class,
UTranslate.class
)) {
throw new UnsupportedOperationException(change.getClass().toString());
}
this.context = other.context;
}
static class Context {
private final List<MinMax> rectangles = new ArrayList<>();
private final List<Snake> snakes = new ArrayList<>();
@ -146,28 +153,6 @@ public class CollisionDetector extends UGraphicNo implements UGraphic {
return true;
}
public CollisionDetector(StringBounder stringBounder) {
this(stringBounder, new UTranslate(), new Context());
}
private CollisionDetector(StringBounder stringBounder, UTranslate translate, Context context) {
this.stringBounder = stringBounder;
this.translate = translate;
this.context = context;
}
private CollisionDetector(CollisionDetector other) {
this(other.stringBounder, other.translate, other.context);
}
public StringBounder getStringBounder() {
return stringBounder;
}
public UParam getParam() {
return new UParamNull();
}
public void draw(UShape shape) {
if (shape instanceof UPolygon) {
drawPolygone((UPolygon) shape);
@ -182,24 +167,17 @@ public class CollisionDetector extends UGraphicNo implements UGraphic {
private void drawSnake(Snake shape) {
if (context.manageSnakes) {
context.snakes.add(shape.translate(translate));
context.snakes.add(shape.translate(getTranslate()));
}
}
private void drawRectangle(URectangle shape) {
context.rectangles.add(shape.getMinMax().translate(translate));
context.rectangles.add(shape.getMinMax().translate(getTranslate()));
}
private void drawPolygone(UPolygon shape) {
context.rectangles.add(shape.getMinMax().translate(translate));
}
public ColorMapper getColorMapper() {
throw new UnsupportedOperationException();
}
public void flushUg() {
context.rectangles.add(shape.getMinMax().translate(getTranslate()));
}
public void drawDebug(UGraphic ug) {
@ -210,12 +188,4 @@ public class CollisionDetector extends UGraphicNo implements UGraphic {
this.context.manageSnakes = manageSnakes;
}
public boolean matchesProperty(String propertyName) {
return false;
}
public double dpiFactor() {
return 1;
}
}

View File

@ -35,38 +35,26 @@
*/
package net.sourceforge.plantuml.activitydiagram3.ftile;
import static net.sourceforge.plantuml.utils.ObjectUtils.instanceOfAny;
import net.sourceforge.plantuml.graphic.StringBounder;
import net.sourceforge.plantuml.ugraphic.MinMax;
import net.sourceforge.plantuml.ugraphic.UBackground;
import net.sourceforge.plantuml.ugraphic.UChange;
import net.sourceforge.plantuml.ugraphic.UGraphic;
import net.sourceforge.plantuml.ugraphic.UGraphicNo;
import net.sourceforge.plantuml.ugraphic.UParam;
import net.sourceforge.plantuml.ugraphic.UParamNull;
import net.sourceforge.plantuml.ugraphic.URectangle;
import net.sourceforge.plantuml.ugraphic.UShape;
import net.sourceforge.plantuml.ugraphic.UStroke;
import net.sourceforge.plantuml.ugraphic.UTranslate;
import net.sourceforge.plantuml.ugraphic.color.ColorMapper;
import net.sourceforge.plantuml.ugraphic.color.HColor;
public class ZadBuilder extends UGraphicNo implements UGraphic {
public class ZadBuilder extends UGraphicNo {
@Override
public UGraphic apply(UChange change) {
if (change instanceof UTranslate) {
return new ZadBuilder(stringBounder, translate.compose((UTranslate) change), this.context);
} else if (change instanceof UStroke) {
return new ZadBuilder(this);
} else if (change instanceof UBackground) {
return new ZadBuilder(this);
} else if (change instanceof HColor) {
return new ZadBuilder(this);
}
throw new UnsupportedOperationException();
return new ZadBuilder(this, change);
}
private final StringBounder stringBounder;
private final UTranslate translate;
private final Context context;
static class Context {
@ -74,25 +62,21 @@ public class ZadBuilder extends UGraphicNo implements UGraphic {
}
public ZadBuilder(StringBounder stringBounder) {
this(stringBounder, new UTranslate(), new Context());
super(stringBounder);
this.context = new Context();
}
private ZadBuilder(StringBounder stringBounder, UTranslate translate, Context context) {
this.stringBounder = stringBounder;
this.translate = translate;
this.context = context;
}
private ZadBuilder(ZadBuilder other) {
this(other.stringBounder, other.translate, other.context);
}
public StringBounder getStringBounder() {
return stringBounder;
}
public UParam getParam() {
return new UParamNull();
private ZadBuilder(ZadBuilder other, UChange change) {
super(other, change);
if (!instanceOfAny(change,
UBackground.class,
HColor.class,
UStroke.class,
UTranslate.class
)) {
throw new UnsupportedOperationException(change.getClass().toString());
}
this.context = other.context;
}
public void draw(UShape shape) {
@ -102,26 +86,11 @@ public class ZadBuilder extends UGraphicNo implements UGraphic {
}
private void drawRectangle(URectangle shape) {
final MinMax area = shape.getMinMax().translate(translate);
final MinMax area = shape.getMinMax().translate(getTranslate());
// System.err.println("ZadBuilder " + shape + " " + area);
context.zad.add(area);
}
public ColorMapper getColorMapper() {
throw new UnsupportedOperationException();
}
public void flushUg() {
}
public boolean matchesProperty(String propertyName) {
return false;
}
public double dpiFactor() {
return 1;
}
public Zad getZad() {
return context.zad;
}

View File

@ -35,6 +35,8 @@
*/
package net.sourceforge.plantuml.svek.image;
import static net.sourceforge.plantuml.utils.ObjectUtils.instanceOfAny;
import java.awt.geom.Dimension2D;
import java.awt.geom.Point2D;
import java.util.ArrayList;
@ -50,8 +52,6 @@ import net.sourceforge.plantuml.ugraphic.UGraphicNo;
import net.sourceforge.plantuml.ugraphic.UHorizontalLine;
import net.sourceforge.plantuml.ugraphic.UImage;
import net.sourceforge.plantuml.ugraphic.ULine;
import net.sourceforge.plantuml.ugraphic.UParam;
import net.sourceforge.plantuml.ugraphic.UParamNull;
import net.sourceforge.plantuml.ugraphic.UPath;
import net.sourceforge.plantuml.ugraphic.URectangle;
import net.sourceforge.plantuml.ugraphic.UShape;
@ -71,48 +71,35 @@ public class Footprint {
}
class MyUGraphic extends UGraphicNo implements UGraphic {
class MyUGraphic extends UGraphicNo {
private final UTranslate translate;
private final List<Point2D.Double> all;
public double dpiFactor() {
return 1;
}
private MyUGraphic(List<Point2D.Double> all, UTranslate translate) {
this.all = all;
this.translate = translate;
}
public boolean matchesProperty(String propertyName) {
return false;
}
public MyUGraphic() {
this(new ArrayList<Point2D.Double>(), new UTranslate());
super(stringBounder);
this.all = new ArrayList<>();
}
private MyUGraphic(MyUGraphic other, UChange change) {
super(other, change);
if (!instanceOfAny(change,
UBackground.class,
HColor.class,
UStroke.class,
UTranslate.class
)) {
throw new UnsupportedOperationException(change.getClass().toString());
}
this.all = other.all;
}
public UGraphic apply(UChange change) {
if (change instanceof UTranslate) {
return new MyUGraphic(all, translate.compose((UTranslate) change));
} else if (change instanceof UStroke || change instanceof HColor || change instanceof UBackground) {
return new MyUGraphic(all, translate);
}
throw new UnsupportedOperationException();
}
public StringBounder getStringBounder() {
return stringBounder;
}
public UParam getParam() {
return new UParamNull();
return new MyUGraphic(this, change);
}
public void draw(UShape shape) {
final double x = translate.getDx();
final double y = translate.getDy();
final double x = getTranslate().getDx();
final double y = getTranslate().getDy();
if (shape instanceof UText) {
drawText(x, y, (UText) shape);
} else if (shape instanceof UHorizontalLine) {
@ -142,7 +129,7 @@ public class Footprint {
}
private void drawText(double x, double y, UText text) {
final Dimension2D dim = stringBounder.calculateDimension(text.getFontConfiguration().getFont(),
final Dimension2D dim = getStringBounder().calculateDimension(text.getFontConfiguration().getFont(),
text.getText());
y -= dim.getHeight() - 1.5;
addPoint(x, y);
@ -172,10 +159,6 @@ public class Footprint {
addPoint(x, y);
addPoint(x + rect.getWidth(), y + rect.getHeight());
}
public void flushUg() {
}
}
public ContainingEllipse getEllipse(UDrawable drawable, double alpha) {

View File

@ -35,6 +35,8 @@
*/
package net.sourceforge.plantuml.ugraphic;
import static net.sourceforge.plantuml.utils.ObjectUtils.instanceOfAny;
import java.awt.geom.Dimension2D;
import net.sourceforge.plantuml.activitydiagram3.ftile.CenteredText;
@ -46,66 +48,38 @@ import net.sourceforge.plantuml.ugraphic.color.ColorMapper;
import net.sourceforge.plantuml.ugraphic.color.ColorMapperIdentity;
import net.sourceforge.plantuml.ugraphic.color.HColor;
public class LimitFinder extends UGraphicNo implements UGraphic {
public boolean matchesProperty(String propertyName) {
return false;
}
public double dpiFactor() {
return 1;
}
public class LimitFinder extends UGraphicNo {
@Override
public UGraphic apply(UChange change) {
if (change instanceof UTranslate) {
return new LimitFinder(stringBounder, minmax, translate.compose((UTranslate) change), clip);
} else if (change instanceof UStroke) {
return new LimitFinder(this);
} else if (change instanceof UBackground) {
return new LimitFinder(this);
} else if (change instanceof HColor) {
return new LimitFinder(this);
} else if (change instanceof UHidden) {
return new LimitFinder(this);
} else if (change instanceof UAntiAliasing) {
return new LimitFinder(this);
} else if (change instanceof UScale) {
return new LimitFinder(this);
} else if (change instanceof UClip) {
final LimitFinder copy = new LimitFinder(this);
copy.clip = (UClip) change;
copy.clip = copy.clip.translate(translate);
return copy;
}
throw new UnsupportedOperationException(change.getClass().toString());
return new LimitFinder(this, change);
}
private final StringBounder stringBounder;
private final UTranslate translate;
private final MinMaxMutable minmax;
private UClip clip;
private final UClip clip;
public LimitFinder(StringBounder stringBounder, boolean initToZero) {
this(stringBounder, MinMaxMutable.getEmpty(initToZero), new UTranslate(), null);
super(stringBounder);
this.minmax = MinMaxMutable.getEmpty(initToZero);
this.clip = null;
}
private LimitFinder(StringBounder stringBounder, MinMaxMutable minmax, UTranslate translate, UClip clip) {
this.stringBounder = stringBounder;
this.minmax = minmax;
this.translate = translate;
this.clip = clip;
}
private LimitFinder(LimitFinder other) {
this(other.stringBounder, other.minmax, other.translate, other.clip);
}
public StringBounder getStringBounder() {
return stringBounder;
}
public UParam getParam() {
return new UParamNull();
private LimitFinder(LimitFinder other, UChange change) {
super(other, change);
if (!instanceOfAny(change,
UAntiAliasing.class,
UBackground.class,
UClip.class,
HColor.class,
UHidden.class,
UScale.class,
UStroke.class,
UTranslate.class
)) {
throw new UnsupportedOperationException(change.getClass().toString());
}
this.clip = change instanceof UClip ? ((UClip) change).translate(getTranslate()) : other.clip;
this.minmax = other.minmax;
}
private void addPoint(double x, double y) {
@ -115,8 +89,8 @@ public class LimitFinder extends UGraphicNo implements UGraphic {
}
public void draw(UShape shape) {
final double x = translate.getDx();
final double y = translate.getDy();
final double x = getTranslate().getDx();
final double y = getTranslate().getDy();
if (shape instanceof UText) {
drawText(x, y, (UText) shape);
} else if (shape instanceof ULine) {
@ -208,7 +182,7 @@ public class LimitFinder extends UGraphicNo implements UGraphic {
}
private void drawText(double x, double y, UText text) {
final Dimension2D dim = stringBounder.calculateDimension(text.getFontConfiguration().getFont(), text.getText());
final Dimension2D dim = getStringBounder().calculateDimension(text.getFontConfiguration().getFont(), text.getText());
y -= dim.getHeight() - 1.5;
addPoint(x, y);
addPoint(x, y + dim.getHeight());
@ -243,7 +217,4 @@ public class LimitFinder extends UGraphicNo implements UGraphic {
return MinMax.fromMutable(minmax);
}
public void flushUg() {
}
}

View File

@ -35,75 +35,50 @@
*/
package net.sourceforge.plantuml.ugraphic;
import static net.sourceforge.plantuml.utils.ObjectUtils.instanceOfAny;
import java.awt.geom.Dimension2D;
import net.sourceforge.plantuml.graphic.StringBounder;
import net.sourceforge.plantuml.ugraphic.color.ColorMapper;
import net.sourceforge.plantuml.ugraphic.color.HColor;
public class TextLimitFinder extends UGraphicNo implements UGraphic {
public boolean matchesProperty(String propertyName) {
return false;
}
public double dpiFactor() {
return 1;
}
public class TextLimitFinder extends UGraphicNo {
@Override
public UGraphic apply(UChange change) {
if (change instanceof UTranslate) {
return new TextLimitFinder(stringBounder, minmax, translate.compose((UTranslate) change));
} else if (change instanceof UStroke) {
return new TextLimitFinder(this);
} else if (change instanceof UBackground) {
return new TextLimitFinder(this);
} else if (change instanceof HColor) {
return new TextLimitFinder(this);
}
throw new UnsupportedOperationException();
return new TextLimitFinder(this, change);
}
private final StringBounder stringBounder;
private final UTranslate translate;
private final MinMaxMutable minmax;
public TextLimitFinder(StringBounder stringBounder, boolean initToZero) {
this(stringBounder, MinMaxMutable.getEmpty(initToZero), new UTranslate());
super(stringBounder);
this.minmax = MinMaxMutable.getEmpty(initToZero);
}
private TextLimitFinder(StringBounder stringBounder, MinMaxMutable minmax, UTranslate translate) {
this.stringBounder = stringBounder;
this.minmax = minmax;
this.translate = translate;
}
private TextLimitFinder(TextLimitFinder other) {
this(other.stringBounder, other.minmax, other.translate);
}
public StringBounder getStringBounder() {
return stringBounder;
}
public UParam getParam() {
return new UParamNull();
private TextLimitFinder(TextLimitFinder other, UChange change) {
super(other, change);
if (!instanceOfAny(change,
UBackground.class,
HColor.class,
UStroke.class,
UTranslate.class
)) {
throw new UnsupportedOperationException(change.getClass().toString());
}
this.minmax = other.minmax;
}
public void draw(UShape shape) {
if (shape instanceof UText) {
final double x = translate.getDx();
final double y = translate.getDy();
final double x = getTranslate().getDx();
final double y = getTranslate().getDy();
drawText(x, y, (UText) shape);
}
}
public ColorMapper getColorMapper() {
throw new UnsupportedOperationException();
}
private void drawText(double x, double y, UText text) {
final Dimension2D dim = stringBounder.calculateDimension(text.getFontConfiguration().getFont(), text.getText());
final Dimension2D dim = getStringBounder().calculateDimension(text.getFontConfiguration().getFont(), text.getText());
y -= dim.getHeight() - 1.5;
minmax.addPoint(x, y);
minmax.addPoint(x, y + dim.getHeight());
@ -127,7 +102,4 @@ public class TextLimitFinder extends UGraphicNo implements UGraphic {
return minmax.getMinY();
}
public void flushUg() {
}
}

View File

@ -36,25 +36,80 @@
package net.sourceforge.plantuml.ugraphic;
import net.sourceforge.plantuml.Url;
import net.sourceforge.plantuml.graphic.StringBounder;
import net.sourceforge.plantuml.ugraphic.color.ColorMapper;
import net.sourceforge.plantuml.ugraphic.color.HColor;
import net.sourceforge.plantuml.ugraphic.color.HColorUtils;
public abstract class UGraphicNo {
public abstract class UGraphicNo implements UGraphic {
private final StringBounder stringBounder;
private final UTranslate translate;
public UGraphicNo(StringBounder stringBounder) {
this.stringBounder = stringBounder;
this.translate = new UTranslate();
}
public UGraphicNo(UGraphicNo other, UChange change) {
this.stringBounder = other.stringBounder;
this.translate = change instanceof UTranslate ? other.translate.compose((UTranslate) change) : other.translate;
}
//
// Implement UGraphic
//
@Override
final public void startUrl(Url url) {
}
@Override
public void startGroup(UGroupType type, String ident) {
}
@Override
final public void closeUrl() {
}
@Override
final public void closeGroup() {
}
@Override
public ColorMapper getColorMapper() {
throw new UnsupportedOperationException();
}
@Override
public HColor getDefaultBackground() {
return HColorUtils.BLACK;
}
@Override
public UParam getParam() {
return new UParamNull();
}
@Override
public StringBounder getStringBounder() {
return stringBounder;
}
@Override
public void flushUg() {
}
@Override
public boolean matchesProperty(String propertyName) {
return false;
}
//
// Internal things
//
protected UTranslate getTranslate() {
return translate;
}
}

View File

@ -35,6 +35,7 @@
*/
package net.sourceforge.plantuml.ugraphic.comp;
import static net.sourceforge.plantuml.utils.ObjectUtils.instanceOfAny;
import net.sourceforge.plantuml.graphic.StringBounder;
import net.sourceforge.plantuml.ugraphic.TextLimitFinder;
import net.sourceforge.plantuml.ugraphic.UBackground;
@ -43,8 +44,6 @@ import net.sourceforge.plantuml.ugraphic.UEllipse;
import net.sourceforge.plantuml.ugraphic.UEmpty;
import net.sourceforge.plantuml.ugraphic.UGraphic;
import net.sourceforge.plantuml.ugraphic.UGraphicNo;
import net.sourceforge.plantuml.ugraphic.UParam;
import net.sourceforge.plantuml.ugraphic.UParamNull;
import net.sourceforge.plantuml.ugraphic.UPath;
import net.sourceforge.plantuml.ugraphic.UPolygon;
import net.sourceforge.plantuml.ugraphic.URectangle;
@ -57,61 +56,40 @@ import net.sourceforge.plantuml.ugraphic.color.ColorMapper;
import net.sourceforge.plantuml.ugraphic.color.ColorMapperIdentity;
import net.sourceforge.plantuml.ugraphic.color.HColor;
public class SlotFinder extends UGraphicNo implements UGraphic {
public boolean matchesProperty(String propertyName) {
return false;
}
public double dpiFactor() {
return 1;
}
public class SlotFinder extends UGraphicNo {
@Override
public UGraphic apply(UChange change) {
if (change instanceof UTranslate) {
return new SlotFinder(mode, stringBounder, slot, translate.compose((UTranslate) change));
} else if (change instanceof UStroke) {
return new SlotFinder(this);
} else if (change instanceof UBackground) {
return new SlotFinder(this);
} else if (change instanceof HColor) {
return new SlotFinder(this);
}
throw new UnsupportedOperationException();
return new SlotFinder(this, change);
}
private final SlotSet slot;
private final StringBounder stringBounder;
private final UTranslate translate;
private final CompressionMode mode;
public SlotFinder(CompressionMode mode, StringBounder stringBounder) {
this(mode, stringBounder, new SlotSet(), new UTranslate());
}
private SlotFinder(CompressionMode mode, StringBounder stringBounder, SlotSet slot, UTranslate translate) {
this.stringBounder = stringBounder;
this.slot = slot;
this.translate = translate;
super(stringBounder);
this.slot = new SlotSet();
this.mode = mode;
}
private SlotFinder(SlotFinder other) {
this(other.mode, other.stringBounder, other.slot, other.translate);
}
public StringBounder getStringBounder() {
return stringBounder;
}
public UParam getParam() {
return new UParamNull();
private SlotFinder(SlotFinder other, UChange change) {
super(other, change);
if (!instanceOfAny(change,
UBackground.class,
HColor.class,
UStroke.class,
UTranslate.class
)) {
throw new UnsupportedOperationException(change.getClass().toString());
}
this.mode = other.mode;
this.slot = other.slot;
}
public void draw(UShape sh) {
final double x = translate.getDx();
final double y = translate.getDy();
final double x = getTranslate().getDx();
final double y = getTranslate().getDy();
if (sh instanceof UShapeIgnorableForCompression) {
final UShapeIgnorableForCompression shape = (UShapeIgnorableForCompression) sh;
if (shape.isIgnoreForCompressionOn(mode)) {
@ -154,7 +132,7 @@ public class SlotFinder extends UGraphicNo implements UGraphic {
}
private void drawText(double x, double y, UText shape) {
final TextLimitFinder finder = new TextLimitFinder(stringBounder, false);
final TextLimitFinder finder = new TextLimitFinder(getStringBounder(), false);
finder.apply(new UTranslate(x, y)).draw(shape);
if (mode == CompressionMode.ON_X) {
slot.addSlot(finder.getMinX(), finder.getMaxX());
@ -198,7 +176,4 @@ public class SlotFinder extends UGraphicNo implements UGraphic {
return slot;
}
public void flushUg() {
}
}

View File

@ -0,0 +1,13 @@
package net.sourceforge.plantuml.utils;
public class ObjectUtils {
public static boolean instanceOfAny(Object object, Class<?>... classes) {
for (Class<?> c : classes) {
if (c.isInstance(object)) {
return true;
}
}
return false;
}
}