KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pdfbox > util > operator > pagedrawer > Invoke


1 /**
2  * Copyright (c) 2005, www.pdfbox.org
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  * 3. Neither the name of pdfbox; nor the names of its
14  * contributors may be used to endorse or promote products derived from this
15  * software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
24  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  * http://www.pdfbox.org
29  *
30  */

31 package org.pdfbox.util.operator.pagedrawer;
32
33 import java.awt.Dimension JavaDoc;
34 import java.awt.Graphics2D JavaDoc;
35 import java.awt.geom.AffineTransform JavaDoc;
36 import java.awt.image.BufferedImage JavaDoc;
37 import java.io.IOException JavaDoc;
38 import java.util.List JavaDoc;
39 import java.util.Map JavaDoc;
40
41 import org.pdfbox.cos.COSName;
42 import org.pdfbox.cos.COSStream;
43 import org.pdfbox.pdfviewer.PageDrawer;
44 import org.pdfbox.pdmodel.PDPage;
45 import org.pdfbox.pdmodel.PDResources;
46 import org.pdfbox.pdmodel.graphics.xobject.PDXObject;
47 import org.pdfbox.pdmodel.graphics.xobject.PDXObjectForm;
48 import org.pdfbox.pdmodel.graphics.xobject.PDXObjectImage;
49 import org.pdfbox.util.Matrix;
50 import org.pdfbox.util.PDFOperator;
51 import org.pdfbox.util.operator.OperatorProcessor;
52
53 /**
54  * Implementation of content stream operator for page drawer.
55  *
56  * @author <a HREF="mailto:ben@benlitchfield.com">Ben Litchfield</a>
57  * @version $Revision: 1.4 $
58  */

59 public class Invoke extends OperatorProcessor
60 {
61     /**
62      * process : re : append rectangle to path.
63      * @param operator The operator that is being executed.
64      * @param arguments List
65      * @throws IOException If there is an error invoking the sub object.
66      */

67     public void process(PDFOperator operator, List JavaDoc arguments) throws IOException JavaDoc
68     {
69         PageDrawer drawer = (PageDrawer)context;
70         PDPage page = drawer.getPage();
71         Dimension JavaDoc pageSize = drawer.getPageSize();
72         Graphics2D JavaDoc graphics = drawer.getGraphics();
73         COSName objectName = (COSName)arguments.get( 0 );
74         Map JavaDoc xobjects = drawer.getResources().getXObjects();
75         PDXObject xobject = (PDXObject)xobjects.get( objectName.getName() );
76         if( xobject instanceof PDXObjectImage )
77         {
78             PDXObjectImage image = (PDXObjectImage)xobject;
79             try
80             {
81                 BufferedImage JavaDoc awtImage = image.getRGBImage();
82                 Matrix ctm = drawer.getGraphicsState().getCurrentTransformationMatrix();
83                 
84                 int width = awtImage.getWidth();
85                 int height = awtImage.getHeight();
86
87                 double rotationInRadians =(page.findRotation() * Math.PI)/180;
88                  
89                 
90                 AffineTransform JavaDoc rotation = new AffineTransform JavaDoc();
91                 rotation.setToRotation( rotationInRadians );
92                 AffineTransform JavaDoc rotationInverse = rotation.createInverse();
93                 Matrix rotationInverseMatrix = new Matrix();
94                 rotationInverseMatrix.setFromAffineTransform( rotationInverse );
95                 Matrix rotationMatrix = new Matrix();
96                 rotationMatrix.setFromAffineTransform( rotation );
97                 
98                 Matrix unrotatedCTM = ctm.multiply( rotationInverseMatrix );
99                 
100                 Matrix scalingParams = unrotatedCTM.extractScaling();
101                 Matrix scalingMatrix = Matrix.getScaleInstance(1f/width,1f/height);
102                 scalingParams = scalingParams.multiply( scalingMatrix );
103                 
104                 Matrix translationParams = unrotatedCTM.extractTranslating();
105                 Matrix translationMatrix = null;
106                 int pageRotation = page.findRotation();
107                 if( pageRotation == 0 )
108                 {
109                     translationParams.setValue(2,1, -translationParams.getValue( 2,1 ));
110                     translationMatrix = Matrix.getTranslatingInstance(
111                         0, (float)pageSize.getHeight()-height*scalingParams.getYScale() );
112                 }
113                 else if( pageRotation == 90 )
114                 {
115                     translationMatrix = Matrix.getTranslatingInstance( 0, (float)pageSize.getHeight() );
116                 }
117                 else
118                 {
119                     //TODO need to figure out other cases
120
}
121                 translationParams = translationParams.multiply( translationMatrix );
122
123                 AffineTransform JavaDoc at = new AffineTransform JavaDoc(
124                         scalingParams.getValue( 0,0), 0,
125                         0, scalingParams.getValue( 1, 1),
126                         translationParams.getValue(2,0), translationParams.getValue( 2,1 )
127                     );
128                 
129                 
130                 
131
132                 //at.setToTranslation( pageSize.getHeight()-ctm.getValue(2,0),ctm.getValue(2,1) );
133
//at.setToScale( ctm.getValue(0,0)/width, ctm.getValue(1,1)/height);
134
//at.setToRotation( (page.findRotation() * Math.PI)/180 );
135

136                 
137                 
138                 //AffineTransform rotation = new AffineTransform();
139
//rotation.rotate( (90*Math.PI)/180);
140

141                 /*
142                 
143                 // The transformation should be done
144                 // 1 - Translation
145                 // 2 - Rotation
146                 // 3 - Scale or Skew
147                 AffineTransform at = new AffineTransform();
148
149                 // Translation
150                 at = new AffineTransform();
151                 //at.setToTranslation((double)ctm.getValue(0,0),
152                 // (double)ctm.getValue(0,1));
153                 
154                 // Rotation
155                 //AffineTransform toAdd = new AffineTransform();
156                 toAdd.setToRotation(1.5705);
157                 toAdd.setToRotation(ctm.getValue(2,0)*(Math.PI/180));
158                 at.concatenate(toAdd);
159                 */

160                 
161                 // Scale / Skew?
162
//toAdd.setToScale(1, 1);
163
//at.concatenate(toAdd);
164

165                 graphics.drawImage( awtImage, at, null );
166             }
167             catch( Exception JavaDoc e )
168             {
169                 e.printStackTrace();
170             }
171         }
172         else if(xobject instanceof PDXObjectForm)
173         {
174             PDXObjectForm form = (PDXObjectForm)xobject;
175             COSStream invoke = (COSStream)form.getCOSObject();
176             PDResources pdResources = form.getResources();
177             if(pdResources == null)
178             {
179                 pdResources = page.findResources();
180             }
181
182             getContext().processSubStream( page, pdResources, invoke );
183         }
184         else
185         {
186             //unknown xobject type
187
}
188         
189         
190         //invoke named object.
191
}
192 }
193
Popular Tags