1 31 package org.pdfbox.pdmodel.graphics; 32 33 import org.pdfbox.cos.COSArray; 34 import org.pdfbox.cos.COSBase; 35 import org.pdfbox.cos.COSDictionary; 36 import org.pdfbox.cos.COSFloat; 37 import org.pdfbox.cos.COSName; 38 import org.pdfbox.cos.COSNumber; 39 40 import org.pdfbox.pdmodel.common.COSObjectable; 41 42 import java.io.IOException ; 43 44 import java.util.Iterator ; 45 46 53 public class PDExtendedGraphicsState implements COSObjectable 54 { 55 private static final COSName LW = COSName.getPDFName( "LW" ); 56 private static final COSName LC = COSName.getPDFName( "LC" ); 57 private static final COSName LJ = COSName.getPDFName( "LJ" ); 58 private static final COSName ML = COSName.getPDFName( "ML" ); 59 private static final COSName D = COSName.getPDFName( "D" ); 60 private static final COSName RI = COSName.getPDFName( "RI" ); 61 private static final COSName OP = COSName.getPDFName( "OP" ); 62 private static final COSName OP_NS = COSName.getPDFName( "op" ); 63 private static final COSName OPM = COSName.getPDFName( "OPM" ); 64 private static final COSName FONT = COSName.getPDFName( "Font" ); 65 private static final COSName FL = COSName.getPDFName( "FL" ); 66 private static final COSName SM = COSName.getPDFName( "SM" ); 67 private static final COSName SA = COSName.getPDFName( "SA" ); 68 private static final COSName CA = COSName.getPDFName( "CA" ); 69 private static final COSName CA_NS = COSName.getPDFName( "ca" ); 70 private static final COSName AIS = COSName.getPDFName( "AIS" ); 71 private static final COSName TK = COSName.getPDFName( "TK" ); 72 73 76 public static final String RENDERING_INTENT_ABSOLUTE_COLORIMETRIC = "AbsoluteColorimetric"; 77 80 public static final String RENDERING_INTENT_RELATIVE_COLORIMETRIC = "RelativeColorimetric"; 81 84 public static final String RENDERING_INTENT_SATURATION = "Saturation"; 85 88 public static final String RENDERING_INTENT_PERCEPTUAL = "Perceptual"; 89 90 91 private COSDictionary graphicsState; 92 93 96 public PDExtendedGraphicsState() 97 { 98 graphicsState = new COSDictionary(); 99 graphicsState.setItem( COSName.TYPE, COSName.getPDFName( "ExtGState" ) ); 100 } 101 102 107 public PDExtendedGraphicsState( COSDictionary dictionary ) 108 { 109 graphicsState = dictionary; 110 } 111 112 119 public void copyIntoGraphicsState( PDGraphicsState gs ) throws IOException 120 { 121 Iterator keys = graphicsState.keyList().iterator(); 122 while( keys.hasNext() ) 123 { 124 COSName key = (COSName)keys.next(); 125 if( key.equals( LW ) ) 126 { 127 gs.setLineWidth( getLineWidth().doubleValue() ); 128 } 129 else if( key.equals( LC ) ) 130 { 131 gs.setLineCap( getLineCapStyle() ); 132 } 133 else if( key.equals( LJ ) ) 134 { 135 gs.setLineJoin( getLineJoinStyle() ); 136 } 137 else if( key.equals( ML ) ) 138 { 139 gs.setMiterLimit( getMiterLimit().doubleValue() ); 140 } 141 else if( key.equals( D ) ) 142 { 143 gs.setLineDashPattern( getLineDashPattern() ); 144 } 145 else if( key.equals( RI ) ) 146 { 147 gs.setRenderingIntent( getRenderingIntent() ); 148 } 149 else if( key.equals( OPM ) ) 150 { 151 gs.setOverprintMode( getOverprintMode().doubleValue() ); 152 } 153 else if( key.equals( FONT ) ) 154 { 155 PDFontSetting setting = getFontSetting(); 156 gs.getTextState().setFont( setting.getFont() ); 157 gs.getTextState().setFontSize( setting.getFontSize() ); 158 } 159 else if( key.equals( FL ) ) 160 { 161 gs.setFlatness( getFlatnessTolerance().floatValue() ); 162 } 163 else if( key.equals( SM ) ) 164 { 165 gs.setSmoothness( getSmoothnessTolerance().floatValue() ); 166 } 167 else if( key.equals( SA ) ) 168 { 169 gs.setStrokeAdjustment( getAutomaticStrokeAdjustment() ); 170 } 171 else if( key.equals( CA ) ) 172 { 173 gs.setAlphaConstants( getStrokingAlpaConstant().floatValue() ); 174 } 178 else if( key.equals( AIS ) ) 179 { 180 gs.setAlphaSource( getAlphaSourceFlag() ); 181 } 182 else if( key.equals( TK ) ) 183 { 184 gs.getTextState().setKnockoutFlag( getTextKnockoutFlag() ); 185 } 186 } 187 } 188 189 194 public COSDictionary getCOSDictionary() 195 { 196 return graphicsState; 197 } 198 199 204 public COSBase getCOSObject() 205 { 206 return graphicsState; 207 } 208 209 214 public Float getLineWidth() 215 { 216 return getFloatItem( LW ); 217 } 218 219 224 public void setLineWidth( Float width ) 225 { 226 setFloatItem( LW, width ); 227 } 228 229 234 public int getLineCapStyle() 235 { 236 return graphicsState.getInt( LC ); 237 } 238 239 244 public void setLineCapStyle( int style ) 245 { 246 graphicsState.setInt( LC, style ); 247 } 248 249 254 public int getLineJoinStyle() 255 { 256 return graphicsState.getInt( LJ ); 257 } 258 259 264 public void setLineJoinStyle( int style ) 265 { 266 graphicsState.setInt( LJ, style ); 267 } 268 269 270 275 public Float getMiterLimit() 276 { 277 return getFloatItem( ML ); 278 } 279 280 285 public void setMiterLimit( Float miterLimit ) 286 { 287 setFloatItem( ML, miterLimit ); 288 } 289 290 295 public PDLineDashPattern getLineDashPattern() 296 { 297 PDLineDashPattern retval = null; 298 COSArray dp = (COSArray)graphicsState.getDictionaryObject( D ); 299 if( dp != null ) 300 { 301 retval = new PDLineDashPattern( dp ); 302 } 303 return retval; 304 } 305 306 311 public void setLineDashPattern( PDLineDashPattern dashPattern ) 312 { 313 graphicsState.setItem( D, dashPattern.getCOSObject() ); 314 } 315 316 321 public String getRenderingIntent() 322 { 323 return graphicsState.getNameAsString( "RI" ); 324 } 325 326 331 public void setRenderingIntent( String ri ) 332 { 333 graphicsState.setName( "RI", ri ); 334 } 335 336 341 public boolean getStrokingOverprintControl() 342 { 343 return graphicsState.getBoolean( OP, false ); 344 } 345 346 351 public void setStrokingOverprintControl( boolean op ) 352 { 353 graphicsState.setBoolean( OP, op ); 354 } 355 356 362 public boolean getNonStrokingOverprintControl() 363 { 364 return graphicsState.getBoolean( OP_NS, getStrokingOverprintControl() ); 365 } 366 367 372 public void setNonStrokingOverprintControl( boolean op ) 373 { 374 graphicsState.setBoolean( OP_NS, op ); 375 } 376 377 382 public Float getOverprintMode() 383 { 384 return getFloatItem( OPM ); 385 } 386 387 392 public void setOverprintMode( Float overprintMode ) 393 { 394 setFloatItem( OPM, overprintMode ); 395 } 396 397 402 public PDFontSetting getFontSetting() 403 { 404 PDFontSetting setting = null; 405 COSArray font = (COSArray)graphicsState.getDictionaryObject( FONT ); 406 if( font != null ) 407 { 408 setting = new PDFontSetting( font ); 409 } 410 return setting; 411 } 412 413 418 public void setFontSetting( PDFontSetting fs ) 419 { 420 graphicsState.setItem( FONT, fs ); 421 } 422 423 428 public Float getFlatnessTolerance() 429 { 430 return getFloatItem( FL ); 431 } 432 433 438 public void setFlatnessTolerance( Float flatness ) 439 { 440 setFloatItem( FL, flatness ); 441 } 442 443 448 public Float getSmoothnessTolerance() 449 { 450 return getFloatItem( SM ); 451 } 452 453 458 public void setSmoothnessTolerance( Float smoothness ) 459 { 460 setFloatItem( SM, smoothness ); 461 } 462 463 468 public boolean getAutomaticStrokeAdjustment() 469 { 470 return graphicsState.getBoolean( SA,false ); 471 } 472 473 478 public void setAutomaticStrokeAdjustment( boolean sa ) 479 { 480 graphicsState.setBoolean( SA, sa ); 481 } 482 483 488 public Float getStrokingAlpaConstant() 489 { 490 return getFloatItem( CA ); 491 } 492 493 498 public void setStrokingAlphaConstant( Float alpha ) 499 { 500 setFloatItem( CA, alpha ); 501 } 502 503 508 public Float getNonStrokingAlpaConstant() 509 { 510 return getFloatItem( CA_NS ); 511 } 512 513 518 public void setNonStrokingAlphaConstant( Float alpha ) 519 { 520 setFloatItem( CA_NS, alpha ); 521 } 522 523 528 public boolean getAlphaSourceFlag() 529 { 530 return graphicsState.getBoolean( AIS, false ); 531 } 532 533 538 public void setAlphaSourceFlag( boolean alpha ) 539 { 540 graphicsState.setBoolean( AIS, alpha ); 541 } 542 543 548 public boolean getTextKnockoutFlag() 549 { 550 return graphicsState.getBoolean( TK,true ); 551 } 552 553 558 public void setTextKnockoutFlag( boolean tk ) 559 { 560 graphicsState.setBoolean( TK, tk ); 561 } 562 563 570 private Float getFloatItem( COSName key ) 571 { 572 Float retval = null; 573 COSNumber value = (COSNumber)graphicsState.getDictionaryObject( key ); 574 if( value != null ) 575 { 576 retval = new Float ( value.floatValue() ); 577 } 578 return retval; 579 } 580 581 587 private void setFloatItem( COSName key, Float value ) 588 { 589 if( value == null ) 590 { 591 graphicsState.removeItem( key ); 592 } 593 else 594 { 595 graphicsState.setItem( key, new COSFloat( value.floatValue() ) ); 596 } 597 } 598 } | Popular Tags |