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 package org.apache.pdfbox.pdmodel.graphics; 18 19 import java.awt.Dimension; 20 import java.awt.Rectangle; 21 import java.awt.Shape; 22 import java.awt.geom.GeneralPath; 23 24 import org.apache.pdfbox.pdmodel.common.PDRectangle; 25 import org.apache.pdfbox.pdmodel.graphics.color.PDColorState; 26 import org.apache.pdfbox.pdmodel.text.PDTextState; 27 import org.apache.pdfbox.util.Matrix; 28 29 /** 30 * This class will hold the current state of the graphics parameters when executing a 31 * content stream. 32 * 33 * @author <a href="ben@benlitchfield.com">Ben Litchfield</a> 34 * @version $Revision: 1.5 $ 35 */ 36 public class PDGraphicsState implements Cloneable 37 { 38 private Matrix currentTransformationMatrix = new Matrix(); 39 40 //Here are some attributes of the Graphics state, but have not been created yet. 41 // 42 //clippingPath 43 private PDColorState strokingColor = new PDColorState(); 44 private PDColorState nonStrokingColor = new PDColorState(); 45 private PDTextState textState = new PDTextState(); 46 private double lineWidth = 0; 47 private int lineCap = 0; 48 private int lineJoin = 0; 49 private double miterLimit = 0; 50 private PDLineDashPattern lineDashPattern; 51 private String renderingIntent; 52 private boolean strokeAdjustment = false; 53 //blend mode 54 //soft mask 55 private double alphaConstants = 0; 56 private boolean alphaSource = false; 57 58 //DEVICE DEPENDENT parameters 59 private boolean overprint = false; 60 private double overprintMode = 0; 61 //black generation 62 //undercolor removal 63 //transfer 64 //halftone 65 private double flatness = 1.0; 66 private double smoothness = 0; 67 68 private GeneralPath currentClippingPath; 69 70 /** 71 * Default constructor. 72 */ 73 public PDGraphicsState() 74 { 75 } 76 77 /** 78 * Constructor with a given pagesize to initialize the clipping path. 79 * @param page the size of the page 80 */ 81 public PDGraphicsState(PDRectangle page) 82 { 83 currentClippingPath = new GeneralPath(new Rectangle(page.createDimension())); 84 } 85 /** 86 * Get the value of the CTM. 87 * 88 * @return The current transformation matrix. 89 */ 90 public Matrix getCurrentTransformationMatrix() 91 { 92 return currentTransformationMatrix; 93 } 94 95 /** 96 * Set the value of the CTM. 97 * 98 * @param value The current transformation matrix. 99 */ 100 public void setCurrentTransformationMatrix(Matrix value) 101 { 102 currentTransformationMatrix = value; 103 } 104 105 /** 106 * Get the value of the line width. 107 * 108 * @return The current line width. 109 */ 110 public double getLineWidth() 111 { 112 return lineWidth; 113 } 114 115 /** 116 * set the value of the line width. 117 * 118 * @param value The current line width. 119 */ 120 public void setLineWidth(double value) 121 { 122 lineWidth = value; 123 } 124 125 /** 126 * Get the value of the line cap. 127 * 128 * @return The current line cap. 129 */ 130 public int getLineCap() 131 { 132 return lineCap; 133 } 134 135 /** 136 * set the value of the line cap. 137 * 138 * @param value The current line cap. 139 */ 140 public void setLineCap(int value) 141 { 142 lineCap = value; 143 } 144 145 /** 146 * Get the value of the line join. 147 * 148 * @return The current line join value. 149 */ 150 public int getLineJoin() 151 { 152 return lineJoin; 153 } 154 155 /** 156 * Get the value of the line join. 157 * 158 * @param value The current line join 159 */ 160 public void setLineJoin(int value) 161 { 162 lineJoin = value; 163 } 164 165 /** 166 * Get the value of the miter limit. 167 * 168 * @return The current miter limit. 169 */ 170 public double getMiterLimit() 171 { 172 return miterLimit; 173 } 174 175 /** 176 * set the value of the miter limit. 177 * 178 * @param value The current miter limit. 179 */ 180 public void setMiterLimit(double value) 181 { 182 miterLimit = value; 183 } 184 185 /** 186 * Get the value of the stroke adjustment parameter. 187 * 188 * @return The current stroke adjustment. 189 */ 190 public boolean isStrokeAdjustment() 191 { 192 return strokeAdjustment; 193 } 194 195 /** 196 * set the value of the stroke adjustment. 197 * 198 * @param value The value of the stroke adjustment parameter. 199 */ 200 public void setStrokeAdjustment(boolean value) 201 { 202 strokeAdjustment = value; 203 } 204 205 /** 206 * Get the value of the alpha constants property. 207 * 208 * @return The value of the alpha constants parameter. 209 */ 210 public double getAlphaConstants() 211 { 212 return alphaConstants; 213 } 214 215 /** 216 * set the value of the alpha constants property. 217 * 218 * @param value The value of the alpha constants parameter. 219 */ 220 public void setAlphaConstants(double value) 221 { 222 alphaConstants = value; 223 } 224 225 /** 226 * get the value of the alpha source property. 227 * 228 * @return The value of the alpha source parameter. 229 */ 230 public boolean isAlphaSource() 231 { 232 return alphaSource; 233 } 234 235 /** 236 * set the value of the alpha source property. 237 * 238 * @param value The value of the alpha source parameter. 239 */ 240 public void setAlphaSource(boolean value) 241 { 242 alphaSource = value; 243 } 244 245 /** 246 * get the value of the overprint property. 247 * 248 * @return The value of the overprint parameter. 249 */ 250 public boolean isOverprint() 251 { 252 return overprint; 253 } 254 255 /** 256 * set the value of the overprint property. 257 * 258 * @param value The value of the overprint parameter. 259 */ 260 public void setOverprint(boolean value) 261 { 262 overprint = value; 263 } 264 265 /** 266 * get the value of the overprint mode property. 267 * 268 * @return The value of the overprint mode parameter. 269 */ 270 public double getOverprintMode() 271 { 272 return overprintMode; 273 } 274 275 /** 276 * set the value of the overprint mode property. 277 * 278 * @param value The value of the overprint mode parameter. 279 */ 280 public void setOverprintMode(double value) 281 { 282 overprintMode = value; 283 } 284 285 /** 286 * get the value of the flatness property. 287 * 288 * @return The value of the flatness parameter. 289 */ 290 public double getFlatness() 291 { 292 return flatness; 293 } 294 295 /** 296 * set the value of the flatness property. 297 * 298 * @param value The value of the flatness parameter. 299 */ 300 public void setFlatness(double value) 301 { 302 flatness = value; 303 } 304 305 /** 306 * get the value of the smoothness property. 307 * 308 * @return The value of the smoothness parameter. 309 */ 310 public double getSmoothness() 311 { 312 return smoothness; 313 } 314 315 /** 316 * set the value of the smoothness property. 317 * 318 * @param value The value of the smoothness parameter. 319 */ 320 public void setSmoothness(double value) 321 { 322 smoothness = value; 323 } 324 325 /** 326 * This will get the graphics text state. 327 * 328 * @return The graphics text state. 329 */ 330 public PDTextState getTextState() 331 { 332 return textState; 333 } 334 335 /** 336 * This will set the graphics text state. 337 * 338 * @param value The graphics text state. 339 */ 340 public void setTextState(PDTextState value) 341 { 342 textState = value; 343 } 344 345 /** 346 * This will get the current line dash pattern. 347 * 348 * @return The line dash pattern. 349 */ 350 public PDLineDashPattern getLineDashPattern() 351 { 352 return lineDashPattern; 353 } 354 355 /** 356 * This will set the current line dash pattern. 357 * 358 * @param value The new line dash pattern. 359 */ 360 public void setLineDashPattern(PDLineDashPattern value) 361 { 362 lineDashPattern = value; 363 } 364 365 /** 366 * This will get the rendering intent. 367 * 368 * @see PDExtendedGraphicsState 369 * 370 * @return The rendering intent 371 */ 372 public String getRenderingIntent() 373 { 374 return renderingIntent; 375 } 376 377 /** 378 * This will set the rendering intent. 379 * 380 * @param value The new rendering intent. 381 */ 382 public void setRenderingIntent(String value) 383 { 384 renderingIntent = value; 385 } 386 387 /** 388 * {@inheritDoc} 389 */ 390 public Object clone() 391 { 392 PDGraphicsState clone = null; 393 try 394 { 395 clone = (PDGraphicsState)super.clone(); 396 clone.setTextState( (PDTextState)textState.clone() ); 397 clone.setCurrentTransformationMatrix( currentTransformationMatrix.copy() ); 398 clone.strokingColor = (PDColorState)strokingColor.clone(); 399 clone.nonStrokingColor = ((PDColorState)nonStrokingColor.clone()); 400 if( lineDashPattern != null ) 401 { 402 clone.setLineDashPattern( (PDLineDashPattern)lineDashPattern.clone() ); 403 } 404 if (currentClippingPath != null) 405 { 406 clone.setCurrentClippingPath((GeneralPath)currentClippingPath.clone()); 407 } 408 } 409 catch( CloneNotSupportedException e ) 410 { 411 e.printStackTrace(); 412 } 413 return clone; 414 } 415 416 /** 417 * Returns the stroking color state. 418 * 419 * @return stroking color state 420 */ 421 public PDColorState getStrokingColor() 422 { 423 return strokingColor; 424 } 425 426 /** 427 * Returns the non-stroking color state. 428 * 429 * @return non-stroking color state 430 */ 431 public PDColorState getNonStrokingColor() 432 { 433 return nonStrokingColor; 434 } 435 436 /** 437 * This will set the current clipping path. 438 * 439 * @param pCurrentClippingPath The current clipping path. 440 * 441 */ 442 public void setCurrentClippingPath(Shape pCurrentClippingPath) 443 { 444 if (pCurrentClippingPath != null) 445 { 446 if (pCurrentClippingPath instanceof GeneralPath) 447 { 448 currentClippingPath = (GeneralPath)pCurrentClippingPath; 449 } 450 else 451 { 452 currentClippingPath = new GeneralPath(); 453 currentClippingPath.append(pCurrentClippingPath,false); 454 } 455 } 456 else 457 { 458 currentClippingPath = null; 459 } 460 } 461 462 /** 463 * This will get the current clipping path. 464 * 465 * @return The current clipping path. 466 */ 467 public Shape getCurrentClippingPath() 468 { 469 return currentClippingPath; 470 } 471 472 }