1 18 package org.apache.batik.dom.svg; 19 20 import org.apache.batik.parser.LengthParser; 21 import org.apache.batik.parser.ParseException; 22 import org.apache.batik.parser.UnitProcessor; 23 import org.w3c.dom.DOMException ; 24 import org.w3c.dom.Element ; 25 import org.w3c.dom.svg.SVGLength; 26 27 47 public abstract class AbstractSVGLength 48 implements SVGLength { 49 50 53 public final static short HORIZONTAL_LENGTH = 54 UnitProcessor.HORIZONTAL_LENGTH; 55 56 59 public final static short VERTICAL_LENGTH = 60 UnitProcessor.VERTICAL_LENGTH; 61 62 65 public final static short OTHER_LENGTH = 66 UnitProcessor.OTHER_LENGTH; 67 68 71 protected short unitType; 72 73 76 protected float value; 77 78 81 protected short direction; 82 83 86 protected UnitProcessor.Context context; 87 88 91 protected final static String [] UNITS = { 92 "", "", "%", "em", "ex", "px", "cm", "mm", "in", "pt", "pc" 93 }; 94 95 98 protected abstract SVGOMElement getAssociatedElement(); 99 100 101 104 public AbstractSVGLength(short direction) { 105 context = new DefaultContext(); 106 this.direction = direction; 107 this.value = 0.0f; 108 this.unitType = SVGLength.SVG_LENGTHTYPE_NUMBER; 109 } 110 111 114 public short getUnitType() { 115 revalidate(); 116 return unitType; 117 } 118 119 122 public float getValue() { 123 revalidate(); 124 return UnitProcessor.svgToUserSpace(value, unitType, 125 direction, context); 126 } 127 128 131 public void setValue(float value) throws DOMException { 132 revalidate(); 133 this.value = UnitProcessor.userSpaceToSVG(value, unitType, 134 direction, context); 135 reset(); 136 } 137 138 141 public float getValueInSpecifiedUnits() { 142 revalidate(); 143 return value; 144 } 145 146 150 public void setValueInSpecifiedUnits(float value) throws DOMException { 151 revalidate(); 152 this.value = value; 153 reset(); 154 } 155 156 159 public String getValueAsString() { 160 revalidate(); 161 return Float.toString(value)+UNITS[unitType]; 162 } 163 164 167 public void setValueAsString(String value) throws DOMException { 168 parse(value); 169 reset(); 170 } 171 172 176 public void newValueSpecifiedUnits(short unit, float value) { 177 unitType = unit; 178 this.value = value; 179 reset(); 180 } 181 182 186 public void convertToSpecifiedUnits(short unit) { 187 float v = getValue(); 188 unitType = unit; 189 setValue(v); 190 } 191 192 198 protected void reset() { 199 } 200 201 207 protected void revalidate() { 208 } 209 210 217 protected void parse(String s){ 218 try { 219 LengthParser lengthParser = new LengthParser(); 220 UnitProcessor.UnitResolver ur = 221 new UnitProcessor.UnitResolver(); 222 lengthParser.setLengthHandler(ur); 223 lengthParser.parse(s); 224 unitType = ur.unit; 225 value = ur.value; 226 } catch (ParseException e) { 227 unitType = SVG_LENGTHTYPE_UNKNOWN; 228 value = 0; 229 } 230 } 231 232 235 protected class DefaultContext implements UnitProcessor.Context { 236 237 240 public Element getElement() { 241 return getAssociatedElement(); 242 } 243 244 247 public float getPixelUnitToMillimeter() { 248 SVGContext ctx = getAssociatedElement().getSVGContext(); 249 return ctx.getPixelUnitToMillimeter(); 250 } 251 252 257 public float getPixelToMM() { 258 return getPixelUnitToMillimeter(); 259 } 260 261 264 public float getFontSize() { 265 return getAssociatedElement().getSVGContext().getFontSize(); 266 } 267 268 271 public float getXHeight() { 272 return 0.5f; 273 } 274 275 278 public float getViewportWidth() { 279 return getAssociatedElement().getSVGContext(). 280 getViewportWidth(); 281 } 282 283 286 public float getViewportHeight() { 287 return getAssociatedElement().getSVGContext(). 288 getViewportHeight(); 289 } 290 } 291 } 292 | Popular Tags |