pdfbox-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From til...@apache.org
Subject svn commit: r1692711 - in /pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools: ./ pdfdebugger/fontencodingpane/
Date Sun, 26 Jul 2015 10:54:11 GMT
Author: tilman
Date: Sun Jul 26 10:54:11 2015
New Revision: 1692711

URL: http://svn.apache.org/r1692711
Log:
PDFBOX-2530: show font encoding, as done by Khyrul Bashar in GSoC2015

Added:
    pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/pdfdebugger/fontencodingpane/
    pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/pdfdebugger/fontencodingpane/FontEncodingPaneController.java
  (with props)
    pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/pdfdebugger/fontencodingpane/FontEncodingView.java
  (with props)
    pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/pdfdebugger/fontencodingpane/SimpleFont.java
  (with props)
    pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/pdfdebugger/fontencodingpane/Type0Font.java
  (with props)
Modified:
    pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/PDFDebugger.java

Modified: pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/PDFDebugger.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/PDFDebugger.java?rev=1692711&r1=1692710&r2=1692711&view=diff
==============================================================================
--- pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/PDFDebugger.java (original)
+++ pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/PDFDebugger.java Sun Jul 26 10:54:11
2015
@@ -77,6 +77,7 @@ import org.apache.pdfbox.tools.pdfdebugg
 import org.apache.pdfbox.tools.pdfdebugger.colorpane.CSIndexed;
 import org.apache.pdfbox.tools.pdfdebugger.colorpane.CSSeparation;
 import org.apache.pdfbox.tools.pdfdebugger.flagbitspane.FlagBitsPane;
+import org.apache.pdfbox.tools.pdfdebugger.fontencodingpane.FontEncodingPaneController;
 import org.apache.pdfbox.tools.pdfdebugger.pagepane.PagePane;
 import org.apache.pdfbox.tools.pdfdebugger.streampane.StreamPane;
 import org.apache.pdfbox.tools.pdfdebugger.treestatus.TreeStatus;
@@ -457,6 +458,11 @@ public class PDFDebugger extends javax.s
                     showStream((COSStream)getUnderneathObject(selectedNode), path);
                     return;
                 }
+                if (isFont(selectedNode))
+                {
+                    showFont(selectedNode, path);
+                    return;
+                }
                 if (!jSplitPane1.getRightComponent().equals(jScrollPane2))
                 {
                     jSplitPane1.setRightComponent(jScrollPane2);
@@ -556,6 +562,26 @@ public class PDFDebugger extends javax.s
         return getUnderneathObject(selectedNode) instanceof COSStream;
     }
 
+    private boolean isFont(Object selectedNode)
+    {
+        selectedNode = getUnderneathObject(selectedNode);
+        if (selectedNode instanceof COSDictionary)
+        {
+            COSDictionary dic = (COSDictionary)selectedNode;
+            return  dic.containsKey(COSName.TYPE) &&
+                    dic.getCOSName(COSName.TYPE).equals(COSName.FONT) &&
+                    !isCIDFont(dic);
+        }
+        return false;
+    }
+
+    private boolean isCIDFont(COSDictionary dic)
+    {
+        return dic.containsKey(COSName.SUBTYPE) &&
+                (dic.getCOSName(COSName.SUBTYPE).equals(COSName.CID_FONT_TYPE0)
+                || dic.getCOSName(COSName.SUBTYPE).equals(COSName.CID_FONT_TYPE2));
+    }
+
     /**
      * Show a Panel describing color spaces in more detail and interactive way.
      * @param csNode the special color space containing node.
@@ -665,6 +691,15 @@ public class PDFDebugger extends javax.s
         jSplitPane1.setRightComponent(streamPane.getPanel());
     }
 
+    private void showFont(Object selectedNode, TreePath path)
+    {
+        COSName fontName = getNodeKey(selectedNode);
+        COSDictionary resourceDic = (COSDictionary) getUnderneathObject(path.getParentPath().getParentPath().getLastPathComponent());
+
+        FontEncodingPaneController fontEncodingPaneController = new FontEncodingPaneController(fontName,
resourceDic);
+        jSplitPane1.setRightComponent(fontEncodingPaneController.getPane());
+    }
+
     private COSName getNodeKey(Object selectedNode)
     {
         if (selectedNode instanceof MapEntry)

Added: pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/pdfdebugger/fontencodingpane/FontEncodingPaneController.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/pdfdebugger/fontencodingpane/FontEncodingPaneController.java?rev=1692711&view=auto
==============================================================================
--- pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/pdfdebugger/fontencodingpane/FontEncodingPaneController.java
(added)
+++ pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/pdfdebugger/fontencodingpane/FontEncodingPaneController.java
Sun Jul 26 10:54:11 2015
@@ -0,0 +1,78 @@
+/*
+ * Copyright 2015 The Apache Software Foundation.
+ *
+ * Licensed 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.
+ */
+
+package org.apache.pdfbox.tools.pdfdebugger.fontencodingpane;
+
+import java.io.IOException;
+import javax.swing.JPanel;
+import org.apache.pdfbox.cos.COSDictionary;
+import org.apache.pdfbox.cos.COSName;
+import org.apache.pdfbox.pdmodel.PDResources;
+import org.apache.pdfbox.pdmodel.font.PDCIDFontType2;
+import org.apache.pdfbox.pdmodel.font.PDFont;
+import org.apache.pdfbox.pdmodel.font.PDSimpleFont;
+import org.apache.pdfbox.pdmodel.font.PDType0Font;
+
+interface FontPane
+{
+    JPanel getPanel();
+}
+
+/**
+ * @author Khyrul Bashar
+ * A class that shows the glyph table or CIDToGID map depending on the font type. PDSimple
and
+ * PDType0Font are supported.
+ */
+public class FontEncodingPaneController
+{
+    private FontPane fontPane;
+
+    /**
+     * Constructor.
+     * @param fontName COSName instance, Font name in the fonts dictionary.
+     * @param dictionary COSDictionary instance for resources which resides the font.
+     */
+    public FontEncodingPaneController(COSName fontName, COSDictionary dictionary)
+    {
+        PDResources resources = new PDResources(dictionary);
+        try
+        {
+            PDFont font = resources.getFont(fontName);
+            if (font instanceof PDSimpleFont)
+            {
+                fontPane = new SimpleFont((PDSimpleFont)font);
+            }
+            else if (font instanceof PDType0Font && ((PDType0Font) font).getDescendantFont()
instanceof PDCIDFontType2)
+            {
+                fontPane = new Type0Font((PDCIDFontType2) ((PDType0Font) font).getDescendantFont(),
font);
+            }
+        }
+        catch (IOException e)
+        {
+            e.printStackTrace();
+        }
+    }
+
+
+    public JPanel getPane()
+    {
+        if (fontPane != null)
+        {
+            return fontPane.getPanel();
+        }
+        return null;
+    }
+}

Propchange: pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/pdfdebugger/fontencodingpane/FontEncodingPaneController.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/pdfdebugger/fontencodingpane/FontEncodingView.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/pdfdebugger/fontencodingpane/FontEncodingView.java?rev=1692711&view=auto
==============================================================================
--- pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/pdfdebugger/fontencodingpane/FontEncodingView.java
(added)
+++ pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/pdfdebugger/fontencodingpane/FontEncodingView.java
Sun Jul 26 10:54:11 2015
@@ -0,0 +1,145 @@
+/*
+ * Copyright 2015 The Apache Software Foundation.
+ *
+ * Licensed 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.
+ */
+
+package org.apache.pdfbox.tools.pdfdebugger.fontencodingpane;
+
+import java.awt.Color;
+import java.awt.Component;
+import java.awt.Dimension;
+import java.awt.Font;
+import java.awt.GridBagConstraints;
+import java.awt.GridBagLayout;
+import java.util.Iterator;
+import java.util.Map;
+import javax.swing.JLabel;
+import javax.swing.JPanel;
+import javax.swing.JScrollPane;
+import javax.swing.JTable;
+import javax.swing.SwingConstants;
+import javax.swing.table.TableCellRenderer;
+
+/**
+ * @author Khyrul Bashar
+ * A class that creates the UI for font encoding pane.
+ */
+class FontEncodingView
+{
+    private JPanel panel;
+
+    /**
+     * Constructor.
+     * @param tableData Object[][] instance as table data.
+     * @param headerAttributes Map<String, String> instance which contains info for
showing in header
+     *                         panel. Here keys will be info type.
+     * @param columnNames String array containing the columns name.
+     */
+    FontEncodingView(Object[][] tableData, Map<String, String> headerAttributes, String[]
columnNames)
+    {
+        createView(getHeaderPanel(headerAttributes), getTable(tableData, columnNames));
+    }
+
+    private void createView(JPanel headerPanel, JTable table)
+    {
+        panel = new JPanel(new GridBagLayout());
+        panel.setPreferredSize(new Dimension(300, 500));
+
+        JScrollPane scrollPane = new JScrollPane(table);
+        table.setFillsViewportHeight(true);
+        scrollPane.setAlignmentX(Component.LEFT_ALIGNMENT);
+
+        GridBagConstraints gbc = new GridBagConstraints();
+        gbc.gridx = 0;
+        gbc.gridy = 0;
+        gbc.weighty = 0.05;
+        gbc.fill = GridBagConstraints.HORIZONTAL;
+        gbc.anchor = GridBagConstraints.PAGE_START;
+
+        panel.add(headerPanel, gbc);
+
+        gbc.gridy = 2;
+        gbc.weighty = 0.9;
+        gbc.weightx = 1;
+        gbc.fill = GridBagConstraints.BOTH;
+        gbc.anchor = GridBagConstraints.BELOW_BASELINE;
+
+        panel.add(scrollPane, gbc);
+    }
+
+    private JTable getTable(Object[][] tableData, String[] columnNames)
+    {
+        JTable table = new JTable(tableData, columnNames);
+        table.setRowHeight(40);
+        table.setDefaultRenderer(Object.class, new GlyphCellRenderer());
+        return table;
+    }
+
+    private JPanel getHeaderPanel(Map<String, String> attributes)
+    {
+        JPanel headerPanel = new JPanel(new GridBagLayout());
+
+        if (attributes != null)
+        {
+            Iterator<String> keys = attributes.keySet().iterator();
+            int row = 0;
+            while (keys.hasNext())
+
+            {
+                String key = keys.next();
+                JLabel encodingNameLabel = new JLabel(key + ": " + attributes.get(key));
+                encodingNameLabel.setFont(new Font(Font.MONOSPACED, Font.BOLD, 17));
+
+                GridBagConstraints gbc = new GridBagConstraints();
+                gbc.gridx = 0;
+                gbc.gridy = row++;
+                gbc.weighty = 0.1;
+                gbc.anchor = GridBagConstraints.LINE_START;
+
+                headerPanel.add(encodingNameLabel, gbc);
+
+            }
+        }
+        return headerPanel;
+    }
+
+    JPanel getPanel()
+    {
+        return panel;
+    }
+
+    private final class GlyphCellRenderer implements TableCellRenderer
+    {
+
+        @Override
+        public Component getTableCellRendererComponent(JTable jTable, Object o, boolean b,
boolean b1, int i, int i1)
+        {
+            if (o != null)
+            {
+                JLabel label = new JLabel(o.toString());
+                label.setHorizontalAlignment(SwingConstants.CENTER);
+                label.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 25));
+                if (SimpleFont.NO_GLYPH.equals(o) || ".notdef".equals(o))
+                {
+                    label.setText(o.toString());
+                    label.setForeground(Color.RED);
+                }
+                return label;
+            }
+            return new JLabel();
+        }
+    }
+}
+
+

Propchange: pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/pdfdebugger/fontencodingpane/FontEncodingView.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/pdfdebugger/fontencodingpane/SimpleFont.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/pdfdebugger/fontencodingpane/SimpleFont.java?rev=1692711&view=auto
==============================================================================
--- pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/pdfdebugger/fontencodingpane/SimpleFont.java
(added)
+++ pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/pdfdebugger/fontencodingpane/SimpleFont.java
Sun Jul 26 10:54:11 2015
@@ -0,0 +1,84 @@
+/*
+ * Copyright 2015 The Apache Software Foundation.
+ *
+ * Licensed 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.
+ */
+
+package org.apache.pdfbox.tools.pdfdebugger.fontencodingpane;
+
+import java.io.IOException;
+import java.util.LinkedHashMap;
+import java.util.Map;
+import javax.swing.JPanel;
+import org.apache.pdfbox.pdmodel.font.PDSimpleFont;
+
+/**
+ * @author Khyrul Bashar
+ * A class that shows the glyph table along with unicode characters for SimpleFont.
+ */
+class SimpleFont implements FontPane
+{
+    public static final String NO_GLYPH = "No glyph";
+    private int totalAvailableGlyph = 0;
+    private FontEncodingView view;
+
+    /**
+     * Constructor.
+     * @param font PDSimpleFont instance.
+     * @throws IOException If fails to parse unicode characters.
+     */
+    SimpleFont(PDSimpleFont font) throws IOException
+    {
+        Object[][] tableData = getGlyphs(font);
+
+        Map<String, String> attributes = new LinkedHashMap<String, String>();
+        attributes.put("Encoding", getEncodingName(font));
+        attributes.put("Font", font.getName());
+        attributes.put("Glyph count", Integer.toString(totalAvailableGlyph));
+
+        view = new FontEncodingView(tableData, attributes, new String[] {"Code", "Glyph Name","Unicode
Character"});
+    }
+
+    private Object[][] getGlyphs(PDSimpleFont font) throws IOException
+    {
+        Object[][] glyphs = new Object[256][3];
+
+        for (int index = 0; index <= 255; index++)
+        {
+            glyphs[index][0] = index;
+            if (font.getEncoding().contains(index))
+            {
+                glyphs[index][1] = font.getEncoding().getName(index);
+                glyphs[index][2] = font.toUnicode(index);
+                totalAvailableGlyph++;
+            }
+            else
+            {
+                glyphs[index][1] = NO_GLYPH;
+                glyphs[index][2] = NO_GLYPH;
+            }
+        }
+        return glyphs;
+    }
+
+
+    private String getEncodingName(PDSimpleFont font)
+    {
+        return font.getEncoding().getClass().getSimpleName();
+    }
+
+    public JPanel getPanel()
+    {
+        return view.getPanel();
+    }
+}

Propchange: pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/pdfdebugger/fontencodingpane/SimpleFont.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/pdfdebugger/fontencodingpane/Type0Font.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/pdfdebugger/fontencodingpane/Type0Font.java?rev=1692711&view=auto
==============================================================================
--- pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/pdfdebugger/fontencodingpane/Type0Font.java
(added)
+++ pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/pdfdebugger/fontencodingpane/Type0Font.java
Sun Jul 26 10:54:11 2015
@@ -0,0 +1,104 @@
+/*
+ * Copyright 2015 The Apache Software Foundation.
+ *
+ * Licensed 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.
+ */
+
+package org.apache.pdfbox.tools.pdfdebugger.fontencodingpane;
+
+import java.awt.Dimension;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.LinkedHashMap;
+import java.util.Map;
+import javax.swing.JPanel;
+import org.apache.pdfbox.cos.COSBase;
+import org.apache.pdfbox.cos.COSDictionary;
+import org.apache.pdfbox.cos.COSName;
+import org.apache.pdfbox.cos.COSStream;
+import org.apache.pdfbox.io.IOUtils;
+import org.apache.pdfbox.pdmodel.font.PDCIDFontType2;
+import org.apache.pdfbox.pdmodel.font.PDFont;
+
+/**
+ * @author Khyrul Bashar
+ * A class that shows the CIDToGID table along with unicode characters for Type0Fonts when
descendent
+ * font is of type PDCIDFontType2.
+ */
+class Type0Font implements FontPane
+{
+    FontEncodingView view;
+
+    /**
+     * Constructor.
+     * @param descendantFont PDCIDFontType2 instance.
+     * @param parentFont PDFont instance.
+     * @throws IOException If fails to parse cidtogid map.
+     */
+    Type0Font(PDCIDFontType2 descendantFont, PDFont parentFont) throws IOException
+    {
+        Object[][] cidtogid = readCIDToGIDMap(descendantFont, parentFont);
+        if (cidtogid != null)
+        {
+            Map<String, String> attributes = new LinkedHashMap<String, String>();
+            attributes.put("Font", descendantFont.getName());
+            attributes.put("CID count", Integer.toString(cidtogid.length));
+
+            view = new FontEncodingView(cidtogid, attributes, new String[]{"CID", "GID",
"Unicode Character"});
+        }
+    }
+
+    private Object[][] readCIDToGIDMap(PDCIDFontType2 font, PDFont parentFont) throws IOException
+    {
+        Object[][] cid2gid = null;
+        COSDictionary dict = font.getCOSObject();
+        COSBase map = dict.getDictionaryObject(COSName.CID_TO_GID_MAP);
+        if (map instanceof COSStream)
+        {
+            COSStream stream = (COSStream) map;
+
+            InputStream is = stream.getUnfilteredStream();
+            byte[] mapAsBytes = IOUtils.toByteArray(is);
+            IOUtils.closeQuietly(is);
+            int numberOfInts = mapAsBytes.length / 2;
+            cid2gid = new Object[numberOfInts][3];
+            int offset = 0;
+            for (int index = 0; index < numberOfInts; index++)
+            {
+                int gid = (mapAsBytes[offset] & 0xff) << 8 | mapAsBytes[offset
+ 1] & 0xff;
+                cid2gid[index][0] = index;
+                cid2gid[index][1] = gid;
+                if (gid != 0 && parentFont.toUnicode(index) != null)
+                {
+                    cid2gid[index][2] = parentFont.toUnicode(index);
+                }
+                offset += 2;
+            }
+        }
+        return cid2gid;
+    }
+
+
+
+    @Override
+    public JPanel getPanel()
+    {
+        if (view != null)
+        {
+            return view.getPanel();
+        }
+        JPanel panel = new JPanel();
+        panel.setPreferredSize(new Dimension(300, 500));
+        return panel;
+    }
+}
\ No newline at end of file

Propchange: pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/pdfdebugger/fontencodingpane/Type0Font.java
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message