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.FontRenderContext;
023: import java.awt.font.LineMetrics;
024: import java.awt.geom.AffineTransform;
025: import java.awt.geom.Rectangle2D;
026: import java.awt.peer.FontPeer;
027: import java.util.ArrayList;
028: import java.util.Locale;
029:
030: import org.apache.harmony.awt.internal.nls.Messages;
031:
032: /**
033: * Abstract class for platform dependent peer implementation of the Font class.
034: */
035: public abstract class FontPeerImpl implements FontPeer {
036:
037: // ascent of this font peer (in pixels)
038: int ascent;
039:
040: // descent of this font peer (in pixels)
041: int descent;
042:
043: // leading of this font peer (in pixels)
044: int leading;
045:
046: // logical maximum advance of this font peer (in pixels)
047: int maxAdvance;
048:
049: // the height of this font peer
050: float height;
051:
052: // the style of this font peer
053: protected int style;
054:
055: // the point size of this font peer (in pixels)
056: protected int size;
057:
058: // the logical hight of this font peer (in pixels)
059: int logicalHeight;
060:
061: // the name of this font peer
062: protected String name;
063:
064: // family name of this font peer
065: String fontFamilyName;
066:
067: // the Face name of this font peer
068: String faceName;
069:
070: // bounds rectanlge of the largest character in this font peer
071: protected Rectangle2D maxCharBounds;
072:
073: // italic angle value of this font peer
074: float italicAngle = 0.0f;
075:
076: // the number of glyphs supported by this font peer
077: int numGlyphs = 0;
078:
079: // native font handle
080: protected long pFont;
081:
082: // cached line metrics object
083: protected LineMetricsImpl nlm = null;
084:
085: // the postscript name of this font peer
086: protected String psName = null;
087:
088: /**
089: * Default glyph index, that is used, when the desired glyph
090: * is unsupported in this Font.
091: */
092: public char defaultChar = (char) 0xFFFF;
093:
094: /**
095: * Uniform LineMetrics flag, that is false for CompositeFont.
096: * Default value is true.
097: */
098: boolean uniformLM = true;
099:
100: /**
101: * Flag of the type of this Font that is indicate is the Font
102: * has TrueType or Type1 type. Default value is FONT_TYPE_UNDEF.
103: */
104: int fontType = FontManager.FONT_TYPE_UNDEF;
105:
106: /**
107: * Flag if this Font was created from stream,
108: * this parameter used in finilize method.
109: */
110: private boolean createdFromStream = false;
111:
112: // temorary Font file name, if this FontPeerImpl was created from InputStream
113: private String tempFontFileName = null;
114:
115: // cached FontExtraMetrics object related to this font peer
116: FontExtraMetrics extraMetrix = null;
117:
118: public abstract FontExtraMetrics getExtraMetrics();
119:
120: /**
121: * Returns LineMetrics object with specified parameters
122: * @param str specified String
123: * @param frc specified render context
124: * @param at specified affine transform
125: * @return
126: */
127: public abstract LineMetrics getLineMetrics(String str,
128: FontRenderContext frc, AffineTransform at);
129:
130: /**
131: * Returns postscript name of the font.
132: */
133: public abstract String getPSName();
134:
135: /**
136: * Set postscript name of the font to the specified parameter.
137: */
138: public void setPSName(String name) {
139: this .psName = name;
140: }
141:
142: /**
143: * Returns code of the missing glyph.
144: */
145: public abstract int getMissingGlyphCode();
146:
147: /**
148: * Returns Glyph representation of the given char.
149: * @param ch specified char
150: */
151: public abstract Glyph getGlyph(char ch);
152:
153: /**
154: * Disposes nesessary resources.
155: */
156: public abstract void dispose();
157:
158: /**
159: * Returns Glyph representing missing char.
160: */
161: public abstract Glyph getDefaultGlyph();
162:
163: /**
164: * Returns true if this FontPeerImpl can display the specified char
165: */
166: public abstract boolean canDisplay(char c);
167:
168: /**
169: * Returns family name of the font in specified locale settings.
170: * @param l specified Locale
171: */
172: public String getFamily(Locale l) {
173: return this .getFamily();
174: }
175:
176: /**
177: * Sets family name of the font in specified locale settings.
178: */
179: public void setFamily(String familyName) {
180: this .fontFamilyName = familyName;
181: }
182:
183: /**
184: * Returns face name of the font in specified locale settings.
185: * @param l specified Locale
186: */
187: public String getFontName(Locale l) {
188: return this .getFontName();
189: }
190:
191: /**
192: * Sets font name of the font in specified locale settings.
193: */
194: public void setFontName(String fontName) {
195: this .faceName = fontName;
196: }
197:
198: /**
199: * Returns true, if this font peer was created from InputStream, false otherwise.
200: * In case of creating fonts from InputStream some font peer implementations
201: * may need to free temporary resources.
202: */
203: public boolean isCreatedFromStream() {
204: return this .createdFromStream;
205: }
206:
207: /**
208: * Sets createdFromStream flag to the specified parameter.
209: * If parameter is true it means font peer was created from InputStream.
210: *
211: * @param value true, if font peer was created from InputStream
212: */
213: public void setCreatedFromStream(boolean value) {
214: this .createdFromStream = value;
215: }
216:
217: /**
218: * Returns font file name of this font.
219: */
220: public String getTempFontFileName() {
221: return this .tempFontFileName;
222: }
223:
224: /**
225: * Sets font file name of this font to the specified one.
226: * @param value String representing font file name
227: */
228: public void setFontFileName(String value) {
229: this .tempFontFileName = value;
230: }
231:
232: /**
233: * Returns the advance width of the specified char of this FontPeerImpl.
234: * Note, if glyph is absent in the font's glyphset - returned value
235: * is the advance of the deafualt glyph. For escape-chars returned
236: * width value is 0.
237: *
238: * @param ch the char which width is to be returned
239: * @return the advance width of the specified char of this FontPeerImpl
240: */
241: public int charWidth(char ch) {
242: Glyph gl = this .getGlyph(ch);
243: return (int) gl.getGlyphPointMetrics().getAdvanceX();
244: }
245:
246: /**
247: * Returns the advance width of the specified char of this FontPeerImpl.
248: *
249: * @param ind the char which width is to be returned
250: * @return the advance width of the specified char of this FontPeerImpl
251: */
252: public int charWidth(int ind) {
253: return charWidth((char) ind);
254: }
255:
256: /**
257: * Returns an array of Glyphs that represent characters from the specified
258: * Unicode range.
259: *
260: * @param uFirst start position in Unicode range
261: * @param uLast end position in Unicode range
262: * @return
263: */
264: public Glyph[] getGlyphs(char uFirst, char uLast) {
265:
266: char i = uFirst;
267: int len = uLast - uFirst;
268: ArrayList<Glyph> lst = new ArrayList<Glyph>(len);
269:
270: if (size < 0) {
271: // awt.09=min range bound value is greater than max range bound
272: throw new IllegalArgumentException(Messages
273: .getString("awt.09")); //$NON-NLS-1$
274: }
275:
276: while (i < uLast) {
277: lst.add(this .getGlyph(i));
278: }
279:
280: return (Glyph[]) lst.toArray();
281: }
282:
283: /**
284: * Returns an array of Glyphs representing given array of chars.
285: *
286: * @param chars specified array of chars
287: */
288: public Glyph[] getGlyphs(char[] chars) {
289: if (chars == null) {
290: return null;
291: }
292:
293: Glyph[] result = new Glyph[chars.length];
294:
295: for (int i = 0; i < chars.length; i++) {
296: result[i] = this .getGlyph(chars[i]);
297: }
298: return result;
299: }
300:
301: /**
302: * Returns an array of Glyphs representing given string.
303: *
304: * @param str specified string
305: */
306: public Glyph[] getGlyphs(String str) {
307:
308: char[] chars = str.toCharArray();
309: return this .getGlyphs(chars);
310: }
311:
312: /**
313: * Returns family name of this FontPeerImpl.
314: */
315: public String getFamily() {
316: return fontFamilyName;
317: }
318:
319: /**
320: * Returns face name of this FontPeerImpl.
321: */
322: public String getFontName() {
323: if (this .fontType == FontManager.FONT_TYPE_T1) {
324: return this .fontFamilyName;
325: }
326:
327: return faceName;
328: }
329:
330: /**
331: * Returns height of this font peer in pixels.
332: */
333: public int getLogicalHeight() {
334: return logicalHeight;
335: }
336:
337: /**
338: * Sets height of this font peer in pixels to the given value.
339: *
340: * @param newHeight new height in pixels value
341: */
342: public void setLogicalHeight(int newHeight) {
343: logicalHeight = newHeight;
344: }
345:
346: /**
347: * Returns font size.
348: */
349: public int getSize() {
350: return size;
351: }
352:
353: /**
354: * Returns font style.
355: */
356: public int getStyle() {
357: return style;
358: }
359:
360: /**
361: * Returns font name.
362: */
363: public String getName() {
364: return name;
365: }
366:
367: /**
368: * Returns the bounds of the largest char in this FontPeerImpl in
369: * specified render context.
370: *
371: * @param frc specified FontRenderContext
372: */
373: public Rectangle2D getMaxCharBounds(FontRenderContext frc) {
374: if (frc != null) {
375: AffineTransform at = frc.getTransform();
376: return at.createTransformedShape(maxCharBounds)
377: .getBounds2D();
378: } else
379: return maxCharBounds;
380: }
381:
382: /**
383: * Returns the number of glyphs in this FontPeerImpl.
384: */
385: public int getNumGlyphs() {
386: return numGlyphs;
387: }
388:
389: /**
390: * Returns tangens of the italic angle of this FontPeerImpl.
391: * If the FontPeerImpl has TrueType font type, italic angle value can be
392: * calculated as (CharSlopeRun / CharSlopeRise) in terms of GDI.
393: */
394: public float getItalicAngle() {
395: return italicAngle;
396: }
397:
398: /**
399: * Returns height of this font peer.
400: */
401: public float getHeight() {
402: return height;
403: }
404:
405: /**
406: * Returns cached LineMetrics object of this font peer.
407: */
408: public LineMetrics getLineMetrics() {
409: if (nlm == null) {
410: nlm = (LineMetricsImpl) getLineMetrics("", null,
411: AffineTransform.getTranslateInstance(0, 0));
412: }
413: return nlm;
414: }
415:
416: /**
417: * Returns native font handle of this font peer.
418: */
419: public long getFontHandle() {
420: return pFont;
421: }
422:
423: /**
424: * Returns ascent of this font peer.
425: */
426: public int getAscent() {
427: return ascent;
428: }
429:
430: /**
431: * Returns descent of this font peer.
432: */
433: public int getDescent() {
434: return descent;
435: }
436:
437: /**
438: * Returns leading of this font peer.
439: */
440: public int getLeading() {
441: return leading;
442: }
443:
444: /**
445: * Returns true if this font peer has uniform line metrics.
446: */
447: public boolean hasUniformLineMetrics() {
448: return uniformLM;
449: }
450:
451: /**
452: * Returns type of this font.
453: *
454: * @return one of constant font type values.
455: */
456: public int getFontType() {
457: return fontType;
458: }
459:
460: /**
461: * Sets new font type to the font object.
462: *
463: * @param newType new type value
464: */
465: public void setFontType(int newType) {
466: if (newType == FontManager.FONT_TYPE_T1
467: || newType == FontManager.FONT_TYPE_TT) {
468: fontType = newType;
469: }
470: }
471:
472: /**
473: * Returns unicode by glyph index.
474: */
475: public char getUnicodeByIndex(int glyphCode) {
476: return 0;
477: }
478:
479: @Override
480: protected void finalize() throws Throwable {
481: super.finalize();
482:
483: dispose();
484: }
485:
486: }
|