pdfbox-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From til...@apache.org
Subject svn commit: r1830346 - /pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDPageContentStream.java
Date Fri, 27 Apr 2018 12:51:02 GMT
Author: tilman
Date: Fri Apr 27 12:51:02 2018
New Revision: 1830346

URL: http://svn.apache.org/viewvc?rev=1830346&view=rev
Log:
PDFBOX-4189: One GsubWorker per font, by Palash Ray

Modified:
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDPageContentStream.java

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDPageContentStream.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDPageContentStream.java?rev=1830346&r1=1830345&r2=1830346&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDPageContentStream.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDPageContentStream.java Fri
Apr 27 12:51:02 2018
@@ -23,8 +23,10 @@ import java.io.Closeable;
 import java.io.IOException;
 import java.io.OutputStream;
 import java.util.ArrayList;
+import java.util.HashMap;
 import java.util.HashSet;
 import java.util.List;
+import java.util.Map;
 import java.util.Set;
 import java.util.Stack;
 import java.util.regex.Pattern;
@@ -105,6 +107,9 @@ public final class PDPageContentStream e
     private final Stack<PDColorSpace> nonStrokingColorSpaceStack = new Stack<>();
     private final Stack<PDColorSpace> strokingColorSpaceStack = new Stack<>();
 
+    private final Map<PDType0Font, GsubWorker> gsubWorkers = new HashMap<>();
+    private final GsubWorkerFactory gsubWorkerFactory = new GsubWorkerFactory();
+
     /**
      * Create a new PDPage content stream. This constructor overwrites all existing content
streams
      * of this page.
@@ -285,6 +290,18 @@ public final class PDPageContentStream e
             document.getFontsToSubset().add(font);
         }
         
+        if (font instanceof PDType0Font)
+        {
+            PDType0Font pdType0Font = (PDType0Font) font;
+            GsubData gsubData = pdType0Font.getGsubData();
+            if (gsubData != GsubData.NO_DATA_FOUND)
+            {
+                GsubWorker gsubWorker = gsubWorkerFactory.getGsubWorker(pdType0Font.getCmapLookup(),
+                        gsubData);
+                gsubWorkers.put((PDType0Font) font, gsubWorker);
+            }
+        }
+
         writeOperand(getResources().add(font));
         writeOperand(fontSize);
         writeOperator("Tf");
@@ -329,12 +346,13 @@ public final class PDPageContentStream e
 
         if (font instanceof PDType0Font)
         {
-            PDType0Font pdType0Font = (PDType0Font) font;
-            GsubData gsubData = pdType0Font.getGsubData();
-            if (gsubData != GsubData.NO_DATA_FOUND)
+
+            GsubWorker gsubWorker = gsubWorkers.get(font.getName());
+            if (gsubWorker != null)
             {
+                PDType0Font pdType0Font = (PDType0Font) font;
                 Set<Integer> glyphIds = new HashSet<>();
-                encodedText = encodeForGsub(gsubData, glyphIds, pdType0Font, text);
+                encodedText = encodeForGsub(gsubWorker, glyphIds, pdType0Font, text);
                 if (pdType0Font.willBeSubset())
                 {
                     pdType0Font.addGlyphsToSubset(glyphIds);
@@ -1176,7 +1194,7 @@ public final class PDPageContentStream e
         writeOperator("Ts");
     }
 
-    private byte[] encodeForGsub(GsubData gsubData,
+    private byte[] encodeForGsub(GsubWorker gsubWorker,
             Set<Integer> glyphIds, PDType0Font font, String text) throws IOException
     {
 
@@ -1196,15 +1214,14 @@ public final class PDPageContentStream e
             }
             else
             {
-                glyphIds.addAll(applyGSUBRules(out, font, gsubData, word));
+                glyphIds.addAll(applyGSUBRules(gsubWorker, out, font, word));
             }
         }
 
         return out.toByteArray();
     }
 
-    private List<Integer> applyGSUBRules(ByteArrayOutputStream out, PDType0Font font,
-            GsubData gsubData, String word) throws IOException
+    private List<Integer> applyGSUBRules(GsubWorker gsubWorker, ByteArrayOutputStream
out, PDType0Font font, String word) throws IOException
     {
         List<Integer> originalGlyphIds = new ArrayList<>();
         CmapLookup cmapLookup = font.getCmapLookup();
@@ -1221,10 +1238,6 @@ public final class PDPageContentStream e
             originalGlyphIds.add(glyphId);
         }
 
-        GsubWorkerFactory gsubWorkerFactory = new GsubWorkerFactory();
-
-        GsubWorker gsubWorker = gsubWorkerFactory.getGsubWorker(cmapLookup, gsubData);
-
         List<Integer> glyphIdsAfterGsub = gsubWorker.applyTransforms(originalGlyphIds);
 
         for (Integer glyphId : glyphIdsAfterGsub)



Mime
View raw message