EmbeddedFontMetrics.java

/* ========================================================================
 * PlantUML : a free UML diagram generator
 * ========================================================================
 *
 * (C) Copyright 2009-2025, 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.drawing.font;

/**
 * Metrics of one face of a font embedded in PlantUML, decoded from the compact
 * tables generated by tools/font-metrics/GenerateFontMetrics.java (see
 * {@link LiberationSansMetrics}).
 *
 * <p>
 * All values are in font units: a value v measures {@code v * size / unitsPerEm}
 * pixels for a font of the given size, which is what Java2D gives with
 * fractional metrics.
 */
public final class EmbeddedFontMetrics {

	private final int unitsPerEm;
	private final int ascent;
	private final int descent;
	private final int lineGap;
	private final int missingAdvance;
	// First and last code point of each range of characters the font has a glyph
	// for, two chars per range, in increasing order
	private final String ranges;
	// The advance width of every character of the ranges, in the same order
	private final String advances;
	// Index in advances of the first character of each range
	private final int[] offsets;

	EmbeddedFontMetrics(int unitsPerEm, int ascent, int descent, int lineGap, int missingAdvance, String ranges,
			String advances) {
		this.unitsPerEm = unitsPerEm;
		this.ascent = ascent;
		this.descent = descent;
		this.lineGap = lineGap;
		this.missingAdvance = missingAdvance;
		this.ranges = ranges;
		this.advances = advances;
		this.offsets = new int[ranges.length() / 2];
		int offset = 0;
		for (int i = 0; i < offsets.length; i++) {
			offsets[i] = offset;
			offset += ranges.charAt(2 * i + 1) - ranges.charAt(2 * i) + 1;
		}
		if (offset != advances.length())
			throw new IllegalStateException("Corrupted font metrics");
	}

	public int getUnitsPerEm() {
		return unitsPerEm;
	}

	/**
	 * Distance from the baseline to the top of the line (positive).
	 */
	public int getAscent() {
		return ascent;
	}

	/**
	 * Distance from the baseline to the bottom of the line (positive).
	 */
	public int getDescent() {
		return descent;
	}

	public int getLineGap() {
		return lineGap;
	}

	/**
	 * Advance width of the glyph drawn for a character the font does not have.
	 */
	public int getMissingAdvance() {
		return missingAdvance;
	}

	/**
	 * @return the advance width of the code point, or -1 if the font has no glyph
	 *         for it
	 */
	public int getAdvance(int codePoint) {
		if (codePoint < 0 || codePoint > 0xFFFF)
			return -1;
		// Last range starting at or before the code point
		int low = 0;
		int high = offsets.length - 1;
		while (low <= high) {
			final int middle = (low + high) >>> 1;
			if (ranges.charAt(2 * middle) <= codePoint)
				low = middle + 1;
			else
				high = middle - 1;
		}
		if (high < 0)
			return -1;
		final int first = ranges.charAt(2 * high);
		final int last = ranges.charAt(2 * high + 1);
		if (codePoint > last)
			return -1;
		return advances.charAt(offsets[high] + codePoint - first);
	}

}