1 17 18 19 20 package org.apache.fop.pdf; 21 22 import java.io.Serializable ; 23 import java.util.List ; 24 import java.util.Iterator ; 25 26 import java.awt.Color ; 27 import java.awt.Paint ; 28 import java.awt.Shape ; 29 import java.awt.geom.AffineTransform ; 30 import java.awt.geom.Area ; 31 import java.awt.geom.GeneralPath ; 32 33 50 public class PDFState { 51 52 private Data data = new Data(); 53 54 private List stateStack = new java.util.ArrayList (); 55 56 59 public PDFState() { 60 61 } 62 63 68 public void push() { 69 Data copy; 70 try { 71 copy = (Data)getData().clone(); 72 getData().resetTransform(); 73 } catch (CloneNotSupportedException e) { 74 throw new RuntimeException (e.getMessage()); 75 } 76 stateStack.add(copy); 77 } 78 79 82 public Data getData() { 83 return data; 84 } 85 86 92 public Data pop() { 93 if (getStackLevel() > 0) { 94 Data popped = (Data)stateStack.remove(stateStack.size() - 1); 95 96 data = popped; 97 return popped; 98 } else { 99 return null; 100 } 101 } 102 103 108 public int getStackLevel() { 109 return stateStack.size(); 110 } 111 112 119 129 130 139 143 144 149 public boolean setLineWidth(float width) { 150 if (getData().lineWidth != width) { 151 getData().lineWidth = width; 152 return true; 153 } else { 154 return false; 155 } 156 } 157 158 165 public boolean setColor(Color col) { 166 if (!col.equals(getData().color)) { 167 getData().color = col; 168 return true; 169 } else { 170 return false; 171 } 172 } 173 174 181 public boolean setBackColor(Color col) { 182 if (!col.equals(getData().backcolor)) { 183 getData().backcolor = col; 184 return true; 185 } else { 186 return false; 187 } 188 } 189 190 197 public boolean setPaint(Paint p) { 198 if (getData().paint == null) { 199 if (p != null) { 200 getData().paint = p; 201 return true; 202 } 203 } else if (!data.paint.equals(p)) { 204 getData().paint = p; 205 return true; 206 } 207 return false; 208 } 209 210 222 public boolean checkClip(Shape cl) { 223 if (getData().clip == null) { 224 if (cl != null) { 225 return true; 226 } 227 } else if (!new Area (getData().clip).equals(new Area (cl))) { 228 return true; 229 } 230 return false; 232 } 233 234 241 public void setClip(Shape cl) { 242 if (getData().clip != null) { 243 Area newClip = new Area (getData().clip); 244 newClip.intersect(new Area (cl)); 245 getData().clip = new GeneralPath (newClip); 246 } else { 247 getData().clip = cl; 248 } 249 } 250 251 260 public boolean checkTransform(AffineTransform tf) { 261 return !tf.equals(getData().transform); 262 } 263 264 272 public void setTransform(AffineTransform tf) { 273 concatenate(tf); 274 } 275 276 280 public void concatenate(AffineTransform tf) { 281 getData().concatenate(tf); 282 } 283 284 291 public AffineTransform getTransform() { 292 AffineTransform tf; 293 AffineTransform at = new AffineTransform (); 294 for (Iterator iter = stateStack.iterator(); iter.hasNext();) { 295 Data d = (Data)iter.next(); 296 tf = d.transform; 297 at.concatenate(tf); 298 } 299 at.concatenate(getData().transform); 300 return at; 301 } 302 303 312 public PDFGState getGState() { 313 PDFGState defaultState = PDFGState.DEFAULT; 314 315 PDFGState state; 316 PDFGState newstate = new PDFGState(); 317 newstate.addValues(defaultState); 318 for (Iterator iter = stateStack.iterator(); iter.hasNext();) { 319 Data d = (Data)iter.next(); 320 state = d.gstate; 321 if (state != null) { 322 newstate.addValues(state); 323 } 324 } 325 if (getData().gstate != null) { 326 newstate.addValues(getData().gstate); 327 } 328 329 return newstate; 330 } 331 332 public class Data implements Cloneable , Serializable { 333 334 public Color color = Color.black; 335 public Color backcolor = Color.black; 336 public Paint paint = null; 337 public Paint backPaint = null; 338 public int lineCap = 0; 339 public int lineJoin = 0; 340 public float lineWidth = 1; 341 public float miterLimit = 0; 342 public boolean text = false; 343 public int dashOffset = 0; 344 public int[] dashArray = new int[0]; 345 public AffineTransform transform = new AffineTransform (); 346 public float fontSize = 0; 347 public String fontName = ""; 348 public Shape clip = null; 349 public PDFGState gstate = null; 350 351 352 353 public Object clone() throws CloneNotSupportedException { 354 Data obj = new Data(); 355 obj.color = this.color; 356 obj.backcolor = this.backcolor; 357 obj.paint = this.paint; 358 obj.backPaint = this.paint; 359 obj.lineCap = this.lineCap; 360 obj.lineJoin = this.lineJoin; 361 obj.lineWidth = this.lineWidth; 362 obj.miterLimit = this.miterLimit; 363 obj.text = this.text; 364 obj.dashOffset = this.dashOffset; 365 obj.dashArray = this.dashArray; 366 obj.transform = new AffineTransform (this.transform); 367 obj.fontSize = this.fontSize; 368 obj.fontName = this.fontName; 369 obj.clip = this.clip; 370 obj.gstate = this.gstate; 371 return obj; 372 } 373 374 377 public AffineTransform getTransform() { 378 return transform; 379 } 380 381 public void resetTransform() { 382 transform = new AffineTransform (); 383 } 384 385 392 public void concatenate(AffineTransform at) { 393 transform.concatenate(at); 394 } 395 396 397 public String toString() { 398 return super.toString() + ", " + this.transform; 399 } 400 } 401 } 402 403 | Popular Tags |