001: /**
002: * Copyright (c) 2005, www.pdfbox.org
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions are met:
007: *
008: * 1. Redistributions of source code must retain the above copyright notice,
009: * this list of conditions and the following disclaimer.
010: * 2. Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: * 3. Neither the name of pdfbox; nor the names of its
014: * contributors may be used to endorse or promote products derived from this
015: * software without specific prior written permission.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
018: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
019: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
020: * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
021: * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
022: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
023: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
024: * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
025: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
026: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
027: *
028: * http://www.pdfbox.org
029: *
030: */package org.pdfbox.util.operator.pagedrawer;
031:
032: import java.awt.Dimension;
033: import java.awt.Graphics2D;
034: import java.awt.geom.AffineTransform;
035: import java.awt.image.BufferedImage;
036: import java.io.IOException;
037: import java.util.List;
038: import java.util.Map;
039:
040: import org.pdfbox.cos.COSName;
041: import org.pdfbox.cos.COSStream;
042: import org.pdfbox.pdfviewer.PageDrawer;
043: import org.pdfbox.pdmodel.PDPage;
044: import org.pdfbox.pdmodel.PDResources;
045: import org.pdfbox.pdmodel.graphics.xobject.PDXObject;
046: import org.pdfbox.pdmodel.graphics.xobject.PDXObjectForm;
047: import org.pdfbox.pdmodel.graphics.xobject.PDXObjectImage;
048: import org.pdfbox.util.Matrix;
049: import org.pdfbox.util.PDFOperator;
050: import org.pdfbox.util.operator.OperatorProcessor;
051:
052: /**
053: * Implementation of content stream operator for page drawer.
054: *
055: * @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a>
056: * @version $Revision: 1.4 $
057: */
058: public class Invoke extends OperatorProcessor {
059: /**
060: * process : re : append rectangle to path.
061: * @param operator The operator that is being executed.
062: * @param arguments List
063: * @throws IOException If there is an error invoking the sub object.
064: */
065: public void process(PDFOperator operator, List arguments)
066: throws IOException {
067: PageDrawer drawer = (PageDrawer) context;
068: PDPage page = drawer.getPage();
069: Dimension pageSize = drawer.getPageSize();
070: Graphics2D graphics = drawer.getGraphics();
071: COSName objectName = (COSName) arguments.get(0);
072: Map xobjects = drawer.getResources().getXObjects();
073: PDXObject xobject = (PDXObject) xobjects.get(objectName
074: .getName());
075: if (xobject instanceof PDXObjectImage) {
076: PDXObjectImage image = (PDXObjectImage) xobject;
077: try {
078: BufferedImage awtImage = image.getRGBImage();
079: Matrix ctm = drawer.getGraphicsState()
080: .getCurrentTransformationMatrix();
081:
082: int width = awtImage.getWidth();
083: int height = awtImage.getHeight();
084:
085: double rotationInRadians = (page.findRotation() * Math.PI) / 180;
086:
087: AffineTransform rotation = new AffineTransform();
088: rotation.setToRotation(rotationInRadians);
089: AffineTransform rotationInverse = rotation
090: .createInverse();
091: Matrix rotationInverseMatrix = new Matrix();
092: rotationInverseMatrix
093: .setFromAffineTransform(rotationInverse);
094: Matrix rotationMatrix = new Matrix();
095: rotationMatrix.setFromAffineTransform(rotation);
096:
097: Matrix unrotatedCTM = ctm
098: .multiply(rotationInverseMatrix);
099:
100: Matrix scalingParams = unrotatedCTM.extractScaling();
101: Matrix scalingMatrix = Matrix.getScaleInstance(
102: 1f / width, 1f / height);
103: scalingParams = scalingParams.multiply(scalingMatrix);
104:
105: Matrix translationParams = unrotatedCTM
106: .extractTranslating();
107: Matrix translationMatrix = null;
108: int pageRotation = page.findRotation();
109: if (pageRotation == 0) {
110: translationParams.setValue(2, 1, -translationParams
111: .getValue(2, 1));
112: translationMatrix = Matrix.getTranslatingInstance(
113: 0, (float) pageSize.getHeight() - height
114: * scalingParams.getYScale());
115: } else if (pageRotation == 90) {
116: translationMatrix = Matrix.getTranslatingInstance(
117: 0, (float) pageSize.getHeight());
118: } else {
119: //TODO need to figure out other cases
120: }
121: translationParams = translationParams
122: .multiply(translationMatrix);
123:
124: AffineTransform at = new AffineTransform(scalingParams
125: .getValue(0, 0), 0, 0, scalingParams.getValue(
126: 1, 1), translationParams.getValue(2, 0),
127: translationParams.getValue(2, 1));
128:
129: //at.setToTranslation( pageSize.getHeight()-ctm.getValue(2,0),ctm.getValue(2,1) );
130: //at.setToScale( ctm.getValue(0,0)/width, ctm.getValue(1,1)/height);
131: //at.setToRotation( (page.findRotation() * Math.PI)/180 );
132:
133: //AffineTransform rotation = new AffineTransform();
134: //rotation.rotate( (90*Math.PI)/180);
135:
136: /*
137:
138: // The transformation should be done
139: // 1 - Translation
140: // 2 - Rotation
141: // 3 - Scale or Skew
142: AffineTransform at = new AffineTransform();
143:
144: // Translation
145: at = new AffineTransform();
146: //at.setToTranslation((double)ctm.getValue(0,0),
147: // (double)ctm.getValue(0,1));
148:
149: // Rotation
150: //AffineTransform toAdd = new AffineTransform();
151: toAdd.setToRotation(1.5705);
152: toAdd.setToRotation(ctm.getValue(2,0)*(Math.PI/180));
153: at.concatenate(toAdd);
154: */
155:
156: // Scale / Skew?
157: //toAdd.setToScale(1, 1);
158: //at.concatenate(toAdd);
159: graphics.drawImage(awtImage, at, null);
160: } catch (Exception e) {
161: e.printStackTrace();
162: }
163: } else if (xobject instanceof PDXObjectForm) {
164: PDXObjectForm form = (PDXObjectForm) xobject;
165: COSStream invoke = (COSStream) form.getCOSObject();
166: PDResources pdResources = form.getResources();
167: if (pdResources == null) {
168: pdResources = page.findResources();
169: }
170:
171: getContext().processSubStream(page, pdResources, invoke);
172: } else {
173: //unknown xobject type
174: }
175:
176: //invoke named object.
177: }
178: }
|