001: /*
002:
003: Licensed to the Apache Software Foundation (ASF) under one or more
004: contributor license agreements. See the NOTICE file distributed with
005: this work for additional information regarding copyright ownership.
006: The ASF licenses this file to You under the Apache License, Version 2.0
007: (the "License"); you may not use this file except in compliance with
008: the License. You may obtain a copy of the License at
009:
010: http://www.apache.org/licenses/LICENSE-2.0
011:
012: Unless required by applicable law or agreed to in writing, software
013: distributed under the License is distributed on an "AS IS" BASIS,
014: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: See the License for the specific language governing permissions and
016: limitations under the License.
017:
018: */
019: package org.apache.batik.bridge;
020:
021: import org.apache.batik.css.engine.SVGCSSEngine;
022: import org.apache.batik.parser.ParseException;
023: import org.w3c.dom.Element;
024:
025: /**
026: * This class provides methods to convert SVG length and coordinate to
027: * float in user units.
028: *
029: * @author <a href="mailto:stephane@hillion.org">Stephane Hillion</a>
030: * @author <a href="mailto:tkormann@apache.org">Thierry Kormann</a>
031: * @version $Id: UnitProcessor.java 501922 2007-01-31 17:47:47Z dvholten $
032: */
033: public abstract class UnitProcessor extends
034: org.apache.batik.parser.UnitProcessor {
035:
036: /**
037: * Creates a context for the specified element.
038: *
039: * @param ctx the bridge context that contains the user agent and
040: * viewport definition
041: * @param e the element interested in its context
042: */
043: public static Context createContext(BridgeContext ctx, Element e) {
044: return new DefaultContext(ctx, e);
045: }
046:
047: /////////////////////////////////////////////////////////////////////////
048: // SVG methods - objectBoundingBox
049: /////////////////////////////////////////////////////////////////////////
050:
051: /**
052: * Returns the specified horizontal coordinate in object bounding box
053: * coordinate system.
054: *
055: * @param s the horizontal coordinate
056: * @param attr the attribute name that represents the coordinate
057: * @param ctx the context used to resolve relative value
058: */
059: public static float svgHorizontalCoordinateToObjectBoundingBox(
060: String s, String attr, Context ctx) {
061: return svgToObjectBoundingBox(s, attr, HORIZONTAL_LENGTH, ctx);
062: }
063:
064: /**
065: * Returns the specified vertical coordinate in object bounding box
066: * coordinate system.
067: *
068: * @param s the vertical coordinate
069: * @param attr the attribute name that represents the coordinate
070: * @param ctx the context used to resolve relative value
071: */
072: public static float svgVerticalCoordinateToObjectBoundingBox(
073: String s, String attr, Context ctx) {
074: return svgToObjectBoundingBox(s, attr, VERTICAL_LENGTH, ctx);
075: }
076:
077: /**
078: * Returns the specified 'other' coordinate in object bounding box
079: * coordinate system.
080: *
081: * @param s the 'other' coordinate
082: * @param attr the attribute name that represents the coordinate
083: * @param ctx the context used to resolve relative value
084: */
085: public static float svgOtherCoordinateToObjectBoundingBox(String s,
086: String attr, Context ctx) {
087: return svgToObjectBoundingBox(s, attr, OTHER_LENGTH, ctx);
088: }
089:
090: /**
091: * Returns the specified horizontal length in object bounding box
092: * coordinate system. A length must be greater than 0.
093: *
094: * @param s the 'other' length
095: * @param attr the attribute name that represents the length
096: * @param ctx the context used to resolve relative value
097: */
098: public static float svgHorizontalLengthToObjectBoundingBox(
099: String s, String attr, Context ctx) {
100: return svgLengthToObjectBoundingBox(s, attr, HORIZONTAL_LENGTH,
101: ctx);
102: }
103:
104: /**
105: * Returns the specified vertical length in object bounding box
106: * coordinate system. A length must be greater than 0.
107: *
108: * @param s the vertical length
109: * @param attr the attribute name that represents the length
110: * @param ctx the context used to resolve relative value
111: */
112: public static float svgVerticalLengthToObjectBoundingBox(String s,
113: String attr, Context ctx) {
114: return svgLengthToObjectBoundingBox(s, attr, VERTICAL_LENGTH,
115: ctx);
116: }
117:
118: /**
119: * Returns the specified 'other' length in object bounding box
120: * coordinate system. A length must be greater than 0.
121: *
122: * @param s the 'other' length
123: * @param attr the attribute name that represents the length
124: * @param ctx the context used to resolve relative value
125: */
126: public static float svgOtherLengthToObjectBoundingBox(String s,
127: String attr, Context ctx) {
128: return svgLengthToObjectBoundingBox(s, attr, OTHER_LENGTH, ctx);
129: }
130:
131: /**
132: * Returns the specified length with the specified direction in
133: * user units. A length must be greater than 0.
134: *
135: * @param s the length
136: * @param attr the attribute name that represents the length
137: * @param d the direction of the length
138: * @param ctx the context used to resolve relative value
139: */
140: public static float svgLengthToObjectBoundingBox(String s,
141: String attr, short d, Context ctx) {
142: float v = svgToObjectBoundingBox(s, attr, d, ctx);
143: if (v < 0) {
144: throw new BridgeException(getBridgeContext(ctx), ctx
145: .getElement(), ErrorConstants.ERR_LENGTH_NEGATIVE,
146: new Object[] { attr, s });
147: }
148: return v;
149: }
150:
151: /**
152: * Returns the specified value with the specified direction in
153: * objectBoundingBox units.
154: *
155: * @param s the value
156: * @param attr the attribute name that represents the value
157: * @param d the direction of the value
158: * @param ctx the context used to resolve relative value
159: */
160: public static float svgToObjectBoundingBox(String s, String attr,
161: short d, Context ctx) {
162: try {
163: return org.apache.batik.parser.UnitProcessor
164: .svgToObjectBoundingBox(s, attr, d, ctx);
165: } catch (ParseException pEx) {
166: throw new BridgeException(getBridgeContext(ctx), ctx
167: .getElement(), pEx,
168: ErrorConstants.ERR_ATTRIBUTE_VALUE_MALFORMED,
169: new Object[] { attr, s, pEx });
170: }
171: }
172:
173: /////////////////////////////////////////////////////////////////////////
174: // SVG methods - userSpace
175: /////////////////////////////////////////////////////////////////////////
176:
177: /**
178: * Returns the specified horizontal length in user units. A length
179: * must be greater than 0.
180: *
181: * @param s the horizontal length
182: * @param attr the attribute name that represents the length
183: * @param ctx the context used to resolve relative value
184: */
185: public static float svgHorizontalLengthToUserSpace(String s,
186: String attr, Context ctx) {
187: return svgLengthToUserSpace(s, attr, HORIZONTAL_LENGTH, ctx);
188: }
189:
190: /**
191: * Returns the specified vertical length in user units. A length
192: * must be greater than 0.
193: *
194: * @param s the vertical length
195: * @param attr the attribute name that represents the length
196: * @param ctx the context used to resolve relative value
197: */
198: public static float svgVerticalLengthToUserSpace(String s,
199: String attr, Context ctx) {
200: return svgLengthToUserSpace(s, attr, VERTICAL_LENGTH, ctx);
201: }
202:
203: /**
204: * Returns the specified 'other' length in user units. A length
205: * must be greater than 0.
206: *
207: * @param s the 'other' length
208: * @param attr the attribute name that represents the length
209: * @param ctx the context used to resolve relative value
210: */
211: public static float svgOtherLengthToUserSpace(String s,
212: String attr, Context ctx) {
213: return svgLengthToUserSpace(s, attr, OTHER_LENGTH, ctx);
214: }
215:
216: /**
217: * Returns the specified horizontal coordinate in user units.
218: *
219: * @param s the horizontal coordinate
220: * @param attr the attribute name that represents the length
221: * @param ctx the context used to resolve relative value
222: */
223: public static float svgHorizontalCoordinateToUserSpace(String s,
224: String attr, Context ctx) {
225: return svgToUserSpace(s, attr, HORIZONTAL_LENGTH, ctx);
226: }
227:
228: /**
229: * Returns the specified vertical coordinate in user units.
230: *
231: * @param s the vertical coordinate
232: * @param attr the attribute name that represents the length
233: * @param ctx the context used to resolve relative value
234: */
235: public static float svgVerticalCoordinateToUserSpace(String s,
236: String attr, Context ctx) {
237: return svgToUserSpace(s, attr, VERTICAL_LENGTH, ctx);
238: }
239:
240: /**
241: * Returns the specified 'other' coordinate in user units.
242: *
243: * @param s the 'other' coordinate
244: * @param attr the attribute name that represents the length
245: * @param ctx the context used to resolve relative value
246: */
247: public static float svgOtherCoordinateToUserSpace(String s,
248: String attr, Context ctx) {
249: return svgToUserSpace(s, attr, OTHER_LENGTH, ctx);
250: }
251:
252: /**
253: * Returns the specified length with the specified direction in
254: * user units. A length must be greater than 0.
255: *
256: * @param s the 'other' coordinate
257: * @param attr the attribute name that represents the length
258: * @param d the direction of the length
259: * @param ctx the context used to resolve relative value
260: */
261: public static float svgLengthToUserSpace(String s, String attr,
262: short d, Context ctx) {
263: float v = svgToUserSpace(s, attr, d, ctx);
264: if (v < 0) {
265: throw new BridgeException(getBridgeContext(ctx), ctx
266: .getElement(), ErrorConstants.ERR_LENGTH_NEGATIVE,
267: new Object[] { attr, s });
268: } else {
269: return v;
270: }
271: }
272:
273: /**
274: * Returns the specified coordinate with the specified direction
275: * in user units.
276: *
277: * @param s the 'other' coordinate
278: * @param attr the attribute name that represents the length
279: * @param d the direction of the coordinate
280: * @param ctx the context used to resolve relative value
281: */
282: public static float svgToUserSpace(String s, String attr, short d,
283: Context ctx) {
284: try {
285: return org.apache.batik.parser.UnitProcessor
286: .svgToUserSpace(s, attr, d, ctx);
287: } catch (ParseException pEx) {
288: throw new BridgeException(getBridgeContext(ctx), ctx
289: .getElement(), pEx,
290: ErrorConstants.ERR_ATTRIBUTE_VALUE_MALFORMED,
291: new Object[] { attr, s, pEx, });
292: }
293: }
294:
295: /**
296: * Returns the {@link BridgeContext} from the given {@link Context}
297: * if it is a {@link DefaultContext}, or null otherwise.
298: */
299: protected static BridgeContext getBridgeContext(Context ctx) {
300: if (ctx instanceof DefaultContext) {
301: return ((DefaultContext) ctx).ctx;
302: }
303: return null;
304: }
305:
306: /**
307: * This class is the default context for a particular element. Information
308: * not available on the element are obtained from the bridge context (such
309: * as the viewport or the pixel to millimeter factor).
310: */
311: public static class DefaultContext implements Context {
312:
313: /**
314: * The element.
315: */
316: protected Element e;
317:
318: /**
319: * The bridge context.
320: */
321: protected BridgeContext ctx;
322:
323: /**
324: * Creates a new DefaultContext.
325: */
326: public DefaultContext(BridgeContext ctx, Element e) {
327: this .ctx = ctx;
328: this .e = e;
329: }
330:
331: /**
332: * Returns the element.
333: */
334: public Element getElement() {
335: return e;
336: }
337:
338: /**
339: * Returns the size of a px CSS unit in millimeters.
340: */
341: public float getPixelUnitToMillimeter() {
342: return ctx.getUserAgent().getPixelUnitToMillimeter();
343: }
344:
345: /**
346: * Returns the size of a px CSS unit in millimeters.
347: * This will be removed after next release.
348: * @see #getPixelUnitToMillimeter()
349: */
350: public float getPixelToMM() {
351: return getPixelUnitToMillimeter();
352:
353: }
354:
355: /**
356: * Returns the font-size value.
357: */
358: public float getFontSize() {
359: return CSSUtilities.getComputedStyle(e,
360: SVGCSSEngine.FONT_SIZE_INDEX).getFloatValue();
361: }
362:
363: /**
364: * Returns the x-height value.
365: */
366: public float getXHeight() {
367: return 0.5f;
368: }
369:
370: /**
371: * Returns the viewport width used to compute units.
372: */
373: public float getViewportWidth() {
374: return ctx.getViewport(e).getWidth();
375: }
376:
377: /**
378: * Returns the viewport height used to compute units.
379: */
380: public float getViewportHeight() {
381: return ctx.getViewport(e).getHeight();
382: }
383: }
384: }
|