You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pdfbox.apache.org by le...@apache.org on 2014/06/03 22:22:03 UTC

svn commit: r1599786 - /pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/ttf/TrueTypeFont.java

Author: lehmi
Date: Tue Jun  3 20:22:02 2014
New Revision: 1599786

URL: http://svn.apache.org/r1599786
Log:
PDFBOX-62: added some convenience methods to get popular values

Modified:
    pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/ttf/TrueTypeFont.java

Modified: pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/ttf/TrueTypeFont.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/ttf/TrueTypeFont.java?rev=1599786&r1=1599785&r2=1599786&view=diff
==============================================================================
--- pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/ttf/TrueTypeFont.java (original)
+++ pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/ttf/TrueTypeFont.java Tue Jun  3 20:22:02 2014
@@ -24,7 +24,6 @@ import java.io.InputStream;
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
-import org.apache.fontbox.util.autodetect.FontFileFinder;
 
 /**
  * A class to hold true type font information.
@@ -39,6 +38,10 @@ public class TrueTypeFont 
 
     private int numberOfGlyphs = -1;
     
+    private int unitsPerEm = -1;
+
+    private int[] advanceWidths = null;
+    
     private Map<String,TTFTable> tables = new HashMap<String,TTFTable>();
     
     private TTFDataStream data;
@@ -306,4 +309,61 @@ public class TrueTypeFont 
         }
         return numberOfGlyphs;
     }
+
+    /**
+     * Returns the units per EM (Header.unitsPerEm).
+     * 
+     * @return units per EM
+     */
+    public int getUnitsPerEm()
+    {
+        if (unitsPerEm == -1)
+        {
+            HeaderTable header = getHeader();
+            if (header != null)
+            {
+                unitsPerEm = header.getUnitsPerEm();
+            }
+            else
+            {
+                // this should never happen
+                unitsPerEm = 0;
+            }
+        }
+        return unitsPerEm;
+    }
+
+    /**
+     * Returns the width for the given glyph code.
+     * 
+     * @param code the glyph code
+     * @return the width
+     */
+    public int getAdvanceWidth(int code)
+    {
+        if (advanceWidths == null)
+        {
+            HorizontalMetricsTable hmtx = getHorizontalMetrics();
+            if (hmtx != null)
+            {
+                advanceWidths = hmtx.getAdvanceWidth();
+            }
+            else
+            {
+                // this should never happen
+                advanceWidths = new int[]{250};
+            }
+        }
+        if (advanceWidths.length > code)
+        {
+            return advanceWidths[code];
+        }
+        else
+        {
+            // monospaced fonts may not have a width for every glyph
+            // the last one is for subsequent glyphs
+            return advanceWidths[advanceWidths.length-1];
+        }
+    }
+
 }