Source for gnu.java.awt.peer.gtk.GdkFontPeer

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