plantuml/src/net/sourceforge/plantuml/graphic/FontConfiguration.java

303 lines
10 KiB
Java
Raw Normal View History

2010-11-15 20:35:36 +00:00
/* ========================================================================
* PlantUML : a free UML diagram generator
* ========================================================================
*
2022-03-07 19:33:46 +00:00
* (C) Copyright 2009-2023, Arnaud Roques
2010-11-15 20:35:36 +00:00
*
2016-03-06 16:47:34 +00:00
* Project Info: http://plantuml.com
2010-11-15 20:35:36 +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
*
2010-11-15 20:35:36 +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
2013-12-10 19:36:50 +00:00
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
2010-11-15 20:35:36 +00:00
* 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.graphic;
import java.util.EnumSet;
2013-12-10 19:36:50 +00:00
import java.util.Map;
2021-05-09 21:14:40 +00:00
import java.util.Objects;
2010-11-15 20:35:36 +00:00
2013-12-10 19:36:50 +00:00
import net.sourceforge.plantuml.FontParam;
import net.sourceforge.plantuml.ISkinParam;
import net.sourceforge.plantuml.SkinParamUtils;
import net.sourceforge.plantuml.cucadiagram.Stereotype;
2015-09-28 20:42:17 +00:00
import net.sourceforge.plantuml.graphic.color.ColorType;
import net.sourceforge.plantuml.graphic.color.Colors;
import net.sourceforge.plantuml.style.PName;
import net.sourceforge.plantuml.style.Style;
2011-08-08 17:48:29 +00:00
import net.sourceforge.plantuml.ugraphic.UFont;
2022-10-05 20:32:57 +00:00
import net.sourceforge.plantuml.ugraphic.UStroke;
2020-03-18 10:50:02 +00:00
import net.sourceforge.plantuml.ugraphic.color.HColor;
2022-08-19 16:34:21 +00:00
import net.sourceforge.plantuml.ugraphic.color.HColors;
2011-08-08 17:48:29 +00:00
2010-11-15 20:35:36 +00:00
public class FontConfiguration {
private final EnumSet<FontStyle> styles;
2011-08-08 17:48:29 +00:00
private final UFont currentFont;
private final UFont motherFont;
2020-03-18 10:50:02 +00:00
private final HColor motherColor;
private final HColor currentColor;
private final HColor extendedColor;
2013-12-10 19:36:50 +00:00
private final FontPosition fontPosition;
private final SvgAttributes svgAttributes;
2022-10-05 20:32:57 +00:00
private final UStroke hyperlinkUnderlineStroke;
private final HColor hyperlinkColor;
2015-09-28 20:42:17 +00:00
private final int tabSize;
2011-08-08 17:48:29 +00:00
public String toStringDebug() {
return getFont().toStringDebug() + " " + styles.toString();
}
2022-04-07 16:53:33 +00:00
public static FontConfiguration create(UFont font, HColor color, HColor hyperlinkColor,
2022-10-05 20:32:57 +00:00
UStroke hyperlinkUnderlineStroke) {
return create(font, color, hyperlinkColor, hyperlinkUnderlineStroke, 8);
2015-09-28 20:42:17 +00:00
}
2022-04-07 16:53:33 +00:00
public static FontConfiguration create(UFont font, HColor color, HColor hyperlinkColor,
2022-10-05 20:32:57 +00:00
UStroke hyperlinkUnderlineStroke, int tabSize) {
2022-04-07 16:53:33 +00:00
return new FontConfiguration(getStyles(font), font, color, font, color, null, FontPosition.NORMAL,
2022-10-05 20:32:57 +00:00
new SvgAttributes(), hyperlinkColor, hyperlinkUnderlineStroke, tabSize);
2015-09-28 20:42:17 +00:00
}
public static FontConfiguration blackBlueTrue(UFont font) {
2022-10-05 20:32:57 +00:00
return create(font, HColors.BLACK.withDark(HColors.WHITE), HColors.BLUE, new UStroke(), 8);
2013-12-10 19:36:50 +00:00
}
2022-03-19 12:48:23 +00:00
public static FontConfiguration create(ISkinParam skinParam, FontParam fontParam, Stereotype stereo) {
return create(SkinParamUtils.getFont(skinParam, fontParam, stereo),
SkinParamUtils.getFontColor(skinParam, fontParam, stereo), skinParam.getHyperlinkColor(),
skinParam.useUnderlineForHyperlink(), skinParam.getTabSize());
}
2022-03-19 12:48:23 +00:00
public static FontConfiguration create(ISkinParam skinParam, Style style) {
2022-05-27 14:18:47 +00:00
return create(skinParam, style, null);
}
public static FontConfiguration create(ISkinParam skinParam, Style style, Colors colors) {
2022-09-18 17:08:06 +00:00
final HColor hyperlinkColor = style.value(PName.HyperLinkColor).asColor(skinParam.getIHtmlColorSet());
2022-10-05 20:32:57 +00:00
final UStroke hyperlinkUnderlineStroke = skinParam.useUnderlineForHyperlink();
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 = style.value(PName.FontColor).asColor(skinParam.getIHtmlColorSet());
2022-10-05 20:32:57 +00:00
return create(style.getUFont(), color, hyperlinkColor, hyperlinkUnderlineStroke, skinParam.getTabSize());
2010-11-15 20:35:36 +00:00
}
2015-09-28 20:42:17 +00:00
// ---
2011-08-08 17:48:29 +00:00
private static EnumSet<FontStyle> getStyles(UFont font) {
final boolean bold = font.isBold();
final boolean italic = font.isItalic();
2022-10-05 20:32:57 +00:00
if (bold && italic)
2011-08-08 17:48:29 +00:00
return EnumSet.of(FontStyle.ITALIC, FontStyle.BOLD);
2022-10-05 20:32:57 +00:00
if (bold)
2011-08-08 17:48:29 +00:00
return EnumSet.of(FontStyle.BOLD);
2022-10-05 20:32:57 +00:00
if (italic)
2011-08-08 17:48:29 +00:00
return EnumSet.of(FontStyle.ITALIC);
2022-10-05 20:32:57 +00:00
2011-08-08 17:48:29 +00:00
return EnumSet.noneOf(FontStyle.class);
}
2010-11-15 20:35:36 +00:00
@Override
public String toString() {
2011-08-08 17:48:29 +00:00
return styles.toString() + " " + currentColor;
2010-11-15 20:35:36 +00:00
}
2020-03-18 10:50:02 +00:00
private FontConfiguration(EnumSet<FontStyle> styles, UFont motherFont, HColor motherColor, UFont currentFont,
HColor currentColor, HColor extendedColor, FontPosition fontPosition, SvgAttributes svgAttributes,
2022-10-05 20:32:57 +00:00
HColor hyperlinkColor, UStroke hyperlinkUnderlineStroke, int tabSize) {
2010-11-15 20:35:36 +00:00
this.styles = styles;
this.currentFont = currentFont;
this.motherFont = motherFont;
this.currentColor = currentColor;
this.motherColor = motherColor;
this.extendedColor = extendedColor;
2013-12-10 19:36:50 +00:00
this.fontPosition = fontPosition;
this.svgAttributes = svgAttributes;
2015-04-07 18:18:37 +00:00
this.hyperlinkColor = hyperlinkColor;
2022-10-05 20:32:57 +00:00
this.hyperlinkUnderlineStroke = hyperlinkUnderlineStroke;
2015-09-28 20:42:17 +00:00
this.tabSize = tabSize;
2010-11-15 20:35:36 +00:00
}
2020-03-18 10:50:02 +00:00
public FontConfiguration forceFont(UFont newFont, HColor htmlColorForStereotype) {
2022-10-05 20:32:57 +00:00
if (newFont == null)
2015-04-07 18:18:37 +00:00
return add(FontStyle.ITALIC);
2022-10-05 20:32:57 +00:00
2015-04-07 18:18:37 +00:00
FontConfiguration result = new FontConfiguration(styles, newFont, motherColor, newFont, currentColor,
2022-10-05 20:32:57 +00:00
extendedColor, fontPosition, svgAttributes, hyperlinkColor, hyperlinkUnderlineStroke, tabSize);
if (htmlColorForStereotype != null)
2015-04-07 18:18:37 +00:00
result = result.changeColor(htmlColorForStereotype);
2022-10-05 20:32:57 +00:00
2015-04-07 18:18:37 +00:00
return result;
}
public FontConfiguration changeAttributes(SvgAttributes toBeAdded) {
return new FontConfiguration(styles, motherFont, motherColor, currentFont, currentColor, extendedColor,
2022-10-05 20:32:57 +00:00
fontPosition, svgAttributes.add(toBeAdded), hyperlinkColor, hyperlinkUnderlineStroke, tabSize);
2015-04-07 18:18:37 +00:00
}
private FontConfiguration withHyperlink() {
2022-10-05 20:32:57 +00:00
return new FontConfiguration(styles, motherFont, motherColor, currentFont, hyperlinkColor, extendedColor,
fontPosition, svgAttributes, hyperlinkColor, hyperlinkUnderlineStroke, tabSize);
2013-12-10 19:36:50 +00:00
}
2021-11-10 21:15:26 +00:00
public FontConfiguration changeColor(HColor newHtmlColor) {
return new FontConfiguration(styles, motherFont, motherColor, currentFont, newHtmlColor, extendedColor,
2022-10-05 20:32:57 +00:00
fontPosition, svgAttributes, hyperlinkColor, hyperlinkUnderlineStroke, tabSize);
2015-09-28 20:42:17 +00:00
}
public FontConfiguration mute(Colors colors) {
2021-05-09 21:14:40 +00:00
final HColor color = Objects.requireNonNull(colors).getColor(ColorType.TEXT);
2022-10-05 20:32:57 +00:00
if (color == null)
2015-09-28 20:42:17 +00:00
return this;
2022-10-05 20:32:57 +00:00
2015-09-28 20:42:17 +00:00
return changeColor(color);
2010-11-15 20:35:36 +00:00
}
2020-03-18 10:50:02 +00:00
FontConfiguration changeExtendedColor(HColor newExtendedColor) {
2013-12-10 19:36:50 +00:00
return new FontConfiguration(styles, motherFont, motherColor, currentFont, currentColor, newExtendedColor,
2022-10-05 20:32:57 +00:00
fontPosition, svgAttributes, hyperlinkColor, hyperlinkUnderlineStroke, tabSize);
2010-11-15 20:35:36 +00:00
}
2013-12-10 19:36:50 +00:00
public FontConfiguration changeSize(float size) {
2017-04-05 17:37:42 +00:00
return new FontConfiguration(styles, motherFont, motherColor, currentFont.withSize(size), currentColor,
2022-10-05 20:32:57 +00:00
extendedColor, fontPosition, svgAttributes, hyperlinkColor, hyperlinkUnderlineStroke, tabSize);
2013-12-10 19:36:50 +00:00
}
public FontConfiguration bigger(double delta) {
return changeSize((float) (currentFont.getSize() + delta));
}
public FontConfiguration changeFontPosition(FontPosition fontPosition) {
return new FontConfiguration(styles, motherFont, motherColor, currentFont, currentColor, extendedColor,
2022-10-05 20:32:57 +00:00
fontPosition, svgAttributes, hyperlinkColor, hyperlinkUnderlineStroke, tabSize);
2013-12-10 19:36:50 +00:00
}
public FontConfiguration changeFamily(String family) {
return new FontConfiguration(styles, motherFont, motherColor,
new UFont(family, currentFont.getStyle(), currentFont.getSize()), currentColor, extendedColor,
2022-10-05 20:32:57 +00:00
fontPosition, svgAttributes, hyperlinkColor, hyperlinkUnderlineStroke, tabSize);
2010-11-15 20:35:36 +00:00
}
public FontConfiguration resetFont() {
2013-12-10 19:36:50 +00:00
return new FontConfiguration(styles, motherFont, motherColor, motherFont, motherColor, null,
2022-10-05 20:32:57 +00:00
FontPosition.NORMAL, new SvgAttributes(), hyperlinkColor, hyperlinkUnderlineStroke, tabSize);
2010-11-15 20:35:36 +00:00
}
2020-05-17 21:15:50 +00:00
public FontConfiguration add(FontStyle style) {
2010-11-15 20:35:36 +00:00
final EnumSet<FontStyle> r = styles.clone();
2022-10-05 20:32:57 +00:00
if (style == FontStyle.PLAIN)
2016-12-14 21:01:03 +00:00
r.clear();
2022-10-05 20:32:57 +00:00
2010-11-15 20:35:36 +00:00
r.add(style);
return new FontConfiguration(r, motherFont, motherColor, currentFont, currentColor, extendedColor, fontPosition,
2022-10-05 20:32:57 +00:00
svgAttributes, hyperlinkColor, hyperlinkUnderlineStroke, tabSize);
2010-11-15 20:35:36 +00:00
}
2011-08-08 17:48:29 +00:00
public FontConfiguration italic() {
return add(FontStyle.ITALIC);
}
2013-12-10 19:36:50 +00:00
public FontConfiguration bold() {
return add(FontStyle.BOLD);
}
2020-05-17 21:15:50 +00:00
public FontConfiguration unbold() {
return remove(FontStyle.BOLD);
}
public FontConfiguration unitalic() {
return remove(FontStyle.ITALIC);
}
2013-12-10 19:36:50 +00:00
public FontConfiguration underline() {
return add(FontStyle.UNDERLINE);
}
2020-03-18 10:50:02 +00:00
public FontConfiguration wave(HColor color) {
2019-05-24 19:59:31 +00:00
return add(FontStyle.WAVE).changeExtendedColor(color);
}
2015-04-07 18:18:37 +00:00
public FontConfiguration hyperlink() {
2022-10-05 20:32:57 +00:00
if (hyperlinkUnderlineStroke != null)
2015-04-07 18:18:37 +00:00
return add(FontStyle.UNDERLINE).withHyperlink();
2022-10-05 20:32:57 +00:00
2015-04-07 18:18:37 +00:00
return withHyperlink();
}
2020-05-17 21:15:50 +00:00
public FontConfiguration remove(FontStyle style) {
2010-11-15 20:35:36 +00:00
final EnumSet<FontStyle> r = styles.clone();
r.remove(style);
return new FontConfiguration(r, motherFont, motherColor, currentFont, currentColor, extendedColor, fontPosition,
2022-10-05 20:32:57 +00:00
svgAttributes, hyperlinkColor, hyperlinkUnderlineStroke, tabSize);
2010-11-15 20:35:36 +00:00
}
2011-08-08 17:48:29 +00:00
public UFont getFont() {
UFont result = currentFont;
2010-11-15 20:35:36 +00:00
for (FontStyle style : styles) {
result = style.mutateFont(result);
}
2013-12-10 19:36:50 +00:00
return fontPosition.mute(result);
2010-11-15 20:35:36 +00:00
}
2020-03-18 10:50:02 +00:00
public HColor getColor() {
2010-11-15 20:35:36 +00:00
return currentColor;
}
2011-08-08 17:48:29 +00:00
2020-03-18 10:50:02 +00:00
public HColor getExtendedColor() {
2010-11-15 20:35:36 +00:00
return extendedColor;
}
public boolean containsStyle(FontStyle style) {
return styles.contains(style);
}
2013-12-10 19:36:50 +00:00
public int getSpace() {
return fontPosition.getSpace();
}
public Map<String, String> getAttributes() {
return svgAttributes.attributes();
}
2015-04-07 18:18:37 +00:00
public double getSize2D() {
return currentFont.getSize2D();
}
2015-09-28 20:42:17 +00:00
public int getTabSize() {
return tabSize;
}
2022-10-05 20:32:57 +00:00
public UStroke getUnderlineStroke() {
return hyperlinkUnderlineStroke;
// return new UStroke();
// return new UStroke(3, 5, 2);
}
2010-11-15 20:35:36 +00:00
}