1 /* GdkClasspathFontPeer.java -- backend implementation for Font object
2    Copyright (C) 2003 Free Software Foundation, Inc.
3 
4    This file is part of GNU Classpath.
5 
6    GNU Classpath is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2, or (at your option)
9    any later version.
10 
11    GNU Classpath is distributed in the hope that it will be useful, but
12    WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14    General Public License for more details.
15 
16    You should have received a copy of the GNU General Public License
17    along with GNU Classpath; see the file COPYING.  If not, write to the
18    Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19    02111-1307 USA.
20 
21    Linking this library statically or dynamically with other modules is
22    making a combined work based on this library.  Thus, the terms and
23    conditions of the GNU General Public License cover the whole
24    combination.
25 
26    As a special exception, the copyright holders of this library give you
27    permission to link this library with independent modules to produce an
28    executable, regardless of the license terms of these independent
29    modules, and to copy and distribute the resulting executable under
30    terms of your choice, provided that you also meet, for each linked
31    independent module, the terms and conditions of the license of that
32    module.  An independent module is a module which is not derived from
33    or based on this library.  If you modify this library, you may extend
34    this exception to your version of the library, but you are not
35    obligated to do so.  If you do not wish to do so, delete this
36    exception statement from your version. */
37 
38 
39 package gnu.java.awt.peer.gtk;
40 
41 import java.awt.*;
42 import java.awt.font.*;
43 import java.awt.geom.*;
44 import java.io.InputStream;
45 import java.io.IOException;
46 import java.io.Serializable;
47 import java.util.Locale;
48 import java.util.Map;
49 import java.util.StringTokenizer;
50 import java.text.CharacterIterator;
51 import java.text.AttributedCharacterIterator;
52 import java.text.StringCharacterIterator;
53 import java.awt.font.TextAttribute;
54 import gnu.classpath.Configuration;
55 import gnu.java.awt.peer.ClasspathFontPeer;
56 
57 /**
58  * This class represents a windowing system font using the Pango
59  * unicode/glyph/font library and the Cairo rendering library.
60  *
61  * @author Graydon Hoare (graydon@redhat.com)
62  */
63 
64 public class GdkClasspathFontPeer extends ClasspathFontPeer
65 {
66 
67   static
68   {
69     if (Configuration.INIT_LOAD_LIBRARY)
70       {
71         System.loadLibrary("gtkpeer");
72       }
initStaticState()73     initStaticState ();
74   }
initStaticState()75   native static void initStaticState ();
76   private final int native_state = GtkGenericPeer.getUniqueInteger ();
77 
78 
79   /* Instance Variables */
80 
initState()81   private native void initState ();
dispose()82   private native void dispose ();
setFont(String family, int style, int size)83   private native void setFont (String family, int style, int size);
84 
sync()85   protected void sync ()
86   {
87     this.setFont (this.familyName, this.style, (int)this.size);
88   }
89 
finalize()90   protected void finalize ()
91   {
92     dispose ();
93   }
94 
95   /*
96    * Helpers for the 3-way overloading that this class seems to suffer
97    * from. Remove them if you feel like they're a performance bottleneck,
98    * for the time being I prefer my code not be written and debugged in
99    * triplicate.
100    */
101 
buildString(CharacterIterator i)102   private String buildString(CharacterIterator i) {
103     String s = new String ();
104     for(char c = i.first(); c != CharacterIterator.DONE; c = i.next())
105       s += c;
106     return s;
107   }
108 
buildString(CharacterIterator iter, int begin, int limit)109   private String buildString(CharacterIterator iter, int begin, int limit) {
110     String s = new String ();
111     int i = 0;
112     for(char c = iter.first(); c != CharacterIterator.DONE; c = iter.next(), i++)
113       {
114         if (begin <= i)
115           s += c;
116         if (limit <= i)
117           break;
118       }
119     return s;
120   }
121 
buildString(char[] chars, int begin, int limit)122   private String buildString(char[] chars, int begin, int limit) {
123     String s = new String ();
124     for(int i = begin; i <= limit; i++)
125       s += chars[i];
126     return s;
127   }
128 
129   /* Public API */
130 
GdkClasspathFontPeer(String name, int style, int size)131   public GdkClasspathFontPeer (String name, int style, int size)
132   {
133     super(name, style, size);
134     initState ();
135     setFont (this.familyName, this.style, (int)this.size);
136   }
137 
GdkClasspathFontPeer(String name, Map attributes)138   public GdkClasspathFontPeer (String name, Map attributes)
139   {
140     super(name, attributes);
141     initState ();
142     setFont (this.familyName, this.style, (int)this.size);
143   }
144 
getSubFamilyName(Font font, Locale locale)145   public String getSubFamilyName(Font font, Locale locale)
146   {
147     return null;
148   }
149 
getPostScriptName(Font font)150   public String getPostScriptName(Font font)
151   {
152     return null;
153   }
154 
canDisplay(Font font, char c)155   public boolean canDisplay (Font font, char c)
156   {
157     throw new UnsupportedOperationException ();
158   }
159 
canDisplayUpTo(Font font, CharacterIterator i, int start, int limit)160   public int canDisplayUpTo (Font font, CharacterIterator i, int start, int limit)
161   {
162     throw new UnsupportedOperationException ();
163   }
164 
createGlyphVector(Font font, FontRenderContext ctx, CharacterIterator i)165   public GlyphVector createGlyphVector (Font font,
166                                         FontRenderContext ctx,
167                                         CharacterIterator i)
168   {
169     return new GdkGlyphVector(font, this, ctx, buildString (i));
170   }
171 
createGlyphVector(Font font, FontRenderContext ctx, int[] glyphCodes)172   public GlyphVector createGlyphVector (Font font,
173                                         FontRenderContext ctx,
174                                         int[] glyphCodes)
175   {
176     return new GdkGlyphVector (font, this, ctx, glyphCodes);
177   }
178 
getBaselineFor(Font font, char c)179   public byte getBaselineFor (Font font, char c)
180   {
181     throw new UnsupportedOperationException ();
182   }
183 
184   protected class GdkFontLineMetrics extends LineMetrics
185   {
186     FontMetrics fm;
187     int nchars;
188 
GdkFontLineMetrics(FontMetrics m, int n)189     public GdkFontLineMetrics (FontMetrics m, int n)
190     {
191       fm = m;
192       nchars = n;
193     }
194 
getAscent()195     public float getAscent()
196     {
197       return (float) fm.getAscent ();
198     }
199 
getBaselineIndex()200     public int getBaselineIndex()
201     {
202       return Font.ROMAN_BASELINE;
203     }
204 
getBaselineOffsets()205     public float[] getBaselineOffsets()
206     {
207       return new float[3];
208     }
209 
getDescent()210     public float getDescent()
211     {
212       return (float) fm.getDescent ();
213     }
214 
getHeight()215     public float getHeight()
216     {
217       return (float) fm.getHeight ();
218     }
219 
getLeading()220     public float getLeading() { return 0.f; }
getNumChars()221     public int getNumChars() { return nchars; }
getStrikethroughOffset()222     public float getStrikethroughOffset() { return 0.f; }
getStrikethroughThickness()223     public float getStrikethroughThickness() { return 0.f; }
getUnderlineOffset()224     public float getUnderlineOffset() { return 0.f; }
getUnderlineThickness()225     public float getUnderlineThickness() { return 0.f; }
226 
227   }
228 
229 
getLineMetrics(Font font, CharacterIterator ci, int begin, int limit, FontRenderContext rc)230   public LineMetrics getLineMetrics (Font font, CharacterIterator ci,
231                                      int begin, int limit, FontRenderContext rc)
232   {
233     return new GdkFontLineMetrics (getFontMetrics (font), limit - begin);
234   }
235 
getMaxCharBounds(Font font, FontRenderContext rc)236   public Rectangle2D getMaxCharBounds (Font font, FontRenderContext rc)
237   {
238     throw new UnsupportedOperationException ();
239   }
240 
getMissingGlyphCode(Font font)241   public int getMissingGlyphCode (Font font)
242   {
243     throw new UnsupportedOperationException ();
244   }
245 
getGlyphName(Font font, int glyphIndex)246   public String getGlyphName (Font font, int glyphIndex)
247   {
248     throw new UnsupportedOperationException ();
249   }
250 
getNumGlyphs(Font font)251   public int getNumGlyphs (Font font)
252   {
253     throw new UnsupportedOperationException ();
254   }
255 
getStringBounds(Font font, CharacterIterator ci, int begin, int limit, FontRenderContext frc)256   public Rectangle2D getStringBounds (Font font, CharacterIterator ci,
257                                       int begin, int limit, FontRenderContext frc)
258   {
259     throw new UnsupportedOperationException ();
260   }
261 
hasUniformLineMetrics(Font font)262   public boolean hasUniformLineMetrics (Font font)
263   {
264     return true;
265   }
266 
layoutGlyphVector(Font font, FontRenderContext frc, char[] chars, int start, int limit, int flags)267   public GlyphVector layoutGlyphVector (Font font, FontRenderContext frc,
268                                         char[] chars, int start, int limit,
269                                         int flags)
270   {
271     int nchars = (limit - start) + 1;
272     char[] nc = new char[nchars];
273 
274     for (int i = 0; i < nchars; ++i)
275       nc[i] = chars[start + i];
276 
277     return createGlyphVector (font, frc,
278                               new StringCharacterIterator (new String (nc)));
279   }
280 
getLineMetrics(Font font, String str, FontRenderContext frc)281   public LineMetrics getLineMetrics (Font font, String str,
282                                      FontRenderContext frc)
283   {
284     return new GdkFontLineMetrics (getFontMetrics (font), str.length ());
285   }
286 
getFontMetrics(Font font)287   public FontMetrics getFontMetrics (Font font)
288   {
289     return new GdkClasspathFontPeerMetrics (font);
290   }
291 
292 }
293 
294