Author: lehmi
Date: Sun Aug 14 17:24:00 2011
New Revision: 1157569

URL: http://svn.apache.org/viewvc?rev=1157569&view=rev
Log:
PDFBOX-1094: added support for pattern resources

Added:
    
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDPatternResources.java
Modified:
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/cos/COSName.java
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDResources.java

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/cos/COSName.java
URL: 
http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/cos/COSName.java?rev=1157569&r1=1157568&r2=1157569&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/cos/COSName.java 
(original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/cos/COSName.java Sun 
Aug 14 17:24:00 2011
@@ -850,6 +850,10 @@ public final class COSName extends COSBa
     /**
     * A common COSName value.
     */
+    public static final COSName PAINT_TYPE = new COSName( "PaintType" );
+    /**
+    * A common COSName value.
+    */
     public static final COSName PARENT = new COSName( "Parent" );
     /**
      * "ParentTreeNextKey"
@@ -862,6 +866,10 @@ public final class COSName extends COSBa
     /**
     * A common COSName value.
     */
+    public static final COSName PATTERN_TYPE = new COSName( "PatternType" );
+    /**
+    * A common COSName value.
+    */
     public static final COSName PDF_DOC_ENCODING = new COSName( 
"PDFDocEncoding" );
     /**
      * "Pg"
@@ -1049,6 +1057,10 @@ public final class COSName extends COSBa
     /**
      * A common COSName value.
      */
+    public static final COSName TILING_TYPE = new COSName( "TilingType" );
+    /**
+     * A common COSName value.
+     */
     public static final COSName TITLE = new COSName( "Title" );
     /**
      * A common COSName value.
@@ -1145,6 +1157,14 @@ public final class COSName extends COSBa
      */
     public static final COSName XREF = new COSName( "XRef" );
     /**
+     * A common COSName value.
+     */
+    public static final COSName X_STEP = new COSName( "XStep" );
+    /**
+     * A common COSName value.
+     */
+    public static final COSName Y_STEP = new COSName( "YStep" );
+    /**
      * The prefix to a PDF name.
      */
     public static final byte[] NAME_PREFIX = new byte[] { 47  }; // The / 
character

Added: 
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDPatternResources.java
URL: 
http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDPatternResources.java?rev=1157569&view=auto
==============================================================================
--- 
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDPatternResources.java
 (added)
+++ 
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDPatternResources.java
 Sun Aug 14 17:24:00 2011
@@ -0,0 +1,319 @@
+/*
+ * 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.pdmodel;
+
+
+import java.awt.geom.AffineTransform;
+
+import org.apache.pdfbox.cos.COSArray;
+import org.apache.pdfbox.cos.COSBase;
+import org.apache.pdfbox.cos.COSDictionary;
+import org.apache.pdfbox.cos.COSFloat;
+import org.apache.pdfbox.cos.COSName;
+import org.apache.pdfbox.cos.COSNumber;
+import org.apache.pdfbox.pdmodel.common.COSObjectable;
+import org.apache.pdfbox.pdmodel.common.PDRectangle;
+import org.apache.pdfbox.util.Matrix;
+
+/**
+ * This represents the resources for a pattern colorspace.
+ *
+ * @version $Revision: 1.0 $
+ */
+public class PDPatternResources implements COSObjectable
+{
+    private COSDictionary patternDictionary;
+
+    /**
+     * Default constructor.
+     */
+    public PDPatternResources()
+    {
+        patternDictionary = new COSDictionary();
+    }
+
+    /**
+     * Prepopulated pattern resources.
+     *
+     * @param resourceDictionary The cos dictionary for this pattern resource.
+     */
+    public PDPatternResources( COSDictionary resourceDictionary )
+    {
+        patternDictionary = resourceDictionary;
+    }
+
+    /**
+     * This will get the underlying dictionary.
+     *
+     * @return The dictionary for these pattern resources.
+     */
+    public COSDictionary getCOSDictionary()
+    {
+        return patternDictionary;
+    }
+
+    /**
+     * Convert this standard java object to a COS object.
+     *
+     * @return The cos object that matches this Java object.
+     */
+    public COSBase getCOSObject()
+    {
+        return patternDictionary;
+    }
+
+    /**
+     * Sets the filter entry of the encryption dictionary.
+     *
+     * @param filter The filter name.
+     */
+    public void setFilter(String filter)
+    {
+        patternDictionary.setItem( COSName.FILTER, COSName.getPDFName( filter 
) );
+    }
+
+    /**
+     * Get the name of the filter.
+     *
+     * @return The filter name contained in this encryption dictionary.
+     */
+    public String getFilter()
+    {
+        return patternDictionary.getNameAsString( COSName.FILTER );
+    }
+
+    /**
+     * This will set the length of the content stream.
+     *
+     * @param length The new stream length.
+     */
+    public void setLength(int length)
+    {
+        patternDictionary.setInt(COSName.LENGTH, length);
+    }
+
+    /**
+     * This will return the length of the content stream.
+     *
+     * @return The length of the content stream
+     */
+    public int getLength()
+    {
+        return patternDictionary.getInt( COSName.LENGTH, 0 );
+    }
+
+    /**
+     * This will set the paint type.
+     *
+     * @param paintType The new paint type.
+     */
+    public void setPaintType(int paintType)
+    {
+        patternDictionary.setInt(COSName.PAINT_TYPE, paintType);
+    }
+
+    /**
+     * This will return the paint type.
+     *
+     * @return The paint type
+     */
+    public int getPaintType()
+    {
+        return patternDictionary.getInt( COSName.PAINT_TYPE, 0 );
+    }
+
+    /**
+     * This will set the pattern type.
+     *
+     * @param patternType The new pattern type.
+     */
+    public void setPatternType(int patternType)
+    {
+        patternDictionary.setInt(COSName.PATTERN_TYPE, patternType);
+    }
+
+    /**
+     * This will return the pattern type.
+     *
+     * @return The pattern type
+     */
+    public int getPatternType()
+    {
+        return patternDictionary.getInt( COSName.PATTERN_TYPE, 0 );
+    }
+    
+    /**
+     * This will set the tiling type.
+     *
+     * @param tilingType The new tiling type.
+     */
+    public void setTilingType(int tilingType)
+    {
+        patternDictionary.setInt(COSName.TILING_TYPE, tilingType);
+    }
+
+    /**
+     * This will return the tiling type.
+     *
+     * @return The tiling type
+     */
+    public int getTilingType()
+    {
+        return patternDictionary.getInt( COSName.TILING_TYPE, 0 );
+    }
+
+    /**
+     * This will set the XStep value.
+     *
+     * @param xStep The new XStep value.
+     */
+    public void setXStep(int xStep)
+    {
+        patternDictionary.setInt(COSName.X_STEP, xStep);
+    }
+
+    /**
+     * This will return the XStep value.
+     *
+     * @return The XStep value
+     */
+    public int getXStep()
+    {
+        return patternDictionary.getInt( COSName.X_STEP, 0 );
+    }
+
+    /**
+     * This will set the YStep value.
+     *
+     * @param yStep The new YStep value.
+     */
+    public void setYStep(int yStep)
+    {
+        patternDictionary.setInt(COSName.Y_STEP, yStep);
+    }
+
+    /**
+     * This will return the YStep value.
+     *
+     * @return The YStep value
+     */
+    public int getYStep()
+    {
+        return patternDictionary.getInt( COSName.Y_STEP, 0 );
+    }
+
+    /**
+     * This will get the resources for this pattern.
+     * This will return null if no resources are available at this level.
+     *
+     * @return The resources for this pattern.
+     */
+    public PDResources getResources()
+    {
+        PDResources retval = null;
+        COSDictionary resources = 
(COSDictionary)patternDictionary.getDictionaryObject( COSName.RESOURCES );
+        if( resources != null )
+        {
+            retval = new PDResources( resources );
+        }
+        return retval;
+    }
+
+    /**
+     * This will set the resources for this pattern.
+     *
+     * @param resources The new resources for this pattern.
+     */
+    public void setResources( PDResources resources )
+    {
+        patternDictionary.setItem( COSName.RESOURCES, resources );
+    }
+
+    /**
+     * An array of four numbers in the form coordinate system (see
+     * below), giving the coordinates of the left, bottom, right, and top 
edges,
+     * respectively, of the pattern's bounding box.
+     *
+     * @return The BBox of the form.
+     */
+    public PDRectangle getBBox()
+    {
+        PDRectangle retval = null;
+        COSArray array = (COSArray)patternDictionary.getDictionaryObject( 
COSName.BBOX );
+        if( array != null )
+        {
+            retval = new PDRectangle( array );
+        }
+        return retval;
+    }
+
+    /**
+     * This will set the BBox (bounding box) for this Pattern.
+     *
+     * @param bbox The new BBox for this Pattern.
+     */
+    public void setBBox(PDRectangle bbox)
+    {
+        if( bbox == null )
+        {
+            patternDictionary.removeItem( COSName.BBOX );
+        }
+        else
+        {
+            patternDictionary.setItem( COSName.BBOX, bbox.getCOSArray() );
+        }
+    }
+
+    /**
+     * This will get the optional Matrix of a Pattern.
+     * It maps the form space into the user space
+     * @return the form matrix
+     */
+    public Matrix getMatrix()
+    {
+        Matrix retval = null;
+        COSArray array = (COSArray)patternDictionary.getDictionaryObject( 
COSName.MATRIX );
+        if( array != null )
+        {
+            retval = new Matrix();
+            retval.setValue(0, 0, ((COSNumber) array.get(0)).floatValue());
+            retval.setValue(0, 1, ((COSNumber) array.get(1)).floatValue());
+            retval.setValue(1, 0, ((COSNumber) array.get(2)).floatValue());
+            retval.setValue(1, 1, ((COSNumber) array.get(3)).floatValue());
+            retval.setValue(2, 0, ((COSNumber) array.get(4)).floatValue());
+            retval.setValue(2, 1, ((COSNumber) array.get(5)).floatValue());
+        }
+        return retval;
+    }
+
+    /**
+     * Sets the optional Matrix entry for the Pattern.
+     * @param transform the transformation matrix
+     */
+    public void setMatrix(AffineTransform transform)
+    {
+        COSArray matrix = new COSArray();
+        double[] values = new double[6];
+        transform.getMatrix(values);
+        for (double v : values)
+        {
+            matrix.add(new COSFloat((float)v));
+        }
+        patternDictionary.setItem(COSName.MATRIX, matrix);
+    }
+
+}

Modified: 
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDResources.java
URL: 
http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDResources.java?rev=1157569&r1=1157568&r2=1157569&view=diff
==============================================================================
--- 
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDResources.java 
(original)
+++ 
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDResources.java 
Sun Aug 14 17:24:00 2011
@@ -27,8 +27,10 @@ import org.apache.pdfbox.cos.COSName;
 import org.apache.pdfbox.cos.COSStream;
 import org.apache.pdfbox.pdmodel.common.COSDictionaryMap;
 import org.apache.pdfbox.pdmodel.common.COSObjectable;
+import org.apache.pdfbox.pdmodel.font.PDFont;
 import org.apache.pdfbox.pdmodel.font.PDFontFactory;
 import org.apache.pdfbox.pdmodel.graphics.PDExtendedGraphicsState;
+import org.apache.pdfbox.pdmodel.graphics.color.PDColorSpace;
 import org.apache.pdfbox.pdmodel.graphics.color.PDColorSpaceFactory;
 import org.apache.pdfbox.pdmodel.graphics.xobject.PDXObject;
 import org.apache.pdfbox.pdmodel.graphics.xobject.PDXObjectImage;
@@ -91,9 +93,9 @@ public class PDResources implements COSO
      *
      * @throws IOException If there is an error getting the fonts.
      */
-    public Map getFonts( Map fontCache ) throws IOException
+    public Map<String,PDFont> getFonts( Map<String,PDFont> fontCache ) throws 
IOException
     {
-        Map retval = null;
+        Map<String,PDFont> retval = null;
         COSDictionary fonts = (COSDictionary)resources.getDictionaryObject( 
COSName.FONT );
 
         if( fonts == null )
@@ -102,7 +104,7 @@ public class PDResources implements COSO
             resources.setItem( COSName.FONT, fonts );
         }
 
-        Map actuals = new HashMap();
+        Map<String,PDFont> actuals = new HashMap<String,PDFont>();
         retval = new COSDictionaryMap( actuals, fonts );
         for( COSName fontName : fonts.keySet() )
         {
@@ -126,7 +128,7 @@ public class PDResources implements COSO
      *
      * @throws IOException If there is an error getting the fonts.
      */
-    public Map getFonts() throws IOException
+    public Map<String,PDFont> getFonts() throws IOException
     {
         return getFonts( null );
     }
@@ -138,9 +140,9 @@ public class PDResources implements COSO
      *
      * @throws IOException If there is an error creating the xobjects.
      */
-    public Map getXObjects() throws IOException
+    public Map<String,PDXObject> getXObjects() throws IOException
     {
-        Map retval = null;
+        Map<String,PDXObject> retval = null;
         COSDictionary xobjects = (COSDictionary)resources.getDictionaryObject( 
COSName.XOBJECT );
 
         if( xobjects == null )
@@ -149,7 +151,7 @@ public class PDResources implements COSO
             resources.setItem( COSName.XOBJECT, xobjects );
         }
 
-        Map actuals = new HashMap();
+        Map<String,PDXObject> actuals = new HashMap<String,PDXObject>();
         retval = new COSDictionaryMap( actuals, xobjects );
         for( COSName objName : xobjects.keySet() )
         {
@@ -173,9 +175,9 @@ public class PDResources implements COSO
      * @return The map of images.
      * @throws IOException If there is an error writing the picture.
      */
-    public Map getImages() throws IOException
+    public Map<String,PDXObjectImage> getImages() throws IOException
     {
-        Map retval = null;
+        Map<String,PDXObjectImage> retval = null;
         COSDictionary images = (COSDictionary)resources.getDictionaryObject( 
COSName.XOBJECT );
 
         if( images == null )
@@ -184,7 +186,7 @@ public class PDResources implements COSO
             resources.setItem( COSName.XOBJECT, images );
         }
 
-        Map actuals = new HashMap();
+        Map<String,PDXObjectImage> actuals = new 
HashMap<String,PDXObjectImage>();
         retval = new COSDictionaryMap( actuals, images );
         for( COSName imageName : images.keySet() )
         {
@@ -208,7 +210,7 @@ public class PDResources implements COSO
      *
      * @param fonts The new map of fonts.
      */
-    public void setFonts( Map fonts )
+    public void setFonts( Map<String,PDFont> fonts )
     {
         resources.setItem( COSName.FONT, COSDictionaryMap.convert( fonts ) );
     }
@@ -222,14 +224,14 @@ public class PDResources implements COSO
      *
      * @throws IOException If there is an error getting the colorspaces.
      */
-    public Map getColorSpaces() throws IOException
+    public Map<String,PDColorSpace> getColorSpaces() throws IOException
     {
-        Map retval = null;
-        COSDictionary colorspaces = 
(COSDictionary)resources.getDictionaryObject( COSName.getPDFName( "ColorSpace" 
) );
+        Map<String,PDColorSpace> retval = null;
+        COSDictionary colorspaces = 
(COSDictionary)resources.getDictionaryObject( COSName.COLORSPACE );
 
         if( colorspaces != null )
         {
-            Map actuals = new HashMap();
+            Map<String,PDColorSpace> actuals = new 
HashMap<String,PDColorSpace>();
             retval = new COSDictionaryMap( actuals, colorspaces );
             for( COSName csName : colorspaces.keySet() )
             {
@@ -245,7 +247,7 @@ public class PDResources implements COSO
      *
      * @param colorspaces The new map of colorspaces.
      */
-    public void setColorSpaces( Map colorspaces )
+    public void setColorSpaces( Map<String,PDColorSpace> colorspaces )
     {
         resources.setItem( COSName.COLORSPACE, COSDictionaryMap.convert( 
colorspaces ) );
     }
@@ -257,14 +259,14 @@ public class PDResources implements COSO
      *
      * @return The map of extended graphic state objects.
      */
-    public Map getGraphicsStates()
+    public Map<String,PDExtendedGraphicsState> getGraphicsStates()
     {
-        Map retval = null;
+        Map<String,PDExtendedGraphicsState> retval = null;
         COSDictionary states = (COSDictionary)resources.getDictionaryObject( 
COSName.EXT_G_STATE );
 
         if( states != null )
         {
-            Map actuals = new HashMap();
+            Map<String,PDExtendedGraphicsState> actuals = new 
HashMap<String,PDExtendedGraphicsState>();
             retval = new COSDictionaryMap( actuals, states );
             for( COSName name : states.keySet() )
             {
@@ -280,14 +282,14 @@ public class PDResources implements COSO
      *
      * @param states The new map of states.
      */
-    public void setGraphicsStates( Map states )
+    public void setGraphicsStates( Map<String,PDExtendedGraphicsState> states )
     {
-        Iterator iter = states.keySet().iterator();
+        Iterator<String> iter = states.keySet().iterator();
         COSDictionary dic = new COSDictionary();
         while( iter.hasNext() )
         {
             String name = (String)iter.next();
-            PDExtendedGraphicsState state = 
(PDExtendedGraphicsState)states.get( name );
+            PDExtendedGraphicsState state = states.get( name );
             dic.setItem( COSName.getPDFName( name ), state.getCOSObject() );
         }
         resources.setItem( COSName.EXT_G_STATE, dic );
@@ -319,4 +321,48 @@ public class PDResources implements COSO
     {
         resources.setItem(COSName.PROPERTIES, props.getCOSObject());
     }
+
+    /**
+     * This will get the map of patterns.  This will return null if the 
underlying
+     * resources dictionary does not have a patterns dictionary. The keys are 
the pattern
+     * name as a String and the values are PDPatternResources objects.
+     *
+     * @return The map of pattern resources objects.
+     */
+    public Map<String,PDPatternResources> getPatterns()
+    {
+        Map<String,PDPatternResources> retval = null;
+        COSDictionary patterns = (COSDictionary)resources.getDictionaryObject( 
COSName.PATTERN );
+
+        if( patterns != null )
+        {
+            Map<String,PDPatternResources> actuals = new 
HashMap<String,PDPatternResources>();
+            retval = new COSDictionaryMap( actuals, patterns );
+            for( COSName name : patterns.keySet() )
+            {
+                COSDictionary dictionary = 
(COSDictionary)patterns.getDictionaryObject( name );
+                actuals.put( name.getName(), new PDPatternResources( 
dictionary ) );
+            }
+        }
+        return retval;
+    }
+
+    /**
+     * This will set the map of patterns.
+     *
+     * @param patterns The new map of patterns.
+     */
+    public void setPatterns( Map<String,PDPatternResources> patterns )
+    {
+        Iterator<String> iter = patterns.keySet().iterator();
+        COSDictionary dic = new COSDictionary();
+        while( iter.hasNext() )
+        {
+            String name = iter.next();
+            PDPatternResources state = patterns.get( name );
+            dic.setItem( COSName.getPDFName( name ), state.getCOSObject() );
+        }
+        resources.setItem( COSName.PATTERN, dic );
+    }
+
 }


Reply via email to