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 ph...@apache.org on 2011/09/26 11:48:21 UTC

svn commit: r1175754 - in /xmlgraphics/fop/trunk: ./ src/java/org/apache/fop/fonts/ src/java/org/apache/fop/pdf/ test/java/org/apache/fop/ test/java/org/apache/fop/pdf/

Author: phancock
Date: Mon Sep 26 09:48:20 2011
New Revision: 1175754

URL: http://svn.apache.org/viewvc?rev=1175754&view=rev
Log:
PDFFactory responsible for asdigning name to a subset font.

Added:
    xmlgraphics/fop/trunk/test/java/org/apache/fop/pdf/PDFFactoryTestCase.java
Modified:
    xmlgraphics/fop/trunk/src/java/org/apache/fop/fonts/FontDescriptor.java
    xmlgraphics/fop/trunk/src/java/org/apache/fop/fonts/LazyFont.java
    xmlgraphics/fop/trunk/src/java/org/apache/fop/fonts/MultiByteFont.java
    xmlgraphics/fop/trunk/src/java/org/apache/fop/fonts/SingleByteFont.java
    xmlgraphics/fop/trunk/src/java/org/apache/fop/pdf/PDFFactory.java
    xmlgraphics/fop/trunk/status.xml
    xmlgraphics/fop/trunk/test/java/org/apache/fop/UtilityCodeTestSuite.java

Modified: xmlgraphics/fop/trunk/src/java/org/apache/fop/fonts/FontDescriptor.java
URL: http://svn.apache.org/viewvc/xmlgraphics/fop/trunk/src/java/org/apache/fop/fonts/FontDescriptor.java?rev=1175754&r1=1175753&r2=1175754&view=diff
==============================================================================
--- xmlgraphics/fop/trunk/src/java/org/apache/fop/fonts/FontDescriptor.java (original)
+++ xmlgraphics/fop/trunk/src/java/org/apache/fop/fonts/FontDescriptor.java Mon Sep 26 09:48:20 2011
@@ -86,5 +86,10 @@ public interface FontDescriptor extends 
      */
     boolean isEmbeddable();
 
+    /**
+     * Indicates whether this font is subset embedded.
+     * @return true if this font is subset embedded
+     */
+    boolean isSubsetEmbedded();
 
 }

Modified: xmlgraphics/fop/trunk/src/java/org/apache/fop/fonts/LazyFont.java
URL: http://svn.apache.org/viewvc/xmlgraphics/fop/trunk/src/java/org/apache/fop/fonts/LazyFont.java?rev=1175754&r1=1175753&r2=1175754&view=diff
==============================================================================
--- xmlgraphics/fop/trunk/src/java/org/apache/fop/fonts/LazyFont.java (original)
+++ xmlgraphics/fop/trunk/src/java/org/apache/fop/fonts/LazyFont.java Mon Sep 26 09:48:20 2011
@@ -27,12 +27,10 @@ import java.util.Set;
 import javax.xml.transform.Source;
 import javax.xml.transform.stream.StreamSource;
 
-import org.xml.sax.InputSource;
-
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
-
 import org.apache.fop.apps.FOPException;
+import org.xml.sax.InputSource;
 
 /**
  * This class is used to defer the loading of a font until it is really used.
@@ -375,5 +373,13 @@ public class LazyFont extends Typeface i
         return realFontDescriptor.isEmbeddable();
     }
 
+    /**
+     * {@inheritDoc}
+     */
+    public boolean isSubsetEmbedded() {
+        load(true);
+        return realFont.isMultiByte();
+    }
+
 }
 

Modified: xmlgraphics/fop/trunk/src/java/org/apache/fop/fonts/MultiByteFont.java
URL: http://svn.apache.org/viewvc/xmlgraphics/fop/trunk/src/java/org/apache/fop/fonts/MultiByteFont.java?rev=1175754&r1=1175753&r2=1175754&view=diff
==============================================================================
--- xmlgraphics/fop/trunk/src/java/org/apache/fop/fonts/MultiByteFont.java (original)
+++ xmlgraphics/fop/trunk/src/java/org/apache/fop/fonts/MultiByteFont.java Mon Sep 26 09:48:20 2011
@@ -20,7 +20,6 @@
 package org.apache.fop.fonts;
 
 //Java
-import java.text.DecimalFormat;
 import java.util.Map;
 
 
@@ -29,16 +28,12 @@ import java.util.Map;
  */
 public class MultiByteFont extends CIDFont {
 
-    private static int uniqueCounter = -1;
-
     private String ttcName = null;
     private String encoding = "Identity-H";
 
     private int defaultWidth = 0;
     private CIDFontType cidType = CIDFontType.CIDTYPE2;
 
-    private String namePrefix = null;    // Quasi unique prefix
-
     private CIDSubset subset = new CIDSubset();
 
     /** A map from Unicode indices to glyph indices */
@@ -49,31 +44,11 @@ public class MultiByteFont extends CIDFo
      */
     public MultiByteFont() {
         subset.setupFirstGlyph();
-
-        // Create a quasiunique prefix for fontname
-        synchronized (this.getClass()) {
-            uniqueCounter++;
-            if (uniqueCounter > 99999 || uniqueCounter < 0) {
-                uniqueCounter = 0; //We need maximum 5 character then we start again
-            }
-        }
-        DecimalFormat counterFormat = new DecimalFormat("00000");
-        String cntString = counterFormat.format(uniqueCounter);
-
-        //Subset prefix as described in chapter 5.5.3 of PDF 1.4
-        StringBuffer sb = new StringBuffer("E");
-        for (int i = 0, c = cntString.length(); i < c; i++) {
-            //translate numbers to uppercase characters
-            sb.append((char)(cntString.charAt(i) + (65 - 48)));
-        }
-        sb.append("+");
-        namePrefix = sb.toString();
-
         setFontType(FontType.TYPE0);
     }
 
-    /** {@inheritDoc} */
-    public int getDefaultWidth() {
+    /** {@inheritdoc} */
+    public int getdefaultwidth() {
         return defaultWidth;
     }
 
@@ -105,14 +80,10 @@ public class MultiByteFont extends CIDFo
         this.cidType = cidType;
     }
 
-    private String getPrefixedFontName() {
-        return namePrefix + FontUtil.stripWhiteSpace(super.getFontName());
-    }
-
     /** {@inheritDoc} */
     public String getEmbedFontName() {
         if (isEmbeddable()) {
-            return getPrefixedFontName();
+            return FontUtil.stripWhiteSpace(super.getFontName());
         } else {
             return super.getFontName();
         }
@@ -124,6 +95,11 @@ public class MultiByteFont extends CIDFo
     }
 
     /** {@inheritDoc} */
+    public boolean isSubsetEmbedded() {
+        return true;
+    }
+
+    /** {@inheritDoc} */
     public CIDSubset getCIDSubset() {
         return this.subset;
     }

Modified: xmlgraphics/fop/trunk/src/java/org/apache/fop/fonts/SingleByteFont.java
URL: http://svn.apache.org/viewvc/xmlgraphics/fop/trunk/src/java/org/apache/fop/fonts/SingleByteFont.java?rev=1175754&r1=1175753&r2=1175754&view=diff
==============================================================================
--- xmlgraphics/fop/trunk/src/java/org/apache/fop/fonts/SingleByteFont.java (original)
+++ xmlgraphics/fop/trunk/src/java/org/apache/fop/fonts/SingleByteFont.java Mon Sep 26 09:48:20 2011
@@ -63,7 +63,11 @@ public class SingleByteFont extends Cust
     }
 
     /** {@inheritDoc} */
-    @Override
+    public boolean isSubsetEmbedded() {
+        return false;
+    }
+
+    /** {@inheritDoc} */
     public String getEncodingName() {
         return this.mapping.getName();
     }

Modified: xmlgraphics/fop/trunk/src/java/org/apache/fop/pdf/PDFFactory.java
URL: http://svn.apache.org/viewvc/xmlgraphics/fop/trunk/src/java/org/apache/fop/pdf/PDFFactory.java?rev=1175754&r1=1175753&r2=1175754&view=diff
==============================================================================
--- xmlgraphics/fop/trunk/src/java/org/apache/fop/pdf/PDFFactory.java (original)
+++ xmlgraphics/fop/trunk/src/java/org/apache/fop/pdf/PDFFactory.java Mon Sep 26 09:48:20 2011
@@ -27,6 +27,7 @@ import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.io.InputStream;
 import java.net.MalformedURLException;
+import java.text.DecimalFormat;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.BitSet;
@@ -63,6 +64,7 @@ import org.apache.fop.fonts.truetype.Fon
 import org.apache.fop.fonts.truetype.TTFSubSetFile;
 import org.apache.fop.fonts.type1.PFBData;
 import org.apache.fop.fonts.type1.PFBParser;
+import org.apache.xmlgraphics.xmp.Metadata;
 
 /**
  * This class provides method to create and register PDF objects.
@@ -76,6 +78,8 @@ public class PDFFactory {
 
     private Log log = LogFactory.getLog(PDFFactory.class);
 
+    private int subsetFontCounter = -1;
+
     /**
      * Creates a new PDFFactory.
      * @param document the parent PDFDocument needed to register the generated
@@ -1377,10 +1381,15 @@ public class PDFFactory {
         } else {
             FontType fonttype = metrics.getFontType();
 
-            PDFFontDescriptor pdfdesc = makeFontDescriptor(descriptor);
+            String fontPrefix = descriptor.isSubsetEmbedded() ? createSubsetFontPrefix() : "";
+
+            String subsetFontName = fontPrefix + basefont;
+
+            PDFFontDescriptor pdfdesc = makeFontDescriptor(descriptor, fontPrefix);
 
             PDFFont font = null;
-            font = PDFFont.createFont(fontname, fonttype, basefont, null);
+
+            font = PDFFont.createFont(fontname, fonttype, subsetFontName, null);
             getDocument().registerObject(font);
 
             if (fonttype == FontType.TYPE0) {
@@ -1395,8 +1404,7 @@ public class PDFFactory {
                     = new PDFCIDSystemInfo(cidMetrics.getRegistry(),
                                          cidMetrics.getOrdering(),
                                          cidMetrics.getSupplement());
-                PDFCIDFont cidFont
-                    = new PDFCIDFont(basefont,
+                PDFCIDFont cidFont = new PDFCIDFont(subsetFontName,
                                    cidMetrics.getCIDType(),
                                    cidMetrics.getDefaultWidth(),
                                    getSubsetWidths(cidMetrics), sysInfo,
@@ -1550,18 +1558,35 @@ public class PDFFactory {
         return warray;
     }
 
+    private String createSubsetFontPrefix() {
+        subsetFontCounter++;
+        DecimalFormat counterFormat = new DecimalFormat("00000");
+        String counterString = counterFormat.format(subsetFontCounter);
+
+        // Subset prefix as described in chapter 5.5.3 of PDF 1.4
+        StringBuffer sb = new StringBuffer("E");
+
+        for (char c : counterString.toCharArray()) {
+            // translate numbers to uppercase characters
+            sb.append((char) (c + ('A' - '0')));
+        }
+        sb.append("+");
+        return sb.toString();
+    }
+
     /**
      * make a /FontDescriptor object
      *
      * @param desc the font descriptor
+     * @param fontPrefix the String with which to prefix the font name
      * @return the new PDF font descriptor
      */
-    public PDFFontDescriptor makeFontDescriptor(FontDescriptor desc) {
+    private PDFFontDescriptor makeFontDescriptor(FontDescriptor desc, String fontPrefix) {
         PDFFontDescriptor descriptor = null;
 
         if (desc.getFontType() == FontType.TYPE0) {
             // CID Font
-            descriptor = new PDFCIDFontDescriptor(desc.getEmbedFontName(),
+            descriptor = new PDFCIDFontDescriptor(fontPrefix + desc.getEmbedFontName(),
                                             desc.getFontBBox(),
                                             desc.getCapHeight(),
                                             desc.getFlags(),

Modified: xmlgraphics/fop/trunk/status.xml
URL: http://svn.apache.org/viewvc/xmlgraphics/fop/trunk/status.xml?rev=1175754&r1=1175753&r2=1175754&view=diff
==============================================================================
--- xmlgraphics/fop/trunk/status.xml (original)
+++ xmlgraphics/fop/trunk/status.xml Mon Sep 26 09:48:20 2011
@@ -60,6 +60,9 @@
       documents. Example: the fix of marks layering will be such a case when it's done.
     -->
     <release version="FOP Trunk" date="TBD">
+      <action context="Fonts" dev="PH" type="fix" fixes-bug="51759" due-to="Mehdi Houshmand">
+        PDFFactory responsible for asdigning name to a subset font.
+      </action>
       <action context="Fonts" dev="PH" type="fix" fixes-bug="51530" due-to="Mehdi Houshmand">
         Improved support for EBCDIC encoded double byte fonts fo AFP.
       </action>

Modified: xmlgraphics/fop/trunk/test/java/org/apache/fop/UtilityCodeTestSuite.java
URL: http://svn.apache.org/viewvc/xmlgraphics/fop/trunk/test/java/org/apache/fop/UtilityCodeTestSuite.java?rev=1175754&r1=1175753&r2=1175754&view=diff
==============================================================================
--- xmlgraphics/fop/trunk/test/java/org/apache/fop/UtilityCodeTestSuite.java (original)
+++ xmlgraphics/fop/trunk/test/java/org/apache/fop/UtilityCodeTestSuite.java Mon Sep 26 09:48:20 2011
@@ -25,6 +25,7 @@ import junit.framework.TestSuite;
 import org.apache.fop.events.BasicEventTestCase;
 import org.apache.fop.pdf.FileIDGeneratorTestCase;
 import org.apache.fop.pdf.PDFEncryptionJCETestCase;
+import org.apache.fop.pdf.PDFFactoryTestCase;
 import org.apache.fop.pdf.PDFObjectTestCase;
 import org.apache.fop.traits.BorderPropsTestCase;
 import org.apache.fop.util.BitmapImageUtilTestCase;
@@ -49,6 +50,7 @@ public class UtilityCodeTestSuite {
         suite.addTest(new TestSuite(PDFNumberTestCase.class));
         suite.addTest(new TestSuite(PDFObjectTestCase.class));
         suite.addTest(FileIDGeneratorTestCase.suite());
+        suite.addTest(new TestSuite(PDFFactoryTestCase.class));
         suite.addTest(new TestSuite(ColorUtilTestCase.class));
         suite.addTest(new TestSuite(BorderPropsTestCase.class));
         suite.addTest(new TestSuite(ElementListUtilsTestCase.class));

Added: xmlgraphics/fop/trunk/test/java/org/apache/fop/pdf/PDFFactoryTestCase.java
URL: http://svn.apache.org/viewvc/xmlgraphics/fop/trunk/test/java/org/apache/fop/pdf/PDFFactoryTestCase.java?rev=1175754&view=auto
==============================================================================
--- xmlgraphics/fop/trunk/test/java/org/apache/fop/pdf/PDFFactoryTestCase.java (added)
+++ xmlgraphics/fop/trunk/test/java/org/apache/fop/pdf/PDFFactoryTestCase.java Mon Sep 26 09:48:20 2011
@@ -0,0 +1,58 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/* $Id$ */
+
+package org.apache.fop.pdf;
+
+import junit.framework.TestCase;
+
+import org.apache.fop.fonts.CIDSubset;
+import org.apache.fop.fonts.MultiByteFont;
+
+/**
+ * Test case for {@link PDFFactory}.
+ */
+public class PDFFactoryTestCase extends TestCase {
+
+    /**
+     * This tests that when a font is subset embedded in a PDF, the font name is prefixed with a
+     * pseudo-random tag as per the PDF spec.
+     */
+    public void testSubsetFontNamePrefix() {
+        class MockedFont extends MultiByteFont {
+            @Override
+            public int[] getWidths() {
+                return new int[] { 0 };
+            }
+
+            @Override
+            public CIDSubset getCIDSubset() {
+                return new CIDSubset();
+            }
+        }
+        PDFDocument doc = new PDFDocument("Test");
+        PDFFactory pdfFactory = new PDFFactory(doc);
+        MockedFont font = new MockedFont();
+
+        PDFFont pdfDejaVu = pdfFactory.makeFont("DejaVu", "DejaVu", "TTF", font, font);
+        assertEquals("/EAAAAA+DejaVu", pdfDejaVu.getBaseFont().toString());
+
+        PDFFont pdfArial = pdfFactory.makeFont("Arial", "Arial", "TTF", font, font);
+        assertEquals("/EAAAAB+Arial", pdfArial.getBaseFont().toString());
+    }
+}



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