pdfbox-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From til...@apache.org
Subject svn commit: r1684038 - in /pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools: PDFDebugger.java pdfdebugger/colorpane/ pdfdebugger/colorpane/CSSeparation.java
Date Sun, 07 Jun 2015 15:22:05 GMT
Author: tilman
Date: Sun Jun  7 15:22:05 2015
New Revision: 1684038

URL: http://svn.apache.org/r1684038
Log:
PDFBOX-2530: show separation colorspace in pane, as done by Khyrul Bashar in GSoC2015

Added:
    pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/pdfdebugger/colorpane/
    pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/pdfdebugger/colorpane/CSSeparation.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=1684038&r1=1684037&r2=1684038&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 Jun  7 15:22:05
2015
@@ -21,6 +21,7 @@ import java.awt.Dimension;
 import java.awt.event.ActionEvent;
 import java.awt.event.WindowAdapter;
 import java.awt.event.WindowEvent;
+import java.util.Arrays;
 import java.util.List;
 import javax.swing.AbstractAction;
 import javax.swing.Action;
@@ -33,6 +34,10 @@ import javax.swing.UIManager;
 import javax.swing.border.BevelBorder;
 import javax.swing.event.TreeSelectionEvent;
 import javax.swing.event.TreeSelectionListener;
+import javax.swing.tree.TreeModel;
+import javax.swing.tree.TreePath;
+import org.apache.pdfbox.cos.COSArray;
+import org.apache.pdfbox.cos.COSBase;
 import org.apache.pdfbox.tools.gui.PDFTreeModel;
 import org.apache.pdfbox.tools.gui.PDFTreeCellRenderer;
 import org.apache.pdfbox.tools.gui.ArrayEntry;
@@ -48,13 +53,11 @@ import org.apache.pdfbox.cos.COSNull;
 import org.apache.pdfbox.cos.COSStream;
 import org.apache.pdfbox.cos.COSString;
 
-import javax.swing.tree.TreeModel;
-import javax.swing.tree.TreePath;
-
 import java.io.ByteArrayOutputStream;
 import java.io.File;
 import java.io.InputStream;
 import java.io.IOException;
+import org.apache.pdfbox.tools.pdfdebugger.colorpane.CSSeparation;
 import org.apache.pdfbox.tools.util.FileOpenSaveDialog;
 import org.apache.pdfbox.tools.pdfdebugger.ui.Tree;
 import org.apache.pdfbox.tools.pdfdebugger.treestatus.TreeStatus;
@@ -69,11 +72,13 @@ import org.apache.pdfbox.tools.util.Rece
 public class PDFDebugger extends javax.swing.JFrame
 {
     private TreeStatusPane statusPane;
+    private RecentFiles recentFiles;
 
     private PDDocument document = null;
     private String currentFilePath = null;
 
-    private RecentFiles recentFiles;
+    List<COSName> specialColorSpaces =
+            Arrays.asList(COSName.INDEXED, COSName.SEPARATION, COSName.DEVICEN);
 
     private static final String PASSWORD = "-password";
 
@@ -268,14 +273,23 @@ public class PDFDebugger extends javax.s
             try
             {
                 Object selectedNode = path.getLastPathComponent();
-                String data=convertToString(selectedNode);
+                if (isSpecialColorSpace(selectedNode))
+                {
+                    showColorPane(selectedNode);
+                    return;
+                }
+                if (!jSplitPane1.getRightComponent().equals(jScrollPane2))
+                {
+                    jSplitPane1.setRightComponent(jScrollPane2);
+                }
+                String data = convertToString(selectedNode);
                 if (data != null)
                 {
                     jTextPane1.setText(data);
                 }
                 else
                 {
-                    jTextPane1.setText( "" );
+                    jTextPane1.setText("");
                 }
             }
             catch (Exception e)
@@ -285,6 +299,68 @@ public class PDFDebugger extends javax.s
         }
     }//GEN-LAST:event_jTree1ValueChanged
 
+    private boolean isSpecialColorSpace(Object selectedNode)
+    {
+        if (selectedNode instanceof MapEntry)
+        {
+            selectedNode = ((MapEntry) selectedNode).getValue();
+        }
+        else if (selectedNode instanceof ArrayEntry)
+        {
+            selectedNode = ((ArrayEntry) selectedNode).getValue();
+        }
+
+        if (selectedNode instanceof COSArray)
+        {
+            COSBase arrayEntry = ((COSArray)selectedNode).get(0);
+            if (arrayEntry instanceof COSName)
+            {
+                COSName name = (COSName) arrayEntry;
+                return specialColorSpaces.contains(name);
+            }
+        }
+        return false;
+    }
+
+    /**
+     * Show a Panel describing the special color spaces in more detail and interactive way.
+     * For now only Separation Color space is shown.
+     * @param csNode the special color space containing node.
+     */
+    //TODO implement DeviceN and Indexed color spaces related features
+    private void showColorPane(Object csNode)
+    {
+        if (csNode instanceof MapEntry)
+        {
+            csNode = ((MapEntry) csNode).getValue();
+        }
+        else if (csNode instanceof ArrayEntry)
+        {
+            csNode = ((ArrayEntry) csNode).getValue();
+        }
+
+        if (csNode instanceof COSArray)
+        {
+            COSArray array = (COSArray)csNode;
+            COSBase arrayEntry = array.get(0);
+            if (arrayEntry instanceof COSName)
+            {
+                COSName csName = (COSName) arrayEntry;
+                if (csName.equals(COSName.SEPARATION))
+                {
+                    jSplitPane1.setRightComponent(new CSSeparation(array).getPanel());
+                }
+                else
+                {
+                    if (!jSplitPane1.getRightComponent().equals(jScrollPane2))
+                    {
+                        jSplitPane1.setRightComponent(jScrollPane2);
+                    }
+                }
+            }
+        }
+    }
+
     private String convertToString( Object selectedNode )
     {
         String data = null;

Added: pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/pdfdebugger/colorpane/CSSeparation.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/pdfdebugger/colorpane/CSSeparation.java?rev=1684038&view=auto
==============================================================================
--- pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/pdfdebugger/colorpane/CSSeparation.java
(added)
+++ pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/pdfdebugger/colorpane/CSSeparation.java
Sun Jun  7 15:22:05 2015
@@ -0,0 +1,260 @@
+/*
+ * 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.
+ */
+package org.apache.pdfbox.tools.pdfdebugger.colorpane;
+
+import java.awt.Color;
+import java.awt.Dimension;
+import java.awt.Font;
+import java.awt.GridBagConstraints;
+import java.awt.GridBagLayout;
+import java.awt.Insets;
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
+import java.io.IOException;
+import java.util.Dictionary;
+import java.util.Hashtable;
+import javax.swing.JLabel;
+import javax.swing.JPanel;
+import javax.swing.JSlider;
+import javax.swing.JTextField;
+import javax.swing.border.BevelBorder;
+import javax.swing.event.ChangeEvent;
+import javax.swing.event.ChangeListener;
+import org.apache.pdfbox.cos.COSArray;
+import org.apache.pdfbox.pdmodel.graphics.color.PDSeparation;
+
+/**
+ * @author Khyrul Bashar.
+ */
+
+/**
+ *A class that provides the necessary UI and functionalities to show the Separation color
space.
+ */
+public class CSSeparation implements ChangeListener, ActionListener
+{
+    private JSlider slider;
+    private JTextField tintField;
+    private JLabel colorBar;
+    private JPanel panel;
+
+    private PDSeparation separation;
+    private float tintValue = 1;
+
+    /**
+     * Constructor
+     * @param array COSArray instance of the separation color space.
+     */
+    public CSSeparation(COSArray array)
+    {
+        try
+        {
+            separation = new PDSeparation(array);
+        }
+        catch (IOException e)
+        {
+            throw new RuntimeException(e);
+        }
+        initUI();
+        initValues();
+    }
+
+    /**
+     * initialize all the UI elements and arrange them.
+     */
+    private void initUI()
+    {
+        Font boldFont = new Font("Monospaced", Font.BOLD, 20);
+
+        GridBagConstraints gbc = new GridBagConstraints();
+        gbc.insets = new Insets(2, 2, 2, 2);
+        
+        JPanel inputPanel = new JPanel(new GridBagLayout());
+
+        slider = new JSlider(0, 100, 50);
+        slider.setMajorTickSpacing(50);
+        slider.setPaintTicks(true);
+
+        Dictionary labelTable = new Hashtable();
+        JLabel lightest = new JLabel("lightest");
+        lightest.setFont(new Font("Monospaced", Font.BOLD, 10));
+        JLabel darkest = new JLabel("darkest");
+        darkest.setFont(new Font("Monospaced", Font.BOLD, 10));
+        JLabel midPoint = new JLabel("0.5");
+        midPoint.setFont(new Font("Monospaced", Font.BOLD, 10));
+        labelTable.put(0, lightest);
+        labelTable.put(50, midPoint);
+        labelTable.put(100, darkest);
+
+        slider.setPaintLabels(true);
+        slider.setLabelTable(labelTable);
+        slider.addChangeListener(this);
+
+        gbc.gridx = 0;
+        gbc.gridy = 0;
+        gbc.gridwidth = 10;
+        gbc.weightx = 1;
+        gbc.fill = GridBagConstraints.HORIZONTAL;
+        inputPanel.add(slider, gbc);
+
+        JLabel tintLabel = new JLabel("Tint Value:");
+        tintLabel.setFont(boldFont);
+        gbc.gridx = 0;
+        gbc.gridy = 1;
+        gbc.weightx = 0.5;
+        gbc.gridwidth = 1;
+        gbc.fill = GridBagConstraints.HORIZONTAL;
+        inputPanel.add(tintLabel, gbc);
+
+        tintField = new JTextField();
+        tintField.addActionListener(this);
+        tintField.setPreferredSize(new Dimension(10, 30));
+        gbc.gridx = 1;
+        gbc.weightx = 0.5;
+        gbc.gridwidth = 1;
+        gbc.fill = GridBagConstraints.HORIZONTAL;
+        inputPanel.add(tintField, gbc);
+
+        JPanel contentPanel = new JPanel(new GridBagLayout());
+        GridBagConstraints gbc2 = new GridBagConstraints();
+        gbc2.gridx = 0;
+        gbc2.gridy = 0;
+        gbc2.gridwidth = 2;
+        gbc2.weightx = 0.3;
+        gbc2.weighty = 1;
+        gbc2.fill = GridBagConstraints.HORIZONTAL;
+        contentPanel.add(inputPanel, gbc2);
+
+        colorBar = new JLabel();
+        colorBar.setOpaque(true);
+        gbc2.gridx = 2;
+        gbc2.weightx = 0.7;
+        gbc2.gridwidth = 4;
+        gbc2.gridheight = 2;
+        gbc2.fill = GridBagConstraints.BOTH;
+        contentPanel.add(colorBar, gbc2);
+        setColorBarBorder();
+
+        panel = new JPanel(new GridBagLayout());
+
+        JLabel colorantNameLabel = new JLabel("Colorant: " + separation.getColorantName());
+        colorantNameLabel.setFont(boldFont);
+
+        GridBagConstraints maingbc = new GridBagConstraints();
+        maingbc.gridx = 0;
+        maingbc.gridy = 0;
+        maingbc.weightx = 1;
+        maingbc.weighty = 0.03;
+        maingbc.anchor = GridBagConstraints.FIRST_LINE_START;
+        panel.add(colorantNameLabel, maingbc);
+
+        maingbc.gridx = 0;
+        maingbc.gridy = 1;
+        maingbc.weighty = 0.97;
+        maingbc.gridwidth = 10;
+        maingbc.fill = GridBagConstraints.HORIZONTAL;
+        panel.add(contentPanel, maingbc);
+
+    }
+
+    private void initValues()
+    {
+        slider.setValue(getIntRepresentation(tintValue));
+        tintField.setText(Float.toString(tintValue));
+    }
+
+    /**
+     * return the main panel that hold all the UI elements.
+     * @return JPanel instance
+     */
+    public JPanel getPanel()
+    {
+        return panel;
+    }
+
+    /**
+     * input changed in slider.
+     * @param changeEvent
+     */
+    @Override
+    public void stateChanged(ChangeEvent changeEvent)
+    {
+            int value = slider.getValue();
+            tintValue = getFloatRepresentation(value);
+            tintField.setText(Float.toString(tintValue));
+            updateColorBar();
+    }
+
+    /**
+     * input changed in text field.
+     * @param actionEvent
+     */
+    @Override
+    public void actionPerformed(ActionEvent actionEvent)
+    {
+        String input = tintField.getText();
+        try
+        {
+            tintValue= Float.parseFloat(input);
+            slider.setValue(getIntRepresentation(tintValue));
+            updateColorBar();
+        }
+        catch (NumberFormatException e)
+        {
+            tintField.setText(Float.toString(tintValue));
+        }
+    }
+
+    private void updateColorBar()
+    {
+        try
+        {
+            float[] rgbValues = separation.toRGB(new float[] {tintValue});
+            colorBar.setBackground(new Color(rgbValues[0], rgbValues[1], rgbValues[2]));
+        }
+        catch (IOException e)
+        {
+            throw new RuntimeException(e);
+        }
+    }
+
+    /**
+     * Set a little border around colorbar. color of the border is the darkest of the colorant.
+     */
+    private void setColorBarBorder()
+    {
+        try
+        {
+            float[] rgbValues = separation.toRGB(new float[] {1});
+            Color darkest= new Color(rgbValues[0], rgbValues[1], rgbValues[2]);
+            colorBar.setBorder(new BevelBorder(BevelBorder.LOWERED, darkest, darkest));
+        }
+        catch (IOException e)
+        {
+           throw new RuntimeException(e);
+        }
+    }
+
+    private float getFloatRepresentation(int value)
+    {
+        return (float) value/100;
+    }
+
+    private int getIntRepresentation(float value)
+    {
+        return (int) (value*100);
+    }
+}

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



Mime
View raw message