123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233 |
- /*
- * $Id$
- * Copyright (C) 2001 The Apache Software Foundation. All rights reserved.
- * For details on use and redistribution please refer to the
- * LICENSE file included with these sources.
- */
-
- package org.apache.fop.svg;
-
- // FOP
- import org.apache.fop.fo.*;
- import org.apache.fop.fo.properties.*;
- import org.apache.fop.layout.FontState;
- import org.apache.fop.apps.FOPException;
-
- import org.apache.batik.dom.svg.*;
- import org.apache.batik.dom.util.XMLSupport;
- import org.w3c.dom.*;
- import org.w3c.dom.svg.*;
- import org.w3c.dom.svg.SVGLength;
- import org.xml.sax.Attributes;
- import org.apache.batik.bridge.*;
- import org.apache.batik.swing.svg.*;
- import org.apache.batik.swing.gvt.*;
- 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.util.SVGConstants;
-
- import org.w3c.dom.DOMImplementation;
- import org.w3c.dom.css.CSSPrimitiveValue;
-
- import org.apache.batik.dom.svg.SVGDOMImplementation;
-
- import java.io.File;
- import java.net.URL;
- import java.util.List;
- import java.util.ArrayList;
- import java.awt.geom.AffineTransform;
- import java.awt.geom.Point2D;
- import java.awt.geom.Rectangle2D;
- import java.awt.Dimension;
-
- /**
- * class representing svg:svg pseudo flow object.
- */
- public class SVGElement extends SVGObj {
-
- /**
- * constructs an SVG object (called by Maker).
- *
- * @param parent the parent formatting object
- * @param propertyList the explicit properties of this object
- */
- public SVGElement(FONode parent) {
- super(parent);
- }
-
- public void handleAttrs(Attributes attlist) throws FOPException {
- super.handleAttrs(attlist);
- init();
- }
-
- public Point2D getDimension(final Point2D view) {
-
- // TODO - change so doesn't hold onto fo,area tree
- Element svgRoot = element;
- /* create an SVG area */
- /* if width and height are zero, get the bounds of the content. */
-
- try {
- String baseDir = userAgent.getBaseURL();
- if(baseDir != null) {
- ((SVGOMDocument)doc).setURLObject(new URL(baseDir));
- }
- } catch (Exception e) {
- getLogger().error("Could not set base URL for svg", e);
- }
-
- Element e = ((SVGDocument)doc).getRootElement();
- final float ptmm = userAgent.getPixelToMM();
- SVGContext dc = new SVGContext() {
- public float getPixelToMM() {
- return ptmm;
- }
- public float getPixelUnitToMillimeter() {
- return ptmm;
- }
-
- public Rectangle2D getBBox() {
- return new Rectangle2D.Double(0, 0, view.getX(), view.getY());
- }
-
- public AffineTransform getCTM() {
- return new AffineTransform();
- }
-
- public AffineTransform getGlobalTransform() {
- return new AffineTransform();
- }
-
- public float getViewportWidth() {
- return (float)view.getX();
- }
-
- public float getViewportHeight() {
- return (float)view.getY();
- }
-
- public float getFontSize(){
- return 12;
- }
- };
- ((SVGOMElement)e).setSVGContext(dc);
-
- //if(!e.hasAttributeNS(XMLSupport.XMLNS_NAMESPACE_URI, "xmlns")) {
- e.setAttributeNS(XMLSupport.XMLNS_NAMESPACE_URI, "xmlns", SVGDOMImplementation.SVG_NAMESPACE_URI);
- //}
-
- Point2D p2d = getSize(12 /* font size */, svgRoot, userAgent.getPixelToMM());
- ((SVGOMElement)e).setSVGContext(null);
-
- return p2d;
- }
-
- private void init() {
- DOMImplementation impl = SVGDOMImplementation.getDOMImplementation();
- String svgNS = SVGDOMImplementation.SVG_NAMESPACE_URI;
- doc = impl.createDocument(svgNS, "svg", null);
-
- element = doc.getDocumentElement();
-
- buildTopLevel(doc, element);
- }
-
- public static Point2D getSize(int size, Element svgRoot, float ptmm) {
- String str;
- UnitProcessor.Context ctx;
- ctx = new PDFUnitContext(size, svgRoot, ptmm);
- 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 int fontSize;
- float pixeltoMM;
-
- public PDFUnitContext(int size, Element e, float ptmm) {
- this.e = e;
- this.fontSize = size;
- }
-
- /**
- * 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 pixeltoMM;
- }
- public float getPixelUnitToMillimeter() {
- return pixeltoMM;
- }
-
- /**
- * Returns the font-size medium value in pt.
- */
- public float getMediumFontSize() {
- return 9f;
- }
-
- /**
- * Returns the font-size value.
- */
- public float getFontSize() {
- return fontSize;
- }
-
- /**
- * 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;
- }
- }
- }
|