You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SVGElement.java 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*
  2. * $Id$
  3. * Copyright (C) 2001 The Apache Software Foundation. All rights reserved.
  4. * For details on use and redistribution please refer to the
  5. * LICENSE file included with these sources.
  6. */
  7. package org.apache.fop.svg;
  8. // FOP
  9. import org.apache.fop.fo.*;
  10. import org.apache.fop.fo.properties.*;
  11. import org.apache.fop.layout.FontState;
  12. import org.apache.fop.apps.FOPException;
  13. import org.apache.batik.dom.svg.*;
  14. import org.apache.batik.dom.util.XMLSupport;
  15. import org.w3c.dom.*;
  16. import org.w3c.dom.svg.*;
  17. import org.w3c.dom.svg.SVGLength;
  18. import org.xml.sax.Attributes;
  19. import org.apache.batik.bridge.*;
  20. import org.apache.batik.swing.svg.*;
  21. import org.apache.batik.swing.gvt.*;
  22. import org.apache.batik.gvt.*;
  23. import org.apache.batik.gvt.renderer.*;
  24. import org.apache.batik.gvt.filter.*;
  25. import org.apache.batik.gvt.event.*;
  26. import org.apache.batik.bridge.UnitProcessor;
  27. import org.apache.batik.util.SVGConstants;
  28. import org.w3c.dom.DOMImplementation;
  29. import org.w3c.dom.css.CSSPrimitiveValue;
  30. import org.apache.batik.dom.svg.SVGDOMImplementation;
  31. import java.io.File;
  32. import java.net.URL;
  33. import java.util.List;
  34. import java.util.ArrayList;
  35. import java.awt.geom.AffineTransform;
  36. import java.awt.geom.Point2D;
  37. import java.awt.geom.Rectangle2D;
  38. import java.awt.Dimension;
  39. /**
  40. * class representing svg:svg pseudo flow object.
  41. */
  42. public class SVGElement extends SVGObj {
  43. /**
  44. * constructs an SVG object (called by Maker).
  45. *
  46. * @param parent the parent formatting object
  47. * @param propertyList the explicit properties of this object
  48. */
  49. public SVGElement(FONode parent) {
  50. super(parent);
  51. }
  52. public void handleAttrs(Attributes attlist) throws FOPException {
  53. super.handleAttrs(attlist);
  54. init();
  55. }
  56. public Point2D getDimension(final Point2D view) {
  57. // TODO - change so doesn't hold onto fo,area tree
  58. Element svgRoot = element;
  59. /* create an SVG area */
  60. /* if width and height are zero, get the bounds of the content. */
  61. try {
  62. String baseDir = userAgent.getBaseURL();
  63. if(baseDir != null) {
  64. ((SVGOMDocument)doc).setURLObject(new URL(baseDir));
  65. }
  66. } catch (Exception e) {
  67. getLogger().error("Could not set base URL for svg", e);
  68. }
  69. Element e = ((SVGDocument)doc).getRootElement();
  70. final float ptmm = userAgent.getPixelToMM();
  71. SVGContext dc = new SVGContext() {
  72. public float getPixelToMM() {
  73. return ptmm;
  74. }
  75. public float getPixelUnitToMillimeter() {
  76. return ptmm;
  77. }
  78. public Rectangle2D getBBox() {
  79. return new Rectangle2D.Double(0, 0, view.getX(), view.getY());
  80. }
  81. public AffineTransform getCTM() {
  82. return new AffineTransform();
  83. }
  84. public AffineTransform getGlobalTransform() {
  85. return new AffineTransform();
  86. }
  87. public float getViewportWidth() {
  88. return (float)view.getX();
  89. }
  90. public float getViewportHeight() {
  91. return (float)view.getY();
  92. }
  93. public float getFontSize(){
  94. return 12;
  95. }
  96. };
  97. ((SVGOMElement)e).setSVGContext(dc);
  98. //if(!e.hasAttributeNS(XMLSupport.XMLNS_NAMESPACE_URI, "xmlns")) {
  99. e.setAttributeNS(XMLSupport.XMLNS_NAMESPACE_URI, "xmlns", SVGDOMImplementation.SVG_NAMESPACE_URI);
  100. //}
  101. Point2D p2d = getSize(12 /* font size */, svgRoot, userAgent.getPixelToMM());
  102. ((SVGOMElement)e).setSVGContext(null);
  103. return p2d;
  104. }
  105. private void init() {
  106. DOMImplementation impl = SVGDOMImplementation.getDOMImplementation();
  107. String svgNS = SVGDOMImplementation.SVG_NAMESPACE_URI;
  108. doc = impl.createDocument(svgNS, "svg", null);
  109. element = doc.getDocumentElement();
  110. buildTopLevel(doc, element);
  111. }
  112. public static Point2D getSize(int size, Element svgRoot, float ptmm) {
  113. String str;
  114. UnitProcessor.Context ctx;
  115. ctx = new PDFUnitContext(size, svgRoot, ptmm);
  116. str = svgRoot.getAttributeNS(null, SVGConstants.SVG_WIDTH_ATTRIBUTE);
  117. if (str.length() == 0) str = "100%";
  118. float width = UnitProcessor.svgHorizontalLengthToUserSpace
  119. (str, SVGConstants.SVG_WIDTH_ATTRIBUTE, ctx);
  120. str = svgRoot.getAttributeNS(null, SVGConstants.SVG_HEIGHT_ATTRIBUTE);
  121. if (str.length() == 0) str = "100%";
  122. float height = UnitProcessor.svgVerticalLengthToUserSpace
  123. (str, SVGConstants.SVG_HEIGHT_ATTRIBUTE, ctx);
  124. return new Point2D.Float(width, height);
  125. }
  126. /**
  127. * This class is the default context for a particular
  128. * element. Informations not available on the element are get from
  129. * the bridge context (such as the viewport or the pixel to
  130. * millimeter factor.
  131. */
  132. public static class PDFUnitContext implements UnitProcessor.Context {
  133. /** The element. */
  134. protected Element e;
  135. protected int fontSize;
  136. float pixeltoMM;
  137. public PDFUnitContext(int size, Element e, float ptmm) {
  138. this.e = e;
  139. this.fontSize = size;
  140. }
  141. /**
  142. * Returns the element.
  143. */
  144. public Element getElement() {
  145. return e;
  146. }
  147. /**
  148. * Returns the context of the parent element of this context.
  149. * Since this is always for the root SVG element there never
  150. * should be one...
  151. */
  152. public UnitProcessor.Context getParentElementContext() {
  153. return null;
  154. }
  155. /**
  156. * Returns the pixel to mm factor.
  157. */
  158. public float getPixelToMM() {
  159. return pixeltoMM;
  160. }
  161. public float getPixelUnitToMillimeter() {
  162. return pixeltoMM;
  163. }
  164. /**
  165. * Returns the font-size medium value in pt.
  166. */
  167. public float getMediumFontSize() {
  168. return 9f;
  169. }
  170. /**
  171. * Returns the font-size value.
  172. */
  173. public float getFontSize() {
  174. return fontSize;
  175. }
  176. /**
  177. * Returns the x-height value.
  178. */
  179. public float getXHeight() {
  180. return 0.5f;
  181. }
  182. /**
  183. * Returns the viewport width used to compute units.
  184. */
  185. public float getViewportWidth() {
  186. return 100;
  187. }
  188. /**
  189. * Returns the viewport height used to compute units.
  190. */
  191. public float getViewportHeight() {
  192. return 100;
  193. }
  194. }
  195. }