1 18 package org.apache.batik.svggen; 19 20 import java.awt.Font ; 21 import java.net.URL ; 22 import java.util.Iterator ; 23 import java.util.Map ; 24 25 import org.w3c.dom.CDATASection ; 26 import org.w3c.dom.DOMImplementation ; 27 import org.w3c.dom.Document ; 28 import org.w3c.dom.Element ; 29 30 import org.apache.batik.dom.svg.SVGDOMImplementation; 31 import org.apache.batik.svggen.SVGGeneratorContext.GraphicContextDefaults; 32 import org.apache.batik.util.SVGConstants; 33 34 41 public class GeneratorContext extends SVGAccuracyTest implements SVGConstants { 42 public static class TestIDGenerator extends SVGIDGenerator { 43 public String generateID(String prefix) { 44 return "test"+super.generateID(prefix); 45 } 46 } 47 48 public static class TestStyleHandler extends DefaultStyleHandler { 49 private CDATASection styleSheet; 50 public TestStyleHandler(CDATASection styleSheet) { 51 this.styleSheet = styleSheet; 52 } 53 public void setStyle(Element element, Map styleMap, 54 SVGGeneratorContext generatorContext) { 55 Iterator iter = styleMap.keySet().iterator(); 56 String id = generatorContext.getIDGenerator().generateID("C"); 58 styleSheet.appendData("."+id+" {"); 59 while (iter.hasNext()) { 61 String key = (String )iter.next(); 62 String value = (String )styleMap.get(key); 63 styleSheet.appendData(key+":"+value+";"); 64 } 65 styleSheet.appendData("}\n"); 66 element.setAttribute("class", id); 68 } 69 } 70 71 private Element topLevelGroup = null; 72 73 public GeneratorContext(Painter painter, 74 URL refURL) { 75 super(painter, refURL); 76 } 77 78 protected SVGGraphics2D buildSVGGraphics2D(){ 79 DOMImplementation impl = SVGDOMImplementation.getDOMImplementation(); 81 String namespaceURI = SVGDOMImplementation.SVG_NAMESPACE_URI; 82 Document domFactory = impl.createDocument(namespaceURI, SVG_SVG_TAG, null); 83 84 SVGGeneratorContext ctx = SVGGeneratorContext.createDefault(domFactory); 86 87 ctx.setIDGenerator(new TestIDGenerator()); 89 90 92 GenericImageHandler ihandler = new CachedImageHandlerBase64Encoder(); 94 ctx.setGenericImageHandler(ihandler); 95 96 CDATASection styleSheet = domFactory.createCDATASection(""); 98 ctx.setStyleHandler(new TestStyleHandler(styleSheet)); 99 100 ctx.setComment("Generated by the Batik Test Framework. Test:\u00e9j"); 102 103 ctx.setEmbeddedFontsOn(true); 105 106 GraphicContextDefaults defaults 108 = new GraphicContextDefaults(); 109 defaults.font = new Font ("Arial", Font.PLAIN, 12); 110 ctx.setGraphicContextDefaults(defaults); 111 112 SVGGraphics2D g2d = new SVGGraphics2D(ctx, false); 116 117 topLevelGroup = g2d.getTopLevelGroup(); 119 Element style = domFactory.createElementNS(SVG_NAMESPACE_URI, SVG_STYLE_TAG); 120 style.setAttributeNS(null, SVG_TYPE_ATTRIBUTE, "text/css"); 121 style.appendChild(styleSheet); 122 topLevelGroup.appendChild(style); 123 124 return g2d; 125 } 126 127 protected void configureSVGGraphics2D(SVGGraphics2D g2d) { 128 topLevelGroup.appendChild(g2d.getTopLevelGroup()); 129 g2d.setTopLevelGroup(topLevelGroup); 130 } 131 } 132 133 | Popular Tags |