]> source.dussan.org Git - xmlgraphics-fop.git/commitdiff
Applied patch from bugzilla 5335, comment 10.
authorBertrand Delacretaz <bdelacretaz@apache.org>
Tue, 10 Oct 2006 13:00:05 +0000 (13:00 +0000)
committerBertrand Delacretaz <bdelacretaz@apache.org>
Tue, 10 Oct 2006 13:00:05 +0000 (13:00 +0000)
Generates a ToUnicode table for embedded CID fonts.
Patch provided by Adam Strzelecki, ono@java.pl.
The patch contains code for the FOray project, used with permission (bugzilla 5335 comment #13).

git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@454725 13f79535-47bb-0310-9956-ffa450edef68

src/java/org/apache/fop/fonts/CIDFont.java
src/java/org/apache/fop/fonts/MultiByteFont.java
src/java/org/apache/fop/fonts/SingleByteFont.java
src/java/org/apache/fop/pdf/PDFCMap.java
src/java/org/apache/fop/pdf/PDFFactory.java

index facfb9c882a491b7544f465d3bdac9c639777046..dd12991c11468c2e52f967b71241f7396cae1bee 100644 (file)
@@ -38,6 +38,11 @@ public abstract class CIDFont extends CustomFont {
     public Map usedGlyphsIndex = new java.util.HashMap();
     public int usedGlyphsCount = 0;
 
+    /**
+     * usedCharsIndex contains new glyph, original char
+     */
+    public Map usedCharsIndex = new java.util.HashMap();
+
     //private PDFWArray warray = new PDFWArray();
     public int width[] = null;
 
@@ -91,4 +96,8 @@ public abstract class CIDFont extends CustomFont {
         return true;
     }
 
+    /**
+     * Returns char[] array .
+     */
+    public abstract char[] getCharsUsed();
 }
\ No newline at end of file
index ed1bbd5054fc72b4df5e2f563d604d1e6f72012e..6db84b68df301ed01018526b79b9870660dba80c 100644 (file)
@@ -126,7 +126,7 @@ public class MultiByteFont extends CIDFont {
      * @see org.apache.fop.fonts.FontDescriptor#isEmbeddable()
      */
     public boolean isEmbeddable() {
-        return !(getEmbedFileName() == null && embedResourceName == null); 
+        return !(getEmbedFileName() == null && embedResourceName == null);
     }
 
     /**
@@ -199,8 +199,8 @@ public class MultiByteFont extends CIDFont {
         for (int i = 0; (i < bfentries.length) && retIdx == 0; i++) {
             if (bfentries[i].getUnicodeStart() <= idx
                     && bfentries[i].getUnicodeEnd() >= idx) {
-                        
-                retIdx = bfentries[i].getGlyphStartIndex() 
+
+                retIdx = bfentries[i].getGlyphStartIndex()
                     + idx
                     - bfentries[i].getUnicodeStart();
             }
@@ -223,6 +223,8 @@ public class MultiByteFont extends CIDFont {
                                new Integer(usedGlyphsCount));
                 usedGlyphsIndex.put(new Integer(usedGlyphsCount),
                                     new Integer(retIdx));
+                usedCharsIndex.put(new Integer(usedGlyphsCount),
+                                    new Integer((int) c));
                 retIdx = usedGlyphsCount;
                 usedGlyphsCount++;
             } else {
@@ -300,5 +302,26 @@ public class MultiByteFont extends CIDFont {
         return usedGlyphs;
     }
 
+    /** The invalid Unicode character, suitable as a return value in methods
+     * that need to return an invalid character. */
+    public static final char INVALID_UNICODE_CHAR = 0xFFFF;
+
+    public char[] getCharsUsed() {
+        if (! isEmbeddable()) {
+            return null;
+        }
+        char[] charArray = new char[usedGlyphsCount];
+        for (int i = 0; i < usedGlyphsCount; i++) {
+            Integer mapValue = (Integer)usedCharsIndex.get(new Integer(i));
+            if(mapValue != null) {
+                char arrayItem = (char) mapValue.intValue();
+                charArray[i] = arrayItem;
+            }
+            else {
+                charArray[i] = INVALID_UNICODE_CHAR;
+            }
+        }
+        return charArray;
+    }
 }
 
index a170514a979cbdef0f6018b77f772181d99478fd..99245fe2878e8045e562df377645eae5aa3625e8 100644 (file)
@@ -95,7 +95,7 @@ public class SingleByteFont extends CustomFont {
             return '#';
         }
     }
-    
+
     /**
      * @see org.apache.fop.fonts.Typeface#hasChar(char)
      */
@@ -117,5 +117,8 @@ public class SingleByteFont extends CustomFont {
         this.width[index] = width;
     }
 
+    public char[] getCharsUsed() {
+        return null;
+    }
 }
 
index 84d9f8359cc2b8c413077b01da8001d9fd6e824a..d62e45d850951c43340346952dc2ee73acd5a07c 100644 (file)
@@ -19,6 +19,9 @@
  
 package org.apache.fop.pdf;
 
+import java.io.IOException;
+import java.io.OutputStream;
+
 /**
  * Class representing the CMap encodings.
  *
@@ -424,53 +427,91 @@ public class PDFCMap extends PDFStream {
      * @param p the string buffer to add the pdf data to
      */
     public void fillInPDF(StringBuffer p) {
+        writePreStream(p);
+        writeStreamComments(p);
+        writeCIDInit(p);
+        writeCIDSystemInfo(p);
+        writeVersionTypeName(p);
+        writeCodeSpaceRange(p);
+        writeCIDRange(p);
+        writeBFEntries(p);
+        writeWrapUp(p);
+        writeStreamAfterComments(p);
+        writeUseCMap(p);
+        add(p.toString());
+    }
+
+    protected void writePreStream(StringBuffer p) {
         // p.append("/Type /CMap\n");
         // p.append(sysInfo.toPDFString());
-        // p.append("/CMapName /" + name);
-        // p.append("\n");
+        // p.append("/CMapName /" + name + EOL);
+    }
+
+    protected void writeStreamComments(StringBuffer p) {
         p.append("%!PS-Adobe-3.0 Resource-CMap\n");
         p.append("%%DocumentNeededResources: ProcSet (CIDInit)\n");
         p.append("%%IncludeResource: ProcSet (CIDInit)\n");
         p.append("%%BeginResource: CMap (" + name + ")\n");
         p.append("%%EndComments\n");
+    }
 
+    protected void writeCIDInit(StringBuffer p) {
         p.append("/CIDInit /ProcSet findresource begin\n");
         p.append("12 dict begin\n");
         p.append("begincmap\n");
+    }
 
+    protected void writeCIDSystemInfo(StringBuffer p) {
         p.append("/CIDSystemInfo 3 dict dup begin\n");
         p.append("  /Registry (Adobe) def\n");
         p.append("  /Ordering (Identity) def\n");
         p.append("  /Supplement 0 def\n");
         p.append("end def\n");
+    }
 
+    protected void writeVersionTypeName(StringBuffer p) {
         p.append("/CMapVersion 1 def\n");
         p.append("/CMapType 1 def\n");
         p.append("/CMapName /" + name + " def\n");
+    }
 
+    protected void writeCodeSpaceRange(StringBuffer p) {
         p.append("1 begincodespacerange\n");
         p.append("<0000> <FFFF>\n");
         p.append("endcodespacerange\n");
+    }
+
+    protected void writeCIDRange(StringBuffer p) {
         p.append("1 begincidrange\n");
         p.append("<0000> <FFFF> 0\n");
         p.append("endcidrange\n");
+    }
 
+    protected void writeBFEntries(StringBuffer p) {
         // p.append("1 beginbfrange\n");
         // p.append("<0020> <0100> <0000>\n");
         // p.append("endbfrange\n");
+    }
 
+    protected void writeWrapUp(StringBuffer p) {
         p.append("endcmap\n");
         p.append("CMapName currentdict /CMap defineresource pop\n");
         p.append("end\n");
         p.append("end\n");
+    }
+
+    protected void writeStreamAfterComments(StringBuffer p) {
         p.append("%%EndResource\n");
         p.append("%%EOF\n");
+    }
+
+    protected void writeUseCMap(StringBuffer p) {
         /*
-         * p.append(" /Type /CMap\n/CMapName /" + name);
-         * p.append("\n");
-         * p.append("\n/WMode "); p.append(wMode);
+         * p.append(" /Type /CMap");
+         * p.append("/CMapName /" + name + EOL);
+         * p.append("/WMode " + wMode + EOL);
          * if (base != null) {
-         * p.append("\n/UseCMap ");
+         *     p.append("/UseCMap ");
          * if (base instanceof String) {
          * p.append("/"+base);
          * } else {// base instanceof PDFStream
@@ -480,4 +521,8 @@ public class PDFCMap extends PDFStream {
          */
     }
 
+    protected int output(OutputStream stream) throws IOException {
+        fillInPDF(new StringBuffer());
+        return super.output(stream);
+    }
 }
index 77c25bc8943ba4ecc5bd1875ddb831c165efddf7..4ab6ebd7bfc2ce6df6932bfadcf1b2d6d15b9e79 100644 (file)
@@ -32,9 +32,6 @@ import java.util.Map;
 import javax.xml.transform.Source;
 import javax.xml.transform.stream.StreamSource;
 
-//W3C DOM
-import org.w3c.dom.Document;
-
 // Apache libs
 import org.apache.commons.io.IOUtils;
 import org.apache.commons.io.output.ByteArrayOutputStream;
@@ -957,7 +954,7 @@ public class PDFFactory {
      * @return the new PDF outline object
      */
     public PDFOutline makeOutline(PDFOutline parent, String label,
-                                  String destination, float yoffset, 
+                                  String destination, float yoffset,
                                   boolean showSubItems) {
 
         String goToRef = getGoToReference(destination, yoffset);
@@ -1060,6 +1057,12 @@ public class PDFFactory {
                                    (PDFCIDFontDescriptor)pdfdesc);
                 getDocument().registerObject(cidFont);
 
+                PDFCMap cmap = new PDFToUnicodeCMap(cidMetrics, "fop-ucs-H",
+                    new PDFCIDSystemInfo("Adobe",
+                        "Identity",
+                        0));
+                getDocument().registerObject(cmap);
+                ((PDFFontType0)font).setCMAP(cmap);
                 ((PDFFontType0)font).setDescendantFonts(cidFont);
             } else {
                 int firstChar = 0;