1/*
2 *  Licensed to the Apache Software Foundation (ASF) under one or more
3 *  contributor license agreements.  See the NOTICE file distributed with
4 *  this work for additional information regarding copyright ownership.
5 *  The ASF licenses this file to You under the Apache License, Version 2.0
6 *  (the "License"); you may not use this file except in compliance with
7 *  the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *  Unless required by applicable law or agreed to in writing, software
12 *  distributed under the License is distributed on an "AS IS" BASIS,
13 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *  See the License for the specific language governing permissions and
15 *  limitations under the License.
16 */
17/**
18 * @author Ilya S. Okomin
19 * @version $Revision$
20 */
21package org.apache.harmony.awt.gl.font;
22
23import com.android.internal.awt.AndroidGraphics2D;
24
25import java.awt.Font;
26import java.awt.FontMetrics;
27//import java.awt.Paint;
28import java.awt.geom.AffineTransform;
29
30import android.graphics.Paint;
31
32/**
33 * FontMetrics implementation
34 */
35
36public class FontMetricsImpl extends FontMetrics {
37
38	private static final long serialVersionUID = 844695615201925138L;
39
40	// ascent of the font
41	private int ascent;
42
43	// descent of the font
44	private int descent;
45
46	// leading of the font
47	private int leading;
48
49	// maximum ascent of the font
50	private int maxAscent;
51
52	// maximum descent of the font
53	private int maxDescent;
54
55	// maximum advance of the font
56	private int maxAdvance;
57
58	// array of char advance widths
59	private int[] widths = new int[256];
60
61	// font peer corresponding to this FontPeerImpl
62	private transient FontPeerImpl peer;
63
64	// X scale parameter of the font transform
65	private float scaleX = 1;
66
67	public AndroidGraphics2D mSg;
68
69	private Font mFn;
70
71	// Y scale parameter of the font transform
72	private float scaleY = 1;
73
74	/**
75	 * Creates new FontMericsImpl object described by the specified Font.
76	 *
77	 * @param fnt
78	 *            the specified Font object
79	 */
80	public FontMetricsImpl(Font fnt) {
81		super(fnt);
82		this.mFn = fnt;
83
84		mSg = AndroidGraphics2D.getInstance();
85		Paint p = mSg.getAndroidPaint();
86
87		this.ascent = (int)-p.ascent();
88		this.descent = (int)p.descent();
89		this.leading = p.getFontMetricsInt().leading;
90
91		AffineTransform at = fnt.getTransform();
92		if (!at.isIdentity()) {
93			scaleX = (float) at.getScaleX();
94			scaleY = (float) at.getScaleY();
95		}
96
97	    /*
98	     * metrics[5] - strikethrough thickness<p>
99	     * -metrics[6] - strikethrough offset<p>
100	     * metrics[7] - maximum char width<p>
101	     * metrics[8] - ascent in pixels<p>
102	     * metrics[9] - descent in pixles<p>
103	     * metrics[10] - external leading in pixels<p>
104	     * metrics[11] - underline thickness in pixels<p>
105	     * -metrics[12] - underline offset in pixels<p>
106	     * metrics[13] - strikethrough thickness in pixels<p>
107	     * -metrics[14] - strikethrough offset in pixels<p>
108	     * metrics[15] - maximum char width in pixels<p>
109
110	     * @param _baselineData an array of 3 elements with baseline offsets metrics<p>
111	     * _baselineData[0] - roman baseline offset<p>
112	     * _baselineData[1] - center baseline offset<p>
113	     * _baselineData[2] - hanging baseline offset<p>
114	     */
115	}
116
117
118	/**
119	 * Initialize the array of the first 256 chars' advance widths of the Font
120	 * describing this FontMetricsImpl object.
121	 */
122	private void initWidths() {
123
124		this.widths = new int[256];
125		for (int chr = 0; chr < 256; chr++) {
126			widths[chr] = (int) (getFontPeer().charWidth((char) chr) * scaleX);
127		}
128
129	}
130
131	/**
132	 * Returns the ascent of the Font describing this FontMetricsImpl object.
133	 */
134	@Override
135	public int getAscent() {
136		return this.ascent;
137	}
138
139	/**
140	 * Returns the descent of the Font describing this FontMetricsImpl object.
141	 */
142	@Override
143	public int getDescent() {
144		return this.descent;
145	}
146
147	/**
148	 * Returns the leading of the Font describing this FontMetricsImpl object.
149	 */
150	@Override
151	public int getLeading() {
152		return this.leading;
153	}
154
155	/**
156	 * Returns the advance width of the specified char of the Font describing
157	 * this FontMetricsImpl object.
158	 *
159	 * @param ch
160	 *            the char which width is to be returned
161	 * @return the advance width of the specified char of the Font describing
162	 *         this FontMetricsImpl object
163	 */
164	@Override
165	public int charWidth(int ch) {
166		if (ch < 256) {
167			return widths[ch];
168		}
169
170		return getFontPeer().charWidth((char) ch);
171	}
172
173	/**
174	 * Returns the advance width of the specified char of the Font describing
175	 * this FontMetricsImpl object.
176	 *
177	 * @param ch
178	 *            the char which width is to be returned
179	 * @return the advance width of the specified char of the Font describing
180	 *         this FontMetricsImpl object
181	 */
182	@Override
183	public int charWidth(char ch) {
184		if (ch < 256) {
185			return widths[ch];
186		}
187		return (int) (getFontPeer().charWidth(ch) * scaleX);
188	}
189
190	/**
191	 * Returns the maximum advance of the Font describing this FontMetricsImpl
192	 * object.
193	 */
194	@Override
195	public int getMaxAdvance() {
196		return this.maxAdvance;
197	}
198
199	/**
200	 * Returns the maximum ascent of the Font describing this FontMetricsImpl
201	 * object.
202	 */
203	@Override
204	public int getMaxAscent() {
205		return this.maxAscent;
206	}
207
208	/**
209	 * Returns the maximum descent of the Font describing this FontMetricsImpl
210	 * object.
211	 */
212	@SuppressWarnings("deprecation")
213	@Deprecated
214	@Override
215	public int getMaxDecent() {
216		return this.maxDescent;
217	}
218
219	/**
220	 * Returns the maximum descent of the Font describing this FontMetricsImpl
221	 * object.
222	 */
223	@Override
224	public int getMaxDescent() {
225		return this.maxDescent;
226	}
227
228	/**
229	 * Returns the advance widths of the first 256 characters in the Font
230	 * describing this FontMetricsImpl object.
231	 */
232	@Override
233	public int[] getWidths() {
234		return this.widths;
235	}
236
237	/**
238	 * Returns the total advance width of the specified string in the metrics of
239	 * the Font describing this FontMetricsImpl object.
240	 *
241	 * @param str
242	 *            the String which width is to be measured
243	 * @return the total advance width of the specified string in the metrics of
244	 *         the Font describing this FontMetricsImpl object
245	 */
246	@Override
247	public int stringWidth(String str) {
248
249		int width = 0;
250		char chr;
251
252		for (int i = 0; i < str.length(); i++) {
253			chr = str.charAt(i);
254			width += charWidth(chr);
255		}
256		return width;
257
258		/*
259		 * float res = 0; int ln = str.length(); char[] c = new char[ln]; float[] f =
260		 * new float[ln]; str.getChars(0, ln, c, 0); mSg.getPaint().getTextWidths(c, 0,
261		 * ln, f);
262		 *
263		 * for(int i = 0; i < f.length; i++) { res += f[i]; } return (int)res;
264		 */
265	}
266
267	/**
268	 * Returns FontPeer implementation of the Font describing this
269	 * FontMetricsImpl object.
270	 *
271	 * @return a FontPeer object, that is the platform dependent FontPeer
272	 *         implementation for the Font describing this FontMetricsImpl
273	 *         object.
274	 */
275	@SuppressWarnings("deprecation")
276	public FontPeerImpl getFontPeer() {
277		if (peer == null) {
278			peer = (FontPeerImpl) font.getPeer();
279		}
280		return peer;
281	}
282}
283