1 58 package org.krysalis.barcode.output.svg; 59 60 import org.jdom.Document; 61 import org.jdom.Element; 62 import org.jdom.JDOMException; 63 import org.jdom.Namespace; 64 import org.krysalis.barcode.BarcodeDimension; 65 import org.krysalis.barcode.output.BarcodeCanvasSetupException; 66 67 72 public class JDOMSVGCanvasProvider extends AbstractSVGGeneratingCanvasProvider { 73 74 private Namespace ns; 75 private Document doc; 76 private Element detailGroup; 77 78 83 public JDOMSVGCanvasProvider(String namespacePrefix) 84 throws BarcodeCanvasSetupException { 85 super(namespacePrefix); 86 } 87 88 93 public JDOMSVGCanvasProvider(boolean useNamespace) 94 throws BarcodeCanvasSetupException { 95 super(useNamespace); 96 init(); 97 } 98 99 104 public JDOMSVGCanvasProvider() throws BarcodeCanvasSetupException { 105 super(); 106 init(); 107 } 108 109 private void init() { 110 Element svg; 111 if (isNamespaceEnabled()) { 112 if (getNamespacePrefix() != null) { 113 ns = Namespace.getNamespace(getNamespacePrefix(), SVG_NAMESPACE); 114 } else { 115 ns = Namespace.getNamespace(SVG_NAMESPACE); 116 } 117 } else { 118 ns = null; 119 } 120 svg = new Element("svg", ns); 121 doc = new Document(svg); 122 123 detailGroup = new Element("g", ns); 124 svg.addContent(detailGroup); 125 detailGroup.setAttribute("style", "fill:black; stroke:none"); 126 } 127 128 132 public Document getDocument() { 133 return this.doc; 134 } 135 136 140 public org.w3c.dom.Document getDOM() { 141 org.jdom.output.DOMOutputter output = new org.jdom.output.DOMOutputter(); 142 try { 143 return output.output(this.doc); 144 } catch (JDOMException e) { 145 e.printStackTrace(); 146 return null; 147 } 148 } 149 150 154 public org.w3c.dom.DocumentFragment getDOMFragment() { 155 org.w3c.dom.Document doc = getDOM(); 156 org.w3c.dom.DocumentFragment frag = doc.createDocumentFragment(); 157 frag.appendChild(doc.getDocumentElement()); 158 return frag; 159 } 160 161 162 public void establishDimensions(BarcodeDimension dim) { 163 super.establishDimensions(dim); 164 Element svg = doc.getRootElement(); 165 svg.setAttribute("width", addUnit(dim.getWidthPlusQuiet())); 166 svg.setAttribute("height", addUnit(dim.getHeightPlusQuiet())); 167 } 168 169 170 public void deviceFillRect(double x, double y, double w, double h) { 171 Element el = new Element("rect", ns); 172 el.setAttribute("x", addUnit(x)); 173 el.setAttribute("y", addUnit(y)); 174 el.setAttribute("width", addUnit(w)); 175 el.setAttribute("height", addUnit(h)); 176 detailGroup.addContent(el); 177 } 178 179 180 public void deviceJustifiedText(String text, double x1, double x2, double y1, 181 String fontName, double fontSize) { 182 deviceCenteredText(text, x1, x2, y1, fontName, fontSize, true); 183 } 184 185 186 public void deviceCenteredText(String text, double x1, double x2, double y1, 187 String fontName, double fontSize) { 188 deviceCenteredText(text, x1, x2, y1, fontName, fontSize, false); 189 } 190 191 201 public void deviceCenteredText(String text, double x1, double x2, double y1, 202 String fontName, double fontSize, boolean justify) { 203 Element el = new Element("text", ns); 204 el.setAttribute("style", "font-family:" + fontName + "; font-size:" 205 + fontSize + "pt; text-anchor:middle"); 206 el.setAttribute("x", addUnit(x1 + (x2 - x1) / 2)); 207 el.setAttribute("y", addUnit(y1)); 208 if (justify) { 209 el.setAttribute("textLength", addUnit(x2 - x1)); 210 } 211 el.addContent(text); 212 detailGroup.addContent(el); 213 214 } 215 216 } | Popular Tags |