gzz-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Gzz-commits] libvob/org/nongnu/libvob impl/gl/GLTextStyle.ja...


From: Tuomas J. Lukka
Subject: [Gzz-commits] libvob/org/nongnu/libvob impl/gl/GLTextStyle.ja...
Date: Mon, 30 Jun 2003 14:18:32 -0400

CVSROOT:        /cvsroot/libvob
Module name:    libvob
Branch:         
Changes by:     Tuomas J. Lukka <address@hidden>        03/06/30 14:18:32

Modified files:
        org/nongnu/libvob/impl/gl: GLTextStyle.java 
        org/nongnu/libvob/vobs: TextVob.java 

Log message:
        Support for multiple fonts -- the pattern should be clear, change the 
defaults and add more if you want.

CVSWeb URLs:
http://savannah.gnu.org/cgi-bin/viewcvs/libvob/libvob/org/nongnu/libvob/impl/gl/GLTextStyle.java.diff?tr1=1.4&tr2=1.5&r1=text&r2=text
http://savannah.gnu.org/cgi-bin/viewcvs/libvob/libvob/org/nongnu/libvob/vobs/TextVob.java.diff?tr1=1.5&tr2=1.6&r1=text&r2=text

Patches:
Index: libvob/org/nongnu/libvob/impl/gl/GLTextStyle.java
diff -u libvob/org/nongnu/libvob/impl/gl/GLTextStyle.java:1.4 
libvob/org/nongnu/libvob/impl/gl/GLTextStyle.java:1.5
--- libvob/org/nongnu/libvob/impl/gl/GLTextStyle.java:1.4       Tue Apr  8 
09:00:23 2003
+++ libvob/org/nongnu/libvob/impl/gl/GLTextStyle.java   Mon Jun 30 14:18:32 2003
@@ -29,7 +29,7 @@
 import org.nongnu.libvob.*;
 import org.nongnu.libvob.gl.*;
 import java.awt.*;
-import java.util.List;
+import java.util.*;
 
 
 /** OpenGL implementation of TextStyle.
@@ -37,51 +37,140 @@
  * it's much better to use GraphicsAPI.getTextStyle.
  */
 public class GLTextStyle extends TextStyle {
-public static final String rcsid = "$Id: GLTextStyle.java,v 1.4 2003/04/08 
13:00:23 tjl Exp $";
+public static final String rcsid = "$Id: GLTextStyle.java,v 1.5 2003/06/30 
18:18:32 tjl Exp $";
     public static boolean dbg = false;
     private static void pa(String s) { System.err.println(s); }
 
+    /** A particular loaded font.
+     */
+    static private class Face {
+       GL.Font glFont;
+       float[] charWidths = new float[256];
+       GLTextStyle[] instances = new GLTextStyle[250];
+    };
+
+    /** The different styles within a family.
+     */
+    static private class Family {
+       Face normal;
+       Face bold;
+       Face bolditalic;
+       Face italic;
+
+       Face get(int flags) {
+           if((flags & Font.BOLD) != 0) {
+               if((flags & Font.ITALIC) != 0)
+                   return bolditalic;
+               else 
+                   return bold;
+           } else {
+               if((flags & Font.ITALIC) != 0)
+                   return italic;
+               else 
+                   return normal;
+           }
+       }
+       void set(int flags, Face f) {
+           if((flags & Font.BOLD) != 0) {
+               if((flags & Font.ITALIC) != 0)
+                   bolditalic = f;
+               else 
+                   bold = f;
+           } else {
+               if((flags & Font.ITALIC) != 0)
+                   italic = f;
+               else 
+                   normal = f;
+           }
+       }
+    }
 
+    /** The families, keyed by name.
+     */
+    static private Map families = Collections.synchronizedMap(new HashMap());
+
+    /** The actual font faces: several name-flags pair may map to the same
+     * font, which is why we cache them here.
+     */
+    static private Map loadedFonts = Collections.synchronizedMap(new 
HashMap());
+
+    static private Face getFace(String fileName) {
+       Face f = (Face)loadedFonts.get(fileName);
+       if(f == null) {
+           f = new Face();
+           f.glFont = GL.createFont(fileName, 64, 40);
+           f.glFont.getWidths(f.charWidths);
+           if(dbg) for(int i=0; i<256; i++) {
+               pa("char '"+((char)i)+"': width "+f.charWidths[i]);
+           }
+           loadedFonts.put(fileName, f);
+       }
+       return f;
+    }
 
-    static GL.Font theStaticFont;
-    static float[] charWidths = new float[256];
-    static GLTextStyle[] instances = new GLTextStyle[250];
+    static private String styleToFile(String family, int style, int size) {
+       family = family.toLowerCase();
+       String baseName;
+       String directory = "/usr/share/fonts/type1/gsfonts/";
+       if(family.equals("sans") || family.equals("sansserif")) {
+           if((style & Font.BOLD) == 0) {
+               baseName = "n019003l.pfb";
+           } else {
+               baseName = "n019004l.pfb";
+           }
+       } else if(family.equals("monospaced")) {
+           if((style & Font.BOLD) == 0) {
+               baseName = "n022003l.pfb";
+           } else {
+               baseName = "n022004l.pfb";
+           }
+       } else {
+           pa("Strange font family '"+family+"' - using default (sans)");
+           return styleToFile("sans", style, size);
+       }
+       return directory + baseName;
+    }
 
     static public GLTextStyle create(String family, int style, int size) {
        if(size > 200) size = 200;
-       GLTextStyle s = instances[size];
+
+       Family fam = (Family)families.get(family);
+       if(fam == null) {
+           fam = new Family();
+           families.put(family, fam);
+       }
+       Face fac = fam.get(style);
+       if(fac == null) {
+           fac = getFace(styleToFile(family, style, size));
+           fam.set(style, fac);
+       }
+       GLTextStyle s = fac.instances[size];
        if(s == null) {
-           if(theStaticFont == null)
-               theStaticFont = GL.createFont(null, 64, 40);
-           theStaticFont.getWidths(charWidths);
-           if(dbg) for(int i=0; i<256; i++) {
-               pa("char '"+((char)i)+"': width "+charWidths[i]);
-           }
-           s = new GLTextStyle(theStaticFont, size);
-           instances[size] = s;
+           s = new GLTextStyle(fac, size);
+           fac.instances[size] = s;
        }
        return s;
     }
 
 
     public float fontScale;
-    public GL.Font theFont;
+    public Face face;
 
-    private GLTextStyle(GL.Font f, float scale) {
-       this.theFont = f;
+    private GLTextStyle(Face f, float scale) {
+       this.face = f;
        this.fontScale = scale;
     }
 
     public float getScaleByHeight(float h) {
-       return h / theFont.getHeight() / fontScale;
+       return h / face.glFont.getHeight() / fontScale;
     }
 
     private float getWidth(String s) {
        float sum = 0;
        for(int i=0; i<s.length(); i++) {
            char c = s.charAt(i);
-           if(c < charWidths.length)
-               sum += charWidths[c];
+           if(c < face.charWidths.length)
+               sum += face.charWidths[c];
        }
        return sum * fontScale;
     }
@@ -89,8 +178,8 @@
        float sum = 0;
        for(int i=offs; i<offs+len; i++) {
            char c = chars[i];
-           if(c < charWidths.length)
-               sum += charWidths[c];
+           if(c < face.charWidths.length)
+               sum += face.charWidths[c];
        }
        return sum * fontScale;
     }
@@ -103,20 +192,23 @@
     }
 
     public float getHeight(float scale) {
-       return scale * fontScale * theFont.getHeight() ;
+       return scale * fontScale * face.glFont.getHeight() ;
     }
 
     public float getAscent(float scale) {
-       return scale * fontScale * theFont.getYOffs();
+       return scale * fontScale * face.glFont.getYOffs();
     }
 
     public float getDescent(float scale) {
-       return scale * fontScale * (theFont.getHeight() - theFont.getYOffs());
+       return scale * fontScale * (face.glFont.getHeight() - 
face.glFont.getYOffs());
     }
 
     public float getLeading(float scale) {
-       return scale * 0.05f * fontScale * theFont.getHeight();
+       return scale * 0.05f * fontScale * face.glFont.getHeight();
     }
 
+    public GL.Font getGLFont() {
+       return face.glFont;
+    }
 
 }
Index: libvob/org/nongnu/libvob/vobs/TextVob.java
diff -u libvob/org/nongnu/libvob/vobs/TextVob.java:1.5 
libvob/org/nongnu/libvob/vobs/TextVob.java:1.6
--- libvob/org/nongnu/libvob/vobs/TextVob.java:1.5      Sun Jun  8 07:17:32 2003
+++ libvob/org/nongnu/libvob/vobs/TextVob.java  Mon Jun 30 14:18:32 2003
@@ -54,7 +54,7 @@
  * XXX Diagram!
  */
 public class TextVob extends HBox.VobHBox {
-String rcsid = "$Id: TextVob.java,v 1.5 2003/06/08 11:17:32 benja Exp $";
+String rcsid = "$Id: TextVob.java,v 1.6 2003/06/30 18:18:32 tjl Exp $";
     public static boolean dbg = false;
     private static void pa(String s) { System.err.println(s); }
 
@@ -215,9 +215,9 @@
        if(ht == null) {
            GLTextStyle gls = (GLTextStyle)style;
            ht = GLRen.createText1(
-                   gls.theFont,
+                   gls.getGLFont(),
                    text, 
-                   (baselined ? 1 : gls.theFont.getYOffs()),
+                   (baselined ? 1 : gls.getGLFont().getYOffs()),
                    0);
        }
        return ht;




reply via email to

[Prev in Thread] Current Thread [Next in Thread]