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 2011/08/19 08:14:05 UTC
svn commit: r1159510 - in
/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel: ./
graphics/pattern/
Author: lehmi
Date: Fri Aug 19 06:14:05 2011
New Revision: 1159510
URL: http://svn.apache.org/viewvc?rev=1159510&view=rev
Log:
PDFBOX-1094: extented support for pattern resources
Added:
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDResources.java
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/pattern/
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/pattern/PDPatternResources.java
- copied, changed from r1157573, pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDPatternResources.java
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/pattern/PDShadingPatternResources.java (with props)
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/pattern/PDTilingPatternResources.java (with props)
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/pattern/package.html (with props)
Removed:
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDPatternResources.java
Added: 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=1159510&view=auto
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDResources.java (added)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDResources.java Fri Aug 19 06:14:05 2011
@@ -0,0 +1,371 @@
+/*
+ * 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.io.IOException;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
+
+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.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.pattern.PDPatternResources;
+import org.apache.pdfbox.pdmodel.graphics.xobject.PDXObject;
+import org.apache.pdfbox.pdmodel.graphics.xobject.PDXObjectImage;
+import org.apache.pdfbox.pdmodel.markedcontent.PDPropertyList;
+
+/**
+ * This represents a set of resources available at the page/pages/stream level.
+ *
+ * @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a>
+ * @version $Revision: 1.16 $
+ */
+public class PDResources implements COSObjectable
+{
+ private COSDictionary resources;
+
+ /**
+ * Default constructor.
+ */
+ public PDResources()
+ {
+ resources = new COSDictionary();
+ }
+
+ /**
+ * Prepopulated resources.
+ *
+ * @param resourceDictionary The cos dictionary for this resource.
+ */
+ public PDResources( COSDictionary resourceDictionary )
+ {
+ resources = resourceDictionary;
+ }
+
+ /**
+ * This will get the underlying dictionary.
+ *
+ * @return The dictionary for these resources.
+ */
+ public COSDictionary getCOSDictionary()
+ {
+ return resources;
+ }
+
+ /**
+ * Convert this standard java object to a COS object.
+ *
+ * @return The cos object that matches this Java object.
+ */
+ public COSBase getCOSObject()
+ {
+ return resources;
+ }
+
+ /**
+ * This will get the map of fonts. This will never return null. The keys are string
+ * and the values are PDFont objects.
+ *
+ * @param fontCache A map of existing PDFont objects to reuse.
+ * @return The map of fonts.
+ *
+ * @throws IOException If there is an error getting the fonts.
+ */
+ public Map<String,PDFont> getFonts( Map<String,PDFont> fontCache ) throws IOException
+ {
+ Map<String,PDFont> retval = null;
+ COSDictionary fonts = (COSDictionary)resources.getDictionaryObject( COSName.FONT );
+
+ if( fonts == null )
+ {
+ fonts = new COSDictionary();
+ resources.setItem( COSName.FONT, fonts );
+ }
+
+ Map<String,PDFont> actuals = new HashMap<String,PDFont>();
+ retval = new COSDictionaryMap( actuals, fonts );
+ for( COSName fontName : fonts.keySet() )
+ {
+ COSBase font = fonts.getDictionaryObject( fontName );
+ //data-000174.pdf contains a font that is a COSArray, looks to be an error in the
+ //PDF, we will just ignore entries that are not dictionaries.
+ if( font instanceof COSDictionary )
+ {
+ COSDictionary fontDictionary = (COSDictionary)font;
+ actuals.put( fontName.getName(), PDFontFactory.createFont( fontDictionary, fontCache ));
+ }
+ }
+ return retval;
+ }
+
+ /**
+ * This will get the map of fonts. This will never return null. The keys are string
+ * and the values are PDFont objects.
+ *
+ * @return The map of fonts.
+ *
+ * @throws IOException If there is an error getting the fonts.
+ */
+ public Map<String,PDFont> getFonts() throws IOException
+ {
+ return getFonts( null );
+ }
+
+ /**
+ * This will get the map of PDXObjects that are in the resource dictionary.
+ *
+ * @return The map of xobjects.
+ *
+ * @throws IOException If there is an error creating the xobjects.
+ */
+ public Map<String,PDXObject> getXObjects() throws IOException
+ {
+ Map<String,PDXObject> retval = null;
+ COSDictionary xobjects = (COSDictionary)resources.getDictionaryObject( COSName.XOBJECT );
+
+ if( xobjects == null )
+ {
+ xobjects = new COSDictionary();
+ resources.setItem( COSName.XOBJECT, xobjects );
+ }
+
+ Map<String,PDXObject> actuals = new HashMap<String,PDXObject>();
+ retval = new COSDictionaryMap( actuals, xobjects );
+ for( COSName objName : xobjects.keySet() )
+ {
+ COSBase cosObject = xobjects.getDictionaryObject(objName);
+ PDXObject xobject = PDXObject.createXObject( cosObject );
+ if( xobject !=null )
+ {
+ actuals.put( objName.getName(), xobject);
+ }
+ }
+ return retval;
+ }
+
+ /**
+ * This will get the map of images. An empty map will be returned if there
+ * are no underlying images.
+ * So far the keys are COSName of the image
+ * and the value is the corresponding PDXObjectImage.
+ *
+ * @author By BM
+ * @return The map of images.
+ * @throws IOException If there is an error writing the picture.
+ */
+ public Map<String,PDXObjectImage> getImages() throws IOException
+ {
+ Map<String,PDXObjectImage> retval = null;
+ COSDictionary images = (COSDictionary)resources.getDictionaryObject( COSName.XOBJECT );
+
+ if( images == null )
+ {
+ images = new COSDictionary();
+ resources.setItem( COSName.XOBJECT, images );
+ }
+
+ Map<String,PDXObjectImage> actuals = new HashMap<String,PDXObjectImage>();
+ retval = new COSDictionaryMap( actuals, images );
+ for( COSName imageName : images.keySet() )
+ {
+ COSStream image = (COSStream)(images.getDictionaryObject(imageName));
+
+ COSName subType =(COSName)image.getDictionaryObject(COSName.SUBTYPE);
+ if( subType.equals(COSName.IMAGE) )
+ {
+ PDXObjectImage ximage = (PDXObjectImage)PDXObject.createXObject( image );
+ if( ximage !=null )
+ {
+ actuals.put( imageName.getName(), ximage);
+ }
+ }
+ }
+ return retval;
+ }
+
+ /**
+ * This will set the map of fonts.
+ *
+ * @param fonts The new map of fonts.
+ */
+ public void setFonts( Map<String,PDFont> fonts )
+ {
+ resources.setItem( COSName.FONT, COSDictionaryMap.convert( fonts ) );
+ }
+
+ /**
+ * This will get the map of colorspaces. This will return null if the underlying
+ * resources dictionary does not have a colorspace dictionary. The keys are string
+ * and the values are PDColorSpace objects.
+ *
+ * @return The map of colorspaces.
+ *
+ * @throws IOException If there is an error getting the colorspaces.
+ */
+ public Map<String,PDColorSpace> getColorSpaces() throws IOException
+ {
+ Map<String,PDColorSpace> retval = null;
+ COSDictionary colorspaces = (COSDictionary)resources.getDictionaryObject( COSName.COLORSPACE );
+
+ if( colorspaces != null )
+ {
+ Map<String,PDColorSpace> actuals = new HashMap<String,PDColorSpace>();
+ retval = new COSDictionaryMap( actuals, colorspaces );
+ for( COSName csName : colorspaces.keySet() )
+ {
+ COSBase cs = colorspaces.getDictionaryObject( csName );
+ actuals.put( csName.getName(), PDColorSpaceFactory.createColorSpace( cs ) );
+ }
+ }
+ return retval;
+ }
+
+ /**
+ * This will set the map of colorspaces.
+ *
+ * @param colorspaces The new map of colorspaces.
+ */
+ public void setColorSpaces( Map<String,PDColorSpace> colorspaces )
+ {
+ resources.setItem( COSName.COLORSPACE, COSDictionaryMap.convert( colorspaces ) );
+ }
+
+ /**
+ * This will get the map of graphic states. This will return null if the underlying
+ * resources dictionary does not have a graphics dictionary. The keys are the graphic state
+ * name as a String and the values are PDExtendedGraphicsState objects.
+ *
+ * @return The map of extended graphic state objects.
+ */
+ public Map<String,PDExtendedGraphicsState> getGraphicsStates()
+ {
+ Map<String,PDExtendedGraphicsState> retval = null;
+ COSDictionary states = (COSDictionary)resources.getDictionaryObject( COSName.EXT_G_STATE );
+
+ if( states != null )
+ {
+ Map<String,PDExtendedGraphicsState> actuals = new HashMap<String,PDExtendedGraphicsState>();
+ retval = new COSDictionaryMap( actuals, states );
+ for( COSName name : states.keySet() )
+ {
+ COSDictionary dictionary = (COSDictionary)states.getDictionaryObject( name );
+ actuals.put( name.getName(), new PDExtendedGraphicsState( dictionary ) );
+ }
+ }
+ return retval;
+ }
+
+ /**
+ * This will set the map of graphics states.
+ *
+ * @param states The new map of states.
+ */
+ public void setGraphicsStates( Map<String,PDExtendedGraphicsState> states )
+ {
+ Iterator<String> iter = states.keySet().iterator();
+ COSDictionary dic = new COSDictionary();
+ while( iter.hasNext() )
+ {
+ String name = (String)iter.next();
+ PDExtendedGraphicsState state = states.get( name );
+ dic.setItem( COSName.getPDFName( name ), state.getCOSObject() );
+ }
+ resources.setItem( COSName.EXT_G_STATE, dic );
+ }
+
+ /**
+ * Returns the dictionary mapping resource names to property list dictionaries for marked
+ * content.
+ * @return the property list
+ */
+ public PDPropertyList getProperties()
+ {
+ PDPropertyList retval = null;
+ COSDictionary props = (COSDictionary)resources.getDictionaryObject(COSName.PROPERTIES);
+
+ if (props != null)
+ {
+ retval = new PDPropertyList(props);
+ }
+ return retval;
+ }
+
+ /**
+ * Sets the dictionary mapping resource names to property list dictionaries for marked
+ * content.
+ * @param props the property list
+ */
+ public void setProperties(PDPropertyList props)
+ {
+ 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.
+ *
+ * @throws IOException If there is an error getting the pattern resources.
+ */
+ public Map<String,PDPatternResources> getPatterns() throws IOException
+ {
+ 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(), PDPatternResources.create( 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 );
+ }
+
+}
Copied: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/pattern/PDPatternResources.java (from r1157573, 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/graphics/pattern/PDPatternResources.java?p2=pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/pattern/PDPatternResources.java&p1=pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDPatternResources.java&r1=1157573&r2=1159510&rev=1159510&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDPatternResources.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/pattern/PDPatternResources.java Fri Aug 19 06:14:05 2011
@@ -14,42 +14,43 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-package org.apache.pdfbox.pdmodel;
+package org.apache.pdfbox.pdmodel.graphics.pattern;
-import java.awt.geom.AffineTransform;
+import java.io.IOException;
-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;
+import org.apache.pdfbox.pdmodel.graphics.pattern.PDShadingPatternResources;
+import org.apache.pdfbox.pdmodel.graphics.pattern.PDTilingPatternResources;
/**
* This represents the resources for a pattern colorspace.
*
* @version $Revision: 1.0 $
*/
-public class PDPatternResources implements COSObjectable
+public abstract class PDPatternResources implements COSObjectable
{
private COSDictionary patternDictionary;
+ public static final int TILING_PATTERN = 1;
+ public static final int SHADING_PATTERN = 2;
+
/**
* Default constructor.
*/
public PDPatternResources()
{
patternDictionary = new COSDictionary();
+ patternDictionary.setName(COSName.TYPE, COSName.PATTERN.getName());
}
/**
* Prepopulated pattern resources.
*
- * @param resourceDictionary The cos dictionary for this pattern resource.
+ * @param resourceDictionary The COSDictionary for this pattern resource.
*/
public PDPatternResources( COSDictionary resourceDictionary )
{
@@ -129,11 +130,11 @@ public class PDPatternResources implemen
/**
* This will return the paint type.
*
- * @return The paint type
+ * @return The type of object that this is.
*/
- public int getPaintType()
+ public String getType()
{
- return patternDictionary.getInt( COSName.PAINT_TYPE, 0 );
+ return COSName.PATTERN.getName();
}
/**
@@ -151,169 +152,33 @@ public class PDPatternResources implemen
*
* @return The pattern type
*/
- public int getPatternType()
- {
- return patternDictionary.getInt( COSName.PATTERN_TYPE, 0 );
- }
+ public abstract int getPatternType();
/**
- * 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));
+ * Create the correct PD Model pattern based on the COS base pattern.
+ *
+ * @param resourceDictionary the COS pattern dictionary
+ *
+ * @return the newly created pattern resources object
+ *
+ * @throws IOException If we are unable to create the PDPattern object.
+ */
+ public static PDPatternResources create(COSDictionary resourceDictionary) throws IOException
+ {
+ PDPatternResources pattern = null;
+ int patternType = resourceDictionary.getInt( COSName.PATTERN_TYPE, 0 );
+ switch (patternType)
+ {
+ case TILING_PATTERN:
+ pattern = new PDTilingPatternResources(resourceDictionary);
+ break;
+ case SHADING_PATTERN:
+ pattern = new PDShadingPatternResources(resourceDictionary);
+ break;
+ default:
+ throw new IOException( "Error: Unknown pattern type " + patternType );
}
- patternDictionary.setItem(COSName.MATRIX, matrix);
+ return pattern;
}
-
+
}
Added: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/pattern/PDShadingPatternResources.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/pattern/PDShadingPatternResources.java?rev=1159510&view=auto
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/pattern/PDShadingPatternResources.java (added)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/pattern/PDShadingPatternResources.java Fri Aug 19 06:14:05 2011
@@ -0,0 +1,146 @@
+/*
+ * 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.graphics.pattern;
+
+
+import java.awt.geom.AffineTransform;
+
+import org.apache.pdfbox.cos.COSArray;
+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.graphics.PDExtendedGraphicsState;
+import org.apache.pdfbox.pdmodel.graphics.pattern.PDPatternResources;
+import org.apache.pdfbox.util.Matrix;
+
+/**
+ * This represents the resources for a shading pattern.
+ *
+ * @version $Revision: 1.0 $
+ */
+public class PDShadingPatternResources extends PDPatternResources
+{
+ private COSDictionary patternDictionary;
+ private PDExtendedGraphicsState extendedGraphicsState;
+ private COSArray matrix = null;
+
+ /**
+ * Default constructor.
+ */
+ public PDShadingPatternResources()
+ {
+ super();
+ patternDictionary.setInt(COSName.PATTERN_TYPE, PDPatternResources.SHADING_PATTERN);
+ }
+
+ /**
+ * Prepopulated pattern resources.
+ *
+ * @param resourceDictionary The COSDictionary for this pattern resource.
+ */
+ public PDShadingPatternResources( COSDictionary resourceDictionary )
+ {
+ patternDictionary = resourceDictionary;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public int getPatternType()
+ {
+ return PDPatternResources.SHADING_PATTERN;
+ }
+
+ /**
+ * 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 returnMatrix = null;
+ if (matrix == null)
+ {
+ matrix = (COSArray)patternDictionary.getDictionaryObject( COSName.MATRIX );
+ }
+ if( matrix != null )
+ {
+ returnMatrix = new Matrix();
+ returnMatrix.setValue(0, 0, ((COSNumber) matrix.get(0)).floatValue());
+ returnMatrix.setValue(0, 1, ((COSNumber) matrix.get(1)).floatValue());
+ returnMatrix.setValue(1, 0, ((COSNumber) matrix.get(2)).floatValue());
+ returnMatrix.setValue(1, 1, ((COSNumber) matrix.get(3)).floatValue());
+ returnMatrix.setValue(2, 0, ((COSNumber) matrix.get(4)).floatValue());
+ returnMatrix.setValue(2, 1, ((COSNumber) matrix.get(5)).floatValue());
+ }
+ return returnMatrix;
+ }
+
+ /**
+ * Sets the optional Matrix entry for the Pattern.
+ * @param transform the transformation matrix
+ */
+ public void setMatrix(AffineTransform transform)
+ {
+ 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);
+ }
+
+ /**
+ * This will get the extended graphics state for this pattern.
+ *
+ * @return The extended graphics state for this pattern.
+ */
+ public PDExtendedGraphicsState getExtendedGraphicsState()
+ {
+ if (extendedGraphicsState == null)
+ {
+ COSDictionary dictionary = (COSDictionary)patternDictionary.getDictionaryObject( COSName.EXT_G_STATE );
+ if( dictionary != null )
+ {
+ extendedGraphicsState = new PDExtendedGraphicsState( dictionary );
+ }
+ }
+ return extendedGraphicsState;
+ }
+
+ /**
+ * This will set the extended graphics state for this pattern.
+ *
+ * @param extendedGraphicsState The new extended graphics state for this pattern.
+ */
+ public void setExtendedGraphicsState( PDExtendedGraphicsState extendedGraphicsState )
+ {
+ this.extendedGraphicsState = extendedGraphicsState;
+ if (extendedGraphicsState != null)
+ {
+ patternDictionary.setItem( COSName.EXT_G_STATE, extendedGraphicsState );
+ }
+ else
+ {
+ patternDictionary.removeItem(COSName.EXT_G_STATE);
+ }
+ }
+
+}
Propchange: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/pattern/PDShadingPatternResources.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/pattern/PDTilingPatternResources.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/pattern/PDTilingPatternResources.java?rev=1159510&view=auto
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/pattern/PDTilingPatternResources.java (added)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/pattern/PDTilingPatternResources.java Fri Aug 19 06:14:05 2011
@@ -0,0 +1,275 @@
+/*
+ * 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.graphics.pattern;
+
+
+import java.awt.geom.AffineTransform;
+
+import org.apache.pdfbox.cos.COSArray;
+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.graphics.pattern.PDPatternResources;
+import org.apache.pdfbox.pdmodel.PDResources;
+import org.apache.pdfbox.pdmodel.common.PDRectangle;
+import org.apache.pdfbox.util.Matrix;
+
+/**
+ * This represents the resources for a tiling pattern.
+ *
+ * @version $Revision: 1.0 $
+ */
+public class PDTilingPatternResources extends PDPatternResources
+{
+ private COSDictionary patternDictionary;
+
+ /**
+ * Default constructor.
+ */
+ public PDTilingPatternResources()
+ {
+ super();
+ patternDictionary.setInt(COSName.PATTERN_TYPE, PDPatternResources.TILING_PATTERN);
+ }
+
+ /**
+ * Prepopulated pattern resources.
+ *
+ * @param resourceDictionary The COSDictionary for this pattern resource.
+ */
+ public PDTilingPatternResources( COSDictionary resourceDictionary )
+ {
+ patternDictionary = resourceDictionary;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public int getPatternType()
+ {
+ return PDPatternResources.TILING_PATTERN;
+ }
+
+ /**
+ * 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 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 )
+ {
+ if (resources != null)
+ {
+ patternDictionary.setItem( COSName.RESOURCES, resources );
+ }
+ else
+ {
+ patternDictionary.removeItem( COSName.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);
+ }
+
+}
Propchange: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/pattern/PDTilingPatternResources.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/pattern/package.html
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/pattern/package.html?rev=1159510&view=auto
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/pattern/package.html (added)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/pattern/package.html Fri Aug 19 06:14:05 2011
@@ -0,0 +1,25 @@
+<!--
+ ! 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.
+ !-->
+<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
+<html>
+<head>
+
+</head>
+<body>
+This package deals with patterns which are used instead of colors.
+</body>
+</html>
Propchange: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/pattern/package.html
------------------------------------------------------------------------------
svn:eol-style = native