001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /**
018: * @author Ilya S. Okomin
019: * @version $Revision$
020: */package org.apache.harmony.awt.gl.font;
021:
022: import java.awt.font.LineMetrics;
023:
024: import org.apache.harmony.awt.internal.nls.Messages;
025:
026: /**
027: *
028: * LineMetrics implementation class.
029: */
030:
031: public class LineMetricsImpl extends LineMetrics implements Cloneable {
032:
033: // array of baseline offsets
034: float[] baselineOffsets;
035:
036: // the number of characters to measure
037: int numChars;
038:
039: // baseline index of the font corresponding to this line metrics
040: int baseLineIndex;
041:
042: // underline thickness
043: float underlineThickness;
044:
045: // underline offset
046: float underlineOffset;
047:
048: // strikethrough thickness
049: float strikethroughThickness;
050:
051: // strikethrough offset
052: float strikethroughOffset;
053:
054: // External leading
055: float leading;
056:
057: // Height of the font ( == (ascent+descent+leading))
058: float height;
059:
060: // Ascent of the font
061: float ascent;
062:
063: // Descent of the font
064: float descent;
065:
066: // Width of the widest char in the font
067: float maxCharWidth;
068:
069: // underline thickness (in pixels)
070: int lUnderlineThickness;
071:
072: // underline offset (in pixels)
073: int lUnderlineOffset;
074:
075: // strikethrough thickness (in pixels)
076: int lStrikethroughThickness;
077:
078: // strikethrough offset (in pixels)
079: int lStrikethroughOffset;
080:
081: // External leading (in pixels)
082: int lLeading;
083:
084: // Height of the font ( == (ascent+descent+leading)) (in pixels)
085: int lHeight;
086:
087: // Ascent of the font (in pixels)
088: int lAscent;
089:
090: // Descent of the font (in pixels)
091: int lDescent;
092:
093: // Width of the widest char in the font (in pixels)
094: int lMaxCharWidth;
095:
096: // units per EM square in font value
097: int units_per_EM = 0;
098:
099: /**
100: * Creates LineMetricsImpl object from specified parameters. If baseline data parameter
101: * is null than {0, (-ascent+descent)/2, -ascent} values are used for baseline offsets.
102: *
103: * @param len a number of characters
104: * @param metrics an array of 16 elements with metrics values that can be
105: * initialized in native code.<p>
106: * metrics[0] - ascent<p>
107: * metrics[1] - descent<p>
108: * metrics[2] - external leading<p>
109: * metrics[3] - underline thickness<p>
110: * -metrics[4] - underline offset<p>
111: * metrics[5] - strikethrough thickness<p>
112: * -metrics[6] - strikethrough offset<p>
113: * metrics[7] - maximum char width<p>
114: * metrics[8] - ascent in pixels<p>
115: * metrics[9] - descent in pixles<p>
116: * metrics[10] - external leading in pixels<p>
117: * metrics[11] - underline thickness in pixels<p>
118: * -metrics[12] - underline offset in pixels<p>
119: * metrics[13] - strikethrough thickness in pixels<p>
120: * -metrics[14] - strikethrough offset in pixels<p>
121: * metrics[15] - maximum char width in pixels<p>
122:
123: * @param _baselineData an array of 3 elements with baseline offsets metrics<p>
124: * _baselineData[0] - roman baseline offset<p>
125: * _baselineData[1] - center baseline offset<p>
126: * _baselineData[2] - hanging baseline offset<p>
127: */
128: public LineMetricsImpl(int len, float[] metrics,
129: float[] _baselineData) {
130: if (metrics.length < 16) {
131: // awt.40=Wrong number of metrics\!
132: throw new IllegalArgumentException(Messages
133: .getString("awt.40")); //$NON-NLS-1$
134: }
135: numChars = len;
136:
137: ascent = metrics[0]; // Ascent of the font
138: descent = metrics[1]; // Descent of the font
139: leading = metrics[2]; // External leading
140:
141: if (_baselineData == null) {
142: baseLineIndex = 0;
143: baselineOffsets = new float[] { 0, (-ascent + descent) / 2,
144: -ascent };
145: } else {
146: baseLineIndex = (int) _baselineData[3];
147: baselineOffsets = new float[3];
148: System.arraycopy(_baselineData, 0, baselineOffsets, 0, 3);
149: }
150:
151: height = metrics[0] + metrics[1] + metrics[2]; // Height of the font ( == (ascent + descent + leading))
152: underlineThickness = metrics[3];
153: underlineOffset = -metrics[4];
154: strikethroughThickness = metrics[5];
155: strikethroughOffset = -metrics[6];
156: maxCharWidth = metrics[7];
157:
158: lAscent = (int) metrics[8]; // Ascent of the font
159: lDescent = (int) metrics[9]; // Descent of the font
160: lLeading = (int) metrics[10]; // External leading
161: lHeight = lAscent + lDescent + lLeading; // Height of the font ( == (ascent + descent + leading))
162: lUnderlineThickness = (int) metrics[11];
163: lUnderlineOffset = -(int) metrics[12];
164: lStrikethroughThickness = (int) metrics[13];
165: lStrikethroughOffset = -(int) metrics[14];
166: lMaxCharWidth = (int) metrics[15];
167:
168: }
169:
170: /**
171: * Creates LineMetricsImpl object from specified parameters. If baseline data parameter
172: * is null than {0, (-ascent+descent)/2, -ascent} values are used for baseline offsets.
173: *
174: * @param _numChars number of chars
175: * @param _baseLineIndex index of the baseline offset
176: * @param _baselineOffsets an array of baseline offsets
177: * @param _underlineThickness underline thickness
178: * @param _underlineOffset underline offset
179: * @param _strikethroughThickness strikethrough thickness
180: * @param _strikethroughOffset strinkethrough offset
181: * @param _leading leading of the font
182: * @param _height font height
183: * @param _ascent ascent of the font
184: * @param _descent descent of the font
185: * @param _maxCharWidth max char width
186: */
187: public LineMetricsImpl(int _numChars, int _baseLineIndex,
188: float[] _baselineOffsets, float _underlineThickness,
189: float _underlineOffset, float _strikethroughThickness,
190: float _strikethroughOffset, float _leading, float _height,
191: float _ascent, float _descent, float _maxCharWidth) {
192:
193: numChars = _numChars;
194: baseLineIndex = _baseLineIndex;
195: underlineThickness = _underlineThickness;
196: underlineOffset = _underlineOffset;
197: strikethroughThickness = _strikethroughThickness;
198: strikethroughOffset = _strikethroughOffset;
199: leading = _leading;
200: height = _height;
201: ascent = _ascent;
202: descent = _descent;
203: baselineOffsets = _baselineOffsets;
204: lUnderlineThickness = (int) underlineThickness;
205: lUnderlineOffset = (int) underlineOffset;
206: lStrikethroughThickness = (int) strikethroughThickness;
207: lStrikethroughOffset = (int) strikethroughOffset;
208: lLeading = (int) leading;
209: lHeight = (int) height;
210: lAscent = (int) ascent;
211: lDescent = (int) descent;
212: maxCharWidth = _maxCharWidth;
213: }
214:
215: public LineMetricsImpl() {
216:
217: }
218:
219: /**
220: * All metrics are scaled according to scaleX and scaleY values.
221: * This function helps to recompute metrics according to the scale factors
222: * of desired AffineTransform.
223: *
224: * @param scaleX scale X factor
225: * @param scaleY scale Y factor
226: */
227: public void scale(float scaleX, float scaleY) {
228: float absScaleX = Math.abs(scaleX);
229: float absScaleY = Math.abs(scaleY);
230:
231: underlineThickness *= absScaleY;
232: underlineOffset *= scaleY;
233: strikethroughThickness *= absScaleY;
234: strikethroughOffset *= scaleY;
235: leading *= absScaleY;
236: height *= absScaleY;
237: ascent *= absScaleY;
238: descent *= absScaleY;
239:
240: if (baselineOffsets == null) {
241: getBaselineOffsets();
242: }
243:
244: for (int i = 0; i < baselineOffsets.length; i++) {
245: baselineOffsets[i] *= scaleY;
246: }
247:
248: lUnderlineThickness *= absScaleY;
249: lUnderlineOffset *= scaleY;
250: lStrikethroughThickness *= absScaleY;
251: lStrikethroughOffset *= scaleY;
252: lLeading *= absScaleY;
253: lHeight *= absScaleY;
254: lAscent *= absScaleY;
255: lDescent *= absScaleY;
256: maxCharWidth *= absScaleX;
257:
258: }
259:
260: /**
261: * Returns offset of the baseline.
262: */
263: @Override
264: public float[] getBaselineOffsets() {
265: // XXX: at the moment there only horizontal metrics are taken into
266: // account. If there is no baseline information in TrueType font
267: // file default values used: {0, -ascent, (-ascent+descent)/2}
268:
269: return baselineOffsets;
270: }
271:
272: /**
273: * Returns a number of chars in specified text
274: */
275: @Override
276: public int getNumChars() {
277: return numChars;
278: }
279:
280: /**
281: * Returns index of the baseline, one of predefined constants.
282: */
283: @Override
284: public int getBaselineIndex() {
285: // Baseline index is the deafult baseline index value
286: // taken from the TrueType table "BASE".
287: return baseLineIndex;
288: }
289:
290: /**
291: * Returns thickness of the Underline.
292: */
293: @Override
294: public float getUnderlineThickness() {
295: return underlineThickness;
296: }
297:
298: /**
299: * Returns offset of the Underline.
300: */
301: @Override
302: public float getUnderlineOffset() {
303: return underlineOffset;
304: }
305:
306: /**
307: * Returns thickness of the Strikethrough line.
308: */
309: @Override
310: public float getStrikethroughThickness() {
311: return strikethroughThickness;
312: }
313:
314: /**
315: * Returns offset of the Strikethrough line.
316: */
317: @Override
318: public float getStrikethroughOffset() {
319: return strikethroughOffset;
320: }
321:
322: /**
323: * Returns the leading.
324: */
325: @Override
326: public float getLeading() {
327: return leading;
328: }
329:
330: /**
331: * Returns the height of the font.
332: */
333: @Override
334: public float getHeight() {
335: return height; // equals to (ascent + descent + leading);
336: }
337:
338: /**
339: * Returns the descent.
340: */
341: @Override
342: public float getDescent() {
343: return descent;
344: }
345:
346: /**
347: * Returns the ascent.
348: */
349: @Override
350: public float getAscent() {
351: return ascent;
352: }
353:
354: /**
355: * Returns logical thickness of the Underline.
356: */
357: public int getLogicalUnderlineThickness() {
358: return lUnderlineThickness;
359: }
360:
361: /**
362: * Returns logical offset of the Underline.
363: */
364: public int getLogicalUnderlineOffset() {
365: return lUnderlineOffset;
366: }
367:
368: /**
369: * Returns logical thickness of the Strikethrough line.
370: */
371: public int getLogicalStrikethroughThickness() {
372: return lStrikethroughThickness;
373: }
374:
375: /**
376: * Returns logical offset of the Strikethrough line.
377: */
378: public int getLogicalStrikethroughOffset() {
379: return lStrikethroughOffset;
380: }
381:
382: /**
383: * Returns the logical leading.
384: */
385: public int getLogicalLeading() {
386: return lLeading;
387: }
388:
389: /**
390: * Returns the logical height of the font.
391: */
392: public int getLogicalHeight() {
393: return lHeight; // equals to (ascent + descent + leading);
394: }
395:
396: /**
397: * Returns the logical descent.
398: */
399: public int getLogicalDescent() {
400: return lDescent;
401: }
402:
403: /**
404: * Returns the logical ascent.
405: */
406: public int getLogicalAscent() {
407: return lAscent;
408: }
409:
410: /**
411: * Returns the logical size of the widest char.
412: */
413: public int getLogicalMaxCharWidth() {
414: return lMaxCharWidth;
415: }
416:
417: /**
418: * Returns the size of the widest char.
419: */
420: public float getMaxCharWidth() {
421: return maxCharWidth;
422: }
423:
424: /**
425: * Set num chars to the desired value.
426: *
427: * @param num specified number of chars
428: */
429: public void setNumChars(int num) {
430: numChars = num;
431: }
432:
433: @Override
434: public Object clone() {
435: try {
436: return super .clone();
437: } catch (CloneNotSupportedException e) {
438: return null;
439: }
440: }
441:
442: }
|