HtmlFont.java

/* ========================================================================
 * PlantUML : a free UML diagram generator
 * ========================================================================
 *
 * (C) Copyright 2009-2024, Arnaud Roques
 *
 * Project Info:  https://plantuml.com
 * 
 * If you like this project or if you find it useful, you can support us at:
 * 
 * https://plantuml.com/patreon (only 1$ per month!)
 * https://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.ngdot.html;

/**
 * The font changes of a piece of text: what the enclosing {@code <FONT>},
 * {@code <B>}, {@code <I>}... elements ask for. A {@code null} value keeps the
 * font of the label.
 */
public final class HtmlFont {

	public static final HtmlFont NONE = new HtmlFont(null, null, null, false, false, false, false, false, false,
			false);

	private final String color;
	private final String face;
	private final Double size;
	private final boolean bold;
	private final boolean italic;
	private final boolean underline;
	private final boolean overline;
	private final boolean strike;
	private final boolean sub;
	private final boolean sup;

	private HtmlFont(String color, String face, Double size, boolean bold, boolean italic, boolean underline,
			boolean overline, boolean strike, boolean sub, boolean sup) {
		this.color = color;
		this.face = face;
		this.size = size;
		this.bold = bold;
		this.italic = italic;
		this.underline = underline;
		this.overline = overline;
		this.strike = strike;
		this.sub = sub;
		this.sup = sup;
	}

	/**
	 * The font inside an element of this font: {@code B I U O S SUB SUP}, or
	 * {@code FONT} with its {@code COLOR}, {@code FACE} and {@code POINT-SIZE}.
	 */
	HtmlFont with(String tag, HtmlAttributes attributes) {
		if (tag.equals("B"))
			return new HtmlFont(color, face, size, true, italic, underline, overline, strike, sub, sup);
		if (tag.equals("I"))
			return new HtmlFont(color, face, size, bold, true, underline, overline, strike, sub, sup);
		if (tag.equals("U"))
			return new HtmlFont(color, face, size, bold, italic, true, overline, strike, sub, sup);
		if (tag.equals("O"))
			return new HtmlFont(color, face, size, bold, italic, underline, true, strike, sub, sup);
		if (tag.equals("S"))
			return new HtmlFont(color, face, size, bold, italic, underline, overline, true, sub, sup);
		if (tag.equals("SUB"))
			return new HtmlFont(color, face, size, bold, italic, underline, overline, strike, true, false);
		if (tag.equals("SUP"))
			return new HtmlFont(color, face, size, bold, italic, underline, overline, strike, false, true);
		// FONT
		final String newColor = attributes.get("COLOR");
		final String newFace = attributes.get("FACE");
		final Double newSize = attributes.getNumber("POINT-SIZE");
		return new HtmlFont(newColor == null ? color : newColor, newFace == null ? face : newFace,
				newSize == null || newSize <= 0 ? size : newSize, bold, italic, underline, overline, strike, sub,
				sup);
	}

	public String getColor() {
		return color;
	}

	public String getFace() {
		return face;
	}

	public Double getSize() {
		return size;
	}

	public boolean isBold() {
		return bold;
	}

	public boolean isItalic() {
		return italic;
	}

	public boolean isUnderline() {
		return underline;
	}

	public boolean isOverline() {
		return overline;
	}

	public boolean isStrike() {
		return strike;
	}

	public boolean isSub() {
		return sub;
	}

	public boolean isSup() {
		return sup;
	}

	@Override
	public String toString() {
		final StringBuilder sb = new StringBuilder();
		if (bold)
			sb.append('B');
		if (italic)
			sb.append('I');
		if (underline)
			sb.append('U');
		if (overline)
			sb.append('O');
		if (strike)
			sb.append('S');
		if (sub)
			sb.append("_");
		if (sup)
			sb.append("^");
		if (color != null)
			sb.append(" color=").append(color);
		if (face != null)
			sb.append(" face=").append(face);
		if (size != null)
			sb.append(" size=").append(size);
		return sb.toString().trim();
	}

}