1 18 package org.apache.batik.dom.svg; 19 20 import java.awt.geom.Point2D ; 21 import java.awt.geom.Rectangle2D ; 22 23 import org.w3c.dom.DOMException ; 24 import org.w3c.dom.Element ; 25 import org.w3c.dom.svg.SVGMatrix; 26 import org.w3c.dom.svg.SVGPoint; 27 import org.w3c.dom.svg.SVGRect; 28 29 35 public class SVGTextContentSupport 36 { 37 38 42 public static int getNumberOfChars(Element elt) 43 { 44 final SVGOMElement svgelt = (SVGOMElement)elt; 45 46 return (((SVGTextContent)svgelt.getSVGContext()).getNumberOfChars()); 47 } 48 49 50 54 public static SVGRect getExtentOfChar(Element elt, final int charnum ) { 55 final SVGOMElement svgelt = (SVGOMElement)elt; 56 57 if ( (charnum < 0) || 58 (charnum >= getNumberOfChars(elt)) ){ 59 throw svgelt.createDOMException 60 (DOMException.INDEX_SIZE_ERR, 61 "",null); 62 } 63 64 final SVGTextContent context = (SVGTextContent)svgelt.getSVGContext(); 65 Rectangle2D r2d = getExtent(svgelt, context, charnum); 66 67 return new SVGRect() { 68 public float getX() { 69 return (float)SVGTextContentSupport.getExtent 70 (svgelt, context, charnum).getX(); 71 } 72 public void setX(float x) throws DOMException { 73 throw svgelt.createDOMException 74 (DOMException.NO_MODIFICATION_ALLOWED_ERR, 75 "readonly.rect", null); 76 } 77 78 public float getY() { 79 return (float)SVGTextContentSupport.getExtent 80 (svgelt, context, charnum).getY(); 81 } 82 public void setY(float y) throws DOMException { 83 throw svgelt.createDOMException 84 (DOMException.NO_MODIFICATION_ALLOWED_ERR, 85 "readonly.rect", null); 86 } 87 88 public float getWidth() { 89 return (float)SVGTextContentSupport.getExtent 90 (svgelt, context, charnum).getWidth(); 91 } 92 public void setWidth(float width) throws DOMException { 93 throw svgelt.createDOMException 94 (DOMException.NO_MODIFICATION_ALLOWED_ERR, 95 "readonly.rect", null); 96 } 97 98 public float getHeight() { 99 return (float)SVGTextContentSupport.getExtent 100 (svgelt, context, charnum).getHeight(); 101 } 102 public void setHeight(float height) throws DOMException { 103 throw svgelt.createDOMException 104 (DOMException.NO_MODIFICATION_ALLOWED_ERR, 105 "readonly.rect", null); 106 } 107 }; 108 } 109 110 protected static Rectangle2D getExtent 111 (SVGOMElement svgelt, SVGTextContent context, int charnum) { 112 Rectangle2D r2d = context.getExtentOfChar(charnum); 113 if (r2d == null) throw svgelt.createDOMException 114 (DOMException.INDEX_SIZE_ERR, "",null); 115 return r2d; 116 } 117 118 122 public static SVGPoint getStartPositionOfChar 123 (Element elt, final int charnum) throws DOMException { 124 125 final SVGOMElement svgelt = (SVGOMElement)elt; 126 127 if ( (charnum < 0) || 128 (charnum >= getNumberOfChars(elt)) ){ 129 throw svgelt.createDOMException 130 (DOMException.INDEX_SIZE_ERR, 131 "",null); 132 } 133 134 final SVGTextContent context = (SVGTextContent)svgelt.getSVGContext(); 135 Point2D p2d = getStartPos(svgelt, context, charnum); 136 137 return new SVGTextPoint(svgelt){ 138 public float getX(){ 139 return (float)SVGTextContentSupport.getStartPos 140 (this.svgelt, context, charnum).getX(); 141 } 142 public float getY(){ 143 return (float)SVGTextContentSupport.getStartPos 144 (this.svgelt, context, charnum).getY(); 145 } 146 }; 147 } 148 149 protected static Point2D getStartPos 150 (SVGOMElement svgelt, SVGTextContent context, int charnum) { 151 Point2D p2d = context.getStartPositionOfChar(charnum); 152 if (p2d == null) throw svgelt.createDOMException 153 (DOMException.INDEX_SIZE_ERR, "",null); 154 return p2d; 155 } 156 157 161 public static SVGPoint getEndPositionOfChar 162 (Element elt,final int charnum) throws DOMException { 163 164 final SVGOMElement svgelt = (SVGOMElement)elt; 165 166 if ( (charnum < 0) || 167 (charnum >= getNumberOfChars(elt)) ){ 168 throw svgelt.createDOMException 169 (DOMException.INDEX_SIZE_ERR, 170 "",null); 171 } 172 173 final SVGTextContent context = (SVGTextContent)svgelt.getSVGContext(); 174 Point2D p2d = getEndPos(svgelt, context, charnum); 175 176 return new SVGTextPoint(svgelt){ 177 public float getX(){ 178 return (float)SVGTextContentSupport.getEndPos 179 (this.svgelt, context, charnum).getX(); 180 } 181 public float getY(){ 182 return (float)SVGTextContentSupport.getEndPos 183 (this.svgelt, context, charnum).getY(); 184 } 185 }; 186 } 187 188 protected static Point2D getEndPos 189 (SVGOMElement svgelt, SVGTextContent context, int charnum) { 190 Point2D p2d = context.getEndPositionOfChar(charnum); 191 if (p2d == null) throw svgelt.createDOMException 192 (DOMException.INDEX_SIZE_ERR, "",null); 193 return p2d; 194 } 195 196 200 public static void selectSubString(Element elt, int charnum, int nchars){ 201 202 final SVGOMElement svgelt = (SVGOMElement)elt; 203 204 if ( (charnum < 0) || 205 (charnum >= getNumberOfChars(elt)) ){ 206 throw svgelt.createDOMException 207 (DOMException.INDEX_SIZE_ERR, 208 "",null); 209 } 210 211 final SVGTextContent context = (SVGTextContent)svgelt.getSVGContext(); 212 213 context.selectSubString(charnum, nchars); 214 } 215 216 220 public static float getRotationOfChar(Element elt, final int charnum ) { 221 final SVGOMElement svgelt = (SVGOMElement)elt; 222 223 if ( (charnum < 0) || 224 (charnum >= getNumberOfChars(elt)) ){ 225 throw svgelt.createDOMException 226 (DOMException.INDEX_SIZE_ERR, 227 "",null); 228 } 229 230 final SVGTextContent context = (SVGTextContent)svgelt.getSVGContext(); 231 232 return context.getRotationOfChar(charnum); 233 } 234 235 239 public static float getComputedTextLength(Element elt){ 240 241 final SVGOMElement svgelt = (SVGOMElement)elt; 242 243 final SVGTextContent context = (SVGTextContent)svgelt.getSVGContext(); 244 245 return context.getComputedTextLength(); 246 } 247 248 252 public static float getSubStringLength(Element elt, int charnum, int nchars){ 253 254 final SVGOMElement svgelt = (SVGOMElement)elt; 255 256 if ( (charnum < 0) || 257 (charnum >= getNumberOfChars(elt)) ){ 258 throw svgelt.createDOMException 259 (DOMException.INDEX_SIZE_ERR, 260 "",null); 261 } 262 263 final SVGTextContent context = (SVGTextContent)svgelt.getSVGContext(); 264 265 return context.getSubStringLength(charnum,nchars); 266 } 267 268 272 public static int getCharNumAtPosition(Element elt, final float x, final float y) throws DOMException { 273 274 final SVGOMElement svgelt = (SVGOMElement)elt; 275 276 final SVGTextContent context = (SVGTextContent)svgelt.getSVGContext(); 277 278 return context.getCharNumAtPosition(x,y); 279 } 280 281 public static class SVGTextPoint extends SVGOMPoint { 282 SVGOMElement svgelt; 283 SVGTextPoint(SVGOMElement elem) { 284 svgelt = elem; 285 } 286 public void setX(float x) throws DOMException { 287 throw svgelt.createDOMException 288 (DOMException.NO_MODIFICATION_ALLOWED_ERR, 289 "readonly.point", null); 290 } 291 public void setY(float y) throws DOMException { 292 throw svgelt.createDOMException 293 (DOMException.NO_MODIFICATION_ALLOWED_ERR, 294 "readonly.point", null); 295 } 296 } 297 298 } 299 | Popular Tags |