001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: /* $Id: PSState.java 548990 2007-06-20 08:28:52Z jeremias $ */
019:
020: package org.apache.xmlgraphics.ps;
021:
022: import java.io.IOException;
023: import java.io.Serializable;
024: import java.util.List;
025: import java.awt.Color;
026: import java.awt.geom.AffineTransform;
027:
028: /**
029: * This class holds the current state of the PostScript interpreter.
030: *
031: * @version $Id: PSState.java 548990 2007-06-20 08:28:52Z jeremias $
032: */
033: public class PSState implements Serializable {
034:
035: /** Default for setdash */
036: public static final String DEFAULT_DASH = "[] 0";
037: /** Default color in PostScript */
038: public static final Color DEFAULT_RGB_COLOR = Color.black;
039:
040: private AffineTransform transform = new AffineTransform();
041: private List transformConcatList = new java.util.ArrayList();
042:
043: private int linecap = 0;
044: private double linewidth = 1.0f;
045: private String dashpattern = DEFAULT_DASH;
046: private Color color = DEFAULT_RGB_COLOR;
047:
048: //Font state
049: private String fontname;
050: private float fontsize;
051:
052: /**
053: * Default constructor
054: */
055: public PSState() {
056: //nop
057: }
058:
059: /**
060: * Copy constructor
061: * @param org the original to copy from
062: * @param copyTransforms true if the list of matrix concats should be cloned, too
063: */
064: public PSState(PSState org, boolean copyTransforms) {
065: this .transform = (AffineTransform) org.transform.clone();
066: if (copyTransforms) {
067: this .transformConcatList.addAll(org.transformConcatList);
068: }
069: this .linecap = org.linecap;
070: this .linewidth = org.linewidth;
071: this .dashpattern = org.dashpattern;
072: this .color = org.color;
073: this .fontname = org.fontname;
074: this .fontsize = org.fontsize;
075: }
076:
077: /**
078: * Returns the transform.
079: * @return the current transformation matrix
080: */
081: public AffineTransform getTransform() {
082: return this .transform;
083: }
084:
085: /**
086: * Check the current transform.
087: * The transform for the current state is the combination of all
088: * transforms in the current state. The parameter is compared
089: * against this current transform.
090: *
091: * @param tf the transform the check against
092: * @return true if the new transform is different then the current transform
093: */
094: public boolean checkTransform(AffineTransform tf) {
095: return !tf.equals(this .transform);
096: }
097:
098: /**
099: * Concats the given transformation matrix with the current one.
100: * @param transform The new transformation matrix
101: */
102: public void concatMatrix(AffineTransform transform) {
103: this .transformConcatList.add(transform);
104: this .transform.concatenate(transform);
105: }
106:
107: /**
108: * Establishes the specified line cap.
109: * @param value line cap (0, 1 or 2) as defined by the setlinecap command
110: * @return true if the line cap changed compared to the previous setting
111: */
112: public boolean useLineCap(int value) {
113: if (linecap != value) {
114: linecap = value;
115: return true;
116: } else {
117: return false;
118: }
119: }
120:
121: /**
122: * Establishes the specified line width.
123: * @param value line width as defined by the setlinewidth command
124: * @return true if the line width changed compared to the previous setting
125: */
126: public boolean useLineWidth(double value) {
127: if (linewidth != value) {
128: linewidth = value;
129: return true;
130: } else {
131: return false;
132: }
133: }
134:
135: /**
136: * Establishes the specified dash.
137: * @param pattern dash pattern as defined by the setdash command
138: * @return true if the dash pattern changed compared to the previous setting
139: */
140: public boolean useDash(String pattern) {
141: if (!dashpattern.equals(pattern)) {
142: dashpattern = pattern;
143: return true;
144: } else {
145: return false;
146: }
147: }
148:
149: /**
150: * Establishes the specified color (RGB).
151: * @param value color as defined by the setrgbcolor command
152: * @return true if the color changed compared to the previous setting
153: */
154: public boolean useColor(Color value) {
155: if (!color.equals(value)) {
156: color = value;
157: return true;
158: } else {
159: return false;
160: }
161: }
162:
163: /**
164: * Establishes the specified font and size.
165: * @param name name of the font for the "F" command (see FOP Std Proc Set)
166: * @param size size of the font
167: * @return true if the font changed compared to the previous setting
168: */
169: public boolean useFont(String name, float size) {
170: if (name == null) {
171: throw new NullPointerException("font name must not be null");
172: }
173: if (fontname == null || !fontname.equals(name)
174: || fontsize != size) {
175: fontname = name;
176: fontsize = size;
177: return true;
178: } else {
179: return false;
180: }
181: }
182:
183: /**
184: * Reestablishes the graphics state represented by this instance by issueing the
185: * necessary commands.
186: * @param gen The generator to use for output
187: * @exception IOException In case of an I/O problem
188: */
189: public void reestablish(PSGenerator gen) throws IOException {
190: for (int i = 0, len = transformConcatList.size(); i < len; i++) {
191: gen.concatMatrix((AffineTransform) transformConcatList
192: .get(i));
193: }
194: gen.useLineCap(linecap);
195: gen.useLineWidth(linewidth);
196: gen.useDash(dashpattern);
197: gen.useColor(color);
198: if (fontname != null) {
199: gen.useFont(fontname, fontsize);
200: }
201: }
202:
203: }
|