1 58 package org.krysalis.barcode.output.svg; 59 60 import javax.xml.parsers.DocumentBuilder ; 61 import javax.xml.parsers.DocumentBuilderFactory ; 62 import javax.xml.parsers.ParserConfigurationException ; 63 64 import org.w3c.dom.DOMImplementation ; 65 import org.w3c.dom.Document ; 66 import org.w3c.dom.DocumentFragment ; 67 import org.w3c.dom.Element ; 68 69 import org.krysalis.barcode.BarcodeDimension; 70 import org.krysalis.barcode.output.BarcodeCanvasSetupException; 71 72 77 public class SVGCanvasProvider extends AbstractSVGGeneratingCanvasProvider { 78 79 private DOMImplementation domImpl; 80 private Document doc; 81 private Element detailGroup; 82 83 88 public SVGCanvasProvider(String namespacePrefix) 89 throws BarcodeCanvasSetupException { 90 this(null, namespacePrefix); 91 } 92 93 100 public SVGCanvasProvider(DOMImplementation domImpl, String namespacePrefix) 101 throws BarcodeCanvasSetupException { 102 super(namespacePrefix); 103 this.domImpl = domImpl; 104 init(); 105 } 106 107 112 public SVGCanvasProvider(boolean useNamespace) 113 throws BarcodeCanvasSetupException { 114 this(null, useNamespace); 115 } 116 117 124 public SVGCanvasProvider(DOMImplementation domImpl, boolean useNamespace) 125 throws BarcodeCanvasSetupException { 126 super(useNamespace); 127 this.domImpl = domImpl; 128 init(); 129 } 130 131 136 public SVGCanvasProvider() throws BarcodeCanvasSetupException { 137 super(); 138 init(); 139 } 140 141 private void init() { 142 doc = createDocument(); 143 Element svg = doc.getDocumentElement(); 144 145 detailGroup = createElement("g"); 146 svg.appendChild(detailGroup); 147 detailGroup.setAttribute("style", "fill:black; stroke:none"); 148 } 149 150 151 private Element createElement(String localName) { 152 Element el; 153 if (isNamespaceEnabled()) { 154 el = doc.createElementNS(SVG_NAMESPACE, getQualifiedName(localName)); 155 } else { 156 el = doc.createElement(localName); 157 } 158 return el; 159 } 160 161 162 private Document createDocument() { 163 try { 164 if (this.domImpl == null) { 165 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 166 dbf.setNamespaceAware(true); 167 dbf.setValidating(false); 168 DocumentBuilder db = dbf.newDocumentBuilder(); 169 this.domImpl = db.getDOMImplementation(); 170 } 171 172 if (isNamespaceEnabled()) { 173 Document doc = this.domImpl.createDocument( 174 SVG_NAMESPACE, getQualifiedName("svg"), null); 175 183 return doc; 184 } else { 185 return this.domImpl.createDocument(null, "svg", null); 186 } 187 } catch (ParserConfigurationException pce) { 188 throw new RuntimeException (pce.getMessage()); 189 } 190 } 191 192 196 public org.w3c.dom.Document getDOM() { 197 return this.doc; 198 } 199 200 204 public org.w3c.dom.DocumentFragment getDOMFragment() { 205 DocumentFragment frag = doc.createDocumentFragment(); 206 frag.appendChild(doc.importNode(doc.getFirstChild(), true)); 207 return frag; 208 } 209 210 211 public void establishDimensions(BarcodeDimension dim) { 212 super.establishDimensions(dim); 213 Element svg = (Element )doc.getDocumentElement(); 214 svg.setAttribute("width", addUnit(dim.getWidthPlusQuiet())); 215 svg.setAttribute("height", addUnit(dim.getHeightPlusQuiet())); 216 } 217 218 219 public void deviceFillRect(double x, double y, double w, double h) { 220 Element el = createElement("rect"); 221 el.setAttribute("x", addUnit(x)); 222 el.setAttribute("y", addUnit(y)); 223 el.setAttribute("width", addUnit(w)); 224 el.setAttribute("height", addUnit(h)); 225 detailGroup.appendChild(el); 226 } 227 228 229 public void deviceJustifiedText(String text, double x1, double x2, double y1, 230 String fontName, double fontSize) { 231 deviceCenteredText(text, x1, x2, y1, fontName, fontSize, true); 232 } 233 234 235 public void deviceCenteredText(String text, double x1, double x2, double y1, 236 String fontName, double fontSize) { 237 deviceCenteredText(text, x1, x2, y1, fontName, fontSize, false); 238 } 239 240 250 public void deviceCenteredText(String text, double x1, double x2, double y1, 251 String fontName, double fontSize, boolean justify) { 252 Element el = createElement("text"); 253 el.setAttribute("style", "font-family:" + fontName + "; font-size:" 254 + fontSize + "pt; text-anchor:middle"); 255 el.setAttribute("x", addUnit(x1 + (x2 - x1) / 2)); 256 el.setAttribute("y", addUnit(y1)); 257 if (justify) { 258 el.setAttribute("textLength", addUnit(x2 - x1)); 259 } 260 el.appendChild(doc.createTextNode(text)); 261 detailGroup.appendChild(el); 262 263 } 264 265 } | Popular Tags |