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.

SVGReader.java 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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.image.analyser;
  8. // Java
  9. import java.io.BufferedInputStream;
  10. import java.io.IOException;
  11. import org.w3c.dom.svg.SVGDocument;
  12. import org.w3c.dom.svg.SVGSVGElement;
  13. // FOP
  14. import org.apache.fop.messaging.*;
  15. import org.apache.fop.image.SVGImage;
  16. import org.xml.sax.InputSource;
  17. import org.xml.sax.XMLReader;
  18. import org.apache.batik.dom.svg.SAXSVGDocumentFactory;
  19. import org.apache.batik.dom.svg.*;
  20. import org.w3c.dom.*;
  21. import org.w3c.dom.svg.*;
  22. import org.w3c.dom.svg.SVGLength;
  23. import org.apache.batik.bridge.*;
  24. import org.apache.batik.swing.svg.*;
  25. import org.apache.batik.swing.gvt.*;
  26. import org.apache.batik.gvt.*;
  27. import org.apache.batik.gvt.renderer.*;
  28. import org.apache.batik.gvt.filter.*;
  29. import org.apache.batik.gvt.event.*;
  30. import org.w3c.dom.DOMImplementation;
  31. import org.apache.batik.dom.svg.SVGDOMImplementation;
  32. import java.io.File;
  33. import java.net.URL;
  34. import java.util.List;
  35. import java.util.ArrayList;
  36. import java.awt.geom.AffineTransform;
  37. import java.awt.Point;
  38. import java.awt.geom.Dimension2D;
  39. import java.awt.Dimension;
  40. /**
  41. * ImageReader object for SVG document image type.
  42. */
  43. public class SVGReader extends AbstractImageReader {
  44. public boolean verifySignature(String uri,
  45. BufferedInputStream fis) throws IOException {
  46. this.imageStream = fis;
  47. return loadImage(uri);
  48. }
  49. public String getMimeType() {
  50. return "image/svg+xml";
  51. }
  52. /**
  53. * This means the external svg document will be loaded twice.
  54. * Possibly need a slightly different design for the image stuff.
  55. */
  56. protected boolean loadImage(String uri) {
  57. // parse document and get the size attributes of the svg element
  58. try {
  59. SAXSVGDocumentFactory factory =
  60. new SAXSVGDocumentFactory(SVGImage.getParserName());
  61. SVGDocument doc = factory.createDocument(uri, imageStream);
  62. Element e = ((SVGDocument)doc).getRootElement();
  63. String s;
  64. UserAgent userAgent = new MUserAgent(new AffineTransform());
  65. BridgeContext ctx = new BridgeContext(userAgent);
  66. UnitProcessor.Context uctx = UnitProcessor.createContext(ctx, e);
  67. // 'width' attribute - default is 100%
  68. s = e.getAttributeNS(null, SVGOMDocument.SVG_WIDTH_ATTRIBUTE);
  69. if (s.length() == 0) {
  70. s = SVGOMDocument.SVG_SVG_WIDTH_DEFAULT_VALUE;
  71. }
  72. width = (int)UnitProcessor.svgHorizontalLengthToUserSpace
  73. (s, SVGOMDocument.SVG_WIDTH_ATTRIBUTE, uctx);
  74. // 'height' attribute - default is 100%
  75. s = e.getAttributeNS(null, SVGOMDocument.SVG_HEIGHT_ATTRIBUTE);
  76. if (s.length() == 0) {
  77. s = SVGOMDocument.SVG_SVG_HEIGHT_DEFAULT_VALUE;
  78. }
  79. height = (int)UnitProcessor.svgVerticalLengthToUserSpace
  80. (s, SVGOMDocument.SVG_HEIGHT_ATTRIBUTE, uctx);
  81. return true;
  82. } catch (NoClassDefFoundError ncdfe) {
  83. //log.error("Batik not in class path");
  84. return false;
  85. }
  86. catch (Exception e) {
  87. //log.error("Could not load external SVG: " +
  88. // e.getMessage());
  89. // assuming any exception means this document is not svg
  90. // or could not be loaded for some reason
  91. return false;
  92. }
  93. }
  94. protected class MUserAgent implements UserAgent {
  95. AffineTransform currentTransform = null;
  96. /**
  97. * Creates a new SVGUserAgent.
  98. */
  99. protected MUserAgent(AffineTransform at) {
  100. currentTransform = at;
  101. }
  102. /**
  103. * Displays an error message.
  104. */
  105. public void displayError(String message) {
  106. System.err.println(message);
  107. }
  108. /**
  109. * Displays an error resulting from the specified Exception.
  110. */
  111. public void displayError(Exception ex) {
  112. ex.printStackTrace(System.err);
  113. }
  114. /**
  115. * Displays a message in the User Agent interface.
  116. * The given message is typically displayed in a status bar.
  117. */
  118. public void displayMessage(String message) {
  119. System.out.println(message);
  120. }
  121. /**
  122. * Returns a customized the pixel to mm factor.
  123. */
  124. public float getPixelToMM() {
  125. // this is set to 72dpi as the values in fo are 72dpi
  126. return 0.35277777777777777778f; // 72 dpi
  127. // return 0.26458333333333333333333333333333f; // 96dpi
  128. }
  129. /**
  130. * Returns the language settings.
  131. */
  132. public String getLanguages() {
  133. return "en"; // userLanguages;
  134. }
  135. public String getMedia() {
  136. return "print";
  137. }
  138. /**
  139. * Returns the user stylesheet uri.
  140. * @return null if no user style sheet was specified.
  141. */
  142. public String getUserStyleSheetURI() {
  143. return null; // userStyleSheetURI;
  144. }
  145. /**
  146. * Returns the class name of the XML parser.
  147. */
  148. public String getXMLParserClassName() {
  149. return org.apache.fop.apps.Driver.getParserClassName();
  150. }
  151. public boolean isXMLParserValidating() {
  152. return false;
  153. }
  154. /**
  155. * Opens a link in a new component.
  156. * @param doc The current document.
  157. * @param uri The document URI.
  158. */
  159. public void openLink(SVGAElement elt) {
  160. }
  161. public Point getClientAreaLocationOnScreen() {
  162. return new Point(0, 0);
  163. }
  164. public void setSVGCursor(java.awt.Cursor cursor) {}
  165. public AffineTransform getTransform() {
  166. return currentTransform;
  167. }
  168. public Dimension2D getViewportSize() {
  169. return new Dimension(100, 100);
  170. }
  171. public EventDispatcher getEventDispatcher() {
  172. return null;
  173. }
  174. public boolean supportExtension(String str) {
  175. return false;
  176. }
  177. public boolean hasFeature(String str) {
  178. return false;
  179. }
  180. public void registerExtension(BridgeExtension be) {}
  181. public void handleElement(Element elt, Object data) {}
  182. }
  183. }