KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > render > pdf > CTMHelper


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 /* $Id: CTMHelper.java 426576 2006-07-28 15:44:37Z jeremias $ */
19  
20 package org.apache.fop.render.pdf;
21
22 import java.awt.geom.AffineTransform JavaDoc;
23
24 import org.apache.fop.area.CTM;
25 import org.apache.fop.pdf.PDFNumber;
26
27 /**
28  * CTMHelper converts FOP transformation matrices to those
29  * suitable for use by the PDFRenderer. The e and f elements
30  * of the matrix will be divided by 1000 as FOP uses millipoints
31  * as it's default user space and PDF uses points.
32  *
33  * @see org.apache.fop.area.CTM
34  *
35  * @author <a HREF="kevin@rocketred.com>Kevin O'Neill</a>
36  */

37 public final class CTMHelper {
38     /**
39      * <p>Converts the sourceMatrix to a string for use in the PDFRenderer cm operations.</p>
40      * <p>For example:
41      * <pre>
42      * org.apache.fop.area.CTM ctm =
43      * new org.apache.fop.area.CTM(1.0, 0.0, 0.0, 1.0, 1000.0, 1000.0);
44      * String pdfMatrix = org.apache.fop.render.pdf.CTMHelper.toPDFString(ctm);
45      * </pre>
46      * will return the string "<code>1.0 0.0 0.0 1.0 1.0 1.0</code>".
47      *
48      * @param sourceMatrix - The matrix to convert.
49      *
50      * @return a space seperated string containing the matrix elements.
51      */

52     public static String JavaDoc toPDFString(CTM sourceMatrix) {
53         if (null == sourceMatrix) {
54             throw new NullPointerException JavaDoc("sourceMatrix must not be null");
55         }
56
57         final double[] matrix = toPDFArray(sourceMatrix);
58
59         return constructPDFArray(matrix);
60     }
61
62     /**
63      * <p>Converts the AffineTransform instance to a string for use in the PDFRenderer
64      * cm operations.</p>
65      *
66      * @param transform The matrix to convert.
67      * @param convertMillipoints Indicates that the matrix needs to be converted from millipoints
68      * to points.
69      * @return a space seperated string containing the matrix elements.
70      */

71     public static String JavaDoc toPDFString(AffineTransform JavaDoc transform, boolean convertMillipoints) {
72         if (null == transform) {
73             throw new NullPointerException JavaDoc("transform must not be null");
74         }
75
76         final double[] matrix = new double[6];
77         transform.getMatrix(matrix);
78         if (convertMillipoints) {
79             //Convert from millipoints to points
80
matrix[4] /= 1000;
81             matrix[5] /= 1000;
82         }
83
84         return constructPDFArray(matrix);
85     }
86
87     private static String JavaDoc constructPDFArray(double[] matrix) {
88         return PDFNumber.doubleOut(matrix[0], 8) + " "
89                 + PDFNumber.doubleOut(matrix[1], 8) + " "
90                 + PDFNumber.doubleOut(matrix[2], 8) + " "
91                 + PDFNumber.doubleOut(matrix[3], 8) + " "
92                 + PDFNumber.doubleOut(matrix[4], 8) + " "
93                 + PDFNumber.doubleOut(matrix[5], 8);
94     }
95     
96     /**
97      * <p>Creates a new CTM based in the sourceMatrix.</p>
98      * <p>For example:
99      * <pre>
100      * org.apache.fop.area.CTM inCTM =
101      * new org.apache.fop.area.CTM(1.0, 0.0, 0.0, 1.0, 1000.0, 1000.0);
102      * org.apache.fop.area.CTM outCTM =
103      * org.apache.fop.render.pdf.CTMHelper.toPDFCTM(ctm);
104      * </pre>
105      * will return a new CTM where a == 1.0, b == 0.0, c == 0.0, d == 1.0, e == 1.0 and f == 1.0.
106      *
107      * @param sourceMatrix - The matrix to convert.
108      *
109      * @return a new converted matrix.
110      */

111     public static CTM toPDFCTM(CTM sourceMatrix) {
112         if (null == sourceMatrix) {
113             throw new NullPointerException JavaDoc("sourceMatrix must not be null");
114         }
115
116         final double[] matrix = toPDFArray(sourceMatrix);
117
118         return new CTM(matrix[0], matrix[1], matrix[2], matrix[3],
119                        matrix[4], matrix[5]);
120     }
121
122     /**
123      * <p>Creates an array of six doubles from the source CTM.</p>
124      * <p>For example:
125      * <pre>
126      * org.apache.fop.area.CTM inCTM =
127      * new org.apache.fop.area.CTM(1.0, 0.0, 0.0, 1.0, 1000.0, 1000.0);
128      * double matrix[] = org.apache.fop.render.pdf.CTMHelper.toPDFArray(ctm);
129      * </pre>
130      * will return a new array where matrix[0] == 1.0, matrix[1] == 0.0,
131      * matrix[2] == 0.0, matrix[3] == 1.0,
132      * matrix[4] == 1.0 and matrix[5] == 1.0.
133      *
134      * @param sourceMatrix - The matrix to convert.
135      * @return an array of doubles containing the converted matrix.
136      */

137     public static double[] toPDFArray(CTM sourceMatrix) {
138         if (null == sourceMatrix) {
139             throw new NullPointerException JavaDoc("sourceMatrix must not be null");
140         }
141
142         final double[] matrix = sourceMatrix.toArray();
143
144         return new double[]{matrix[0], matrix[1], matrix[2], matrix[3],
145                             matrix[4] / 1000.0, matrix[5] / 1000.0};
146     }
147
148 }
149
150
Popular Tags