summaryrefslogtreecommitdiffstats
path: root/awt/org/apache/harmony/awt/gl/font/LineMetricsImpl.java
blob: 370146d91e0c24a685df2c8cb61bcbfc20c13afa (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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
/*
 *  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 org.apache.harmony.awt.gl.font;

import java.awt.font.LineMetrics;

import org.apache.harmony.awt.internal.nls.Messages;

/**
 *
 * LineMetrics implementation class.
 */

public class LineMetricsImpl extends LineMetrics implements Cloneable{

    // array of baseline offsets
    float[] baselineOffsets;

    // the number of characters to measure
    int numChars;

    // baseline index of the font corresponding to this line metrics
    int baseLineIndex;

    // underline thickness
    float underlineThickness;

    // underline offset
    float underlineOffset;

    // strikethrough thickness
    float strikethroughThickness;

    // strikethrough offset
    float strikethroughOffset;

    // External leading
    float leading;

    // Height of the font ( == (ascent+descent+leading))
    float height;

    // Ascent of the font
    float ascent;

    // Descent of the font
    float descent;

    // Width of the widest char in the font
    float maxCharWidth;

    // underline thickness (in pixels)
    int lUnderlineThickness;

    // underline offset (in pixels)
    int lUnderlineOffset;

    // strikethrough thickness (in pixels)
    int lStrikethroughThickness;

    // strikethrough offset (in pixels)
    int lStrikethroughOffset;

    // External leading (in pixels)
    int lLeading;

    // Height of the font ( == (ascent+descent+leading)) (in pixels)
    int lHeight;

    // Ascent of the font (in pixels)
    int lAscent;
    
    // Descent of the font (in pixels)
    int lDescent;

    //  Width of the widest char in the font (in pixels)
    int lMaxCharWidth;

    // units per EM square in font value
    int units_per_EM = 0;

    /**
     * Creates LineMetricsImpl object from specified parameters. If baseline data parameter
     * is null than {0, (-ascent+descent)/2, -ascent} values are used for baseline offsets.
     *  
     * @param len a number of characters 
     * @param metrics an array of 16 elements with metrics values that can be 
     * initialized in native code.<p>
     * metrics[0] - ascent<p>
     * metrics[1] - descent<p>
     * metrics[2] - external leading<p>
     * metrics[3] - underline thickness<p>
     * -metrics[4] - underline offset<p>
     * metrics[5] - strikethrough thickness<p>
     * -metrics[6] - strikethrough offset<p>
     * metrics[7] - maximum char width<p>
     * metrics[8] - ascent in pixels<p>
     * metrics[9] - descent in pixles<p>
     * metrics[10] - external leading in pixels<p>
     * metrics[11] - underline thickness in pixels<p>
     * -metrics[12] - underline offset in pixels<p>
     * metrics[13] - strikethrough thickness in pixels<p>
     * -metrics[14] - strikethrough offset in pixels<p>
     * metrics[15] - maximum char width in pixels<p>

     * @param _baselineData an array of 3 elements with baseline offsets metrics<p>
     * _baselineData[0] - roman baseline offset<p> 
     * _baselineData[1] - center baseline offset<p>
     * _baselineData[2] - hanging baseline offset<p>
     */
    public LineMetricsImpl(int len, float[] metrics, float[] _baselineData){
        numChars = len;

        ascent = metrics[0];    // Ascent of the font
        descent = metrics[1];   // Descent of the font
        leading = metrics[2];  // External leading
        height = metrics[0] + metrics[1] + metrics[2];  // Height of the font ( == (ascent + descent + leading))
    }

    /**
     * Creates LineMetricsImpl object from specified parameters. If baseline data parameter
     * is null than {0, (-ascent+descent)/2, -ascent} values are used for baseline offsets.
     *  
     * @param _numChars number of chars 
     * @param _baseLineIndex index of the baseline offset
     * @param _baselineOffsets an array of baseline offsets
     * @param _underlineThickness underline thickness
     * @param _underlineOffset underline offset
     * @param _strikethroughThickness strikethrough thickness
     * @param _strikethroughOffset strinkethrough offset
     * @param _leading leading of the font
     * @param _height font height
     * @param _ascent ascent of the font
     * @param _descent descent of the font
     * @param _maxCharWidth max char width
     */
    public LineMetricsImpl(int _numChars, int _baseLineIndex,
            float[] _baselineOffsets, float _underlineThickness,
            float _underlineOffset, float _strikethroughThickness,
            float _strikethroughOffset, float _leading, float _height,
            float _ascent, float _descent, float _maxCharWidth) {

        numChars = _numChars;
        baseLineIndex = _baseLineIndex;
        underlineThickness = _underlineThickness;
        underlineOffset = _underlineOffset;
        strikethroughThickness = _strikethroughThickness;
        strikethroughOffset = _strikethroughOffset;
        leading = _leading;
        height = _height;
        ascent = _ascent;
        descent = _descent;
        baselineOffsets = _baselineOffsets;
        lUnderlineThickness = (int) underlineThickness;
        lUnderlineOffset = (int) underlineOffset;
        lStrikethroughThickness = (int) strikethroughThickness;
        lStrikethroughOffset = (int) strikethroughOffset;
        lLeading = (int) leading;
        lHeight = (int) height;
        lAscent = (int) ascent;
        lDescent = (int) descent;
        maxCharWidth = _maxCharWidth;
    }

    public LineMetricsImpl(){

    }

    /**
     * All metrics are scaled according to scaleX and scaleY values. 
     * This function helps to recompute metrics according to the scale factors
     * of desired AffineTransform.
     * 
     * @param scaleX scale X factor
     * @param scaleY scale Y factor
     */
    public void scale(float scaleX, float scaleY){
        float absScaleX = Math.abs(scaleX);
        float absScaleY = Math.abs(scaleY);

        underlineThickness *= absScaleY;
        underlineOffset *= scaleY;
        strikethroughThickness *= absScaleY;
        strikethroughOffset *= scaleY;
        leading *= absScaleY;
        height *= absScaleY;
        ascent *= absScaleY;
        descent *= absScaleY;

        if(baselineOffsets == null) {
            getBaselineOffsets();
        }

        for (int i=0; i< baselineOffsets.length; i++){
            baselineOffsets[i] *= scaleY;
        }

        lUnderlineThickness *= absScaleY;
        lUnderlineOffset *= scaleY;
        lStrikethroughThickness *= absScaleY;
        lStrikethroughOffset *= scaleY;
        lLeading  *= absScaleY;
        lHeight *= absScaleY;
        lAscent *= absScaleY;
        lDescent *= absScaleY;
        maxCharWidth *= absScaleX;

    }


    /**
     * Returns offset of the baseline.
     */
    @Override
    public float[] getBaselineOffsets() {
        // XXX: at the moment there only horizontal metrics are taken into
        // account. If there is no baseline information in TrueType font
        // file default values used: {0, -ascent, (-ascent+descent)/2}

        return baselineOffsets;
    }

    /**
     * Returns a number of chars in specified text
     */
    @Override
    public int getNumChars() {
        return numChars;
    }

    /**
     * Returns index of the baseline, one of predefined constants.
     */
    @Override
    public int getBaselineIndex() {
        // Baseline index is the deafult baseline index value
        // taken from the TrueType table "BASE".
        return baseLineIndex;
    }

    /**
     * Returns thickness of the Underline.
     */
    @Override
    public float getUnderlineThickness() {
        return underlineThickness;
    }

    /**
     * Returns offset of the Underline.
     */
    @Override
    public float getUnderlineOffset() {
        return underlineOffset;
    }

    /**
     * Returns thickness of the Strikethrough line.
     */
    @Override
    public float getStrikethroughThickness() {
        return strikethroughThickness;
    }

    /**
     * Returns offset of the Strikethrough line.
     */
    @Override
    public float getStrikethroughOffset() {
        return strikethroughOffset;
    }

    /**
     * Returns the leading.
     */
    @Override
    public float getLeading() {
        return leading;
    }

    /**
     * Returns the height of the font.
     */
    @Override
    public float getHeight() {
        //return height; // equals to (ascent + descent + leading);
    	return ascent + descent + leading;
    }

    /**
     * Returns the descent.
     */
    @Override
    public float getDescent() {
        return descent;
    }

    /**
     * Returns the ascent.
     */
    @Override
    public float getAscent() {
        return ascent;
    }

    /**
     * Returns logical thickness of the Underline.
     */
    public int getLogicalUnderlineThickness() {
        return lUnderlineThickness;
    }

    /**
     * Returns logical offset of the Underline.
     */
    public int getLogicalUnderlineOffset() {
        return lUnderlineOffset;
    }

    /**
     * Returns logical thickness of the Strikethrough line.
     */
    public int getLogicalStrikethroughThickness() {
        return lStrikethroughThickness;
    }

    /**
     * Returns logical offset of the Strikethrough line.
     */
    public int getLogicalStrikethroughOffset() {
        return lStrikethroughOffset;
    }

    /**
     * Returns the logical leading.
     */
    public int getLogicalLeading() {
        return lLeading;
    }

    /**
     * Returns the logical height of the font.
     */
    public int getLogicalHeight() {
        return lHeight; // equals to (ascent + descent + leading);
    }

    /**
     * Returns the logical descent.
     */
    public int getLogicalDescent() {
        return lDescent;
    }

    /**
     * Returns the logical ascent.
     */
    public int getLogicalAscent() {
        return lAscent;
    }

    /**
     * Returns the logical size of the widest char.
     */
    public int getLogicalMaxCharWidth() {
        return lMaxCharWidth;
    }

    /**
     * Returns the size of the widest char.
     */
    public float getMaxCharWidth() {
        return maxCharWidth;
    }

    /**
     * Set num chars to the desired value.
     * 
     * @param num specified number of chars
     */
    public void setNumChars(int num){
        numChars = num;
    }

    @Override
    public Object clone(){
        try{
            return super.clone();
        }catch (CloneNotSupportedException e){
            return null;
        }
    }

}