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 2021/09/03 06:20:36 UTC

svn commit: r1892838 - in /pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel: font/Standard14Fonts.java interactive/annotation/handlers/PDTextAppearanceHandler.java

Author: lehmi
Date: Fri Sep  3 06:20:36 2021
New Revision: 1892838

URL: http://svn.apache.org/viewvc?rev=1892838&view=rev
Log:
PDFBOX-5214: add additional mapping for the glyph names for standard 14 fonts

Modified:
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/Standard14Fonts.java
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/annotation/handlers/PDTextAppearanceHandler.java

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/Standard14Fonts.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/Standard14Fonts.java?rev=1892838&r1=1892837&r2=1892838&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/Standard14Fonts.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/Standard14Fonts.java Fri Sep  3 06:20:36 2021
@@ -17,6 +17,9 @@
 
 package org.apache.pdfbox.pdmodel.font;
 
+import static org.apache.pdfbox.pdmodel.font.UniUtil.getUniNameOfCodePoint;
+
+import java.awt.geom.GeneralPath;
 import java.io.BufferedInputStream;
 import java.io.IOException;
 import java.io.InputStream;
@@ -28,6 +31,8 @@ import java.util.Set;
 import org.apache.fontbox.FontBoxFont;
 import org.apache.fontbox.afm.AFMParser;
 import org.apache.fontbox.afm.FontMetrics;
+import org.apache.pdfbox.pdmodel.font.encoding.GlyphList;
+import org.apache.pdfbox.pdmodel.font.encoding.SymbolEncoding;
 
 /**
  * The "Standard 14" PDF fonts, also known as the "base 14" fonts.
@@ -235,16 +240,19 @@ public final class Standard14Fonts
      * @param baseName name of the standard 14 font
      * @return the mapped font
      */
-    public static FontBoxFont getMappedFont(FontName baseName)
+    private static FontBoxFont getMappedFont(FontName baseName)
     {
-        if (GENERIC_FONTS.get(baseName) == null)
+        if (!GENERIC_FONTS.containsKey(baseName))
         {
             synchronized (GENERIC_FONTS)
             {
-                if (GENERIC_FONTS.get(baseName) == null)
+                if (!GENERIC_FONTS.containsKey(baseName))
                 {
                     PDType1Font type1Font = new PDType1Font(baseName);
-                    GENERIC_FONTS.put(baseName, type1Font.getFontBoxFont());
+                    if (type1Font != null)
+                    {
+                        GENERIC_FONTS.put(baseName, type1Font.getFontBoxFont());
+                    }
                 }
             }
         }
@@ -252,6 +260,57 @@ public final class Standard14Fonts
     }
 
     /**
+     * Returns the path for the character with the given name for the specified Standard 14 font. The mapped font is
+     * cached. The path may differ in different environments as it depends on the mapped font.
+     *
+     * @param baseName name of the standard 14 font
+     * @param glyphName name of glyph
+     * @return the mapped font
+     */
+    public static GeneralPath getGlyphPath(FontName baseName, String glyphName) throws IOException
+    {
+        // copied and adapted from PDType1Font.getNameInFont(String)
+        if (!glyphName.equals(".notdef"))
+        {
+            FontBoxFont mappedFont = getMappedFont(baseName);
+            if (mappedFont != null)
+            {
+                if (mappedFont.hasGlyph(glyphName))
+                {
+                    return mappedFont.getPath(glyphName);
+                }
+                String unicodes = getGlyphList(baseName).toUnicode(glyphName);
+                if (unicodes != null && unicodes.length() == 1)
+                {
+                    String uniName = getUniNameOfCodePoint(unicodes.codePointAt(0));
+                    if (mappedFont.hasGlyph(uniName))
+                    {
+                        return mappedFont.getPath(uniName);
+                    }
+                }
+                if ("SymbolMT".equals(mappedFont.getName()))
+                {
+                    Integer code = SymbolEncoding.INSTANCE.getNameToCodeMap().get(glyphName);
+                    if (code != null)
+                    {
+                        String uniName = getUniNameOfCodePoint(code + 0xF000);
+                        if (mappedFont.hasGlyph(uniName))
+                        {
+                            return mappedFont.getPath(uniName);
+                        }
+                    }
+                }
+            }
+        }
+        return new GeneralPath();
+    }
+
+    private static GlyphList getGlyphList(FontName baseName)
+    {
+        return FontName.ZAPF_DINGBATS == baseName ? GlyphList.getZapfDingbats()
+                : GlyphList.getAdobeGlyphList();
+    }
+    /**
      * Enum for the names of the 14 standard fonts.
      */
     public enum FontName

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/annotation/handlers/PDTextAppearanceHandler.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/annotation/handlers/PDTextAppearanceHandler.java?rev=1892838&r1=1892837&r2=1892838&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/annotation/handlers/PDTextAppearanceHandler.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/annotation/handlers/PDTextAppearanceHandler.java Fri Sep  3 06:20:36 2021
@@ -343,8 +343,7 @@ public class PDTextAppearanceHandler ext
 
         // we get the shape of an Helvetica bold "?" and use that one.
         // Adobe uses a different font (which one?), or created the shape from scratch.
-        GeneralPath path = Standard14Fonts.getMappedFont(FontName.HELVETICA_BOLD)
-                .getPath("question");
+        GeneralPath path = Standard14Fonts.getGlyphPath(FontName.HELVETICA_BOLD, "question");
         addPath(contentStream, path);
         contentStream.restoreGraphicsState();
         // draw the outer circle counterclockwise to fill area between circle and "?"
@@ -386,7 +385,7 @@ public class PDTextAppearanceHandler ext
 
         // we get the shape of an Helvetica "?" and use that one.
         // Adobe uses a different font (which one?), or created the shape from scratch.
-        GeneralPath path = Standard14Fonts.getMappedFont(FontName.HELVETICA).getPath("paragraph");
+        GeneralPath path = Standard14Fonts.getGlyphPath(FontName.HELVETICA, "paragraph");
         addPath(contentStream, path);
         contentStream.restoreGraphicsState();
         contentStream.fillAndStroke();
@@ -415,10 +414,10 @@ public class PDTextAppearanceHandler ext
         contentStream.transform(Matrix.getScaleInstance(0.001f * 4, 0.001f * 4));
         contentStream.transform(Matrix.getTranslateInstance(200, 0));
         addPath(contentStream,
-                Standard14Fonts.getMappedFont(FontName.HELVETICA_BOLD).getPath("N"));
+                Standard14Fonts.getGlyphPath(FontName.HELVETICA_BOLD, "N"));
         contentStream.transform(Matrix.getTranslateInstance(1300, 0));
         addPath(contentStream,
-                Standard14Fonts.getMappedFont(FontName.HELVETICA_BOLD).getPath("P"));
+                Standard14Fonts.getGlyphPath(FontName.HELVETICA_BOLD, "P"));
         contentStream.fill();
     }
 
@@ -438,7 +437,7 @@ public class PDTextAppearanceHandler ext
 
         // we get the shape of a Zapf Dingbats star (0x2605) and use that one.
         // Adobe uses a different font (which one?), or created the shape from scratch.
-        GeneralPath path = Standard14Fonts.getMappedFont(FontName.ZAPF_DINGBATS).getPath("a35");
+        GeneralPath path = Standard14Fonts.getGlyphPath(FontName.ZAPF_DINGBATS, "a35");
         addPath(contentStream, path);
         contentStream.fillAndStroke();
     }
@@ -463,7 +462,7 @@ public class PDTextAppearanceHandler ext
 
         // we get the shape of a Zapf Dingbats check (0x2714) and use that one.
         // Adobe uses a different font (which one?), or created the shape from scratch.
-        GeneralPath path = Standard14Fonts.getMappedFont(FontName.ZAPF_DINGBATS).getPath("a20");
+        GeneralPath path = Standard14Fonts.getGlyphPath(FontName.ZAPF_DINGBATS, "a20");
         addPath(contentStream, path);
         contentStream.fillAndStroke();
     }
@@ -486,7 +485,7 @@ public class PDTextAppearanceHandler ext
 
         // we get the shape of a Zapf Dingbats right pointer (0x27A4) and use that one.
         // Adobe uses a different font (which one?), or created the shape from scratch.
-        GeneralPath path = Standard14Fonts.getMappedFont(FontName.ZAPF_DINGBATS).getPath("a174");
+        GeneralPath path = Standard14Fonts.getGlyphPath(FontName.ZAPF_DINGBATS, "a174");
         addPath(contentStream, path);
         contentStream.fillAndStroke();
     }
@@ -508,7 +507,7 @@ public class PDTextAppearanceHandler ext
 
         // we get the shape of a Symbol crosshair (0x2295) and use that one.
         // Adobe uses a different font (which one?), or created the shape from scratch.
-        GeneralPath path = Standard14Fonts.getMappedFont(FontName.SYMBOL).getPath("circleplus");
+        GeneralPath path = Standard14Fonts.getGlyphPath(FontName.SYMBOL, "circleplus");
         addPath(contentStream, path);
         contentStream.fillAndStroke();
     }
@@ -589,7 +588,7 @@ public class PDTextAppearanceHandler ext
 
         // we get the shape of a Zapf Dingbats right arrow (0x2794) and use that one.
         // Adobe uses a different font (which one?), or created the shape from scratch.
-        GeneralPath path = Standard14Fonts.getMappedFont(FontName.ZAPF_DINGBATS).getPath("a160");
+        GeneralPath path = Standard14Fonts.getGlyphPath(FontName.ZAPF_DINGBATS, "a160");
         addPath(contentStream, path);
         contentStream.restoreGraphicsState();
         // surprisingly, this one not counterclockwise.