1 18 19 package org.apache.batik.parser; 20 21 import org.w3c.dom.Element ; 22 import org.w3c.dom.svg.SVGLength; 23 24 32 public abstract class UnitProcessor { 33 34 37 public final static short HORIZONTAL_LENGTH = 2; 38 39 42 public final static short VERTICAL_LENGTH = 1; 43 44 47 public final static short OTHER_LENGTH = 0; 48 49 52 protected UnitProcessor() { } 53 54 55 64 public static float svgToObjectBoundingBox(String s, 65 String attr, 66 short d, 67 Context ctx) 68 throws ParseException { 69 LengthParser lengthParser = new LengthParser(); 70 UnitResolver ur = new UnitResolver(); 71 lengthParser.setLengthHandler(ur); 72 lengthParser.parse(s); 73 return svgToObjectBoundingBox(ur.value, ur.unit, d, ctx); 74 } 75 76 85 public static float svgToObjectBoundingBox(float value, 86 short type, 87 short d, 88 Context ctx) { 89 switch (type) { 90 case SVGLength.SVG_LENGTHTYPE_NUMBER: 91 return value; 93 case SVGLength.SVG_LENGTHTYPE_PERCENTAGE: 94 return value / 100f; 97 case SVGLength.SVG_LENGTHTYPE_PX: 98 case SVGLength.SVG_LENGTHTYPE_MM: 99 case SVGLength.SVG_LENGTHTYPE_CM: 100 case SVGLength.SVG_LENGTHTYPE_IN: 101 case SVGLength.SVG_LENGTHTYPE_PT: 102 case SVGLength.SVG_LENGTHTYPE_PC: 103 case SVGLength.SVG_LENGTHTYPE_EMS: 104 case SVGLength.SVG_LENGTHTYPE_EXS: 105 return svgToUserSpace(value, type, d, ctx); 108 default: 109 throw new Error (); } 111 } 112 113 117 118 127 public static float svgToUserSpace(String s, 128 String attr, 129 short d, 130 Context ctx) throws ParseException { 131 LengthParser lengthParser = new LengthParser(); 132 UnitResolver ur = new UnitResolver(); 133 lengthParser.setLengthHandler(ur); 134 lengthParser.parse(s); 135 return svgToUserSpace(ur.value, ur.unit, d, ctx); 136 } 137 138 147 public static float svgToUserSpace(float v, 148 short type, 149 short d, 150 Context ctx) { 151 switch (type) { 152 case SVGLength.SVG_LENGTHTYPE_NUMBER: 153 case SVGLength.SVG_LENGTHTYPE_PX: 154 return v; 155 case SVGLength.SVG_LENGTHTYPE_MM: 156 return (v / ctx.getPixelUnitToMillimeter()); 157 case SVGLength.SVG_LENGTHTYPE_CM: 158 return (v * 10f / ctx.getPixelUnitToMillimeter()); 159 case SVGLength.SVG_LENGTHTYPE_IN: 160 return (v * 25.4f / ctx.getPixelUnitToMillimeter()); 161 case SVGLength.SVG_LENGTHTYPE_PT: 162 return (v * 25.4f / (72f * ctx.getPixelUnitToMillimeter())); 163 case SVGLength.SVG_LENGTHTYPE_PC: 164 return (v * 25.4f / (6f * ctx.getPixelUnitToMillimeter())); 165 case SVGLength.SVG_LENGTHTYPE_EMS: 166 return emsToPixels(v, d, ctx); 167 case SVGLength.SVG_LENGTHTYPE_EXS: 168 return exsToPixels(v, d, ctx); 169 case SVGLength.SVG_LENGTHTYPE_PERCENTAGE: 170 return percentagesToPixels(v, d, ctx); 171 default: 172 throw new Error (); } 174 } 175 176 185 public static float userSpaceToSVG(float v, 186 short type, 187 short d, 188 Context ctx) { 189 switch (type) { 190 case SVGLength.SVG_LENGTHTYPE_NUMBER: 191 case SVGLength.SVG_LENGTHTYPE_PX: 192 return v; 193 case SVGLength.SVG_LENGTHTYPE_MM: 194 return (v * ctx.getPixelUnitToMillimeter()); 195 case SVGLength.SVG_LENGTHTYPE_CM: 196 return (v * ctx.getPixelUnitToMillimeter() / 10f); 197 case SVGLength.SVG_LENGTHTYPE_IN: 198 return (v * ctx.getPixelUnitToMillimeter() / 25.4f); 199 case SVGLength.SVG_LENGTHTYPE_PT: 200 return (v * (72f * ctx.getPixelUnitToMillimeter()) / 25.4f); 201 case SVGLength.SVG_LENGTHTYPE_PC: 202 return (v * (6f * ctx.getPixelUnitToMillimeter()) / 25.4f); 203 case SVGLength.SVG_LENGTHTYPE_EMS: 204 return pixelsToEms(v, d, ctx); 205 case SVGLength.SVG_LENGTHTYPE_EXS: 206 return pixelsToExs(v, d, ctx); 207 case SVGLength.SVG_LENGTHTYPE_PERCENTAGE: 208 return pixelsToPercentages(v, d, ctx); 209 default: 210 throw new Error (); } 212 } 213 214 218 225 protected static float percentagesToPixels(float v, short d, Context ctx) { 226 if (d == HORIZONTAL_LENGTH) { 227 float w = ctx.getViewportWidth(); 228 return w * v / 100f; 229 } else if (d == VERTICAL_LENGTH) { 230 float h = ctx.getViewportHeight(); 231 return h * v / 100f; 232 } else { 233 double w = ctx.getViewportWidth(); 234 double h = ctx.getViewportHeight(); 235 double vpp = Math.sqrt(w * w + h * h) / Math.sqrt(2); 236 return (float)(vpp * v / 100d); 237 } 238 } 239 240 247 protected static float pixelsToPercentages(float v, short d, Context ctx) { 248 if (d == HORIZONTAL_LENGTH) { 249 float w = ctx.getViewportWidth(); 250 return v * 100f / w; 251 } else if (d == VERTICAL_LENGTH) { 252 float h = ctx.getViewportHeight(); 253 return v * 100f / h; 254 } else { 255 double w = ctx.getViewportWidth(); 256 double h = ctx.getViewportHeight(); 257 double vpp = Math.sqrt(w * w + h * h) / Math.sqrt(2); 258 return (float)(v * 100d / vpp); 259 } 260 } 261 262 269 protected static float pixelsToEms(float v, short d, Context ctx) { 270 return v / ctx.getFontSize(); 271 } 272 273 280 protected static float emsToPixels(float v, short d, Context ctx) { 281 return v * ctx.getFontSize(); 282 } 283 284 291 protected static float pixelsToExs(float v, short d, Context ctx) { 292 float xh = ctx.getXHeight(); 293 return v / xh / ctx.getFontSize(); 294 } 295 296 303 protected static float exsToPixels(float v, short d, Context ctx) { 304 float xh = ctx.getXHeight(); 305 return v * xh * ctx.getFontSize(); 306 } 307 308 309 312 public static class UnitResolver implements LengthHandler { 313 314 317 public float value; 318 319 322 public short unit = SVGLength.SVG_LENGTHTYPE_NUMBER; 323 324 327 public void startLength() throws ParseException { 328 } 329 330 333 public void lengthValue(float v) throws ParseException { 334 this.value = v; 335 } 336 337 340 public void em() throws ParseException { 341 this.unit = SVGLength.SVG_LENGTHTYPE_EMS; 342 } 343 344 347 public void ex() throws ParseException { 348 this.unit = SVGLength.SVG_LENGTHTYPE_EXS; 349 } 350 351 354 public void in() throws ParseException { 355 this.unit = SVGLength.SVG_LENGTHTYPE_IN; 356 } 357 358 361 public void cm() throws ParseException { 362 this.unit = SVGLength.SVG_LENGTHTYPE_CM; 363 } 364 365 368 public void mm() throws ParseException { 369 this.unit = SVGLength.SVG_LENGTHTYPE_MM; 370 } 371 372 375 public void pc() throws ParseException { 376 this.unit = SVGLength.SVG_LENGTHTYPE_PC; 377 } 378 379 382 public void pt() throws ParseException { 383 this.unit = SVGLength.SVG_LENGTHTYPE_PT; 384 } 385 386 389 public void px() throws ParseException { 390 this.unit = SVGLength.SVG_LENGTHTYPE_PX; 391 } 392 393 396 public void percentage() throws ParseException { 397 this.unit = SVGLength.SVG_LENGTHTYPE_PERCENTAGE; 398 } 399 400 403 public void endLength() throws ParseException { 404 } 405 } 406 407 410 public interface Context { 411 412 415 Element getElement(); 416 417 420 float getPixelUnitToMillimeter(); 421 422 427 float getPixelToMM(); 428 429 432 float getFontSize(); 433 434 437 float getXHeight(); 438 439 442 float getViewportWidth(); 443 444 447 float getViewportHeight(); 448 } 449 } 450 | Popular Tags |