001: /*
002: Copyright © 2007 Stefano Chizzolini. http://clown.stefanochizzolini.it
003:
004: Contributors:
005: * Stefano Chizzolini (original code developer, http://www.stefanochizzolini.it):
006: contributed code is Copyright © 2007 by Stefano Chizzolini.
007:
008: This file should be part of the source code distribution of "PDF Clown library"
009: (the Program): see the accompanying README files for more info.
010:
011: This Program is free software; you can redistribute it and/or modify it under
012: the terms of the GNU General Public License as published by the Free Software
013: Foundation; either version 2 of the License, or (at your option) any later version.
014:
015: This Program is distributed in the hope that it will be useful, but WITHOUT ANY
016: WARRANTY, either expressed or implied; without even the implied warranty of
017: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the License for more details.
018:
019: You should have received a copy of the GNU General Public License along with this
020: Program (see README files); if not, go to the GNU website (http://www.gnu.org/).
021:
022: Redistribution and use, with or without modification, are permitted provided that such
023: redistributions retain the above copyright notice, license and disclaimer, along with
024: this list of conditions.
025: */
026:
027: package it.stefanochizzolini.clown.documents.contents.objects;
028:
029: import it.stefanochizzolini.clown.objects.IPdfNumber;
030: import it.stefanochizzolini.clown.objects.PdfDirectObject;
031: import it.stefanochizzolini.clown.objects.PdfReal;
032: import it.stefanochizzolini.clown.util.math.SquareMatrix;
033:
034: import java.util.Arrays;
035: import java.util.List;
036:
037: /**
038: 'Modify the current transformation matrix (CTM) by concatenating the specified matrix' operation
039: [PDF:1.6:4.3.3].
040:
041: @author Stefano Chizzolini
042: @version 0.0.4, 06/17/07
043: @since 0.0.4
044: */
045: public final class ModifyCTM extends Operation {
046: // <class>
047: // <static>
048: // <fields>
049: public static final String Operator = "cm";
050:
051: // </fields>
052:
053: // <interface>
054: // <public>
055: public static ModifyCTM getResetCTM(double[] ctm) {
056: double[][] inverseMatrixData = new SquareMatrix(new double[][] {
057: { ctm[0], ctm[1], 0 }, { ctm[2], ctm[3], 0 },
058: { ctm[4], ctm[5], 1 } }).getInverse().getData();
059:
060: return new ModifyCTM(inverseMatrixData[0][0],
061: inverseMatrixData[0][1], inverseMatrixData[1][0],
062: inverseMatrixData[1][1], inverseMatrixData[2][0],
063: inverseMatrixData[2][1]);
064: }
065:
066: // </static>
067:
068: // <dynamic>
069: // <constructors>
070: public ModifyCTM(double a, double b, double c, double d, double e,
071: double f) {
072: super (
073: Operator,
074: (List<PdfDirectObject>) (List<? extends PdfDirectObject>) Arrays
075: .asList(new PdfReal(a), new PdfReal(b),
076: new PdfReal(c), new PdfReal(d),
077: new PdfReal(e), new PdfReal(f)));
078: }
079:
080: public ModifyCTM(List<PdfDirectObject> operands) {
081: super (Operator, operands);
082: }
083:
084: // </constructors>
085:
086: // <interface>
087: // <public>
088: /**
089: Applies the transformation to the specified transformation matrix by concatenation.
090: @param ctm Current Transformation Matrix.
091: @return Transformed transformation matrix.
092: */
093: public double[] applyTo(double[] ctm) {
094: // Get the new transformation matrix!
095: double[] ntm = getMatrix();
096: // Initialize the resulting transformation matrix!
097: double[] rtm = new double[6];
098: // Apply the transformation to the existing transformation matrix!
099: rtm[0] = ntm[0] * ctm[0] + ntm[1] * ctm[2]; // a.
100: rtm[1] = ntm[0] * ctm[1] + ntm[1] * ctm[3]; // b.
101: rtm[2] = ntm[2] * ctm[0] + ntm[3] * ctm[2]; // c.
102: rtm[3] = ntm[2] * ctm[1] + ntm[3] * ctm[3]; // d.
103: rtm[4] = ntm[4] * ctm[0] + ntm[5] * ctm[2] + 1 * ctm[4]; // e.
104: rtm[5] = ntm[4] * ctm[1] + ntm[5] * ctm[3] + 1 * ctm[5]; // f.
105:
106: return rtm;
107: }
108:
109: public double[] getMatrix() {
110: return new double[] {
111: ((IPdfNumber) operands.get(0)).getNumberValue(), // a.
112: ((IPdfNumber) operands.get(1)).getNumberValue(), // b.
113: ((IPdfNumber) operands.get(2)).getNumberValue(), // c.
114: ((IPdfNumber) operands.get(3)).getNumberValue(), // d.
115: ((IPdfNumber) operands.get(4)).getNumberValue(), // e.
116: ((IPdfNumber) operands.get(5)).getNumberValue() // f.
117: };
118: }
119: // </public>
120: // </interface>
121: // </dynamic>
122: // </class>
123: }
|