1
0
mirror of https://github.com/octoleo/plantuml.git synced 2024-06-01 08:00:48 +00:00
plantuml/src/net/sourceforge/plantuml/graphic/FontConfiguration.java

293 lines
9.7 KiB
Java
Raw Normal View History

2010-11-15 20:35:36 +00:00
/* ========================================================================
* PlantUML : a free UML diagram generator
* ========================================================================
*
2019-01-16 18:34:41 +00:00
* (C) Copyright 2009-2020, 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;
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;
2020-03-18 10:50:02 +00:00
import net.sourceforge.plantuml.ugraphic.color.HColor;
import net.sourceforge.plantuml.ugraphic.color.HColorUtils;
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 hyperlinkColor;
private final HColor currentColor;
private final HColor extendedColor;
2013-12-10 19:36:50 +00:00
private final FontPosition fontPosition;
private final SvgAttributes svgAttributes;
2015-04-07 18:18:37 +00:00
private final boolean hyperlink;
private final boolean useUnderlineForHyperlink;
2015-09-28 20:42:17 +00:00
private final int tabSize;
2011-08-08 17:48:29 +00:00
2020-03-18 10:50:02 +00:00
public FontConfiguration(UFont font, HColor color, HColor hyperlinkColor, boolean useUnderlineForHyperlink) {
2015-09-28 20:42:17 +00:00
this(font, color, hyperlinkColor, useUnderlineForHyperlink, 8);
}
2020-03-18 10:50:02 +00:00
public FontConfiguration(UFont font, HColor color, HColor hyperlinkColor, boolean useUnderlineForHyperlink,
2015-09-28 20:42:17 +00:00
int tabSize) {
2015-04-07 18:18:37 +00:00
this(getStyles(font), font, color, font, color, null, FontPosition.NORMAL, new SvgAttributes(), false,
2015-09-28 20:42:17 +00:00
hyperlinkColor, useUnderlineForHyperlink, tabSize);
}
public static FontConfiguration blackBlueTrue(UFont font) {
2020-03-18 10:50:02 +00:00
return new FontConfiguration(font, HColorUtils.BLACK, HColorUtils.BLUE, true, 8);
2013-12-10 19:36:50 +00:00
}
public FontConfiguration(ISkinParam skinParam, FontParam fontParam, Stereotype stereo) {
this(SkinParamUtils.getFont(skinParam, fontParam, stereo),
SkinParamUtils.getFontColor(skinParam, fontParam, stereo), skinParam.getHyperlinkColor(),
skinParam.useUnderlineForHyperlink(), skinParam.getTabSize());
}
public FontConfiguration(Style style, ISkinParam skinParam, Stereotype stereo, FontParam fontParam) {
this(style.getUFont(), style.value(PName.FontColor).asColor(skinParam.getIHtmlColorSet()),
skinParam.getHyperlinkColor(), skinParam.useUnderlineForHyperlink(), skinParam.getTabSize());
2010-11-15 20:35:36 +00:00
}
2015-09-28 20:42:17 +00:00
// ---
2015-05-03 15:36:36 +00:00
public final boolean useUnderlineForHyperlink() {
return useUnderlineForHyperlink;
}
2020-03-18 10:50:02 +00:00
public final HColor getHyperlinkColor() {
2015-05-03 15:36:36 +00:00
return hyperlinkColor;
}
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();
if (bold && italic) {
return EnumSet.of(FontStyle.ITALIC, FontStyle.BOLD);
}
if (bold) {
return EnumSet.of(FontStyle.BOLD);
}
if (italic) {
return EnumSet.of(FontStyle.ITALIC);
}
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,
boolean hyperlink, HColor hyperlinkColor, boolean useUnderlineForHyperlink, 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.hyperlink = hyperlink;
this.hyperlinkColor = hyperlinkColor;
this.useUnderlineForHyperlink = useUnderlineForHyperlink;
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) {
2015-04-07 18:18:37 +00:00
if (newFont == null) {
return add(FontStyle.ITALIC);
}
FontConfiguration result = new FontConfiguration(styles, newFont, motherColor, newFont, currentColor,
2015-09-28 20:42:17 +00:00
extendedColor, fontPosition, svgAttributes, hyperlink, hyperlinkColor, useUnderlineForHyperlink,
tabSize);
2015-04-07 18:18:37 +00:00
if (htmlColorForStereotype != null) {
result = result.changeColor(htmlColorForStereotype);
}
return result;
}
public FontConfiguration changeAttributes(SvgAttributes toBeAdded) {
return new FontConfiguration(styles, motherFont, motherColor, currentFont, currentColor, extendedColor,
2015-09-28 20:42:17 +00:00
fontPosition, svgAttributes.add(toBeAdded), hyperlink, hyperlinkColor, useUnderlineForHyperlink,
tabSize);
2015-04-07 18:18:37 +00:00
}
private FontConfiguration withHyperlink() {
2013-12-10 19:36:50 +00:00
return new FontConfiguration(styles, motherFont, motherColor, currentFont, currentColor, extendedColor,
2015-09-28 20:42:17 +00:00
fontPosition, svgAttributes, true, hyperlinkColor, useUnderlineForHyperlink, tabSize);
2013-12-10 19:36:50 +00:00
}
2020-03-18 10:50:02 +00:00
public FontConfiguration changeColor(HColor htmlColor) {
2013-12-10 19:36:50 +00:00
return new FontConfiguration(styles, motherFont, motherColor, currentFont, htmlColor, extendedColor,
2015-09-28 20:42:17 +00:00
fontPosition, svgAttributes, hyperlink, hyperlinkColor, useUnderlineForHyperlink, tabSize);
}
public FontConfiguration mute(Colors colors) {
if (colors == null) {
throw new IllegalArgumentException();
}
2020-03-18 10:50:02 +00:00
final HColor color = colors.getColor(ColorType.TEXT);
2015-09-28 20:42:17 +00:00
if (color == null) {
return this;
}
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,
2015-09-28 20:42:17 +00:00
fontPosition, svgAttributes, hyperlink, hyperlinkColor, useUnderlineForHyperlink, 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,
2015-09-28 20:42:17 +00:00
extendedColor, fontPosition, svgAttributes, hyperlink, hyperlinkColor, useUnderlineForHyperlink,
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,
2015-09-28 20:42:17 +00:00
fontPosition, svgAttributes, hyperlink, hyperlinkColor, useUnderlineForHyperlink, 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,
fontPosition, svgAttributes, hyperlink, hyperlinkColor, useUnderlineForHyperlink, 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,
2015-09-28 20:42:17 +00:00
FontPosition.NORMAL, new SvgAttributes(), hyperlink, hyperlinkColor, useUnderlineForHyperlink, tabSize);
2010-11-15 20:35:36 +00:00
}
FontConfiguration add(FontStyle style) {
final EnumSet<FontStyle> r = styles.clone();
2016-12-14 21:01:03 +00:00
if (style == FontStyle.PLAIN) {
r.clear();
}
2010-11-15 20:35:36 +00:00
r.add(style);
return new FontConfiguration(r, motherFont, motherColor, currentFont, currentColor, extendedColor, fontPosition,
svgAttributes, hyperlink, hyperlinkColor, useUnderlineForHyperlink, 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);
}
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() {
if (useUnderlineForHyperlink) {
return add(FontStyle.UNDERLINE).withHyperlink();
}
return withHyperlink();
}
2010-11-15 20:35:36 +00:00
FontConfiguration remove(FontStyle style) {
final EnumSet<FontStyle> r = styles.clone();
r.remove(style);
return new FontConfiguration(r, motherFont, motherColor, currentFont, currentColor, extendedColor, fontPosition,
svgAttributes, hyperlink, hyperlinkColor, useUnderlineForHyperlink, 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() {
2015-04-07 18:18:37 +00:00
if (hyperlink) {
return hyperlinkColor;
}
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;
}
2010-11-15 20:35:36 +00:00
}