001: /*
002: * $Id: MetaState.java 2366 2006-09-14 23:10:58Z xlv $
003: * $Name$
004: *
005: * Copyright 2001, 2002 Paulo Soares
006: *
007: * The contents of this file are subject to the Mozilla Public License Version 1.1
008: * (the "License"); you may not use this file except in compliance with the License.
009: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
010: *
011: * Software distributed under the License is distributed on an "AS IS" basis,
012: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
013: * for the specific language governing rights and limitations under the License.
014: *
015: * The Original Code is 'iText, a free JAVA-PDF library'.
016: *
017: * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
018: * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
019: * All Rights Reserved.
020: * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
021: * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
022: *
023: * Contributor(s): all the names of the contributors are added in the source code
024: * where applicable.
025: *
026: * Alternatively, the contents of this file may be used under the terms of the
027: * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
028: * provisions of LGPL are applicable instead of those above. If you wish to
029: * allow use of your version of this file only under the terms of the LGPL
030: * License and not to allow others to use your version of this file under
031: * the MPL, indicate your decision by deleting the provisions above and
032: * replace them with the notice and other provisions required by the LGPL.
033: * If you do not delete the provisions above, a recipient may use your version
034: * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
035: *
036: * This library is free software; you can redistribute it and/or modify it
037: * under the terms of the MPL as stated above or under the terms of the GNU
038: * Library General Public License as published by the Free Software Foundation;
039: * either version 2 of the License, or any later version.
040: *
041: * This library is distributed in the hope that it will be useful, but WITHOUT
042: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
043: * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
044: * details.
045: *
046: * If you didn't download this code from the following link, you should check if
047: * you aren't using an obsolete version:
048: * http://www.lowagie.com/iText/
049: */
050:
051: package com.lowagie.text.pdf.codec.wmf;
052:
053: import java.awt.Color;
054: import java.awt.Point;
055: import java.util.ArrayList;
056: import java.util.Stack;
057:
058: import com.lowagie.text.pdf.PdfContentByte;
059:
060: public class MetaState {
061:
062: public static final int TA_NOUPDATECP = 0;
063: public static final int TA_UPDATECP = 1;
064: public static final int TA_LEFT = 0;
065: public static final int TA_RIGHT = 2;
066: public static final int TA_CENTER = 6;
067: public static final int TA_TOP = 0;
068: public static final int TA_BOTTOM = 8;
069: public static final int TA_BASELINE = 24;
070:
071: public static final int TRANSPARENT = 1;
072: public static final int OPAQUE = 2;
073:
074: public static final int ALTERNATE = 1;
075: public static final int WINDING = 2;
076:
077: public Stack savedStates;
078: public ArrayList MetaObjects;
079: public Point currentPoint;
080: public MetaPen currentPen;
081: public MetaBrush currentBrush;
082: public MetaFont currentFont;
083: public Color currentBackgroundColor = Color.white;
084: public Color currentTextColor = Color.black;
085: public int backgroundMode = OPAQUE;
086: public int polyFillMode = ALTERNATE;
087: public int lineJoin = 1;
088: public int textAlign;
089: public int offsetWx;
090: public int offsetWy;
091: public int extentWx;
092: public int extentWy;
093: public float scalingX;
094: public float scalingY;
095:
096: /** Creates new MetaState */
097: public MetaState() {
098: savedStates = new Stack();
099: MetaObjects = new ArrayList();
100: currentPoint = new Point(0, 0);
101: currentPen = new MetaPen();
102: currentBrush = new MetaBrush();
103: currentFont = new MetaFont();
104: }
105:
106: public MetaState(MetaState state) {
107: setMetaState(state);
108: }
109:
110: public void setMetaState(MetaState state) {
111: savedStates = state.savedStates;
112: MetaObjects = state.MetaObjects;
113: currentPoint = state.currentPoint;
114: currentPen = state.currentPen;
115: currentBrush = state.currentBrush;
116: currentFont = state.currentFont;
117: currentBackgroundColor = state.currentBackgroundColor;
118: currentTextColor = state.currentTextColor;
119: backgroundMode = state.backgroundMode;
120: polyFillMode = state.polyFillMode;
121: textAlign = state.textAlign;
122: lineJoin = state.lineJoin;
123: offsetWx = state.offsetWx;
124: offsetWy = state.offsetWy;
125: extentWx = state.extentWx;
126: extentWy = state.extentWy;
127: scalingX = state.scalingX;
128: scalingY = state.scalingY;
129: }
130:
131: public void addMetaObject(MetaObject object) {
132: for (int k = 0; k < MetaObjects.size(); ++k) {
133: if (MetaObjects.get(k) == null) {
134: MetaObjects.set(k, object);
135: return;
136: }
137: }
138: MetaObjects.add(object);
139: }
140:
141: public void selectMetaObject(int index, PdfContentByte cb) {
142: MetaObject obj = (MetaObject) MetaObjects.get(index);
143: if (obj == null)
144: return;
145: int style;
146: switch (obj.getType()) {
147: case MetaObject.META_BRUSH:
148: currentBrush = (MetaBrush) obj;
149: style = currentBrush.getStyle();
150: if (style == MetaBrush.BS_SOLID) {
151: Color color = currentBrush.getColor();
152: cb.setColorFill(color);
153: } else if (style == MetaBrush.BS_HATCHED) {
154: Color color = currentBackgroundColor;
155: cb.setColorFill(color);
156: }
157: break;
158: case MetaObject.META_PEN: {
159: currentPen = (MetaPen) obj;
160: style = currentPen.getStyle();
161: if (style != MetaPen.PS_NULL) {
162: Color color = currentPen.getColor();
163: cb.setColorStroke(color);
164: cb.setLineWidth(Math.abs((float) currentPen
165: .getPenWidth()
166: * scalingX / extentWx));
167: switch (style) {
168: case MetaPen.PS_DASH:
169: cb.setLineDash(18, 6, 0);
170: break;
171: case MetaPen.PS_DASHDOT:
172: cb.setLiteral("[9 6 3 6]0 d\n");
173: break;
174: case MetaPen.PS_DASHDOTDOT:
175: cb.setLiteral("[9 3 3 3 3 3]0 d\n");
176: break;
177: case MetaPen.PS_DOT:
178: cb.setLineDash(3, 0);
179: break;
180: default:
181: cb.setLineDash(0);
182: break;
183: }
184: }
185: break;
186: }
187: case MetaObject.META_FONT: {
188: currentFont = (MetaFont) obj;
189: break;
190: }
191: }
192: }
193:
194: public void deleteMetaObject(int index) {
195: MetaObjects.set(index, null);
196: }
197:
198: public void saveState(PdfContentByte cb) {
199: cb.saveState();
200: MetaState state = new MetaState(this );
201: savedStates.push(state);
202: }
203:
204: public void restoreState(int index, PdfContentByte cb) {
205: int pops;
206: if (index < 0)
207: pops = Math.min(-index, savedStates.size());
208: else
209: pops = Math.max(savedStates.size() - index, 0);
210: if (pops == 0)
211: return;
212: MetaState state = null;
213: while (pops-- != 0) {
214: cb.restoreState();
215: state = (MetaState) savedStates.pop();
216: }
217: setMetaState(state);
218: }
219:
220: public void cleanup(PdfContentByte cb) {
221: int k = savedStates.size();
222: while (k-- > 0)
223: cb.restoreState();
224: }
225:
226: public float transformX(int x) {
227: return ((float) x - offsetWx) * scalingX / extentWx;
228: }
229:
230: public float transformY(int y) {
231: return (1f - ((float) y - offsetWy) / extentWy) * scalingY;
232: }
233:
234: public void setScalingX(float scalingX) {
235: this .scalingX = scalingX;
236: }
237:
238: public void setScalingY(float scalingY) {
239: this .scalingY = scalingY;
240: }
241:
242: public void setOffsetWx(int offsetWx) {
243: this .offsetWx = offsetWx;
244: }
245:
246: public void setOffsetWy(int offsetWy) {
247: this .offsetWy = offsetWy;
248: }
249:
250: public void setExtentWx(int extentWx) {
251: this .extentWx = extentWx;
252: }
253:
254: public void setExtentWy(int extentWy) {
255: this .extentWy = extentWy;
256: }
257:
258: public float transformAngle(float angle) {
259: float ta = scalingY < 0 ? -angle : angle;
260: return (float) (scalingX < 0 ? Math.PI - ta : ta);
261: }
262:
263: public void setCurrentPoint(Point p) {
264: currentPoint = p;
265: }
266:
267: public Point getCurrentPoint() {
268: return currentPoint;
269: }
270:
271: public MetaBrush getCurrentBrush() {
272: return currentBrush;
273: }
274:
275: public MetaPen getCurrentPen() {
276: return currentPen;
277: }
278:
279: public MetaFont getCurrentFont() {
280: return currentFont;
281: }
282:
283: /** Getter for property currentBackgroundColor.
284: * @return Value of property currentBackgroundColor.
285: */
286: public Color getCurrentBackgroundColor() {
287: return currentBackgroundColor;
288: }
289:
290: /** Setter for property currentBackgroundColor.
291: * @param currentBackgroundColor New value of property currentBackgroundColor.
292: */
293: public void setCurrentBackgroundColor(Color currentBackgroundColor) {
294: this .currentBackgroundColor = currentBackgroundColor;
295: }
296:
297: /** Getter for property currentTextColor.
298: * @return Value of property currentTextColor.
299: */
300: public Color getCurrentTextColor() {
301: return currentTextColor;
302: }
303:
304: /** Setter for property currentTextColor.
305: * @param currentTextColor New value of property currentTextColor.
306: */
307: public void setCurrentTextColor(Color currentTextColor) {
308: this .currentTextColor = currentTextColor;
309: }
310:
311: /** Getter for property backgroundMode.
312: * @return Value of property backgroundMode.
313: */
314: public int getBackgroundMode() {
315: return backgroundMode;
316: }
317:
318: /** Setter for property backgroundMode.
319: * @param backgroundMode New value of property backgroundMode.
320: */
321: public void setBackgroundMode(int backgroundMode) {
322: this .backgroundMode = backgroundMode;
323: }
324:
325: /** Getter for property textAlign.
326: * @return Value of property textAlign.
327: */
328: public int getTextAlign() {
329: return textAlign;
330: }
331:
332: /** Setter for property textAlign.
333: * @param textAlign New value of property textAlign.
334: */
335: public void setTextAlign(int textAlign) {
336: this .textAlign = textAlign;
337: }
338:
339: /** Getter for property polyFillMode.
340: * @return Value of property polyFillMode.
341: */
342: public int getPolyFillMode() {
343: return polyFillMode;
344: }
345:
346: /** Setter for property polyFillMode.
347: * @param polyFillMode New value of property polyFillMode.
348: */
349: public void setPolyFillMode(int polyFillMode) {
350: this .polyFillMode = polyFillMode;
351: }
352:
353: public void setLineJoinRectangle(PdfContentByte cb) {
354: if (lineJoin != 0) {
355: lineJoin = 0;
356: cb.setLineJoin(0);
357: }
358: }
359:
360: public void setLineJoinPolygon(PdfContentByte cb) {
361: if (lineJoin == 0) {
362: lineJoin = 1;
363: cb.setLineJoin(1);
364: }
365: }
366:
367: public boolean getLineNeutral() {
368: return (lineJoin == 0);
369: }
370:
371: }
|