plantuml/src/net/sourceforge/plantuml/style/Style.java

320 lines
11 KiB
Java
Raw Normal View History

2019-07-14 20:09:26 +00:00
/* ========================================================================
* PlantUML : a free UML diagram generator
* ========================================================================
*
2022-03-07 19:33:46 +00:00
* (C) Copyright 2009-2023, Arnaud Roques
2019-07-14 20:09:26 +00:00
*
* 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;
import net.sourceforge.plantuml.ugraphic.color.HColorSet;
2022-08-19 16:34:21 +00:00
import net.sourceforge.plantuml.ugraphic.color.HColors;
2019-07-14 20:09:26 +00:00
public class Style {
private final Map<PName, Value> map;
2022-03-01 18:11:51 +00:00
private final StyleSignatureBasic signature;
2019-07-14 20:09:26 +00:00
2022-03-01 18:11:51 +00:00
public Style(StyleSignatureBasic signature, Map<PName, Value> map) {
2019-08-26 17:07:21 +00:00
this.map = map;
this.signature = signature;
2019-07-14 20:09:26 +00:00
}
2021-08-30 17:13:54 +00:00
public Style deltaPriority(int delta) {
2022-01-28 21:45:34 +00:00
if (signature.isStarred() == false)
2021-08-30 17:13:54 +00:00
throw new UnsupportedOperationException();
2022-01-28 21:45:34 +00:00
2021-08-30 17:13:54 +00:00
final EnumMap<PName, Value> copy = new EnumMap<PName, Value>(PName.class);
2022-01-28 21:45:34 +00:00
for (Entry<PName, Value> ent : this.map.entrySet())
2022-02-10 18:16:18 +00:00
copy.put(ent.getKey(), ((ValueImpl) ent.getValue()).addPriority(delta));
2022-01-28 21:45:34 +00:00
2021-08-30 17:13:54 +00:00
return new Style(this.signature, copy);
}
2021-05-03 20:11:48 +00:00
public void printMe() {
2022-01-28 21:45:34 +00:00
if (map.size() == 0)
2021-05-03 20:11:48 +00:00
return;
2022-01-28 21:45:34 +00:00
2021-05-03 20:11:48 +00:00
System.err.println(signature + " {");
2022-01-28 21:45:34 +00:00
for (Entry<PName, Value> ent : map.entrySet())
2021-05-03 20:11:48 +00:00
System.err.println(" " + ent.getKey() + ": " + ent.getValue().asString());
System.err.println("}");
}
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);
2022-01-28 21:45:34 +00:00
if (result == null)
2019-07-14 20:09:26 +00:00
return ValueNull.NULL;
2022-01-28 21:45:34 +00:00
2019-07-14 20:09:26 +00:00
return result;
}
public boolean hasValue(PName name) {
return map.containsKey(name);
}
2022-03-07 19:33:46 +00:00
public Style mergeWith(Style other, MergeStrategy strategy) {
2022-01-28 21:45:34 +00:00
if (other == null)
2019-08-26 17:07:21 +00:00
return this;
2021-08-30 17:13:54 +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());
2022-03-07 19:33:46 +00:00
if (previous != null && previous.getPriority() > StyleLoader.DELTA_PRIORITY_FOR_STEREOTYPE
&& strategy == MergeStrategy.KEEP_EXISTING_VALUE_OF_STEREOTYPE)
continue;
2022-02-10 18:16:18 +00:00
final PName key = ent.getKey();
both.put(key, ((ValueImpl) ent.getValue()).mergeWith(previous));
2021-08-30 17:13:54 +00:00
}
return new Style(this.signature.mergeWith(other.getSignature()), both);
2019-07-14 20:09:26 +00:00
}
2020-03-18 10:50:02 +00:00
public Style eventuallyOverride(PName param, HColor color) {
2022-01-28 21:45:34 +00:00
if (color == null)
2019-07-14 20:09:26 +00:00
return this;
2022-01-28 21:45:34 +00:00
2019-07-14 20:09:26 +00:00
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(this.signature, result);
2019-07-14 20:09:26 +00:00
}
2021-04-07 18:02:23 +00:00
public Style eventuallyOverride(PName param, double value) {
return eventuallyOverride(param, "" + value);
}
public Style eventuallyOverride(PName param, String value) {
final EnumMap<PName, Value> result = new EnumMap<PName, Value>(this.map);
2022-02-10 18:16:18 +00:00
result.put(param, ValueImpl.regular(value, Integer.MAX_VALUE));
2021-04-07 18:02:23 +00:00
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);
2022-01-28 21:45:34 +00:00
if (back != null)
2019-09-14 18:12:04 +00:00
result = result.eventuallyOverride(PName.BackGroundColor, back);
2022-01-28 21:45:34 +00:00
2020-03-18 10:50:02 +00:00
final HColor line = colors.getColor(ColorType.LINE);
2022-01-28 21:45:34 +00:00
if (line != null)
2019-09-14 18:12:04 +00:00
result = result.eventuallyOverride(PName.LineColor, line);
2022-01-28 21:45:34 +00:00
2021-04-02 17:26:59 +00:00
final HColor text = colors.getColor(ColorType.TEXT);
2022-01-28 21:45:34 +00:00
if (text != null)
2021-04-02 17:26:59 +00:00
result = result.eventuallyOverride(PName.FontColor, text);
2022-01-28 21:45:34 +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();
2022-01-28 21:45:34 +00:00
if (back != null)
2019-09-22 17:20:16 +00:00
result = result.eventuallyOverride(PName.BackGroundColor, back);
2022-01-28 21:45:34 +00:00
2019-09-22 17:20:16 +00:00
}
return result;
}
2022-03-01 18:11:51 +00:00
public StyleSignatureBasic getSignature() {
2019-08-26 17:07:21 +00:00
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();
2022-12-16 16:20:14 +00:00
int size = value(PName.FontSize).asInt(true);
if (size == -1)
2022-12-06 19:47:32 +00:00
size = 14;
2019-07-14 20:09:26 +00:00
return new UFont(family, fontStyle, size);
}
2022-09-18 17:08:06 +00:00
public FontConfiguration getFontConfiguration(HColorSet set) {
return getFontConfiguration(set, null);
2022-05-27 14:18:47 +00:00
}
2022-09-18 17:08:06 +00:00
public FontConfiguration getFontConfiguration(HColorSet set, Colors colors) {
2019-07-14 20:09:26 +00:00
final UFont font = getUFont();
2022-05-27 14:18:47 +00:00
HColor color = colors == null ? null : colors.getColor(ColorType.TEXT);
if (color == null)
2022-09-18 17:08:06 +00:00
color = value(PName.FontColor).asColor(set);
2022-05-27 14:18:47 +00:00
2022-09-18 17:08:06 +00:00
final HColor hyperlinkColor = value(PName.HyperLinkColor).asColor(set);
2022-10-05 20:32:57 +00:00
final UStroke stroke = getStroke(PName.HyperlinkUnderlineThickness, PName.HyperlinkUnderlineStyle);
return FontConfiguration.create(font, color, hyperlinkColor, stroke);
2019-07-14 20:09:26 +00:00
}
2022-09-18 17:08:06 +00:00
public SymbolContext getSymbolContext(HColorSet set, Colors colors) {
2022-08-30 19:15:53 +00:00
HColor backColor = colors == null ? null : colors.getColor(ColorType.BACK);
if (backColor == null)
2022-09-18 17:08:06 +00:00
backColor = value(PName.BackGroundColor).asColor(set);
2022-08-30 19:15:53 +00:00
HColor foreColor = colors == null ? null : colors.getColor(ColorType.LINE);
if (foreColor == null)
2022-09-18 17:08:06 +00:00
foreColor = value(PName.LineColor).asColor(set);
2019-07-14 20:09:26 +00:00
final double deltaShadowing = value(PName.Shadowing).asDouble();
2022-10-05 20:32:57 +00:00
final double roundCorner = value(PName.RoundCorner).asDouble();
final double diagonalCorner = value(PName.DiagonalCorner).asDouble();
return new SymbolContext(backColor, foreColor).withStroke(getStroke()).withDeltaShadow(deltaShadowing)
.withCorner(roundCorner, diagonalCorner);
2019-07-14 20:09:26 +00:00
}
2022-09-18 17:08:06 +00:00
public SymbolContext getSymbolContext(HColorSet set) {
return getSymbolContext(set, null);
2022-08-30 19:15:53 +00:00
}
2021-04-07 18:02:23 +00:00
public Style eventuallyOverride(UStroke stroke) {
2022-01-28 21:45:34 +00:00
if (stroke == null)
2021-04-07 18:02:23 +00:00
return this;
2022-01-28 21:45:34 +00:00
2021-04-07 18:02:23 +00:00
Style result = this.eventuallyOverride(PName.LineThickness, stroke.getThickness());
final double space = stroke.getDashSpace();
final double visible = stroke.getDashVisible();
2023-01-09 19:13:37 +00:00
result = result.eventuallyOverride(PName.LineStyle, "" + visible + "-" + space);
2021-04-07 18:02:23 +00:00
return result;
}
2019-07-14 20:09:26 +00:00
public UStroke getStroke() {
2022-10-05 20:32:57 +00:00
return getStroke(PName.LineThickness, PName.LineStyle);
}
private UStroke getStroke(final PName thicknessParam, final PName styleParam) {
final double thickness = value(thicknessParam).asDouble();
final String dash = value(styleParam).asString();
2022-01-28 21:45:34 +00:00
if (dash.length() == 0)
2019-08-26 17:07:21 +00:00
return new UStroke(thickness);
2022-01-28 21:45:34 +00:00
2019-08-26 17:07:21 +00:00
try {
final StringTokenizer st = new StringTokenizer(dash, "-;,");
final double dashVisible = Double.parseDouble(st.nextToken().trim());
double dashSpace = dashVisible;
2022-01-28 21:45:34 +00:00
if (st.hasMoreTokens())
2019-08-26 17:07:21 +00:00
dashSpace = Double.parseDouble(st.nextToken().trim());
2022-01-28 21:45:34 +00:00
2019-08-26 17:07:21 +00:00
return new UStroke(dashVisible, dashSpace, thickness);
} catch (Exception e) {
return new UStroke(thickness);
}
}
2021-04-02 17:26:59 +00:00
public UStroke getStroke(Colors colors) {
final UStroke stroke = colors.getSpecificLineStroke();
2022-01-28 21:45:34 +00:00
if (stroke == null)
2021-04-02 17:26:59 +00:00
return getStroke();
2022-01-28 21:45:34 +00:00
2021-04-02 17:26:59 +00:00
return stroke;
}
2019-08-26 17:07:21 +00:00
public LineBreakStrategy wrapWidth() {
final String value = value(PName.MaximumWidth).asString();
return new LineBreakStrategy(value);
}
public ClockwiseTopRightBottomLeft getPadding() {
final String padding = value(PName.Padding).asString();
return ClockwiseTopRightBottomLeft.read(padding);
2019-08-26 17:07:21 +00:00
}
public ClockwiseTopRightBottomLeft getMargin() {
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) {
2022-09-18 17:08:06 +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
}
2022-08-30 19:15:53 +00:00
2022-06-27 16:33:45 +00:00
public static final String ID_TITLE = "_title";
public static final String ID_CAPTION = "_caption";
public static final String ID_LEGEND = "_legend";
2019-07-14 20:09:26 +00:00
2022-06-27 16:33:45 +00:00
public TextBlock createTextBlockBordered(Display note, HColorSet set, ISkinSimple spriteContainer, String id) {
2019-09-14 18:12:04 +00:00
final HorizontalAlignment alignment = this.getHorizontalAlignment();
final TextBlock textBlock = this.createTextBlockInternal(note, set, spriteContainer, alignment);
2019-07-14 20:09:26 +00:00
2022-09-18 17:08:06 +00:00
final HColor backgroundColor = this.value(PName.BackGroundColor).asColor(set);
final HColor lineColor = this.value(PName.LineColor).asColor(set);
2019-07-14 20:09:26 +00:00
final UStroke stroke = this.getStroke();
2022-12-16 16:20:14 +00:00
final int cornersize = this.value(PName.RoundCorner).asInt(false);
final ClockwiseTopRightBottomLeft margin = this.getMargin();
final ClockwiseTopRightBottomLeft padding = this.getPadding();
2021-03-30 21:19:28 +00:00
final TextBlock result = TextBlockUtils.bordered(textBlock, stroke, lineColor, backgroundColor, cornersize,
2022-06-27 16:33:45 +00:00
padding, id);
return TextBlockUtils.withMargin(result, margin);
2019-07-14 20:09:26 +00:00
}
2022-09-18 17:08:06 +00:00
public UGraphic applyStrokeAndLineColor(UGraphic ug, HColorSet colorSet) {
final HColor color = value(PName.LineColor).asColor(colorSet);
2022-01-28 21:45:34 +00:00
if (color == null)
2022-08-19 16:34:21 +00:00
ug = ug.apply(HColors.none());
2022-01-28 21:45:34 +00:00
else
2020-04-19 16:04:39 +00:00
ug = ug.apply(color);
2022-01-28 21:45:34 +00:00
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
}