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 8.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /*
  2. * Copyright 1999-2005 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /* $Id$ */
  17. package org.apache.fop.fo.extensions.svg;
  18. // FOP
  19. import org.apache.fop.apps.FOPException;
  20. import org.apache.fop.fo.FONode;
  21. import org.apache.fop.fo.PropertyList;
  22. import org.apache.batik.dom.svg.SVGOMDocument;
  23. import org.apache.batik.dom.svg.SVGOMElement;
  24. import org.apache.batik.dom.svg.SVGContext;
  25. import org.apache.batik.dom.util.XMLSupport;
  26. import org.w3c.dom.Element;
  27. import org.w3c.dom.svg.SVGDocument;
  28. import org.xml.sax.Attributes;
  29. import org.xml.sax.Locator;
  30. import org.apache.batik.bridge.UnitProcessor;
  31. import org.apache.batik.util.SVGConstants;
  32. import org.w3c.dom.DOMImplementation;
  33. import org.apache.batik.dom.svg.SVGDOMImplementation;
  34. import java.net.URL;
  35. import java.awt.geom.AffineTransform;
  36. import java.awt.geom.Point2D;
  37. import java.awt.geom.Rectangle2D;
  38. /**
  39. * class representing the SVG root element
  40. * for constructing an svg document.
  41. */
  42. public class SVGElement extends SVGObj {
  43. /**
  44. * Constructs an SVG object
  45. *
  46. * @param parent the parent formatting object
  47. */
  48. public SVGElement(FONode parent) {
  49. super(parent);
  50. }
  51. /**
  52. * @see org.apache.fop.fo.FONode#processNode
  53. */
  54. public void processNode(String elementName, Locator locator,
  55. Attributes attlist, PropertyList propertyList) throws FOPException {
  56. super.processNode(elementName, locator, attlist, propertyList);
  57. init();
  58. }
  59. /**
  60. * Get the dimensions of this XML document.
  61. * @param view the viewport dimensions
  62. * @return the dimensions of this SVG document
  63. */
  64. public Point2D getDimension(final Point2D view) {
  65. // TODO change so doesn't hold onto fo, area tree
  66. Element svgRoot = element;
  67. /* create an SVG area */
  68. /* if width and height are zero, get the bounds of the content. */
  69. try {
  70. URL baseURL = new URL(getUserAgent().getBaseURL() == null
  71. ? new java.io.File("").toURL().toExternalForm()
  72. : getUserAgent().getBaseURL());
  73. if (baseURL != null) {
  74. SVGOMDocument svgdoc = (SVGOMDocument)doc;
  75. svgdoc.setURLObject(baseURL);
  76. //The following line should not be called to leave FOP compatible to Batik 1.6.
  77. //svgdoc.setDocumentURI(baseURL.toString());
  78. }
  79. } catch (Exception e) {
  80. getLogger().error("Could not set base URL for svg", e);
  81. }
  82. Element e = ((SVGDocument)doc).getRootElement();
  83. final float ptmm = getUserAgent().getSourcePixelUnitToMillimeter();
  84. // temporary svg context
  85. SVGContext dc = new SVGContext() {
  86. public float getPixelToMM() {
  87. return ptmm;
  88. }
  89. public float getPixelUnitToMillimeter() {
  90. return ptmm;
  91. }
  92. public Rectangle2D getBBox() {
  93. return new Rectangle2D.Double(0, 0, view.getX(), view.getY());
  94. }
  95. /**
  96. * Returns the transform from the global transform space to pixels.
  97. */
  98. public AffineTransform getScreenTransform() {
  99. throw new UnsupportedOperationException("NYI");
  100. }
  101. /**
  102. * Sets the transform to be used from the global transform space
  103. * to pixels.
  104. */
  105. public void setScreenTransform(AffineTransform at) {
  106. throw new UnsupportedOperationException("NYI");
  107. }
  108. public AffineTransform getCTM() {
  109. return new AffineTransform();
  110. }
  111. public AffineTransform getGlobalTransform() {
  112. return new AffineTransform();
  113. }
  114. public float getViewportWidth() {
  115. return (float)view.getX();
  116. }
  117. public float getViewportHeight() {
  118. return (float)view.getY();
  119. }
  120. public float getFontSize() {
  121. return 12;
  122. }
  123. public void deselectAll() {
  124. }
  125. };
  126. ((SVGOMElement)e).setSVGContext(dc);
  127. //if (!e.hasAttributeNS(XMLSupport.XMLNS_NAMESPACE_URI, "xmlns")) {
  128. e.setAttributeNS(XMLSupport.XMLNS_NAMESPACE_URI, "xmlns",
  129. SVGDOMImplementation.SVG_NAMESPACE_URI);
  130. //}
  131. int fontSize = 12;
  132. Point2D p2d = getSize(fontSize, svgRoot, getUserAgent().getSourcePixelUnitToMillimeter());
  133. ((SVGOMElement)e).setSVGContext(null);
  134. return p2d;
  135. }
  136. private void init() {
  137. DOMImplementation impl = SVGDOMImplementation.getDOMImplementation();
  138. String svgNS = SVGDOMImplementation.SVG_NAMESPACE_URI;
  139. doc = impl.createDocument(svgNS, "svg", null);
  140. element = doc.getDocumentElement();
  141. buildTopLevel(doc, element);
  142. }
  143. /**
  144. * Get the size of the SVG root element.
  145. * @param size the font size
  146. * @param svgRoot the svg root element
  147. * @param ptmm the pixel to millimeter conversion factor
  148. * @return the size of the SVG document
  149. */
  150. public static Point2D getSize(int size, Element svgRoot, float ptmm) {
  151. String str;
  152. UnitProcessor.Context ctx;
  153. ctx = new PDFUnitContext(size, svgRoot, ptmm);
  154. str = svgRoot.getAttributeNS(null, SVGConstants.SVG_WIDTH_ATTRIBUTE);
  155. if (str.length() == 0) {
  156. str = "100%";
  157. }
  158. float width = UnitProcessor.svgHorizontalLengthToUserSpace
  159. (str, SVGConstants.SVG_WIDTH_ATTRIBUTE, ctx);
  160. str = svgRoot.getAttributeNS(null, SVGConstants.SVG_HEIGHT_ATTRIBUTE);
  161. if (str.length() == 0) {
  162. str = "100%";
  163. }
  164. float height = UnitProcessor.svgVerticalLengthToUserSpace
  165. (str, SVGConstants.SVG_HEIGHT_ATTRIBUTE, ctx);
  166. return new Point2D.Float(width, height);
  167. }
  168. /**
  169. * This class is the default context for a particular
  170. * element. Information not available on the element are obtained from
  171. * the bridge context (such as the viewport or the pixel to
  172. * millimeter factor.
  173. */
  174. public static class PDFUnitContext implements UnitProcessor.Context {
  175. /** The element. */
  176. private Element e;
  177. private int fontSize;
  178. private float pixeltoMM;
  179. /**
  180. * Create a PDF unit context.
  181. * @param size the font size.
  182. * @param e the svg element
  183. * @param ptmm the pixel to millimeter factor
  184. */
  185. public PDFUnitContext(int size, Element e, float ptmm) {
  186. this.e = e;
  187. this.fontSize = size;
  188. this.pixeltoMM = ptmm;
  189. }
  190. /**
  191. * Returns the element.
  192. * @return the element
  193. */
  194. public Element getElement() {
  195. return e;
  196. }
  197. /**
  198. * Returns the context of the parent element of this context.
  199. * Since this is always for the root SVG element there never
  200. * should be one...
  201. * @return null
  202. */
  203. public UnitProcessor.Context getParentElementContext() {
  204. return null;
  205. }
  206. /**
  207. * Returns the pixel to mm factor. (this is deprecated)
  208. * @return the pixel to millimeter factor
  209. */
  210. public float getPixelToMM() {
  211. return pixeltoMM;
  212. }
  213. /**
  214. * Returns the pixel to mm factor.
  215. * @return the pixel to millimeter factor
  216. */
  217. public float getPixelUnitToMillimeter() {
  218. return pixeltoMM;
  219. }
  220. /**
  221. * Returns the font-size value.
  222. * @return the default font size
  223. */
  224. public float getFontSize() {
  225. return fontSize;
  226. }
  227. /**
  228. * Returns the x-height value.
  229. * @return the x-height value
  230. */
  231. public float getXHeight() {
  232. return 0.5f;
  233. }
  234. /**
  235. * Returns the viewport width used to compute units.
  236. * @return the default viewport width of 100
  237. */
  238. public float getViewportWidth() {
  239. return 100;
  240. }
  241. /**
  242. * Returns the viewport height used to compute units.
  243. * @return the default viewport height of 100
  244. */
  245. public float getViewportHeight() {
  246. return 100;
  247. }
  248. }
  249. }