2019-07-14 20:09:26 +00:00
|
|
|
/* ========================================================================
|
|
|
|
* PlantUML : a free UML diagram generator
|
|
|
|
* ========================================================================
|
|
|
|
*
|
|
|
|
* (C) Copyright 2009-2020, Arnaud Roques
|
|
|
|
*
|
|
|
|
* Project Info: http://plantuml.com
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
package net.sourceforge.plantuml.style;
|
|
|
|
|
|
|
|
import java.util.EnumMap;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Map.Entry;
|
2019-08-26 17:07:21 +00:00
|
|
|
import java.util.StringTokenizer;
|
2019-07-14 20:09:26 +00:00
|
|
|
|
|
|
|
import net.sourceforge.plantuml.ISkinSimple;
|
2019-08-26 17:07:21 +00:00
|
|
|
import net.sourceforge.plantuml.LineBreakStrategy;
|
2019-07-14 20:09:26 +00:00
|
|
|
import net.sourceforge.plantuml.cucadiagram.Display;
|
|
|
|
import net.sourceforge.plantuml.graphic.FontConfiguration;
|
|
|
|
import net.sourceforge.plantuml.graphic.HorizontalAlignment;
|
|
|
|
import net.sourceforge.plantuml.graphic.SymbolContext;
|
|
|
|
import net.sourceforge.plantuml.graphic.TextBlock;
|
|
|
|
import net.sourceforge.plantuml.graphic.TextBlockUtils;
|
|
|
|
import net.sourceforge.plantuml.graphic.color.ColorType;
|
|
|
|
import net.sourceforge.plantuml.graphic.color.Colors;
|
|
|
|
import net.sourceforge.plantuml.ugraphic.UFont;
|
2019-08-26 17:07:21 +00:00
|
|
|
import net.sourceforge.plantuml.ugraphic.UGraphic;
|
2019-07-14 20:09:26 +00:00
|
|
|
import net.sourceforge.plantuml.ugraphic.UStroke;
|
2020-03-18 10:50:02 +00:00
|
|
|
import net.sourceforge.plantuml.ugraphic.color.HColor;
|
2020-04-19 16:04:39 +00:00
|
|
|
import net.sourceforge.plantuml.ugraphic.color.HColorNone;
|
2020-03-18 10:50:02 +00:00
|
|
|
import net.sourceforge.plantuml.ugraphic.color.HColorSet;
|
2019-07-14 20:09:26 +00:00
|
|
|
|
|
|
|
public class Style {
|
|
|
|
|
|
|
|
private final Map<PName, Value> map;
|
2019-08-26 17:07:21 +00:00
|
|
|
private final StyleSignature signature;
|
2019-07-14 20:09:26 +00:00
|
|
|
|
2019-08-26 17:07:21 +00:00
|
|
|
public Style(StyleSignature signature, Map<PName, Value> map) {
|
|
|
|
this.map = map;
|
|
|
|
this.signature = signature;
|
2019-07-14 20:09:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String toString() {
|
2019-08-26 17:07:21 +00:00
|
|
|
return signature + " " + map;
|
2019-07-14 20:09:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public Value value(PName name) {
|
|
|
|
final Value result = map.get(name);
|
|
|
|
if (result == null) {
|
|
|
|
return ValueNull.NULL;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Style mergeWith(Style other) {
|
|
|
|
if (other == null) {
|
2019-08-26 17:07:21 +00:00
|
|
|
return this;
|
2019-07-14 20:09:26 +00:00
|
|
|
}
|
|
|
|
final EnumMap<PName, Value> both = new EnumMap<PName, Value>(this.map);
|
|
|
|
for (Entry<PName, Value> ent : other.map.entrySet()) {
|
|
|
|
final Value previous = this.map.get(ent.getKey());
|
|
|
|
if (previous == null || ent.getValue().getPriority() > previous.getPriority()) {
|
|
|
|
both.put(ent.getKey(), ent.getValue());
|
|
|
|
}
|
|
|
|
}
|
2019-08-26 17:07:21 +00:00
|
|
|
return new Style(this.signature.mergeWith(other.getSignature()), both);
|
|
|
|
// if (this.name.equals(other.name)) {
|
|
|
|
// return new Style(this.kind.add(other.kind), this.name, both);
|
|
|
|
// }
|
2020-04-19 16:04:39 +00:00
|
|
|
// return new Style(this.kind.add(other.kind), this.name + "," + other.name,
|
|
|
|
// both);
|
2019-07-14 20:09:26 +00:00
|
|
|
}
|
|
|
|
|
2020-03-18 10:50:02 +00:00
|
|
|
public Style eventuallyOverride(PName param, HColor color) {
|
2019-07-14 20:09:26 +00:00
|
|
|
if (color == null) {
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
final EnumMap<PName, Value> result = new EnumMap<PName, Value>(this.map);
|
2019-08-26 17:07:21 +00:00
|
|
|
final Value old = result.get(param);
|
|
|
|
result.put(param, new ValueColor(color, old.getPriority()));
|
|
|
|
// return new Style(kind, name + "-" + color, result);
|
|
|
|
return new Style(this.signature, result);
|
2019-07-14 20:09:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public Style eventuallyOverride(Colors colors) {
|
|
|
|
Style result = this;
|
2019-08-26 17:07:21 +00:00
|
|
|
if (colors != null) {
|
2020-03-18 10:50:02 +00:00
|
|
|
final HColor back = colors.getColor(ColorType.BACK);
|
2019-08-26 17:07:21 +00:00
|
|
|
if (back != null) {
|
2019-09-14 18:12:04 +00:00
|
|
|
result = result.eventuallyOverride(PName.BackGroundColor, back);
|
|
|
|
}
|
2020-03-18 10:50:02 +00:00
|
|
|
final HColor line = colors.getColor(ColorType.LINE);
|
2019-09-14 18:12:04 +00:00
|
|
|
if (line != null) {
|
|
|
|
result = result.eventuallyOverride(PName.LineColor, line);
|
2019-08-26 17:07:21 +00:00
|
|
|
}
|
2019-07-14 20:09:26 +00:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2019-09-22 17:20:16 +00:00
|
|
|
public Style eventuallyOverride(SymbolContext symbolContext) {
|
|
|
|
Style result = this;
|
|
|
|
if (symbolContext != null) {
|
2020-03-18 10:50:02 +00:00
|
|
|
final HColor back = symbolContext.getBackColor();
|
2019-09-22 17:20:16 +00:00
|
|
|
if (back != null) {
|
|
|
|
result = result.eventuallyOverride(PName.BackGroundColor, back);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2019-08-26 17:07:21 +00:00
|
|
|
public StyleSignature getSignature() {
|
|
|
|
return signature;
|
2019-07-14 20:09:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public UFont getUFont() {
|
|
|
|
final String family = value(PName.FontName).asString();
|
|
|
|
final int fontStyle = value(PName.FontStyle).asFontStyle();
|
|
|
|
final int size = value(PName.FontSize).asInt();
|
|
|
|
return new UFont(family, fontStyle, size);
|
|
|
|
}
|
|
|
|
|
2020-03-18 10:50:02 +00:00
|
|
|
public FontConfiguration getFontConfiguration(HColorSet set) {
|
2019-07-14 20:09:26 +00:00
|
|
|
final UFont font = getUFont();
|
2020-03-18 10:50:02 +00:00
|
|
|
final HColor color = value(PName.FontColor).asColor(set);
|
|
|
|
final HColor hyperlinkColor = value(PName.HyperLinkColor).asColor(set);
|
2019-07-14 20:09:26 +00:00
|
|
|
return new FontConfiguration(font, color, hyperlinkColor, true);
|
|
|
|
}
|
|
|
|
|
2020-03-18 10:50:02 +00:00
|
|
|
public SymbolContext getSymbolContext(HColorSet set) {
|
|
|
|
final HColor backColor = value(PName.BackGroundColor).asColor(set);
|
|
|
|
final HColor foreColor = value(PName.LineColor).asColor(set);
|
2019-07-14 20:09:26 +00:00
|
|
|
final double deltaShadowing = value(PName.Shadowing).asDouble();
|
|
|
|
return new SymbolContext(backColor, foreColor).withStroke(getStroke()).withDeltaShadow(deltaShadowing);
|
|
|
|
}
|
|
|
|
|
|
|
|
public UStroke getStroke() {
|
|
|
|
final double thickness = value(PName.LineThickness).asDouble();
|
2019-08-26 17:07:21 +00:00
|
|
|
final String dash = value(PName.LineStyle).asString();
|
|
|
|
if (dash.length() == 0) {
|
|
|
|
return new UStroke(thickness);
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
final StringTokenizer st = new StringTokenizer(dash, "-;,");
|
|
|
|
final double dashVisible = Double.parseDouble(st.nextToken().trim());
|
|
|
|
double dashSpace = dashVisible;
|
|
|
|
if (st.hasMoreTokens()) {
|
|
|
|
dashSpace = Double.parseDouble(st.nextToken().trim());
|
|
|
|
}
|
|
|
|
return new UStroke(dashVisible, dashSpace, thickness);
|
|
|
|
} catch (Exception e) {
|
|
|
|
return new UStroke(thickness);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public LineBreakStrategy wrapWidth() {
|
|
|
|
final String value = value(PName.MaximumWidth).asString();
|
|
|
|
return new LineBreakStrategy(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
public ClockwiseTopRightBottomLeft getPadding() {
|
2020-05-07 14:12:08 +00:00
|
|
|
final String padding = value(PName.Padding).asString();
|
|
|
|
return ClockwiseTopRightBottomLeft.read(padding);
|
2019-08-26 17:07:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public ClockwiseTopRightBottomLeft getMargin() {
|
2020-05-07 14:12:08 +00:00
|
|
|
final String margin = value(PName.Margin).asString();
|
|
|
|
return ClockwiseTopRightBottomLeft.read(margin);
|
2019-08-26 17:07:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public HorizontalAlignment getHorizontalAlignment() {
|
|
|
|
return value(PName.HorizontalAlignment).asHorizontalAlignment();
|
2019-07-14 20:09:26 +00:00
|
|
|
}
|
|
|
|
|
2020-03-18 10:50:02 +00:00
|
|
|
private TextBlock createTextBlockInternal(Display display, HColorSet set, ISkinSimple spriteContainer,
|
2019-09-14 18:12:04 +00:00
|
|
|
HorizontalAlignment alignment) {
|
2019-07-14 20:09:26 +00:00
|
|
|
final FontConfiguration fc = getFontConfiguration(set);
|
2019-09-14 18:12:04 +00:00
|
|
|
return display.create(fc, alignment, spriteContainer);
|
2019-07-14 20:09:26 +00:00
|
|
|
}
|
|
|
|
|
2020-03-18 10:50:02 +00:00
|
|
|
public TextBlock createTextBlockBordered(Display note, HColorSet set, ISkinSimple spriteContainer) {
|
2019-09-14 18:12:04 +00:00
|
|
|
// final HorizontalAlignment alignment = HorizontalAlignment.LEFT;
|
|
|
|
final HorizontalAlignment alignment = this.getHorizontalAlignment();
|
|
|
|
final TextBlock textBlock = this.createTextBlockInternal(note, set, spriteContainer, alignment);
|
2019-07-14 20:09:26 +00:00
|
|
|
|
2020-03-18 10:50:02 +00:00
|
|
|
final HColor legendBackgroundColor = this.value(PName.BackGroundColor).asColor(set);
|
|
|
|
final HColor legendColor = this.value(PName.LineColor).asColor(set);
|
2019-07-14 20:09:26 +00:00
|
|
|
final UStroke stroke = this.getStroke();
|
|
|
|
final int cornersize = this.value(PName.RoundCorner).asInt();
|
2020-05-07 14:12:08 +00:00
|
|
|
final ClockwiseTopRightBottomLeft margin = this.getMargin();
|
|
|
|
final ClockwiseTopRightBottomLeft padding = this.getPadding();
|
2019-07-14 20:09:26 +00:00
|
|
|
final TextBlock result = TextBlockUtils.bordered(textBlock, stroke, legendColor, legendBackgroundColor,
|
2020-05-07 14:12:08 +00:00
|
|
|
cornersize, padding);
|
|
|
|
return TextBlockUtils.withMargin(result, margin);
|
2019-07-14 20:09:26 +00:00
|
|
|
}
|
|
|
|
|
2020-03-18 10:50:02 +00:00
|
|
|
public UGraphic applyStrokeAndLineColor(UGraphic ug, HColorSet colorSet) {
|
2020-04-19 16:04:39 +00:00
|
|
|
final HColor color = value(PName.LineColor).asColor(colorSet);
|
|
|
|
if (color == null) {
|
|
|
|
ug = ug.apply(new HColorNone());
|
|
|
|
} else {
|
|
|
|
ug = ug.apply(color);
|
|
|
|
}
|
2019-08-26 17:07:21 +00:00
|
|
|
ug = ug.apply(getStroke());
|
|
|
|
return ug;
|
|
|
|
}
|
2019-09-22 17:20:16 +00:00
|
|
|
|
2019-07-14 20:09:26 +00:00
|
|
|
}
|