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;
023: import java.awt.FontMetrics;
024: import java.awt.geom.AffineTransform;
025:
026: /**
027: * FontMetrics implementation
028: */
029:
030: public class FontMetricsImpl extends FontMetrics {
031:
032: private static final long serialVersionUID = 844695615201925138L;
033:
034: // ascent of the font
035: private int ascent;
036:
037: //descent of the font
038: private int descent;
039:
040: // leading of the font
041: private int leading;
042:
043: // maximum ascent of the font
044: private int maxAscent;
045:
046: // maximum descent of the font
047: private int maxDescent;
048:
049: // maximum advance of the font
050: private int maxAdvance;
051:
052: // array of char advance widths
053: private int[] widths = new int[256];
054:
055: // font peer corresponding to this FontPeerImpl
056: private transient FontPeerImpl peer;
057:
058: // X scale parameter of the font transform
059: private float scaleX = 1;
060:
061: // Y scale parameter of the font transform
062: // private float scaleY = 1;
063:
064: /**
065: * Creates new FontMericsImpl object described by the specified Font.
066: *
067: * @param fnt the specified Font object
068: */
069: public FontMetricsImpl(Font fnt) {
070: super (fnt);
071: peer = getFontPeer();
072: AffineTransform at = fnt.getTransform();
073: if (!at.isIdentity()) {
074: scaleX = (float) at.getScaleX();
075: // scaleY = (float)at.getScaleY();
076: }
077:
078: LineMetricsImpl lm = (LineMetricsImpl) peer.getLineMetrics(
079: "", null, at); //$NON-NLS-1$
080:
081: this .ascent = lm.getLogicalAscent();
082: this .descent = lm.getLogicalDescent();
083: this .leading = lm.getLogicalLeading();
084: this .maxAscent = ascent;
085: this .maxDescent = descent;
086: this .maxAdvance = lm.getLogicalMaxCharWidth();
087: // initWidths();
088: }
089:
090: /**
091: * Initialize the array of the first 256 chars' advance widths
092: * of the Font describing this FontMetricsImpl object.
093: */
094: private void initWidths() {
095:
096: this .widths = new int[256];
097: for (int chr = 0; chr < 256; chr++) {
098: widths[chr] = (int) (getFontPeer().charWidth((char) chr) * scaleX);
099: }
100:
101: }
102:
103: /**
104: * Returns the ascent of the Font describing this FontMetricsImpl object.
105: */
106: @Override
107: public int getAscent() {
108: return this .ascent;
109: }
110:
111: /**
112: * Returns the descent of the Font describing this FontMetricsImpl object.
113: */
114: @Override
115: public int getDescent() {
116: return this .descent;
117: }
118:
119: /**
120: * Returns the leading of the Font describing this FontMetricsImpl object.
121: */
122: @Override
123: public int getLeading() {
124: return this .leading;
125: }
126:
127: /**
128: * Returns the advance width of the specified char of the Font
129: * describing this FontMetricsImpl object.
130: *
131: * @param ch the char which width is to be returned
132: * @return the advance width of the specified char of the Font
133: * describing this FontMetricsImpl object
134: */
135: @Override
136: public int charWidth(int ch) {
137: // if (ch < 256){
138: // return widths[ch];
139: // }
140:
141: return getFontPeer().charWidth((char) ch);
142: }
143:
144: /**
145: * Returns the advance width of the specified char of the Font
146: * describing this FontMetricsImpl object.
147: *
148: * @param ch the char which width is to be returned
149: * @return the advance width of the specified char of the Font
150: * describing this FontMetricsImpl object
151: */
152: @Override
153: public int charWidth(char ch) {
154: // if (ch < 256){
155: // return widths[ch];
156: // }
157:
158: return (int) (getFontPeer().charWidth(ch) * scaleX);
159: }
160:
161: /**
162: * Returns the maximum advance of the Font describing this
163: * FontMetricsImpl object.
164: */
165: @Override
166: public int getMaxAdvance() {
167: return this .maxAdvance;
168: }
169:
170: /**
171: * Returns the maximum ascent of the Font describing this
172: * FontMetricsImpl object.
173: */
174: @Override
175: public int getMaxAscent() {
176: return this .maxAscent;
177: }
178:
179: /**
180: * Returns the maximum descent of the Font describing this
181: * FontMetricsImpl object.
182: */
183: @SuppressWarnings("deprecation")
184: @Deprecated
185: @Override
186: public int getMaxDecent() {
187: return this .maxDescent;
188: }
189:
190: /**
191: * Returns the maximum descent of the Font describing this
192: * FontMetricsImpl object.
193: */
194: @Override
195: public int getMaxDescent() {
196: return this .maxDescent;
197: }
198:
199: /**
200: * Returns the advance widths of the first 256 characters in the Font
201: * describing this FontMetricsImpl object.
202: */
203: @Override
204: public int[] getWidths() {
205: this .widths = new int[256];
206: for (int chr = 0; chr < 256; chr++) {
207: widths[chr] = (int) (getFontPeer().charWidth((char) chr) * scaleX);
208: }
209: return this .widths;
210: }
211:
212: /**
213: * Returns the total advance width of the specified string in the metrics
214: * of the Font describing this FontMetricsImpl object.
215: *
216: * @param str the String which width is to be measured
217: * @return the total advance width of the specified string in the metrics
218: * of the Font describing this FontMetricsImpl object
219: */
220: @Override
221: public int stringWidth(String str) {
222: int width = 0;
223: char chr;
224:
225: for (int i = 0; i < str.length(); i++) {
226: chr = str.charAt(i);
227: width += charWidth(chr);
228: }
229:
230: return width;
231:
232: }
233:
234: /**
235: * Returns FontPeer implementation of the Font describing this
236: * FontMetricsImpl object.
237: *
238: * @return a FontPeer object, that is the platform dependent FontPeer
239: * implementation for the Font describing this FontMetricsImpl object.
240: */
241: @SuppressWarnings("deprecation")
242: public FontPeerImpl getFontPeer() {
243: if (peer == null) {
244: peer = (FontPeerImpl) font.getPeer();
245: }
246: return peer;
247: }
248: }
|