1:
37:
38: package ;
39:
40: import ;
41: import ;
42: import ;
43: import ;
44: import ;
45: import ;
46: import ;
47: import ;
48: import ;
49: import ;
50: import ;
51: import ;
52: import ;
53: import ;
54: import ;
55: import ;
56: import ;
57: import ;
58: import ;
59: import ;
60:
61: import ;
62: import ;
63: import ;
64:
65: public class XFontPeer2
66: extends ClasspathFontPeer
67: {
68:
69:
72: private static Properties fontProperties;
73: static
74: {
75: fontProperties = new Properties();
76: InputStream in = XFontPeer2.class.getResourceAsStream("fonts.properties");
77: try
78: {
79: fontProperties.load(in);
80: }
81: catch (IOException e)
82: {
83: e.printStackTrace();
84: }
85: }
86:
87: private class XLineMetrics
88: extends LineMetrics
89: {
90:
91: private Font font;
92: private GlyphVector glyphVector;
93:
94:
95:
96: private FontRenderContext fontRenderContext;
97: XLineMetrics(Font f, CharacterIterator ci, int b, int l,
98: FontRenderContext rc)
99: {
100: font = f;
101:
102:
103:
104: fontRenderContext = rc;
105: glyphVector = fontDelegate.createGlyphVector(font, fontRenderContext,
106: ci);
107: }
108:
109: public float getAscent()
110: {
111: return fontDelegate.getAscent(font.getSize(), fontRenderContext.getTransform(),
112: fontRenderContext.isAntiAliased(),
113: fontRenderContext.usesFractionalMetrics(), true);
114: }
115:
116: public int getBaselineIndex()
117: {
118:
119: throw new UnsupportedOperationException("Not yet implemented");
120: }
121:
122: public float[] getBaselineOffsets()
123: {
124:
125: throw new UnsupportedOperationException("Not yet implemented");
126: }
127:
128: public float getDescent()
129: {
130: return (int) fontDelegate.getDescent(font.getSize(), IDENDITY, false,
131: false, false);
132: }
133:
134: public float getHeight()
135: {
136: return (float) glyphVector.getLogicalBounds().getHeight();
137: }
138:
139: public float getLeading()
140: {
141: return getHeight() - getAscent() - getDescent();
142: }
143:
144: public int getNumChars()
145: {
146:
147: throw new UnsupportedOperationException("Not yet implemented");
148: }
149:
150: public float getStrikethroughOffset()
151: {
152: return 0.F;
153: }
154:
155: public float getStrikethroughThickness()
156: {
157: return 0.F;
158: }
159:
160: public float getUnderlineOffset()
161: {
162: return 0.F;
163: }
164:
165: public float getUnderlineThickness()
166: {
167: return 0.F;
168: }
169:
170: }
171:
172: private class XFontMetrics
173: extends FontMetrics
174: {
175:
178: private Point2D cachedPoint = new Point2D.Double();
179:
180: XFontMetrics(Font f)
181: {
182: super(f);
183: }
184:
185: public int getAscent()
186: {
187: return (int) fontDelegate.getAscent(getFont().getSize(), IDENDITY,
188: false, false, false);
189: }
190:
191: public int getDescent()
192: {
193: return (int) fontDelegate.getDescent(getFont().getSize(), IDENDITY,
194: false, false, false);
195: }
196:
197: public int getHeight()
198: {
199: GlyphVector gv = fontDelegate.createGlyphVector(getFont(),
200: new FontRenderContext(IDENDITY, false, false),
201: new StringCharacterIterator("m"));
202: Rectangle2D b = gv.getVisualBounds();
203: return (int) b.getHeight();
204: }
205:
206: public int charWidth(char c)
207: {
208: int code = fontDelegate.getGlyphIndex(c);
209: Point2D advance = cachedPoint;
210: fontDelegate.getAdvance(code, font.getSize2D(), IDENDITY,
211: false, false, true, advance);
212: return (int) advance.getX();
213: }
214:
215: public int charsWidth(char[] chars, int offs, int len)
216: {
217: return stringWidth(new String(chars, offs, len));
218: }
219:
220: public int stringWidth(String s)
221: {
222: GlyphVector gv = fontDelegate.createGlyphVector(getFont(),
223: new FontRenderContext(IDENDITY, false, false),
224: new StringCharacterIterator(s));
225: Rectangle2D b = gv.getVisualBounds();
226: return (int) b.getWidth();
227: }
228: }
229:
230:
233: private static final AffineTransform IDENDITY = new AffineTransform();
234:
235: private FontDelegate fontDelegate;
236:
237: XFontPeer2(String name, int style, int size)
238: {
239: super(name, style, size);
240: try
241: {
242: File fontfile = new File("/usr/share/fonts/truetype/freefont/FreeSans.ttf");
243: FileInputStream in = new FileInputStream(fontfile);
244: FileChannel ch = in.getChannel();
245: ByteBuffer buffer = ch.map(FileChannel.MapMode.READ_ONLY, 0,
246: fontfile.length());
247: fontDelegate = FontFactory.createFonts(buffer)[0];
248: }
249: catch (Exception ex)
250: {
251: ex.printStackTrace();
252: }
253: }
254:
255: XFontPeer2(String name, Map atts)
256: {
257: super(name, atts);
258: try
259: {
260: File fontfile = new File("/usr/share/fonts/truetype/freefont/FreeSans.ttf");
261: FileInputStream in = new FileInputStream(fontfile);
262: FileChannel ch = in.getChannel();
263: ByteBuffer buffer = ch.map(FileChannel.MapMode.READ_ONLY, 0,
264: fontfile.length());
265: fontDelegate = FontFactory.createFonts(buffer)[0];
266: }
267: catch (Exception ex)
268: {
269: ex.printStackTrace();
270: }
271: }
272:
273: public boolean canDisplay(Font font, int c)
274: {
275:
276: throw new UnsupportedOperationException("Not yet implemented");
277: }
278:
279: public int canDisplayUpTo(Font font, CharacterIterator i, int start, int limit)
280: {
281:
282: throw new UnsupportedOperationException("Not yet implemented");
283: }
284:
285: public String getSubFamilyName(Font font, Locale locale)
286: {
287:
288: throw new UnsupportedOperationException("Not yet implemented");
289: }
290:
291: public String getPostScriptName(Font font)
292: {
293:
294: throw new UnsupportedOperationException("Not yet implemented");
295: }
296:
297: public int getNumGlyphs(Font font)
298: {
299:
300: throw new UnsupportedOperationException("Not yet implemented");
301: }
302:
303: public int getMissingGlyphCode(Font font)
304: {
305:
306: throw new UnsupportedOperationException("Not yet implemented");
307: }
308:
309: public byte getBaselineFor(Font font, char c)
310: {
311:
312: throw new UnsupportedOperationException("Not yet implemented");
313: }
314:
315: public String getGlyphName(Font font, int glyphIndex)
316: {
317:
318: throw new UnsupportedOperationException("Not yet implemented");
319: }
320:
321: public GlyphVector createGlyphVector(Font font, FontRenderContext frc, CharacterIterator ci)
322: {
323: return fontDelegate.createGlyphVector(font, frc, ci);
324: }
325:
326: public GlyphVector createGlyphVector(Font font, FontRenderContext ctx, int[] glyphCodes)
327: {
328:
329: throw new UnsupportedOperationException("Not yet implemented");
330: }
331:
332: public GlyphVector layoutGlyphVector(Font font, FontRenderContext frc, char[] chars, int start, int limit, int flags)
333: {
334: StringCharacterIterator i = new StringCharacterIterator(new String(chars), start, limit, 0);
335: return fontDelegate.createGlyphVector(font, frc, i);
336: }
337:
338: public FontMetrics getFontMetrics(Font font)
339: {
340: return new XFontMetrics(font);
341: }
342:
343: public boolean hasUniformLineMetrics(Font font)
344: {
345:
346: throw new UnsupportedOperationException("Not yet implemented");
347: }
348:
349: public LineMetrics getLineMetrics(Font font, CharacterIterator ci, int begin, int limit, FontRenderContext rc)
350: {
351: return new XLineMetrics(font, ci, begin, limit, rc);
352: }
353:
354: public Rectangle2D getMaxCharBounds(Font font, FontRenderContext rc)
355: {
356:
357: throw new UnsupportedOperationException("Not yet implemented");
358: }
359:
360:
374: static String encodeFont(String name, Map atts)
375: {
376: String family = name;
377: if (family == null || family.equals(""))
378: family = (String) atts.get(TextAttribute.FAMILY);
379: if (family == null)
380: family = "SansSerif";
381:
382: int size = 12;
383: Float sizeFl = (Float) atts.get(TextAttribute.SIZE);
384: if (sizeFl != null)
385: size = sizeFl.intValue();
386:
387: int style = 0;
388:
389: Float posture = (Float) atts.get(TextAttribute.POSTURE);
390: if (posture != null && !posture.equals(TextAttribute.POSTURE_REGULAR))
391: style |= Font.ITALIC;
392:
393:
394: Float weight = (Float) atts.get(TextAttribute.WEIGHT);
395: if (weight != null && weight.compareTo(TextAttribute.WEIGHT_REGULAR) > 0)
396: style |= Font.BOLD;
397:
398: return encodeFont(name, style, size);
399: }
400:
401:
416: static String encodeFont(String name, int style, int size)
417: {
418: StringBuilder key = new StringBuilder();
419: key.append(validName(name));
420: key.append('.');
421: switch (style)
422: {
423: case Font.BOLD:
424: key.append("bold");
425: break;
426: case Font.ITALIC:
427: key.append("italic");
428: break;
429: case (Font.BOLD | Font.ITALIC):
430: key.append("bolditalic");
431: break;
432: case Font.PLAIN:
433: default:
434: key.append("plain");
435:
436: }
437:
438: String protoType = fontProperties.getProperty(key.toString());
439: int s = size;
440: return protoType.replaceFirst("%d", String.valueOf(s * 10));
441: }
442:
443:
451: static String validName(String name)
452: {
453: String retVal;
454: if (name.equalsIgnoreCase("sansserif")
455: || name.equalsIgnoreCase("serif")
456: || name.equalsIgnoreCase("monospaced")
457: || name.equalsIgnoreCase("dialog")
458: || name.equalsIgnoreCase("dialoginput"))
459: {
460: retVal = name.toLowerCase();
461: }
462: else
463: {
464: retVal = "sansserif";
465: }
466: return retVal;
467: }
468: }