1 51 package org.apache.fop.svg; 52 53 import java.util.*; 54 import java.awt.geom.AffineTransform ; 55 import java.awt.geom.Rectangle2D ; 56 import java.awt.font.FontRenderContext ; 57 58 import org.apache.fop.svg.*; 59 import org.w3c.dom.*; 60 61 import org.apache.batik.dom.svg.SVGDOMImplementation; 62 63 66 public class SVGUtilities { 67 static final String svgNS = SVGDOMImplementation.SVG_NAMESPACE_URI; 68 69 70 public static final Document createSVGDocument(float width, 71 float height) { 72 DOMImplementation impl = SVGDOMImplementation.getDOMImplementation(); 73 Document doc = impl.createDocument(svgNS, "svg", null); 74 75 Element svgRoot = doc.getDocumentElement(); 76 svgRoot.setAttributeNS(null, "width", "" + width); 77 svgRoot.setAttributeNS(null, "height", "" + height); 78 return doc; 79 } 80 81 84 public static final float getStringWidth(String str, java.awt.Font font) { 85 Rectangle2D rect = 86 font.getStringBounds(str, 0, str.length(), 87 new FontRenderContext (new AffineTransform (), 88 true, true)); 89 return (float)rect.getWidth(); 90 } 91 92 95 public static final float getStringHeight(String str, 96 java.awt.Font font) { 97 Rectangle2D rect = 98 font.getStringBounds(str, 0, str.length(), 99 new FontRenderContext (new AffineTransform (), 100 true, true)); 101 return (float)rect.getHeight(); 102 } 103 104 107 public static final Rectangle2D getStringBounds(String str, 108 java.awt.Font font) { 109 return font.getStringBounds(str, 0, str.length(), 110 new FontRenderContext (new AffineTransform (), 111 true, true)); 112 } 113 114 117 public static final Element createLine(Document doc, float x, float y, 118 float x2, float y2) { 119 Element ellipse = doc.createElementNS(svgNS, "line"); 120 ellipse.setAttributeNS(null, "x1", "" + x); 121 ellipse.setAttributeNS(null, "x2", "" + x2); 122 ellipse.setAttributeNS(null, "y1", "" + y); 123 ellipse.setAttributeNS(null, "y2", "" + y2); 124 return ellipse; 125 } 126 127 130 public static final Element createEllipse(Document doc, float cx, 131 float cy, float rx, float ry) { 132 Element ellipse = doc.createElementNS(svgNS, "ellipse"); 133 ellipse.setAttributeNS(null, "cx", "" + cx); 134 ellipse.setAttributeNS(null, "rx", "" + rx); 135 ellipse.setAttributeNS(null, "cy", "" + cy); 136 ellipse.setAttributeNS(null, "ry", "" + ry); 137 return ellipse; 138 } 139 140 143 public static final Element createPath(Document doc, String str) { 144 Element path = doc.createElementNS(svgNS, "path"); 145 path.setAttributeNS(null, "d", str); 146 return path; 147 } 148 149 152 public static final Element createText(Document doc, float x, float y, 153 String str) { 154 Element textGraph = doc.createElementNS(svgNS, "text"); 155 textGraph.setAttributeNS(null, "x", "" + x); 156 textGraph.setAttributeNS(null, "y", "" + y); 157 org.w3c.dom.Text text = doc.createTextNode(str); 158 textGraph.appendChild(text); 159 return textGraph; 160 } 161 162 165 public static final Element createRect(Document doc, float x, float y, 166 float width, float height) { 167 Element border = doc.createElementNS(svgNS, "rect"); 168 border.setAttributeNS(null, "x", "" + x); 169 border.setAttributeNS(null, "y", "" + y); 170 border.setAttributeNS(null, "width", "" + width); 171 border.setAttributeNS(null, "height", "" + height); 172 return border; 173 } 174 175 178 public static final Element createG(Document doc) { 179 Element border = doc.createElementNS(svgNS, "g"); 180 return border; 181 } 182 183 186 public static final Element createClip(Document doc, Element els, 187 String id) { 188 Element border = doc.createElementNS(svgNS, "clipPath"); 189 border.setAttributeNS(null, "id", id); 190 border.appendChild(els); 191 return border; 192 } 193 194 public static final Element createImage(Document doc, String ref, 195 float width, float height) { 196 Element border = doc.createElementNS(svgNS, "image"); 197 border.setAttributeNS("http://www.w3.org/1999/xlink", "xlink:href", 198 ref); 199 border.setAttributeNS(null, "width", "" + width); 200 border.setAttributeNS(null, "height", "" + height); 201 return border; 202 } 203 204 207 public static final Element wrapText(Document doc, String str, 208 java.awt.Font font, float width) { 209 Element g = createG(doc); 210 Element text; 211 StringTokenizer st = new StringTokenizer(str, " \t\r\n"); 212 float totalWidth = 0; 213 String totalStr = ""; 214 int line = 0; 215 float height = getStringHeight(str, font); 216 while (st.hasMoreTokens()) { 217 String token = st.nextToken(); 218 float strwidth = getStringWidth(token, font); 219 totalWidth += strwidth; 220 if (totalWidth > width) { 221 if (totalStr.equals("")) { 222 totalStr = token; 223 token = ""; 224 strwidth = 0; 225 } 226 text = createText(doc, 0, line * (height + 5), totalStr); 227 g.appendChild(text); 228 totalStr = token; 229 totalWidth = strwidth; 230 line++; 231 } else { 232 totalStr = totalStr + " " + token; 233 } 234 } 235 236 return g; 237 } 238 239 } 240 | Popular Tags |