pdfbox-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From le...@apache.org
Subject svn commit: r1763612 - in /pdfbox/trunk: ./ fontbox/src/main/java/org/apache/fontbox/ttf/CmapSubtable.java pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDCIDFontType2Embedder.java
Date Thu, 06 Oct 2016 15:32:09 GMT
Author: lehmi
Date: Thu Oct  6 15:32:08 2016
New Revision: 1763612

URL: http://svn.apache.org/viewvc?rev=1763612&view=rev
Log:
PDFBOX-3300: create widths/unicode mapping when subsetting the font to avoid unnecessary huge
mapping tables

Modified:
    pdfbox/trunk/   (props changed)
    pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/ttf/CmapSubtable.java   (contents,
props changed)
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDCIDFontType2Embedder.java

Propchange: pdfbox/trunk/
------------------------------------------------------------------------------
--- svn:mergeinfo (original)
+++ svn:mergeinfo Thu Oct  6 15:32:08 2016
@@ -1,3 +1,3 @@
-/pdfbox/branches/2.0:1760418,1761484,1762133
+/pdfbox/branches/2.0:1760418,1761484,1762133,1763609
 /pdfbox/branches/no-awt:1618517-1621410
 /pdfbox/no-awt:1618514-1618516

Modified: pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/ttf/CmapSubtable.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/ttf/CmapSubtable.java?rev=1763612&r1=1763611&r2=1763612&view=diff
==============================================================================
--- pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/ttf/CmapSubtable.java (original)
+++ pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/ttf/CmapSubtable.java Thu Oct  6
15:32:08 2016
@@ -606,7 +606,8 @@ public class CmapSubtable
      * @param gid glyph id
      * @return character code
      * 
-     * @deprecated the mapping may be ambiguous. The first mapped value is returned by default.
+     * @deprecated the mapping may be ambiguous, see {@link #getCharCodes(int)}. The first
mapped value is returned by
+     * default.
      */
     public Integer getCharacterCode(int gid)
     {
@@ -615,12 +616,13 @@ public class CmapSubtable
         {
             return null;
         }
-        // ambiguous mapping, use the first mapping
+        // ambiguous mapping
         if (code == Integer.MIN_VALUE)
         {
             List<Integer> mappedValues = glyphIdToCharacterCodeMultiple.get(gid);
             if (mappedValues != null)
             {
+                // use the first mapping
                 return mappedValues.get(0);
             }
         }
@@ -637,35 +639,30 @@ public class CmapSubtable
     }
 
     /**
-     * Fills the given map with gid to unicode mappings.
-     * 
-     * @param gidToUni the map to put the mappings into
-     * @param maxGid the maximum gid value
+     * Returns all possible character codes for the given gid, or null if there is none.
+     *
+     * @param gid glyph id
+     * @return a list with all character codes the given gid maps to
      * 
      */
-    public void createGID2UnicodeMapping(Map<Integer, Integer> gidToUni, int maxGid)
+    public List<Integer> getCharCodes(int gid)
     {
-        for (int gid = 1; gid <= maxGid; gid++)
+        int code = getCharCode(gid);
+        if (code == -1)
         {
-            int codePoint = getCharCode(gid);
-            // skip composite glyph components that have no code point
-            if (codePoint == -1)
-            {
-                continue;
-            }
-            if (codePoint == Integer.MIN_VALUE)
-            {
-                List<Integer> mappedValues = glyphIdToCharacterCodeMultiple.get(gid);
-                for (Integer mappedValue : mappedValues)
-                {
-                    gidToUni.put(gid, mappedValue); // CID = GID
-                }
-            }
-            else
+            return null;
+        }
+        if (code == Integer.MIN_VALUE)
+        {
+            List<Integer> mappedValues = glyphIdToCharacterCodeMultiple.get(gid);
+            if (mappedValues != null)
             {
-                gidToUni.put(gid, codePoint); // CID = GID
+                return new ArrayList<Integer>(mappedValues);
             }
         }
+        List<Integer> codes = new ArrayList<Integer>(1);
+        codes.add(code);
+        return codes;
     }
 
     @Override

Propchange: pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/ttf/CmapSubtable.java
------------------------------------------------------------------------------
--- svn:mergeinfo (original)
+++ svn:mergeinfo Thu Oct  6 15:32:08 2016
@@ -1,3 +1,3 @@
-/pdfbox/branches/2.0/fontbox/src/main/java/org/apache/fontbox/ttf/CmapSubtable.java:1760418,1761484,1762133,1762140,1763344
+/pdfbox/branches/2.0/fontbox/src/main/java/org/apache/fontbox/ttf/CmapSubtable.java:1760418,1761484,1762133,1762140,1763344,1763609
 /pdfbox/branches/no-awt/fontbox/src/main/java/org/apache/fontbox/ttf/CmapSubtable.java:1618517-1621410
 /pdfbox/no-awt/fontbox/src/main/java/org/apache/fontbox/ttf/CmapSubtable.java:1618514-1618516

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDCIDFontType2Embedder.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDCIDFontType2Embedder.java?rev=1763612&r1=1763611&r2=1763612&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDCIDFontType2Embedder.java
(original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDCIDFontType2Embedder.java
Thu Oct  6 15:32:08 2016
@@ -23,6 +23,7 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.util.Collections;
 import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
 import java.util.Set;
 import java.util.TreeSet;
@@ -48,7 +49,6 @@ final class PDCIDFontType2Embedder exten
     private final PDType0Font parent;
     private final COSDictionary dict;
     private final COSDictionary cidFont;
-    private final Map<Integer, Integer> gidToUni;
 
     /**
      * Creates a new TrueType font embedder for the given TTF as a PDCIDFontType2.
@@ -73,16 +73,16 @@ final class PDCIDFontType2Embedder exten
         dict.setItem(COSName.ENCODING, COSName.IDENTITY_H); // CID = GID
 
         // descendant CIDFont
-        cidFont = createCIDFont();
+        cidFont = createCIDFont(embedSubset);
         COSArray descendantFonts = new COSArray();
         descendantFonts.add(cidFont);
         dict.setItem(COSName.DESCENDANT_FONTS, descendantFonts);
 
-        // build GID -> Unicode map
-        gidToUni = new HashMap<Integer, Integer>(ttf.getMaximumProfile().getNumGlyphs());
-        cmap.createGID2UnicodeMapping(gidToUni, ttf.getMaximumProfile().getNumGlyphs());
-        // ToUnicode CMap
-        buildToUnicodeCMap(null);
+        if (!embedSubset)
+        {
+            // build GID -> Unicode map
+            buildToUnicodeCMap(null);
+        }
     }
 
     /**
@@ -100,7 +100,8 @@ final class PDCIDFontType2Embedder exten
             int oldGID = entry.getValue();
             cidToGid.put(oldGID, newGID);
         }
-
+        // build unicode mapping before subsetting as the subsetted font won't have a cmap
+        buildToUnicodeCMap(gidToCid);
         // rebuild the relevant part of the font
         buildFontFile2(ttfSubset);
         addNameTag(tag);
@@ -135,9 +136,11 @@ final class PDCIDFontType2Embedder exten
             }
 
             // skip composite glyph components that have no code point
-            Integer codePoint = gidToUni.get(cid); // old GID -> Unicode
-            if (codePoint != null)
+            List<Integer> codes = cmap.getCharCodes(cid); // old GID -> Unicode
+            if (codes != null)
             {
+                // use the first entry even for ambiguous mappings
+                int codePoint = codes.get(0);
                 if (codePoint > 0xFFFF)
                 {
                     hasSurrogates = true;
@@ -174,7 +177,7 @@ final class PDCIDFontType2Embedder exten
         return info;
     }
 
-    private COSDictionary createCIDFont() throws IOException
+    private COSDictionary createCIDFont(boolean embedSubset) throws IOException
     {
         COSDictionary cidFont = new COSDictionary();
 
@@ -193,7 +196,12 @@ final class PDCIDFontType2Embedder exten
         cidFont.setItem(COSName.FONT_DESC, fontDescriptor.getCOSObject());
 
         // W - widths
-        buildWidths(cidFont);
+        if (!embedSubset)
+        {
+            // subsetted fonts have a reduced amount of widths
+            // and will be created after subsetting
+            buildWidths(cidFont);
+        }
 
         // CIDToGIDMap
         cidFont.setItem(COSName.CID_TO_GID_MAP, COSName.IDENTITY);



Mime
View raw message