1
0
mirror of https://github.com/octoleo/plantuml.git synced 2024-06-16 23:22:24 +00:00
plantuml/src/net/sourceforge/plantuml/ugraphic/comp/UGraphicCompressOnXorY.java

124 lines
4.1 KiB
Java
Raw Normal View History

2013-12-10 19:36:50 +00:00
/* ========================================================================
* PlantUML : a free UML diagram generator
* ========================================================================
*
2016-01-09 12:15:40 +00:00
* (C) Copyright 2009-2017, Arnaud Roques
2013-12-10 19:36:50 +00:00
*
2016-03-06 16:47:34 +00:00
* Project Info: http://plantuml.com
2013-12-10 19:36:50 +00:00
*
2017-03-15 19:13:31 +00:00
* If you like this project or if you find it useful, you can support us at:
*
* http://plantuml.com/patreon (only 1$ per month!)
* http://plantuml.com/paypal
*
2013-12-10 19:36:50 +00:00
* This file is part of PlantUML.
*
* PlantUML is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* PlantUML distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
* License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
* USA.
*
*
* Original Author: Arnaud Roques
*
*
*/
2018-07-27 21:56:46 +00:00
package net.sourceforge.plantuml.ugraphic.comp;
2013-12-10 19:36:50 +00:00
import net.sourceforge.plantuml.graphic.UGraphicDelegator;
2018-07-27 21:56:46 +00:00
import net.sourceforge.plantuml.ugraphic.UChange;
import net.sourceforge.plantuml.ugraphic.UChangeBackColor;
import net.sourceforge.plantuml.ugraphic.UChangeColor;
import net.sourceforge.plantuml.ugraphic.UGraphic;
import net.sourceforge.plantuml.ugraphic.ULine;
import net.sourceforge.plantuml.ugraphic.URectangle;
import net.sourceforge.plantuml.ugraphic.UShape;
import net.sourceforge.plantuml.ugraphic.UStroke;
import net.sourceforge.plantuml.ugraphic.UTranslate;
2013-12-10 19:36:50 +00:00
2018-07-27 21:56:46 +00:00
public class UGraphicCompressOnXorY extends UGraphicDelegator {
2013-12-10 19:36:50 +00:00
public UGraphic apply(UChange change) {
if (change instanceof UTranslate) {
2018-07-27 21:56:46 +00:00
return new UGraphicCompressOnXorY(mode, getUg(), compressionTransform, translate.compose((UTranslate) change));
2013-12-10 19:36:50 +00:00
} else if (change instanceof UStroke || change instanceof UChangeBackColor || change instanceof UChangeColor) {
2018-07-27 21:56:46 +00:00
return new UGraphicCompressOnXorY(mode, getUg().apply(change), compressionTransform, translate);
2013-12-10 19:36:50 +00:00
}
throw new UnsupportedOperationException();
}
2018-07-27 21:56:46 +00:00
private final CompressionMode mode;
2013-12-10 19:36:50 +00:00
private final CompressionTransform compressionTransform;
private final UTranslate translate;
2018-07-27 21:56:46 +00:00
public UGraphicCompressOnXorY(CompressionMode mode, UGraphic ug, CompressionTransform compressionTransform) {
this(mode, ug, compressionTransform, new UTranslate());
2013-12-10 19:36:50 +00:00
}
2018-07-27 21:56:46 +00:00
private UGraphicCompressOnXorY(CompressionMode mode, UGraphic ug, CompressionTransform compressionTransform,
UTranslate translate) {
2013-12-10 19:36:50 +00:00
super(ug);
2018-07-27 21:56:46 +00:00
this.mode = mode;
2013-12-10 19:36:50 +00:00
this.compressionTransform = compressionTransform;
this.translate = translate;
}
public void draw(UShape shape) {
final double x = translate.getDx();
final double y = translate.getDy();
2018-05-06 19:59:19 +00:00
if (shape instanceof URectangle) {
final URectangle rect = (URectangle) shape;
if (rect.isIgnoreForCompression()) {
2018-07-27 21:56:46 +00:00
if (mode == CompressionMode.ON_X) {
final double x2 = ct(x + rect.getWidth());
shape = rect.withWidth(x2 - ct(x));
} else {
final double y2 = ct(y + rect.getHeight());
shape = rect.withHeight(y2 - ct(y));
}
2018-05-06 19:59:19 +00:00
}
}
2013-12-10 19:36:50 +00:00
if (shape instanceof ULine) {
drawLine(x, y, (ULine) shape);
} else {
2018-07-27 21:56:46 +00:00
if (mode == CompressionMode.ON_X) {
getUg().apply(new UTranslate(ct(x), y)).draw(shape);
} else {
getUg().apply(new UTranslate(x, ct(y))).draw(shape);
}
2013-12-10 19:36:50 +00:00
}
}
private void drawLine(double x, double y, ULine shape) {
2018-07-27 21:56:46 +00:00
if (mode == CompressionMode.ON_X) {
drawLine(ct(x), y, ct(x + shape.getDX()), y + shape.getDY());
} else {
drawLine(x, ct(y), x + shape.getDX(), ct(y + shape.getDY()));
}
2013-12-10 19:36:50 +00:00
}
private double ct(double v) {
return compressionTransform.transform(v);
}
private void drawLine(double x1, double y1, double x2, double y2) {
2015-06-07 10:23:10 +00:00
if (y1 > y2) {
drawLine(x2, y2, x1, y1);
return;
}
assert y1 <= y2;
2018-07-27 21:56:46 +00:00
getUg().apply(new UTranslate(x1, y1)).draw(new ULine(x2 - x1, y2 - y1));
2013-12-10 19:36:50 +00:00
}
}