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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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.image.SVGImage;
  15. import org.xml.sax.InputSource;
  16. import org.xml.sax.XMLReader;
  17. import org.apache.batik.dom.svg.SAXSVGDocumentFactory;
  18. import org.apache.batik.dom.svg.*;
  19. import org.w3c.dom.*;
  20. import org.w3c.dom.svg.*;
  21. import org.w3c.dom.svg.SVGLength;
  22. import org.apache.batik.bridge.*;
  23. import org.apache.batik.swing.svg.*;
  24. import org.apache.batik.swing.gvt.*;
  25. import org.apache.batik.gvt.*;
  26. import org.apache.batik.gvt.renderer.*;
  27. import org.apache.batik.gvt.filter.*;
  28. import org.apache.batik.gvt.event.*;
  29. import org.w3c.dom.DOMImplementation;
  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.Point;
  37. import java.awt.geom.Dimension2D;
  38. import java.awt.Dimension;
  39. import org.apache.fop.fo.FOUserAgent;
  40. import org.apache.fop.svg.SVGUserAgent;
  41. /**
  42. * ImageReader object for SVG document image type.
  43. */
  44. public class SVGReader extends AbstractImageReader {
  45. public static final String SVG_MIME_TYPE = "image/svg+xml";
  46. FOUserAgent userAgent;
  47. SVGDocument doc;
  48. public boolean verifySignature(String uri, BufferedInputStream fis,
  49. FOUserAgent ua) throws IOException {
  50. this.imageStream = fis;
  51. userAgent = ua;
  52. return loadImage(uri);
  53. }
  54. public String getMimeType() {
  55. return SVG_MIME_TYPE;
  56. }
  57. public SVGDocument getDocument() {
  58. return doc;
  59. }
  60. /**
  61. * This means the external svg document will be loaded twice.
  62. * Possibly need a slightly different design for the image stuff.
  63. */
  64. protected boolean loadImage(String uri) {
  65. // parse document and get the size attributes of the svg element
  66. try {
  67. int length = imageStream.available();
  68. imageStream.mark(length);
  69. SAXSVGDocumentFactory factory =
  70. new SAXSVGDocumentFactory(SVGImage.getParserName());
  71. doc = factory.createDocument(uri, imageStream);
  72. Element e = ((SVGDocument) doc).getRootElement();
  73. String s;
  74. SVGUserAgent userAg = new SVGUserAgent(new AffineTransform());
  75. userAg.setLogger(userAgent.getLogger());
  76. BridgeContext ctx = new BridgeContext(userAg);
  77. UnitProcessor.Context uctx =
  78. UnitProcessor.createContext(ctx, e);
  79. // 'width' attribute - default is 100%
  80. s = e.getAttributeNS(null, SVGOMDocument.SVG_WIDTH_ATTRIBUTE);
  81. if (s.length() == 0) {
  82. s = SVGOMDocument.SVG_SVG_WIDTH_DEFAULT_VALUE;
  83. }
  84. width = (int) UnitProcessor.svgHorizontalLengthToUserSpace (
  85. s, SVGOMDocument.SVG_WIDTH_ATTRIBUTE, uctx);
  86. // 'height' attribute - default is 100%
  87. s = e.getAttributeNS(null, SVGOMDocument.SVG_HEIGHT_ATTRIBUTE);
  88. if (s.length() == 0) {
  89. s = SVGOMDocument.SVG_SVG_HEIGHT_DEFAULT_VALUE;
  90. }
  91. height = (int) UnitProcessor.svgVerticalLengthToUserSpace (
  92. s, SVGOMDocument.SVG_HEIGHT_ATTRIBUTE, uctx);
  93. return true;
  94. } catch (NoClassDefFoundError ncdfe) {
  95. //userAgent.getLogger().error("Batik not in class path", ncdfe);
  96. return false;
  97. }
  98. catch (Exception e) {
  99. //userAgent.getLogger().error("Could not load external SVG: " +
  100. // e.getMessage(), e);
  101. // assuming any exception means this document is not svg
  102. // or could not be loaded for some reason
  103. try {
  104. imageStream.reset();
  105. } catch (IOException ioe) { }
  106. return false;
  107. }
  108. }
  109. }