Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

SVGElement.java 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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.io.File;
  24. import java.net.URI;
  25. import org.w3c.dom.Element;
  26. import org.apache.batik.bridge.UnitProcessor;
  27. import org.apache.batik.dom.svg.SVGContext;
  28. import org.apache.batik.dom.svg.SVGDOMImplementation;
  29. import org.apache.batik.dom.svg.SVGOMDocument;
  30. import org.apache.batik.dom.svg.SVGOMElement;
  31. import org.apache.batik.dom.util.XMLSupport;
  32. import org.apache.batik.util.SVGConstants;
  33. import org.apache.fop.fo.FONode;
  34. import org.apache.fop.util.ContentHandlerFactory;
  35. /**
  36. * Class representing the SVG root element
  37. * for constructing an SVG document.
  38. */
  39. public class SVGElement extends SVGObj {
  40. /**
  41. * Constructs an SVG object
  42. *
  43. * @param parent the parent formatting object
  44. */
  45. public SVGElement(FONode parent) {
  46. super(parent);
  47. }
  48. /** {@inheritDoc} */
  49. public ContentHandlerFactory getContentHandlerFactory() {
  50. return new SVGDOMContentHandlerFactory();
  51. }
  52. /**
  53. * Get the dimensions of this XML document.
  54. * @param view the viewport dimensions
  55. * @return the dimensions of this SVG document
  56. */
  57. public Point2D getDimension(final Point2D view) {
  58. // TODO change so doesn't hold onto fo, area tree
  59. Element svgRoot = element;
  60. /* create an SVG area */
  61. /* if width and height are zero, get the bounds of the content. */
  62. try {
  63. URI baseUri = getUserAgent().getNewURIResolver().getBaseURI();
  64. baseUri = baseUri == null ? new File("").toURI() : baseUri;
  65. if (baseUri != null) {
  66. SVGOMDocument svgdoc = (SVGOMDocument)doc;
  67. svgdoc.setURLObject(baseUri.toURL());
  68. //The following line should not be called to leave FOP compatible to Batik 1.6.
  69. //svgdoc.setDocumentURI(baseURL.toString());
  70. }
  71. } catch (Exception e) {
  72. log.error("Could not set base URL for svg", e);
  73. }
  74. final float ptmm = getUserAgent().getSourcePixelUnitToMillimeter();
  75. // temporary svg context
  76. SVGContext dc = new SVGContext() {
  77. public float getPixelToMM() {
  78. return ptmm;
  79. }
  80. public float getPixelUnitToMillimeter() {
  81. return ptmm;
  82. }
  83. public Rectangle2D getBBox() {
  84. return new Rectangle2D.Double(0, 0, view.getX(), view.getY());
  85. }
  86. /**
  87. * Returns the transform from the global transform space to pixels.
  88. */
  89. public AffineTransform getScreenTransform() {
  90. throw new UnsupportedOperationException("NYI");
  91. }
  92. /**
  93. * Sets the transform to be used from the global transform space
  94. * to pixels.
  95. */
  96. public void setScreenTransform(AffineTransform at) {
  97. throw new UnsupportedOperationException("NYI");
  98. }
  99. public AffineTransform getCTM() {
  100. return new AffineTransform();
  101. }
  102. public AffineTransform getGlobalTransform() {
  103. return new AffineTransform();
  104. }
  105. public float getViewportWidth() {
  106. return (float)view.getX();
  107. }
  108. public float getViewportHeight() {
  109. return (float)view.getY();
  110. }
  111. public float getFontSize() {
  112. return 12;
  113. }
  114. public void deselectAll() {
  115. }
  116. };
  117. SVGOMElement e = (SVGOMElement)svgRoot;
  118. e.setSVGContext(dc);
  119. //if (!e.hasAttributeNS(XMLSupport.XMLNS_NAMESPACE_URI, "xmlns")) {
  120. e.setAttributeNS(XMLSupport.XMLNS_NAMESPACE_URI, "xmlns",
  121. SVGDOMImplementation.SVG_NAMESPACE_URI);
  122. //}
  123. int fontSize = 12;
  124. Point2D p2d = getSize(fontSize, svgRoot, getUserAgent().getSourcePixelUnitToMillimeter());
  125. e.setSVGContext(null);
  126. return p2d;
  127. }
  128. /**
  129. * Get the size of the SVG root element.
  130. * @param size the font size
  131. * @param svgRoot the svg root element
  132. * @param ptmm the pixel to millimeter conversion factor
  133. * @return the size of the SVG document
  134. */
  135. public static Point2D getSize(int size, Element svgRoot, float ptmm) {
  136. String str;
  137. UnitProcessor.Context ctx;
  138. ctx = new PDFUnitContext(size, svgRoot, ptmm);
  139. str = svgRoot.getAttributeNS(null, SVGConstants.SVG_WIDTH_ATTRIBUTE);
  140. if (str.length() == 0) {
  141. str = "100%";
  142. }
  143. float width = UnitProcessor.svgHorizontalLengthToUserSpace
  144. (str, SVGConstants.SVG_WIDTH_ATTRIBUTE, ctx);
  145. str = svgRoot.getAttributeNS(null, SVGConstants.SVG_HEIGHT_ATTRIBUTE);
  146. if (str.length() == 0) {
  147. str = "100%";
  148. }
  149. float height = UnitProcessor.svgVerticalLengthToUserSpace
  150. (str, SVGConstants.SVG_HEIGHT_ATTRIBUTE, ctx);
  151. return new Point2D.Float(width, height);
  152. }
  153. /**
  154. * This class is the default context for a particular
  155. * element. Information not available on the element are obtained from
  156. * the bridge context (such as the viewport or the pixel to
  157. * millimeter factor.
  158. */
  159. public static class PDFUnitContext implements UnitProcessor.Context {
  160. /** The element. */
  161. private Element e;
  162. private int fontSize;
  163. private float pixeltoMM;
  164. /**
  165. * Create a PDF unit context.
  166. * @param size the font size.
  167. * @param e the svg element
  168. * @param ptmm the pixel to millimeter factor
  169. */
  170. public PDFUnitContext(int size, Element e, float ptmm) {
  171. this.e = e;
  172. this.fontSize = size;
  173. this.pixeltoMM = ptmm;
  174. }
  175. /**
  176. * Returns the element.
  177. * @return the element
  178. */
  179. public Element getElement() {
  180. return e;
  181. }
  182. /**
  183. * Returns the context of the parent element of this context.
  184. * Since this is always for the root SVG element there never
  185. * should be one...
  186. * @return null
  187. */
  188. public UnitProcessor.Context getParentElementContext() {
  189. return null;
  190. }
  191. /**
  192. * Returns the pixel to mm factor. (this is deprecated)
  193. * @return the pixel to millimeter factor
  194. */
  195. public float getPixelToMM() {
  196. return pixeltoMM;
  197. }
  198. /**
  199. * Returns the pixel to mm factor.
  200. * @return the pixel to millimeter factor
  201. */
  202. public float getPixelUnitToMillimeter() {
  203. return pixeltoMM;
  204. }
  205. /**
  206. * Returns the font-size value.
  207. * @return the default font size
  208. */
  209. public float getFontSize() {
  210. return fontSize;
  211. }
  212. /**
  213. * Returns the x-height value.
  214. * @return the x-height value
  215. */
  216. public float getXHeight() {
  217. return 0.5f;
  218. }
  219. /**
  220. * Returns the viewport width used to compute units.
  221. * @return the default viewport width of 100
  222. */
  223. public float getViewportWidth() {
  224. return 100;
  225. }
  226. /**
  227. * Returns the viewport height used to compute units.
  228. * @return the default viewport height of 100
  229. */
  230. public float getViewportHeight() {
  231. return 100;
  232. }
  233. }
  234. }