1 18 package org.apache.batik.bridge; 19 20 import org.apache.batik.css.engine.SVGCSSEngine; 21 import org.apache.batik.parser.ParseException; 22 import org.w3c.dom.Element ; 23 24 32 public abstract class UnitProcessor 33 extends org.apache.batik.parser.UnitProcessor { 34 35 38 protected UnitProcessor() { } 39 40 47 public static Context createContext(BridgeContext ctx, Element e) { 48 return new DefaultContext(ctx, e); 49 } 50 51 55 63 public static 64 float svgHorizontalCoordinateToObjectBoundingBox(String s, 65 String attr, 66 Context ctx) { 67 return svgToObjectBoundingBox(s, attr, HORIZONTAL_LENGTH, ctx); 68 } 69 70 78 public static 79 float svgVerticalCoordinateToObjectBoundingBox(String s, 80 String attr, 81 Context ctx) { 82 return svgToObjectBoundingBox(s, attr, VERTICAL_LENGTH, ctx); 83 } 84 85 93 public static 94 float svgOtherCoordinateToObjectBoundingBox(String s, 95 String attr, 96 Context ctx) { 97 return svgToObjectBoundingBox(s, attr, OTHER_LENGTH, ctx); 98 } 99 100 108 public static 109 float svgHorizontalLengthToObjectBoundingBox(String s, 110 String attr, 111 Context ctx) { 112 return svgLengthToObjectBoundingBox(s, attr, HORIZONTAL_LENGTH, ctx); 113 } 114 115 123 public static 124 float svgVerticalLengthToObjectBoundingBox(String s, 125 String attr, 126 Context ctx) { 127 return svgLengthToObjectBoundingBox(s, attr, VERTICAL_LENGTH, ctx); 128 } 129 130 138 public static 139 float svgOtherLengthToObjectBoundingBox(String s, 140 String attr, 141 Context ctx) { 142 return svgLengthToObjectBoundingBox(s, attr, OTHER_LENGTH, ctx); 143 } 144 145 154 public static float svgLengthToObjectBoundingBox(String s, 155 String attr, 156 short d, 157 Context ctx) { 158 float v = svgToObjectBoundingBox(s, attr, d, ctx); 159 if (v < 0) { 160 throw new BridgeException(ctx.getElement(), 161 ErrorConstants.ERR_LENGTH_NEGATIVE, 162 new Object [] {attr, s}); 163 } 164 return v; 165 } 166 167 176 public static float svgToObjectBoundingBox(String s, 177 String attr, 178 short d, 179 Context ctx) { 180 try { 181 return org.apache.batik.parser.UnitProcessor. 182 svgToObjectBoundingBox(s, attr, d, ctx); 183 } catch (ParseException ex) { 184 throw new BridgeException(ctx.getElement(), 185 ErrorConstants.ERR_ATTRIBUTE_VALUE_MALFORMED, 186 new Object [] {attr, s, ex}); 187 } 188 } 189 190 194 195 203 public static float svgHorizontalLengthToUserSpace(String s, 204 String attr, 205 Context ctx) { 206 return svgLengthToUserSpace(s, attr, HORIZONTAL_LENGTH, ctx); 207 } 208 209 217 public static float svgVerticalLengthToUserSpace(String s, 218 String attr, 219 Context ctx) { 220 return svgLengthToUserSpace(s, attr, VERTICAL_LENGTH, ctx); 221 } 222 223 231 public static float svgOtherLengthToUserSpace(String s, 232 String attr, 233 Context ctx) { 234 return svgLengthToUserSpace(s, attr, OTHER_LENGTH, ctx); 235 } 236 237 244 public static float svgHorizontalCoordinateToUserSpace(String s, 245 String attr, 246 Context ctx) { 247 return svgToUserSpace(s, attr, HORIZONTAL_LENGTH, ctx); 248 } 249 250 257 public static float svgVerticalCoordinateToUserSpace(String s, 258 String attr, 259 Context ctx) { 260 return svgToUserSpace(s, attr, VERTICAL_LENGTH, ctx); 261 } 262 263 270 public static float svgOtherCoordinateToUserSpace(String s, 271 String attr, 272 Context ctx) { 273 return svgToUserSpace(s, attr, OTHER_LENGTH, ctx); 274 } 275 276 285 public static float svgLengthToUserSpace(String s, 286 String attr, 287 short d, 288 Context ctx) { 289 float v = svgToUserSpace(s, attr, d, ctx); 290 if (v < 0) { 291 throw new BridgeException(ctx.getElement(), 292 ErrorConstants.ERR_LENGTH_NEGATIVE, 293 new Object [] {attr, s}); 294 } else { 295 return v; 296 } 297 } 298 299 308 public static float svgToUserSpace(String s, 309 String attr, 310 short d, 311 Context ctx) { 312 try { 313 return org.apache.batik.parser.UnitProcessor. 314 svgToUserSpace(s, attr, d, ctx); 315 } catch (ParseException ex) { 316 throw new BridgeException(ctx.getElement(), 317 ErrorConstants.ERR_ATTRIBUTE_VALUE_MALFORMED, 318 new Object [] {attr, s, ex}); 319 } 320 } 321 322 328 public static class DefaultContext implements Context { 329 330 331 protected Element e; 332 protected BridgeContext ctx; 333 334 public DefaultContext(BridgeContext ctx, Element e) { 335 this.ctx = ctx; 336 this.e = e; 337 } 338 339 342 public Element getElement() { 343 return e; 344 } 345 346 349 public float getPixelUnitToMillimeter() { 350 return ctx.getUserAgent().getPixelUnitToMillimeter(); 351 } 352 353 358 public float getPixelToMM() { 359 return getPixelUnitToMillimeter(); 360 361 } 362 363 366 public float getFontSize() { 367 return CSSUtilities.getComputedStyle 368 (e, SVGCSSEngine.FONT_SIZE_INDEX).getFloatValue(); 369 } 370 371 374 public float getXHeight() { 375 return 0.5f; 376 } 377 378 381 public float getViewportWidth() { 382 return ctx.getViewport(e).getWidth(); 383 } 384 385 388 public float getViewportHeight() { 389 return ctx.getViewport(e).getHeight(); 390 } 391 } 392 } 393 | Popular Tags |