summaryrefslogtreecommitdiffstats
path: root/awt/java/awt/font/GlyphMetrics.java
blob: 287172231be492050b4ecaaa9b99ff4027c323c3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/*
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF licenses this file to You under the Apache License, Version 2.0
 *  (the "License"); you may not use this file except in compliance with
 *  the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
/**
 * @author Ilya S. Okomin
 * @version $Revision$
 */

package java.awt.font;

import java.awt.geom.Rectangle2D;

/**
 * The GlyphMetrics class provides information about the size and shape of a
 * single glyph. Each glyph has information to specify whether its baseline is
 * horizontal or vertical as well as information on how it interacts with other
 * characters in a text, given as one of the following types: STANDARD,
 * LIGATURE, COMBINING, or COMPONENT.
 * 
 * @since Android 1.0
 */
public final class GlyphMetrics {

    // advance width of the glyph character cell
    /**
     * The advance x.
     */
    private float advanceX;

    // advance height of the glyph character cell
    /**
     * The advance y.
     */
    private float advanceY;

    // flag if the glyph horizontal
    /**
     * The horizontal.
     */
    private boolean horizontal;

    // glyph type code
    /**
     * The glyph type.
     */
    private byte glyphType;

    // bounding box for outline of the glyph
    /**
     * The bounds.
     */
    private Rectangle2D.Float bounds;

    /**
     * The Constant STANDARD indicates a glyph that represents a single
     * character.
     */
    public static final byte STANDARD = 0;

    /**
     * The Constant LIGATURE indicates a glyph that represents multiple
     * characters as a ligature.
     */
    public static final byte LIGATURE = 1;

    /**
     * The Constant COMBINING indicates a glyph which has no caret position
     * between glyphs (for example umlaut).
     */
    public static final byte COMBINING = 2;

    /**
     * The Constant COMPONENT indicates a glyph with no corresponding character
     * in the backing store.
     */
    public static final byte COMPONENT = 3;

    /**
     * The Constant WHITESPACE indicates a glyph without visual representation.
     */
    public static final byte WHITESPACE = 4;

    /**
     * Instantiates a new GlyphMetrics object with the specified parameters.
     * 
     * @param horizontal
     *            specifies if metrics are for a horizontal baseline (true
     *            value), or a vertical baseline (false value).
     * @param advanceX
     *            the X component of the glyph's advance.
     * @param advanceY
     *            the Y component of the glyph's advance.
     * @param bounds
     *            the glyph's bounds.
     * @param glyphType
     *            the glyph's type.
     */
    public GlyphMetrics(boolean horizontal, float advanceX, float advanceY, Rectangle2D bounds,
            byte glyphType) {
        this.horizontal = horizontal;
        this.advanceX = advanceX;
        this.advanceY = advanceY;

        this.bounds = new Rectangle2D.Float();
        this.bounds.setRect(bounds);

        this.glyphType = glyphType;
    }

    /**
     * Instantiates a new horizontal GlyphMetrics with the specified parameters.
     * 
     * @param advanceX
     *            the X component of the glyph's advance.
     * @param bounds
     *            the glyph's bounds.
     * @param glyphType
     *            the glyph's type.
     */
    public GlyphMetrics(float advanceX, Rectangle2D bounds, byte glyphType) {
        this.advanceX = advanceX;
        this.advanceY = 0;

        this.horizontal = true;

        this.bounds = new Rectangle2D.Float();
        this.bounds.setRect(bounds);

        this.glyphType = glyphType;
    }

    /**
     * Gets the glyph's bounds.
     * 
     * @return glyph's bounds.
     */
    public Rectangle2D getBounds2D() {
        return (Rectangle2D.Float)this.bounds.clone();
    }

    /**
     * Checks if this glyph is whitespace or not.
     * 
     * @return true, if this glyph is whitespace, false otherwise.
     */
    public boolean isWhitespace() {
        return ((this.glyphType & 4) == WHITESPACE);
    }

    /**
     * Checks if this glyph is standard or not.
     * 
     * @return true, if this glyph is standard, false otherwise.
     */
    public boolean isStandard() {
        return ((this.glyphType & 3) == STANDARD);
    }

    /**
     * Checks if this glyph is ligature or not.
     * 
     * @return true, if this glyph is ligature, false otherwise.
     */
    public boolean isLigature() {
        return ((this.glyphType & 3) == LIGATURE);
    }

    /**
     * Checks if this glyph is component or not.
     * 
     * @return true, if this glyph is component, false otherwise.
     */
    public boolean isComponent() {
        return ((this.glyphType & 3) == COMPONENT);
    }

    /**
     * Checks if this glyph is combining or not.
     * 
     * @return true, if this glyph is combining, false otherwise.
     */
    public boolean isCombining() {
        return ((this.glyphType & 3) == COMBINING);
    }

    /**
     * Gets the glyph's type.
     * 
     * @return the glyph's type.
     */
    public int getType() {
        return this.glyphType;
    }

    /**
     * Gets the distance from the right (for horizontal) or bottom (for
     * vertical) of the glyph bounds to the advance.
     * 
     * @return the distance from the right (for horizontal) or bottom (for
     *         vertical) of the glyph bounds to the advance.
     */
    public float getRSB() {
        if (this.horizontal) {
            return this.advanceX - this.bounds.x - (float)this.bounds.getWidth();
        }
        return this.advanceY - this.bounds.y - (float)this.bounds.getHeight();
    }

    /**
     * Gets the distance from 0, 0 to the left (for horizontal) or top (for
     * vertical) of the glyph bounds.
     * 
     * @return the distance from 0, 0 to the left (for horizontal) or top (for
     *         vertical) of the glyph bounds.
     */
    public float getLSB() {
        if (this.horizontal) {
            return this.bounds.x;
        }
        return this.bounds.y;
    }

    /**
     * Gets the Y component of the glyph's advance.
     * 
     * @return the Y component of the glyph's advance.
     */
    public float getAdvanceY() {
        return this.advanceY;
    }

    /**
     * Gets the X component of the glyph's advance.
     * 
     * @return the X component of the glyph's advance.
     */
    public float getAdvanceX() {
        return this.advanceX;
    }

    /**
     * Gets the glyph's advance along the baseline.
     * 
     * @return the glyph's advance.
     */
    public float getAdvance() {
        if (this.horizontal) {
            return this.advanceX;
        }
        return this.advanceY;
    }

}