1 /*******************************************************************************
2 * Copyright (c) 2000, 2020 IBM Corporation and others.
3 *
4 * This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License 2.0
6 * which accompanies this distribution, and is available at
7 * https://www.eclipse.org/legal/epl-2.0/
8 *
9 * SPDX-License-Identifier: EPL-2.0
10 *
11 * Contributors:
12 * IBM Corporation - initial API and implementation
13 *******************************************************************************/
14 package org.eclipse.swt.graphics;
15
16 /**
17 * Instances of this class provide measurement information
18 * about fonts including ascent, descent, height, leading
19 * space between rows, and average character width.
20 * <code>FontMetrics</code> are obtained from <code>GC</code>s
21 * using the <code>getFontMetrics()</code> method.
22 *
23 * @see GC#getFontMetrics
24 * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>
25 */
26 public final class FontMetrics {
27 int ascentInPoints, descentInPoints, averageCharWidthInPoints;
28
FontMetrics()29 FontMetrics() {
30 }
31
32 /**
33 * Compares the argument to the receiver, and returns true
34 * if they represent the <em>same</em> object using a class
35 * specific comparison.
36 *
37 * @param object the object to compare with this object
38 * @return <code>true</code> if the object is the same as this object and <code>false</code> otherwise
39 *
40 * @see #hashCode
41 */
42 @Override
equals(Object object)43 public boolean equals (Object object) {
44 if (object == this) return true;
45 if (!(object instanceof FontMetrics)) return false;
46 FontMetrics metrics = (FontMetrics)object;
47 return ascentInPoints == metrics.ascentInPoints && descentInPoints == metrics.descentInPoints &&
48 averageCharWidthInPoints == metrics.averageCharWidthInPoints;
49 }
50
51 /**
52 * Returns the ascent of the font described by the receiver. A
53 * font's <em>ascent</em> is the distance from the baseline to the
54 * top of actual characters, not including any of the leading area,
55 * measured in points.
56 *
57 * @return the ascent of the font
58 */
getAscent()59 public int getAscent() {
60 return ascentInPoints;
61 }
62
63 /**
64 * Returns the average character width, measured in points,
65 * of the font described by the receiver.
66 *
67 * @return the average character width of the font
68 * @since 3.107
69 */
getAverageCharacterWidth()70 public double getAverageCharacterWidth() {
71 return getAverageCharWidth();
72 }
73
74 /**
75 * Returns the average character width, measured in points,
76 * of the font described by the receiver.
77 *
78 * @return the average character width of the font
79 * @deprecated Use getAverageCharacterWidth() instead
80 */
81 @Deprecated
getAverageCharWidth()82 public int getAverageCharWidth() {
83 return averageCharWidthInPoints;
84 }
85
86 /**
87 * Returns the descent of the font described by the receiver. A
88 * font's <em>descent</em> is the distance from the baseline to the
89 * bottom of actual characters, not including any of the leading area,
90 * measured in points.
91 *
92 * @return the descent of the font
93 */
getDescent()94 public int getDescent() {
95 return descentInPoints;
96 }
97
98 /**
99 * Returns the height of the font described by the receiver,
100 * measured in points. A font's <em>height</em> is the sum of
101 * its ascent, descent and leading area.
102 *
103 * @return the height of the font
104 *
105 * @see #getAscent
106 * @see #getDescent
107 * @see #getLeading
108 */
getHeight()109 public int getHeight() {
110 return ascentInPoints + descentInPoints;
111 }
112
113 /**
114 * Returns the leading area of the font described by the
115 * receiver. A font's <em>leading area</em> is the space
116 * above its ascent which may include accents or other marks.
117 *
118 * @return the leading space of the font
119 */
getLeading()120 public int getLeading() {
121 return 0; // Pango has no concept of "leading"
122 }
123
124 /**
125 * Returns an integer hash code for the receiver. Any two
126 * objects that return <code>true</code> when passed to
127 * <code>equals</code> must return the same value for this
128 * method.
129 *
130 * @return the receiver's hash
131 *
132 * @see #equals
133 */
134 @Override
hashCode()135 public int hashCode() {
136 return ascentInPoints ^ descentInPoints ^ averageCharWidthInPoints;
137 }
138
139 }
140