KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > render > txt > TXTState


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: TXTState.java 426576 2006-07-28 15:44:37Z jeremias $ */
19
20 package org.apache.fop.render.txt;
21
22 import java.awt.Point JavaDoc;
23 import java.awt.geom.Rectangle2D JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.LinkedList JavaDoc;
26
27 import org.apache.fop.area.CTM;
28
29 /**
30  * This keeps information about the current state when writing to txt, i.e.
31  * manages coordinate transformation matrices for getting absolute coordinates.
32  */

33 public class TXTState {
34
35     /** Keeps all coordinate transformation matrices during rendering. */
36     private LinkedList JavaDoc stackCTM = new LinkedList JavaDoc();
37
38     /**
39      * Current result coordinate transformation matrix. It's product of
40      * all matrices in order, saved in <code>stackCTM</code>.
41      */

42     private CTM resultCTM = new CTM();
43
44     /**
45      * Constructs a newly allocated <code>TXTState</code> object.
46      */

47     public TXTState() {
48     }
49
50     /**
51      * Updates result coordinate transformation matrix
52      * (i.e. <code>resultCTM</code>), multipliing it by given matrix.
53      *
54      * @param ctm CTM
55      */

56     private void updateResultCTM(CTM ctm) {
57         resultCTM = resultCTM.multiply(ctm);
58     }
59
60     /**
61      * Recalculate current result coordinate transformation matrix.
62      */

63     private void calcResultCTM() {
64         resultCTM = new CTM();
65         for (Iterator JavaDoc i = stackCTM.iterator(); i.hasNext();) {
66             updateResultCTM((CTM) i.next());
67         }
68     }
69
70     /**
71      * Push the current coordinate transformation matrix onto the stack and
72      * reevaluate <code>resultCTM</code>.
73      *
74      * @param ctm instance of CTM
75      */

76     public void push(CTM ctm) {
77         stackCTM.addLast(ctm);
78         updateResultCTM(ctm);
79     }
80
81     /**
82      * Pop the coordinate transformation matrix from the stack and reevaluate
83      * <code>resultCTM</code>.
84      */

85     public void pop() {
86         stackCTM.removeLast();
87         calcResultCTM();
88     }
89     
90     /**
91      * Modifies coordinate transformation matrix in such a way, so
92      * x-shift and y-shift will be transformed in text positions.
93      *
94      * @param ctm CTM to modify
95      * @return instance of CTM
96      */

97     public CTM refineCTM(CTM ctm) {
98         double[] da = ctm.toArray();
99         // refine x-shift
100
da[4] = Helper.roundPosition((int) da[4], TXTRenderer.CHAR_WIDTH);
101         // refine y-shift
102
da[5] = Helper.roundPosition((int) da[5], TXTRenderer.CHAR_HEIGHT);
103         
104         return new CTM(da[0], da[1], da[2], da[3], da[4], da[5]);
105     }
106
107     /**
108      * Transforms <code>point</code> using <code>ctm</code>.
109      *
110      * @param p Point
111      * @param ctm CTM
112      * @return transformed Point
113      */

114     public Point JavaDoc transformPoint(Point JavaDoc p, CTM ctm) {
115         Rectangle2D JavaDoc r = new Rectangle2D.Double JavaDoc(p.x, p.y, 0, 0);
116         CTM nctm = refineCTM(ctm);
117         r = nctm.transform(r);
118         return new Point JavaDoc((int) r.getX(), (int) r.getY());
119     }
120
121     /**
122      * Transforms point (x, y) using <code>resultCTM</code>.
123      *
124      * @param x x-coordinate
125      * @param y y-coordinate
126      * @return transformed Point
127      */

128     public Point JavaDoc transformPoint(int x, int y) {
129         return transformPoint(new Point JavaDoc(x, y), resultCTM);
130     }
131
132     /**
133      * @return current result coordinate transformation matrix
134      */

135     public CTM getResultCTM() {
136         return resultCTM;
137     }
138 }
139
Popular Tags