KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lowagie > text > pdf > codec > wmf > MetaState


1 /*
2  * $Id: MetaState.java 2366 2006-09-14 23:10:58Z xlv $
3  * $Name$
4  *
5  * Copyright 2001, 2002 Paulo Soares
6  *
7  * The contents of this file are subject to the Mozilla Public License Version 1.1
8  * (the "License"); you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the License.
14  *
15  * The Original Code is 'iText, a free JAVA-PDF library'.
16  *
17  * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
18  * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
19  * All Rights Reserved.
20  * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
21  * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
22  *
23  * Contributor(s): all the names of the contributors are added in the source code
24  * where applicable.
25  *
26  * Alternatively, the contents of this file may be used under the terms of the
27  * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
28  * provisions of LGPL are applicable instead of those above. If you wish to
29  * allow use of your version of this file only under the terms of the LGPL
30  * License and not to allow others to use your version of this file under
31  * the MPL, indicate your decision by deleting the provisions above and
32  * replace them with the notice and other provisions required by the LGPL.
33  * If you do not delete the provisions above, a recipient may use your version
34  * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
35  *
36  * This library is free software; you can redistribute it and/or modify it
37  * under the terms of the MPL as stated above or under the terms of the GNU
38  * Library General Public License as published by the Free Software Foundation;
39  * either version 2 of the License, or any later version.
40  *
41  * This library is distributed in the hope that it will be useful, but WITHOUT
42  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
43  * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
44  * details.
45  *
46  * If you didn't download this code from the following link, you should check if
47  * you aren't using an obsolete version:
48  * http://www.lowagie.com/iText/
49  */

50
51 package com.lowagie.text.pdf.codec.wmf;
52
53 import java.awt.Color JavaDoc;
54 import java.awt.Point JavaDoc;
55 import java.util.ArrayList JavaDoc;
56 import java.util.Stack JavaDoc;
57
58 import com.lowagie.text.pdf.PdfContentByte;
59
60 public class MetaState {
61     
62     public static final int TA_NOUPDATECP = 0;
63     public static final int TA_UPDATECP = 1;
64     public static final int TA_LEFT = 0;
65     public static final int TA_RIGHT = 2;
66     public static final int TA_CENTER = 6;
67     public static final int TA_TOP = 0;
68     public static final int TA_BOTTOM = 8;
69     public static final int TA_BASELINE = 24;
70     
71     public static final int TRANSPARENT = 1;
72     public static final int OPAQUE = 2;
73
74     public static final int ALTERNATE = 1;
75     public static final int WINDING = 2;
76
77     public Stack JavaDoc savedStates;
78     public ArrayList JavaDoc MetaObjects;
79     public Point JavaDoc currentPoint;
80     public MetaPen currentPen;
81     public MetaBrush currentBrush;
82     public MetaFont currentFont;
83     public Color JavaDoc currentBackgroundColor = Color.white;
84     public Color JavaDoc currentTextColor = Color.black;
85     public int backgroundMode = OPAQUE;
86     public int polyFillMode = ALTERNATE;
87     public int lineJoin = 1;
88     public int textAlign;
89     public int offsetWx;
90     public int offsetWy;
91     public int extentWx;
92     public int extentWy;
93     public float scalingX;
94     public float scalingY;
95     
96
97     /** Creates new MetaState */
98     public MetaState() {
99         savedStates = new Stack JavaDoc();
100         MetaObjects = new ArrayList JavaDoc();
101         currentPoint = new Point JavaDoc(0, 0);
102         currentPen = new MetaPen();
103         currentBrush = new MetaBrush();
104         currentFont = new MetaFont();
105     }
106
107     public MetaState(MetaState state) {
108         setMetaState(state);
109     }
110     
111     public void setMetaState(MetaState state) {
112         savedStates = state.savedStates;
113         MetaObjects = state.MetaObjects;
114         currentPoint = state.currentPoint;
115         currentPen = state.currentPen;
116         currentBrush = state.currentBrush;
117         currentFont = state.currentFont;
118         currentBackgroundColor = state.currentBackgroundColor;
119         currentTextColor = state.currentTextColor;
120         backgroundMode = state.backgroundMode;
121         polyFillMode = state.polyFillMode;
122         textAlign = state.textAlign;
123         lineJoin = state.lineJoin;
124         offsetWx = state.offsetWx;
125         offsetWy = state.offsetWy;
126         extentWx = state.extentWx;
127         extentWy = state.extentWy;
128         scalingX = state.scalingX;
129         scalingY = state.scalingY;
130     }
131
132     public void addMetaObject(MetaObject object) {
133         for (int k = 0; k < MetaObjects.size(); ++k) {
134             if (MetaObjects.get(k) == null) {
135                 MetaObjects.set(k, object);
136                 return;
137             }
138         }
139         MetaObjects.add(object);
140     }
141     
142     public void selectMetaObject(int index, PdfContentByte cb) {
143         MetaObject obj = (MetaObject)MetaObjects.get(index);
144         if (obj == null)
145             return;
146         int style;
147         switch (obj.getType()) {
148             case MetaObject.META_BRUSH:
149                 currentBrush = (MetaBrush)obj;
150                 style = currentBrush.getStyle();
151                 if (style == MetaBrush.BS_SOLID) {
152                     Color JavaDoc color = currentBrush.getColor();
153                     cb.setColorFill(color);
154                 }
155                 else if (style == MetaBrush.BS_HATCHED) {
156                     Color JavaDoc color = currentBackgroundColor;
157                     cb.setColorFill(color);
158                 }
159                 break;
160             case MetaObject.META_PEN:
161             {
162                 currentPen = (MetaPen)obj;
163                 style = currentPen.getStyle();
164                 if (style != MetaPen.PS_NULL) {
165                     Color JavaDoc color = currentPen.getColor();
166                     cb.setColorStroke(color);
167                     cb.setLineWidth(Math.abs((float)currentPen.getPenWidth() * scalingX / extentWx));
168                     switch (style) {
169                         case MetaPen.PS_DASH:
170                             cb.setLineDash(18, 6, 0);
171                             break;
172                         case MetaPen.PS_DASHDOT:
173                             cb.setLiteral("[9 6 3 6]0 d\n");
174                             break;
175                         case MetaPen.PS_DASHDOTDOT:
176                             cb.setLiteral("[9 3 3 3 3 3]0 d\n");
177                             break;
178                         case MetaPen.PS_DOT:
179                             cb.setLineDash(3, 0);
180                             break;
181                         default:
182                             cb.setLineDash(0);
183                             break;
184                     }
185                 }
186                 break;
187             }
188             case MetaObject.META_FONT:
189             {
190                 currentFont = (MetaFont)obj;
191                 break;
192             }
193         }
194     }
195     
196     public void deleteMetaObject(int index) {
197         MetaObjects.set(index, null);
198     }
199     
200     public void saveState(PdfContentByte cb) {
201         cb.saveState();
202         MetaState state = new MetaState(this);
203         savedStates.push(state);
204     }
205
206     public void restoreState(int index, PdfContentByte cb) {
207         int pops;
208         if (index < 0)
209             pops = Math.min(-index, savedStates.size());
210         else
211             pops = Math.max(savedStates.size() - index, 0);
212         if (pops == 0)
213             return;
214         MetaState state = null;
215         while (pops-- != 0) {
216             cb.restoreState();
217             state = (MetaState)savedStates.pop();
218         }
219         setMetaState(state);
220     }
221     
222     public void cleanup(PdfContentByte cb) {
223         int k = savedStates.size();
224         while (k-- > 0)
225             cb.restoreState();
226     }
227     
228     public float transformX(int x) {
229         return ((float)x - offsetWx) * scalingX / extentWx;
230     }
231
232     public float transformY(int y) {
233         return (1f - ((float)y - offsetWy) / extentWy) * scalingY;
234     }
235     
236     public void setScalingX(float scalingX) {
237         this.scalingX = scalingX;
238     }
239     
240     public void setScalingY(float scalingY) {
241         this.scalingY = scalingY;
242     }
243     
244     public void setOffsetWx(int offsetWx) {
245         this.offsetWx = offsetWx;
246     }
247     
248     public void setOffsetWy(int offsetWy) {
249         this.offsetWy = offsetWy;
250     }
251     
252     public void setExtentWx(int extentWx) {
253         this.extentWx = extentWx;
254     }
255     
256     public void setExtentWy(int extentWy) {
257         this.extentWy = extentWy;
258     }
259     
260     public float transformAngle(float angle) {
261         float ta = scalingY < 0 ? -angle : angle;
262         return (float)(scalingX < 0 ? Math.PI - ta : ta);
263     }
264     
265     public void setCurrentPoint(Point JavaDoc p) {
266         currentPoint = p;
267     }
268
269     public Point JavaDoc getCurrentPoint() {
270         return currentPoint;
271     }
272     
273     public MetaBrush getCurrentBrush() {
274         return currentBrush;
275     }
276
277     public MetaPen getCurrentPen() {
278         return currentPen;
279     }
280
281     public MetaFont getCurrentFont() {
282         return currentFont;
283     }
284     
285     /** Getter for property currentBackgroundColor.
286      * @return Value of property currentBackgroundColor.
287      */

288     public Color JavaDoc getCurrentBackgroundColor() {
289         return currentBackgroundColor;
290     }
291     
292     /** Setter for property currentBackgroundColor.
293      * @param currentBackgroundColor New value of property currentBackgroundColor.
294      */

295     public void setCurrentBackgroundColor(Color JavaDoc currentBackgroundColor) {
296         this.currentBackgroundColor = currentBackgroundColor;
297     }
298     
299     /** Getter for property currentTextColor.
300      * @return Value of property currentTextColor.
301      */

302     public Color JavaDoc getCurrentTextColor() {
303         return currentTextColor;
304     }
305     
306     /** Setter for property currentTextColor.
307      * @param currentTextColor New value of property currentTextColor.
308      */

309     public void setCurrentTextColor(Color JavaDoc currentTextColor) {
310         this.currentTextColor = currentTextColor;
311     }
312     
313     /** Getter for property backgroundMode.
314      * @return Value of property backgroundMode.
315      */

316     public int getBackgroundMode() {
317         return backgroundMode;
318     }
319     
320     /** Setter for property backgroundMode.
321      * @param backgroundMode New value of property backgroundMode.
322      */

323     public void setBackgroundMode(int backgroundMode) {
324         this.backgroundMode = backgroundMode;
325     }
326     
327     /** Getter for property textAlign.
328      * @return Value of property textAlign.
329      */

330     public int getTextAlign() {
331         return textAlign;
332     }
333     
334     /** Setter for property textAlign.
335      * @param textAlign New value of property textAlign.
336      */

337     public void setTextAlign(int textAlign) {
338         this.textAlign = textAlign;
339     }
340     
341     /** Getter for property polyFillMode.
342      * @return Value of property polyFillMode.
343      */

344     public int getPolyFillMode() {
345         return polyFillMode;
346     }
347     
348     /** Setter for property polyFillMode.
349      * @param polyFillMode New value of property polyFillMode.
350      */

351     public void setPolyFillMode(int polyFillMode) {
352         this.polyFillMode = polyFillMode;
353     }
354     
355     public void setLineJoinRectangle(PdfContentByte cb) {
356         if (lineJoin != 0) {
357             lineJoin = 0;
358             cb.setLineJoin(0);
359         }
360     }
361     
362     public void setLineJoinPolygon(PdfContentByte cb) {
363         if (lineJoin == 0) {
364             lineJoin = 1;
365             cb.setLineJoin(1);
366         }
367     }
368     
369     public boolean getLineNeutral() {
370         return (lineJoin == 0);
371     }
372     
373 }
374
Popular Tags