All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.eclipse.swt.graphics.FontMetrics Maven / Gradle / Ivy

There is a newer version: 3.128.0
Show newest version
/*******************************************************************************
 * Copyright (c) 2000, 2011 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.swt.graphics;


/**
 * Instances of this class provide measurement information
 * about fonts including ascent, descent, height, leading
 * space between rows, and average character width.
 * FontMetrics are obtained from GCs
 * using the getFontMetrics() method.
 *
 * @see GC#getFontMetrics
 * @see Sample code and further information
 */
public final class FontMetrics {
	int ascent, descent, leading, height;
	double averageCharWidth;

FontMetrics() {
}

/**
 * Convenience method to make a copy of receiver.
 */
FontMetrics makeCopy () {
	FontMetrics fontMetrics = new FontMetrics();
	fontMetrics.ascent = this.ascent;
	fontMetrics.descent = this.descent;
	fontMetrics.averageCharWidth = this.averageCharWidth;
	fontMetrics.leading = this.leading;
	fontMetrics.height = this.height;
	return fontMetrics;
}

public static FontMetrics cocoa_new (int ascent, int descent, int averageCharWidth, int leading, int height) {
	FontMetrics fontMetrics = new FontMetrics();
	fontMetrics.ascent = ascent;
	fontMetrics.descent = descent;
	fontMetrics.averageCharWidth = averageCharWidth;
	fontMetrics.leading = leading;
	fontMetrics.height = height;
	return fontMetrics;
}

/**
 * Invokes platform specific functionality to allocate a new FontMetrics.
 * 

* IMPORTANT: This method is not part of the public * API for FontMetrics. It is marked public only so that it * can be shared within the packages provided by SWT. It is not * available on all platforms, and should never be called from * application code. *

* * @noreference This method is not intended to be referenced by clients. */ public static FontMetrics cocoa_new (int ascent, int descent, double averageCharWidth, int leading, int height) { FontMetrics fontMetrics = new FontMetrics(); fontMetrics.ascent = ascent; fontMetrics.descent = descent; fontMetrics.averageCharWidth = averageCharWidth; fontMetrics.leading = leading; fontMetrics.height = height; return fontMetrics; } /** * Compares the argument to the receiver, and returns true * if they represent the same object using a class * specific comparison. * * @param object the object to compare with this object * @return true if the object is the same as this object and false otherwise * * @see #hashCode */ @Override public boolean equals (Object object) { if (object == this) return true; if (!(object instanceof FontMetrics metrics)) return false; return ascent == metrics.ascent && descent == metrics.descent && leading == metrics.leading && height == metrics.height && (Double.compare (averageCharWidth, metrics.averageCharWidth) == 0); } /** * Returns the ascent of the font described by the receiver. A * font's ascent is the distance from the baseline to the * top of actual characters, not including any of the leading area, * measured in points. * * @return the ascent of the font */ public int getAscent() { return ascent; } /** * Returns the average character width, measured in points, * of the font described by the receiver. * * @return the average character width of the font * @since 3.107 */ public double getAverageCharacterWidth () { return averageCharWidth; } /** * Returns the average character width, measured in points, * of the font described by the receiver. * * @return the average character width of the font * @deprecated Use getAverageCharacterWidth() instead */ @Deprecated public int getAverageCharWidth() { return (int) averageCharWidth; } /** * Returns the descent of the font described by the receiver. A * font's descent is the distance from the baseline to the * bottom of actual characters, not including any of the leading area, * measured in points. * * @return the descent of the font */ public int getDescent() { return descent; } /** * Returns the height of the font described by the receiver, * measured in points. A font's height is the sum of * its ascent, descent and leading area. * * @return the height of the font * * @see #getAscent * @see #getDescent * @see #getLeading */ public int getHeight() { return height; } /** * Returns the leading area of the font described by the * receiver. A font's leading area is the space * above its ascent which may include accents or other marks. * * @return the leading space of the font */ public int getLeading() { return leading; } /** * Returns an integer hash code for the receiver. Any two * objects that return true when passed to * equals must return the same value for this * method. * * @return the receiver's hash * * @see #equals */ @Override public int hashCode() { return ascent ^ descent ^ Double.hashCode (averageCharWidth) ^ leading ^ height; } String getName () { String string = getClass ().getName (); int index = string.lastIndexOf ('.'); if (index == -1) return string; return string.substring (index + 1, string.length ()); } @Override public String toString() { return getName() + "{" + " ascent=" + ascent + " descent=" + descent + " averageCharWidth=" + averageCharWidth + " leading=" + leading + " height=" + height + "}"; } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy