OSDN Git Service

ee05145102692e267ec45bb76a2c2423af14ae69
[pf3gnuchains/gcc-fork.git] / libjava / gnu / java / awt / peer / gtk / GdkFontPeer.java
1 /* GdkFontPeer.java -- Implements FontPeer with GTK+
2    Copyright (C) 1999, 2004 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 import java.awt.peer.FontPeer;
41 import java.awt.*;
42 import java.awt.geom.*;
43 import java.awt.font.*;
44 import java.util.Locale;
45 import java.util.Map;
46 import java.util.ResourceBundle;
47 import java.util.MissingResourceException;
48 import java.text.CharacterIterator;
49 import java.text.AttributedCharacterIterator;
50 import java.text.StringCharacterIterator;
51 import gnu.classpath.Configuration;
52 import gnu.java.awt.peer.ClasspathFontPeer;
53
54 public class GdkFontPeer extends ClasspathFontPeer
55 {
56
57   native static void initStaticState ();
58   private final int native_state = GtkGenericPeer.getUniqueInteger ();
59   private static ResourceBundle bundle;
60   
61   static 
62   {
63     if (Configuration.INIT_LOAD_LIBRARY)
64       {
65         System.loadLibrary("gtkpeer");
66       }
67
68     initStaticState ();
69
70     try
71       {
72         bundle = ResourceBundle.getBundle ("gnu.java.awt.peer.gtk.font");
73       }
74     catch (Throwable ignored)
75       {
76         bundle = null;
77       }
78   }
79
80   private native void initState ();
81   private native void dispose ();
82   private native void setFont (String family, int style, int size, boolean useGraphics2D);
83
84   protected void finalize ()
85   {
86     if (GtkToolkit.useGraphics2D ())
87       GdkGraphics2D.releasePeerGraphicsResource(this);
88     dispose ();
89   }
90
91   /* 
92    * Helpers for the 3-way overloading that this class seems to suffer
93    * from. Remove them if you feel like they're a performance bottleneck,
94    * for the time being I prefer my code not be written and debugged in
95    * triplicate.
96    */
97
98   private String buildString(CharacterIterator iter)
99   {
100     StringBuffer sb = new StringBuffer();
101     for(char c = iter.first(); c != CharacterIterator.DONE; c = iter.next()) 
102       sb.append(c);
103     return sb.toString();
104   }
105
106   private String buildString(CharacterIterator iter, int begin, int limit)
107   {
108     StringBuffer sb = new StringBuffer();
109     int i = 0;
110     for(char c = iter.first(); c != CharacterIterator.DONE; c = iter.next(), i++) 
111       {
112         if (begin <= i)
113           sb.append(c);
114         if (limit <= i)
115           break;
116       }
117     return sb.toString();
118   }
119   
120   private String buildString(char[] chars, int begin, int limit)
121   {
122     return new String(chars, begin, limit - begin);
123   }
124
125   /* Public API */
126
127   public GdkFontPeer (String name, int style)
128   {
129     // All fonts get a default size of 12 if size is not specified.
130     this(name, style, 12);
131   }
132
133   public GdkFontPeer (String name, int style, int size)
134   {  
135     super(name, style, size);    
136     initState ();
137     setFont (this.familyName, this.style, (int)this.size, 
138              GtkToolkit.useGraphics2D());
139   }
140
141   public GdkFontPeer (String name, Map attributes)
142   {
143     super(name, attributes);
144     initState ();
145     setFont (this.familyName, this.style, (int)this.size,
146              GtkToolkit.useGraphics2D());
147   }
148   
149   public String getSubFamilyName(Font font, Locale locale)
150   {
151     return null;
152   }
153
154   public String getPostScriptName(Font font)
155   {
156     return null;
157   }
158
159   public boolean canDisplay (Font font, char c)
160   {
161     throw new UnsupportedOperationException ();
162   }
163
164   public int canDisplayUpTo (Font font, CharacterIterator i, int start, int limit)
165   {
166     throw new UnsupportedOperationException ();
167   }
168
169   public GlyphVector createGlyphVector (Font font, 
170                                         FontRenderContext ctx, 
171                                         CharacterIterator i)
172   {
173     return new GdkGlyphVector(font, this, ctx, buildString (i));
174   }
175
176   public GlyphVector createGlyphVector (Font font, 
177                                         FontRenderContext ctx, 
178                                         int[] glyphCodes)
179   {
180     return new GdkGlyphVector (font, this, ctx, glyphCodes);
181   }
182
183   public byte getBaselineFor (Font font, char c)
184   {
185     throw new UnsupportedOperationException ();
186   }
187
188   protected class GdkFontLineMetrics extends LineMetrics
189   {
190     FontMetrics fm;
191     int nchars; 
192
193     public GdkFontLineMetrics (FontMetrics m, int n)
194     {
195       fm = m;
196       nchars = n;
197     }
198
199     public float getAscent()
200     {
201       return (float) fm.getAscent ();
202     }
203   
204     public int getBaselineIndex()
205     {
206       return Font.ROMAN_BASELINE;
207     }
208     
209     public float[] getBaselineOffsets()
210     {
211       return new float[3];
212     }
213     
214     public float getDescent()
215     {
216       return (float) fm.getDescent ();
217     }
218     
219     public float getHeight()
220     {
221       return (float) fm.getHeight ();
222     }
223     
224     public float getLeading() { return 0.f; }    
225     public int getNumChars() { return nchars; }
226     public float getStrikethroughOffset() { return 0.f; }    
227     public float getStrikethroughThickness() { return 0.f; }  
228     public float getUnderlineOffset() { return 0.f; }
229     public float getUnderlineThickness() { return 0.f; }
230
231   }
232
233   public LineMetrics getLineMetrics (Font font, CharacterIterator ci, 
234                                      int begin, int limit, FontRenderContext rc)
235   {
236     return new GdkFontLineMetrics (getFontMetrics (font), limit - begin);
237   }
238
239   public Rectangle2D getMaxCharBounds (Font font, FontRenderContext rc)
240   {
241     throw new UnsupportedOperationException ();
242   }
243
244   public int getMissingGlyphCode (Font font)
245   {
246     throw new UnsupportedOperationException ();
247   }
248
249   public String getGlyphName (Font font, int glyphIndex)
250   {
251     throw new UnsupportedOperationException ();
252   }
253
254   public int getNumGlyphs (Font font)
255   {
256     throw new UnsupportedOperationException ();
257   }
258
259   public Rectangle2D getStringBounds (Font font, CharacterIterator ci, 
260                                       int begin, int limit, FontRenderContext frc)
261   {
262     throw new UnsupportedOperationException ();
263   }
264
265   public boolean hasUniformLineMetrics (Font font)
266   {
267     return true;
268   }
269
270   public GlyphVector layoutGlyphVector (Font font, FontRenderContext frc, 
271                                         char[] chars, int start, int limit, 
272                                         int flags)
273   {
274     int nchars = (limit - start) + 1;
275     char[] nc = new char[nchars];
276
277     for (int i = 0; i < nchars; ++i)
278       nc[i] = chars[start + i];
279
280     return createGlyphVector (font, frc, 
281                               new StringCharacterIterator (new String (nc)));
282   }
283
284   public LineMetrics getLineMetrics (Font font, String str, 
285                                      FontRenderContext frc)
286   {
287     return new GdkFontLineMetrics (getFontMetrics (font), str.length ());
288   }
289
290   public FontMetrics getFontMetrics (Font font)
291   {
292     return new GdkFontMetrics (font);
293   }
294
295 }