1 18 package org.apache.batik.svggen; 19 20 import java.awt.Color ; 21 import java.awt.Composite ; 22 import java.awt.Font ; 23 import java.awt.Paint ; 24 import java.awt.RenderingHints ; 25 import java.awt.Shape ; 26 import java.awt.Stroke ; 27 28 import java.text.DecimalFormat ; 29 import java.text.DecimalFormatSymbols ; 30 31 import java.util.Locale ; 32 33 import org.w3c.dom.Document ; 34 35 45 public class SVGGeneratorContext implements ErrorConstants { 46 48 52 Document domFactory; 53 54 63 ImageHandler imageHandler; 64 65 70 GenericImageHandler genericImageHandler; 71 72 75 ExtensionHandler extensionHandler; 76 77 80 SVGIDGenerator idGenerator; 81 82 85 StyleHandler styleHandler; 86 87 90 String generatorComment; 91 92 95 ErrorHandler errorHandler; 96 97 100 boolean svgFont = false; 101 102 105 GraphicContextDefaults gcDefaults; 106 107 111 int precision; 112 113 121 public static class GraphicContextDefaults { 122 protected Paint paint; 123 protected Stroke stroke; 124 protected Composite composite; 125 protected Shape clip; 126 protected RenderingHints hints; 127 protected Font font; 128 protected Color background; 129 130 public void setStroke(Stroke stroke){ 131 this.stroke = stroke; 132 } 133 134 public Stroke getStroke(){ 135 return stroke; 136 } 137 138 public void setComposite(Composite composite){ 139 this.composite = composite; 140 } 141 142 public Composite getComposite(){ 143 return composite; 144 } 145 146 public void setClip(Shape clip){ 147 this.clip = clip; 148 } 149 150 public Shape getClip(){ 151 return clip; 152 } 153 154 public void setRenderingHints(RenderingHints hints){ 155 this.hints = hints; 156 } 157 158 public RenderingHints getRenderingHints(){ 159 return hints; 160 } 161 162 public void setFont(Font font){ 163 this.font = font; 164 } 165 166 public Font getFont(){ 167 return font; 168 } 169 170 public void setBackground(Color background){ 171 this.background = background; 172 } 173 174 public Color getBackground(){ 175 return background; 176 } 177 178 public void setPaint(Paint paint){ 179 this.paint = paint; 180 } 181 182 public Paint getPaint(){ 183 return paint; 184 } 185 } 186 187 198 protected SVGGeneratorContext(Document domFactory) { 199 setDOMFactory(domFactory); 200 } 201 202 212 public static SVGGeneratorContext createDefault(Document domFactory) { 213 SVGGeneratorContext ctx = new SVGGeneratorContext(domFactory); 214 ctx.setIDGenerator(new SVGIDGenerator()); 215 ctx.setExtensionHandler(new DefaultExtensionHandler()); 216 ctx.setImageHandler(new ImageHandlerBase64Encoder()); 217 ctx.setStyleHandler(new DefaultStyleHandler()); 218 ctx.setComment("Generated by the Batik Graphics2D SVG Generator"); 219 ctx.setErrorHandler(new DefaultErrorHandler()); 220 return ctx; 221 } 222 223 227 final public GraphicContextDefaults getGraphicContextDefaults(){ 228 return gcDefaults; 229 } 230 231 236 final public void setGraphicContextDefaults(GraphicContextDefaults gcDefaults){ 237 this.gcDefaults = gcDefaults; 238 } 239 240 244 final public SVGIDGenerator getIDGenerator() { 245 return idGenerator; 246 } 247 248 252 final public void setIDGenerator(SVGIDGenerator idGenerator) { 253 if (idGenerator == null) 254 throw new SVGGraphics2DRuntimeException(ERR_ID_GENERATOR_NULL); 255 this.idGenerator = idGenerator; 256 } 257 258 262 final public Document getDOMFactory() { 263 return domFactory; 264 } 265 266 270 final public void setDOMFactory(Document domFactory) { 271 if (domFactory == null) 272 throw new SVGGraphics2DRuntimeException(ERR_DOM_FACTORY_NULL); 273 this.domFactory = domFactory; 274 } 275 276 280 final public ExtensionHandler getExtensionHandler() { 281 return extensionHandler; 282 } 283 284 288 final public void setExtensionHandler(ExtensionHandler extensionHandler) { 289 if (extensionHandler == null) 290 throw new SVGGraphics2DRuntimeException(ERR_EXTENSION_HANDLER_NULL); 291 this.extensionHandler = extensionHandler; 292 } 293 294 298 final public ImageHandler getImageHandler() { 299 return imageHandler; 300 } 301 302 306 final public void setImageHandler(ImageHandler imageHandler) { 307 if (imageHandler == null) 308 throw new SVGGraphics2DRuntimeException(ERR_IMAGE_HANDLER_NULL); 309 this.imageHandler = imageHandler; 310 this.genericImageHandler = new SimpleImageHandler(imageHandler); 311 } 312 313 317 final public void setGenericImageHandler(GenericImageHandler genericImageHandler){ 318 if (genericImageHandler == null){ 319 throw new SVGGraphics2DRuntimeException(ERR_IMAGE_HANDLER_NULL); 320 } 321 this.imageHandler = null; 322 this.genericImageHandler = genericImageHandler; 323 } 324 325 329 final public StyleHandler getStyleHandler() { 330 return styleHandler; 331 } 332 333 337 final public void setStyleHandler(StyleHandler styleHandler) { 338 if (styleHandler == null) 339 throw new SVGGraphics2DRuntimeException(ERR_STYLE_HANDLER_NULL); 340 this.styleHandler = styleHandler; 341 } 342 343 346 final public String getComment() { 347 return generatorComment; 348 } 349 350 354 final public void setComment(String generatorComment) { 355 this.generatorComment = generatorComment; 356 } 357 358 362 final public ErrorHandler getErrorHandler() { 363 return errorHandler; 364 } 365 366 370 final public void setErrorHandler(ErrorHandler errorHandler) { 371 if (errorHandler == null) 372 throw new SVGGraphics2DRuntimeException(ERR_ERROR_HANDLER_NULL); 373 this.errorHandler = errorHandler; 374 } 375 376 380 final public boolean isEmbeddedFontsOn() { 381 return svgFont; 382 } 383 384 388 final public void setEmbeddedFontsOn(boolean svgFont) { 389 this.svgFont = svgFont; 390 } 391 392 395 final public int getPrecision() { 396 return precision; 397 } 398 399 405 final public void setPrecision(int precision) { 406 if (precision < 0) { 407 this.precision = 0; 408 } else if (precision > 12) { 409 this.precision = 12; 410 } else { 411 this.precision = precision; 412 } 413 decimalFormat = decimalFormats[this.precision]; 414 } 415 416 420 final public String doubleString(double value) { 421 double absvalue = Math.abs(value); 422 if (absvalue >= 10e7 || (int)value == value) { 425 return Integer.toString((int)value); 426 } 427 else { 429 return decimalFormat.format(value); 430 } 431 } 432 433 436 protected DecimalFormat decimalFormat = decimalFormats[3]; 437 438 protected static DecimalFormatSymbols dsf 439 = new DecimalFormatSymbols (Locale.US); 440 441 protected static DecimalFormat decimalFormats[] = new DecimalFormat [13]; 442 443 static { 444 decimalFormats[0] = new DecimalFormat ("#", dsf); 445 446 String format = "#."; 447 for (int i=0; i<=12; i++) { 448 format += "#"; 449 decimalFormats[i] = new DecimalFormat (format, dsf); 450 } 451 } 452 453 } 454 | Popular Tags |