diff options
Diffstat (limited to 'src/org/apache/fop/svg/SVGElement.java')
-rw-r--r-- | src/org/apache/fop/svg/SVGElement.java | 124 |
1 files changed, 102 insertions, 22 deletions
diff --git a/src/org/apache/fop/svg/SVGElement.java b/src/org/apache/fop/svg/SVGElement.java index cf4fd9c63..c30177de8 100644 --- a/src/org/apache/fop/svg/SVGElement.java +++ b/src/org/apache/fop/svg/SVGElement.java @@ -29,8 +29,14 @@ import org.apache.batik.gvt.*; import org.apache.batik.gvt.renderer.*; import org.apache.batik.gvt.filter.*; import org.apache.batik.gvt.event.*; +import org.apache.batik.bridge.UnitProcessor; +import org.apache.batik.css.value.ImmutableFloat; +import org.apache.batik.css.CSSOMReadOnlyValue; +import org.apache.batik.util.SVGConstants; import org.w3c.dom.DOMImplementation; +import org.w3c.dom.css.CSSPrimitiveValue; + import org.apache.batik.dom.svg.SVGDOMImplementation; import java.io.File; @@ -38,6 +44,7 @@ import java.net.URL; import java.util.List; import java.util.ArrayList; import java.awt.geom.AffineTransform; +import java.awt.geom.Point2D; /** * class representing svg:svg pseudo flow object. @@ -127,29 +134,10 @@ public class SVGElement extends SVGObj { e.setAttributeNS(XMLSupport.XMLNS_NAMESPACE_URI, "xmlns", SVGDOMImplementation.SVG_NAMESPACE_URI); //} - String s; - SVGUserAgent userAgent = new SVGUserAgent(new AffineTransform()); - userAgent.setLogger(log); - BridgeContext ctx = new BridgeContext(userAgent); - UnitProcessor.Context uctx = UnitProcessor.createContext(ctx, e); + Point2D p2d = getSize(this.fs, svgRoot); - // 'width' attribute - default is 100% - s = e.getAttributeNS(null, SVGOMDocument.SVG_WIDTH_ATTRIBUTE); - if (s.length() == 0) { - s = SVGOMDocument.SVG_SVG_WIDTH_DEFAULT_VALUE; - } - float width = UnitProcessor.svgHorizontalLengthToUserSpace - (s, SVGOMDocument.SVG_WIDTH_ATTRIBUTE, uctx); - - // 'height' attribute - default is 100% - s = e.getAttributeNS(null, SVGOMDocument.SVG_HEIGHT_ATTRIBUTE); - if (s.length() == 0) { - s = SVGOMDocument.SVG_SVG_HEIGHT_DEFAULT_VALUE; - } - float height = UnitProcessor.svgVerticalLengthToUserSpace - (s, SVGOMDocument.SVG_HEIGHT_ATTRIBUTE, uctx); - - SVGArea svg = new SVGArea(fs, width, height); + SVGArea svg = new SVGArea(fs, (float)p2d.getX(), + (float)p2d.getY()); svg.setSVGDocument(doc); svg.start(); @@ -186,4 +174,96 @@ public class SVGElement extends SVGObj { FONT_FAMILY.add("serif"); } + public static Point2D getSize(FontState fs, Element svgRoot) { + String str; + UnitProcessor.Context ctx; + ctx = new PDFUnitContext(fs, svgRoot); + str = svgRoot.getAttributeNS(null, SVGConstants.SVG_WIDTH_ATTRIBUTE); + if (str.length() == 0) str = "100%"; + float width = UnitProcessor.svgHorizontalLengthToUserSpace + (str, SVGConstants.SVG_WIDTH_ATTRIBUTE, ctx); + + str = svgRoot.getAttributeNS(null, SVGConstants.SVG_HEIGHT_ATTRIBUTE); + if (str.length() == 0) str = "100%"; + float height = UnitProcessor.svgVerticalLengthToUserSpace + (str, SVGConstants.SVG_HEIGHT_ATTRIBUTE, ctx); + return new Point2D.Float(width, height); + } + /** + * This class is the default context for a particular + * element. Informations not available on the element are get from + * the bridge context (such as the viewport or the pixel to + * millimeter factor. + */ + public static class PDFUnitContext implements UnitProcessor.Context { + + /** The element. */ + protected Element e; + protected FontState fs; + public PDFUnitContext(FontState fs, Element e) { + this.e = e; + this.fs = fs; + } + + /** + * Returns the element. + */ + public Element getElement() { + return e; + } + + /** + * Returns the context of the parent element of this context. + * Since this is always for the root SVG element there never + * should be one... + */ + public UnitProcessor.Context getParentElementContext() { + return null; + } + + /** + * Returns the pixel to mm factor. + */ + public float getPixelToMM() { + return 0.264583333333333333333f; + // 72 dpi + } + + /** + * Returns the font-size medium value in pt. + */ + public float getMediumFontSize() { + return 9f; + } + + /** + * Returns the font-size value. + */ + public CSSPrimitiveValue getFontSize() { + return new CSSOMReadOnlyValue + (new ImmutableFloat(CSSPrimitiveValue.CSS_PT, + fs.getFontSize())); + } + + /** + * Returns the x-height value. + */ + public float getXHeight() { + return 0.5f; + } + + /** + * Returns the viewport width used to compute units. + */ + public float getViewportWidth() { + return 100; + } + + /** + * Returns the viewport height used to compute units. + */ + public float getViewportHeight() { + return 100; + } + } } |