UFontImpl.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.klimt.font;

import java.awt.Font;
import java.util.Objects;

public final class UFontImpl implements UFont {

	private final FontStack fontStack;
	private final int style;
	private final int size;

	public static Font getUnderlayingFont(final UFont font, final String text) {
		return ((UFontImpl) font).getUnderlayingFont(text);
	}

	UFontImpl(FontStack fontStack, int style, int size) {
		this.fontStack = fontStack;
		this.style = style;
		this.size = size;
	}

	public Font getUnderlayingFont(String text) {
		return fontStack.getFont(text, style, size);
	}

	public UFont withSize(float size) {
		return new UFontImpl(fontStack, this.style, (int) size);
	}

	public UFont withStyle(int style) {
		return new UFontImpl(fontStack, style, this.size);
	}

	public UFont bold() {
		return withStyle(Font.BOLD);
	}

	public UFont italic() {
		return withStyle(Font.ITALIC);
	}

	public int getStyle() {
		return style;
	}

	public int getSize() {
		return size;
	}

	public double getSize2D() {
		return size;
	}

	public boolean isBold() {
		return (style & Font.BOLD) != 0;
	}

	public boolean isItalic() {
		return (style & Font.ITALIC) != 0;
	}

	public String getFamily(String text, UFontContext context) {
		// ::uncomment when __TEAVM__
//		final String fullDefinition = fontStack.getFullDefinition();
//		// Map Java font name to web-safe equivalent
//		// http://plantuml.sourceforge.net/qa/?qa=5432/svg-monospace-output-has-wrong-font-family
//		if ("monospaced".equalsIgnoreCase(fullDefinition))
//			return "monospace";
//
//		return fullDefinition.replace('"', '\'').replaceAll("(?i)sansserif", "sans-serif");
		// ::done
		// ::comment when __TEAVM__
		if (context == UFontContext.EPS) {
//			if (fontStack.getFamily() == null)
//				return "Times-Roman";
			return getUnderlayingFont(text).getPSName();
		}
		if (context == UFontContext.SVG) {
			String result = fontStack.getFullDefinition().replace('"', '\'');
			result = result.replaceAll("(?i)sansserif", "sans-serif");

			return result;
		}
		throw new IllegalArgumentException();
		// ::done
	}

	// Kludge for testing because font names on some machines (only macOS?) do not
	// end with <DOT><STYLE>
	// See https://github.com/plantuml/plantuml/issues/720
	private String getPortableFontName() {
		final Font font = getUnderlayingFont(null);
		final String name = font.getFontName();
		if (font.isBold() && font.isItalic())
			return name.endsWith(".bolditalic") ? name : name + ".bolditalic";
		else if (font.isBold())
			return name.endsWith(".bold") ? name : name + ".bold";
		else if (font.isItalic())
			return name.endsWith(".italic") ? name : name + ".italic";
		else
			return name.endsWith(".plain") ? name : name + ".plain";
	}

	@Override
	public String toString() {
		final StringBuilder sb = new StringBuilder();
		sb.append(getPortableFontName());
		sb.append("/");
		sb.append(getSize());
		return sb.toString();
	}

	// ::comment when __HAXE__

	@Override
	public int hashCode() {
		return Objects.hash(fontStack, style, size);
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (!(obj instanceof UFontImpl))
			return false;
		UFontImpl other = (UFontImpl) obj;
		return Objects.equals(fontStack, other.fontStack) && style == other.style && size == other.size;
	}

	// ::done

}