You are viewing a plain text version of this content. The canonical link for it is here.
Posted to fop-commits@xmlgraphics.apache.org by je...@apache.org on 2008/02/19 16:47:51 UTC

svn commit: r629131 - in /xmlgraphics/fop/trunk/src/java/org/apache/fop/fonts: CustomFont.java autodetect/FontInfoFinder.java truetype/TTFFile.java truetype/TTFFontLoader.java

Author: jeremias
Date: Tue Feb 19 07:47:48 2008
New Revision: 629131

URL: http://svn.apache.org/viewvc?rev=629131&view=rev
Log:
Added support for reading the OS/2 table's usWeightClass value which supports the same font weight values as we use in XSL-FO.
However, in my tests these values proved to be unreliable (like ExtraBlack fonts returning 400). I just hooked the whole thing in so this work isn't lost if anyone has an idea to make it work. The FontInfoFinder will continue to only use guessed font weights for now.

Modified:
    xmlgraphics/fop/trunk/src/java/org/apache/fop/fonts/CustomFont.java
    xmlgraphics/fop/trunk/src/java/org/apache/fop/fonts/autodetect/FontInfoFinder.java
    xmlgraphics/fop/trunk/src/java/org/apache/fop/fonts/truetype/TTFFile.java
    xmlgraphics/fop/trunk/src/java/org/apache/fop/fonts/truetype/TTFFontLoader.java

Modified: xmlgraphics/fop/trunk/src/java/org/apache/fop/fonts/CustomFont.java
URL: http://svn.apache.org/viewvc/xmlgraphics/fop/trunk/src/java/org/apache/fop/fonts/CustomFont.java?rev=629131&r1=629130&r2=629131&view=diff
==============================================================================
--- xmlgraphics/fop/trunk/src/java/org/apache/fop/fonts/CustomFont.java (original)
+++ xmlgraphics/fop/trunk/src/java/org/apache/fop/fonts/CustomFont.java Tue Feb 19 07:47:48 2008
@@ -46,6 +46,7 @@
     private int descender = 0;
     private int[] fontBBox = {0, 0, 0, 0};
     private int flags = 4;
+    private int weight = 0; //0 means unknown weight
     private int stemV = 0;
     private int italicAngle = 0;
     private int missingWidth = 0;
@@ -197,6 +198,15 @@
     }
 
     /**
+     * Returns the font weight (100, 200...800, 900). This value may be different from the
+     * one that was actually used to register the font.
+     * @return the font weight (or 0 if the font weight is unknown)
+     */
+    public int getWeight() {
+        return this.weight;
+    }
+    
+    /**
      * {@inheritDoc}
      */
     public int getStemV() {
@@ -349,6 +359,17 @@
         this.flags = flags;
     }
 
+    /**
+     * Sets the font weight. Valid values are 100, 200...800, 900.
+     * @param weight the font weight
+     */
+    public void setWeight(int weight) {
+        weight = (weight / 100) * 100;
+        weight = Math.max(100, weight);
+        weight = Math.min(900, weight);
+        this.weight = weight;
+    }
+    
     /**
      * {@inheritDoc}
      */

Modified: xmlgraphics/fop/trunk/src/java/org/apache/fop/fonts/autodetect/FontInfoFinder.java
URL: http://svn.apache.org/viewvc/xmlgraphics/fop/trunk/src/java/org/apache/fop/fonts/autodetect/FontInfoFinder.java?rev=629131&r1=629130&r2=629131&view=diff
==============================================================================
--- xmlgraphics/fop/trunk/src/java/org/apache/fop/fonts/autodetect/FontInfoFinder.java (original)
+++ xmlgraphics/fop/trunk/src/java/org/apache/fop/fonts/autodetect/FontInfoFinder.java Tue Feb 19 07:47:48 2008
@@ -72,7 +72,11 @@
         String searchName = fullName.toLowerCase();
 
         String style = guessStyle(customFont, searchName);
-        int weight = FontUtil.guessWeight(searchName);
+        int weight; //= customFont.getWeight();
+        int guessedWeight = FontUtil.guessWeight(searchName); 
+        //We always take the guessed weight for now since it yield much better results.
+        //OpenType's OS/2 usWeightClass value proves to be unreliable.
+        weight = guessedWeight;
 
         //Full Name usually includes style/weight info so don't use these traits
         //If we still want to use these traits, we have to make FontInfo.fontLookup() smarter

Modified: xmlgraphics/fop/trunk/src/java/org/apache/fop/fonts/truetype/TTFFile.java
URL: http://svn.apache.org/viewvc/xmlgraphics/fop/trunk/src/java/org/apache/fop/fonts/truetype/TTFFile.java?rev=629131&r1=629130&r2=629131&view=diff
==============================================================================
--- xmlgraphics/fop/trunk/src/java/org/apache/fop/fonts/truetype/TTFFile.java (original)
+++ xmlgraphics/fop/trunk/src/java/org/apache/fop/fonts/truetype/TTFFile.java Tue Feb 19 07:47:48 2008
@@ -105,6 +105,7 @@
     //Ascender/descender from OS/2 table
     private int os2Ascender = 0;
     private int os2Descender = 0;
+    private int usWeightClass = 0;
 
     private short lastChar = 0;
 
@@ -620,6 +621,13 @@
         return flags;
     }
 
+    /**
+     * Returns the weight class of this font. Valid values are 100, 200....,800, 900.
+     * @return the weight class value (or 0 if there was no OS/2 table in the font)
+     */
+    public int getWeightClass() {
+        return this.usWeightClass;
+    }
 
     /**
      * Returns the StemV attribute of the font.
@@ -978,7 +986,9 @@
     private final void readOS2(FontFileReader in) throws IOException {
         // Check if font is embeddable
         if (dirTabs.get("OS/2") != null) {
-            seekTab(in, "OS/2", 2 * 4);
+            seekTab(in, "OS/2", 2 * 2);
+            this.usWeightClass = in.readTTFUShort();
+            in.skip(2);
             int fsType = in.readTTFUShort();
             if (fsType == 2) {
                 isEmbeddable = false;

Modified: xmlgraphics/fop/trunk/src/java/org/apache/fop/fonts/truetype/TTFFontLoader.java
URL: http://svn.apache.org/viewvc/xmlgraphics/fop/trunk/src/java/org/apache/fop/fonts/truetype/TTFFontLoader.java?rev=629131&r1=629130&r2=629131&view=diff
==============================================================================
--- xmlgraphics/fop/trunk/src/java/org/apache/fop/fonts/truetype/TTFFontLoader.java (original)
+++ xmlgraphics/fop/trunk/src/java/org/apache/fop/fonts/truetype/TTFFontLoader.java Tue Feb 19 07:47:48 2008
@@ -90,6 +90,7 @@
         returnFont.setStemV(Integer.parseInt(ttf.getStemV())); //not used for TTF
         returnFont.setItalicAngle(Integer.parseInt(ttf.getItalicAngle()));
         returnFont.setMissingWidth(0);
+        returnFont.setWeight(ttf.getWeightClass());
         
         multiFont.setCIDType(CIDFontType.CIDTYPE2);
         int[] wx = ttf.getWidths();



---------------------------------------------------------------------
To unsubscribe, e-mail: fop-commits-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: fop-commits-help@xmlgraphics.apache.org