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.2KB

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