Source for gnu.java.awt.peer.x.XFontPeer2

   1: /* XFontPeer2.java -- A Java based TTF font peer for X
   2:    Copyright (C) 2006 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: package gnu.java.awt.peer.x;
  39: 
  40: import java.awt.Font;
  41: import java.awt.FontMetrics;
  42: import java.awt.font.FontRenderContext;
  43: import java.awt.font.GlyphVector;
  44: import java.awt.font.LineMetrics;
  45: import java.awt.geom.AffineTransform;
  46: import java.awt.geom.Point2D;
  47: import java.awt.geom.Rectangle2D;
  48: import java.io.File;
  49: import java.io.FileInputStream;
  50: import java.nio.ByteBuffer;
  51: import java.nio.channels.FileChannel;
  52: import java.text.CharacterIterator;
  53: import java.text.StringCharacterIterator;
  54: import java.util.Locale;
  55: import java.util.Map;
  56: 
  57: import gnu.java.awt.font.FontDelegate;
  58: import gnu.java.awt.font.FontFactory;
  59: import gnu.java.awt.peer.ClasspathFontPeer;
  60: 
  61: public class XFontPeer2
  62:   extends ClasspathFontPeer
  63: {
  64: 
  65:   private class XLineMetrics
  66:     extends LineMetrics
  67:   {
  68: 
  69:     private Font font;
  70: //    private CharacterIterator characterIterator;
  71: //    private int begin;
  72: //    private int limit;
  73:     private FontRenderContext fontRenderContext;
  74:     XLineMetrics(Font f, CharacterIterator ci, int b, int l,
  75:                  FontRenderContext rc)
  76:     {
  77:       font = f;
  78: //      characterIterator = ci;
  79: //      begin = b;
  80: //      limit = l;
  81:       fontRenderContext = rc;
  82:     }
  83: 
  84:     public float getAscent()
  85:     {
  86:       return fontDelegate.getAscent(font.getSize(), fontRenderContext.getTransform(),
  87:                              fontRenderContext.isAntiAliased(),
  88:                              fontRenderContext.usesFractionalMetrics(), true);
  89:       }
  90: 
  91:     public int getBaselineIndex()
  92:     {
  93:       // FIXME: Implement this.
  94:       throw new UnsupportedOperationException("Not yet implemented");
  95:     }
  96: 
  97:     public float[] getBaselineOffsets()
  98:     {
  99:       // FIXME: Implement this.
 100:       throw new UnsupportedOperationException("Not yet implemented");
 101:     }
 102: 
 103:     public float getDescent()
 104:     {
 105:       return (int) fontDelegate.getDescent(font.getSize(),
 106:                                            new AffineTransform(), false, false,
 107:                                            false);
 108:     }
 109: 
 110:     public float getHeight()
 111:     {
 112:       // FIXME: Implement this.
 113:       throw new UnsupportedOperationException("Not yet implemented");
 114:     }
 115: 
 116:     public float getLeading()
 117:     {
 118:       // FIXME: Implement this.
 119:       throw new UnsupportedOperationException("Not yet implemented");
 120:     }
 121: 
 122:     public int getNumChars()
 123:     {
 124:       // FIXME: Implement this.
 125:       throw new UnsupportedOperationException("Not yet implemented");
 126:     }
 127: 
 128:     public float getStrikethroughOffset()
 129:     {
 130:       return 0.F;
 131:     }
 132: 
 133:     public float getStrikethroughThickness()
 134:     {
 135:       return 0.F;
 136:     }
 137: 
 138:     public float getUnderlineOffset()
 139:     {
 140:       return 0.F;
 141:     }
 142: 
 143:     public float getUnderlineThickness()
 144:     {
 145:       return 0.F;
 146:     }
 147:     
 148:   }
 149: 
 150:   private class XFontMetrics
 151:     extends FontMetrics
 152:   {
 153:     XFontMetrics(Font f)
 154:     {
 155:       super(f);
 156:     }
 157: 
 158:     public int getAscent()
 159:     {
 160:       return (int) fontDelegate.getAscent(getFont().getSize(),
 161:                                           new AffineTransform(), false, false,
 162:                                           false);
 163:     }
 164: 
 165:     public int getDescent()
 166:     {
 167:       return (int) fontDelegate.getDescent(getFont().getSize(),
 168:                                            new AffineTransform(), false, false,
 169:                                            false);
 170:     }
 171:     
 172:     public int getHeight()
 173:     {
 174:       GlyphVector gv = fontDelegate.createGlyphVector(getFont(),
 175:                     new FontRenderContext(new AffineTransform(), false, false),
 176:                     new StringCharacterIterator("m"));
 177:       Rectangle2D b = gv.getVisualBounds();
 178:       return (int) b.getHeight();
 179:     }
 180: 
 181:     public int charWidth(char c)
 182:     {
 183:       Point2D advance = new Point2D.Double();
 184:       fontDelegate.getAdvance(c, getFont().getSize(), new AffineTransform(),
 185:                               false, false, true, advance);
 186:       return (int) advance.getX();
 187:     }
 188: 
 189:     public int charsWidth(char[] chars, int offs, int len)
 190:     {
 191:       return stringWidth(new String(chars, offs, len));
 192:     }
 193: 
 194:     public int stringWidth(String s)
 195:     {
 196:       GlyphVector gv = fontDelegate.createGlyphVector(getFont(),
 197:                     new FontRenderContext(new AffineTransform(), false, false),
 198:                     new StringCharacterIterator(s));
 199:       Rectangle2D b = gv.getVisualBounds();
 200:       return (int) b.getWidth();
 201:     }
 202:   }
 203: 
 204:   private FontDelegate fontDelegate;
 205: 
 206:   XFontPeer2(String name, int style, int size)
 207:   {
 208:     super(name, style, size);
 209:     try
 210:       {
 211:         File fontfile = new File("/usr/share/fonts/truetype/ttf-bitstream-vera/Vera.ttf");
 212:         FileInputStream in = new FileInputStream(fontfile);
 213:         FileChannel ch = in.getChannel();
 214:         ByteBuffer buffer = ch.map(FileChannel.MapMode.READ_ONLY, 0,
 215:                                    fontfile.length());
 216:         fontDelegate = FontFactory.createFonts(buffer)[0];
 217:       }
 218:     catch (Exception ex)
 219:       {
 220:         ex.printStackTrace();
 221:       }
 222:   }
 223: 
 224:   XFontPeer2(String name, Map atts)
 225:   {
 226:     super(name, atts);
 227:     try
 228:       {
 229:         File fontfile = new File("/usr/share/fonts/truetype/freefont/FreeSans.ttf");
 230:         FileInputStream in = new FileInputStream(fontfile);
 231:         FileChannel ch = in.getChannel();
 232:         ByteBuffer buffer = ch.map(FileChannel.MapMode.READ_ONLY, 0,
 233:                                    fontfile.length());
 234:         fontDelegate = FontFactory.createFonts(buffer)[0];
 235:       }
 236:     catch (Exception ex)
 237:       {
 238:         ex.printStackTrace();
 239:       }
 240:   }
 241: 
 242:   public boolean canDisplay(Font font, char c)
 243:   {
 244:     // FIXME: Implement this.
 245:     throw new UnsupportedOperationException("Not yet implemented");
 246:   }
 247: 
 248:   public int canDisplayUpTo(Font font, CharacterIterator i, int start, int limit)
 249:   {
 250:     // FIXME: Implement this.
 251:     throw new UnsupportedOperationException("Not yet implemented");
 252:   }
 253: 
 254:   public String getSubFamilyName(Font font, Locale locale)
 255:   {
 256:     // FIXME: Implement this.
 257:     throw new UnsupportedOperationException("Not yet implemented");
 258:   }
 259: 
 260:   public String getPostScriptName(Font font)
 261:   {
 262:     // FIXME: Implement this.
 263:     throw new UnsupportedOperationException("Not yet implemented");
 264:   }
 265: 
 266:   public int getNumGlyphs(Font font)
 267:   {
 268:     // FIXME: Implement this.
 269:     throw new UnsupportedOperationException("Not yet implemented");
 270:   }
 271: 
 272:   public int getMissingGlyphCode(Font font)
 273:   {
 274:     // FIXME: Implement this.
 275:     throw new UnsupportedOperationException("Not yet implemented");
 276:   }
 277: 
 278:   public byte getBaselineFor(Font font, char c)
 279:   {
 280:     // FIXME: Implement this.
 281:     throw new UnsupportedOperationException("Not yet implemented");
 282:   }
 283: 
 284:   public String getGlyphName(Font font, int glyphIndex)
 285:   {
 286:     // FIXME: Implement this.
 287:     throw new UnsupportedOperationException("Not yet implemented");
 288:   }
 289: 
 290:   public GlyphVector createGlyphVector(Font font, FontRenderContext frc, CharacterIterator ci)
 291:   {
 292:     return fontDelegate.createGlyphVector(font, frc, ci);
 293:   }
 294: 
 295:   public GlyphVector createGlyphVector(Font font, FontRenderContext ctx, int[] glyphCodes)
 296:   {
 297:     // FIXME: Implement this.
 298:     throw new UnsupportedOperationException("Not yet implemented");
 299:   }
 300: 
 301:   public GlyphVector layoutGlyphVector(Font font, FontRenderContext frc, char[] chars, int start, int limit, int flags)
 302:   {
 303:     StringCharacterIterator i = new StringCharacterIterator(new String(chars), start, limit, 0);
 304:     return fontDelegate.createGlyphVector(font, frc, i);
 305:   }
 306: 
 307:   public FontMetrics getFontMetrics(Font font)
 308:   {
 309:     return new XFontMetrics(font);
 310:   }
 311: 
 312:   public boolean hasUniformLineMetrics(Font font)
 313:   {
 314:     // FIXME: Implement this.
 315:     throw new UnsupportedOperationException("Not yet implemented");
 316:   }
 317: 
 318:   public LineMetrics getLineMetrics(Font font, CharacterIterator ci, int begin, int limit, FontRenderContext rc)
 319:   {
 320:     return new XLineMetrics(font, ci, begin, limit, rc);
 321:   }
 322: 
 323:   public Rectangle2D getMaxCharBounds(Font font, FontRenderContext rc)
 324:   {
 325:     // FIXME: Implement this.
 326:     throw new UnsupportedOperationException("Not yet implemented");
 327:   }
 328: 
 329:   public Rectangle2D getStringBounds(Font font, CharacterIterator ci, int begin, int limit, FontRenderContext frc)
 330:   {
 331:     // FIXME: Implement this.
 332:     throw new UnsupportedOperationException("Not yet implemented");
 333:   }
 334: 
 335: }