Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

PreloaderSVG.java 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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.image.loader.batik;
  19. import java.awt.geom.AffineTransform;
  20. import java.io.IOException;
  21. import java.io.InputStream;
  22. import javax.xml.parsers.SAXParserFactory;
  23. import javax.xml.transform.Source;
  24. import org.w3c.dom.Element;
  25. import org.w3c.dom.svg.SVGDocument;
  26. import org.apache.batik.bridge.BridgeContext;
  27. import org.apache.batik.bridge.UnitProcessor;
  28. import org.apache.batik.dom.svg.SAXSVGDocumentFactory;
  29. import org.apache.batik.dom.svg.SVGDOMImplementation;
  30. import org.apache.batik.dom.svg.SVGOMDocument;
  31. import org.apache.commons.logging.Log;
  32. import org.apache.commons.logging.LogFactory;
  33. import org.apache.xmlgraphics.image.loader.ImageContext;
  34. import org.apache.xmlgraphics.image.loader.ImageInfo;
  35. import org.apache.xmlgraphics.image.loader.ImageSize;
  36. import org.apache.xmlgraphics.image.loader.impl.AbstractImagePreloader;
  37. import org.apache.xmlgraphics.image.loader.impl.ImageXMLDOM;
  38. import org.apache.xmlgraphics.image.loader.util.ImageUtil;
  39. import org.apache.xmlgraphics.util.MimeConstants;
  40. import org.apache.fop.svg.SVGUserAgent;
  41. import org.apache.fop.util.UnclosableInputStream;
  42. /**
  43. * Image preloader for SVG images.
  44. */
  45. public class PreloaderSVG extends AbstractImagePreloader {
  46. /** Logger instance */
  47. private static Log log = LogFactory.getLog(PreloaderSVG.class);
  48. private boolean batikAvailable = true;
  49. /** {@inheritDoc} */
  50. public ImageInfo preloadImage(String uri, Source src, ImageContext context)
  51. throws IOException {
  52. if (!ImageUtil.hasInputStream(src)) {
  53. //TODO Remove this and support DOMSource and possibly SAXSource
  54. return null;
  55. }
  56. ImageInfo info = null;
  57. if (batikAvailable) {
  58. try {
  59. Loader loader = new Loader();
  60. info = loader.getImage(uri, src, context);
  61. } catch (NoClassDefFoundError e) {
  62. batikAvailable = false;
  63. log.warn("Batik not in class path", e);
  64. return null;
  65. }
  66. }
  67. if (info != null) {
  68. ImageUtil.closeQuietly(src); //Image is fully read
  69. }
  70. return info;
  71. }
  72. /**
  73. * Returns the fully qualified classname of an XML parser for
  74. * Batik classes that apparently need it (error messages, perhaps)
  75. * @return an XML parser classname
  76. */
  77. public static String getParserName() {
  78. try {
  79. SAXParserFactory factory = SAXParserFactory.newInstance();
  80. return factory.newSAXParser().getXMLReader().getClass().getName();
  81. } catch (Exception e) {
  82. return null;
  83. }
  84. }
  85. /**
  86. * This method is put in another class so that the class loader does not
  87. * attempt to load Batik related classes when constructing the SVGPreloader
  88. * class.
  89. */
  90. class Loader {
  91. private ImageInfo getImage(String uri, Source src,
  92. ImageContext context) {
  93. // parse document and get the size attributes of the svg element
  94. InputStream in = new UnclosableInputStream(ImageUtil.needInputStream(src));
  95. try {
  96. int length = in.available();
  97. in.mark(length + 1);
  98. SAXSVGDocumentFactory factory = new SAXSVGDocumentFactory(
  99. getParserName());
  100. SVGDocument doc = (SVGDocument) factory.createSVGDocument(src.getSystemId(), in);
  101. Element e = doc.getRootElement();
  102. float pxUnitToMillimeter = 25.4f / context.getSourceResolution();
  103. SVGUserAgent userAg = new SVGUserAgent(pxUnitToMillimeter,
  104. new AffineTransform());
  105. BridgeContext ctx = new BridgeContext(userAg);
  106. UnitProcessor.Context uctx = UnitProcessor.createContext(ctx, e);
  107. String s;
  108. // 'width' attribute - default is 100%
  109. s = e.getAttributeNS(null, SVGOMDocument.SVG_WIDTH_ATTRIBUTE);
  110. if (s.length() == 0) {
  111. s = SVGOMDocument.SVG_SVG_WIDTH_DEFAULT_VALUE;
  112. }
  113. float width = UnitProcessor.svgHorizontalLengthToUserSpace(
  114. s, SVGOMDocument.SVG_WIDTH_ATTRIBUTE, uctx);
  115. // 'height' attribute - default is 100%
  116. s = e.getAttributeNS(null, SVGOMDocument.SVG_HEIGHT_ATTRIBUTE);
  117. if (s.length() == 0) {
  118. s = SVGOMDocument.SVG_SVG_HEIGHT_DEFAULT_VALUE;
  119. }
  120. float height = UnitProcessor.svgVerticalLengthToUserSpace(
  121. s, SVGOMDocument.SVG_HEIGHT_ATTRIBUTE, uctx);
  122. ImageInfo info = new ImageInfo(uri, MimeConstants.MIME_SVG);
  123. ImageSize size = new ImageSize();
  124. size.setSizeInMillipoints(Math.round(width * 1000), Math.round(height * 1000));
  125. //Set the resolution to that of the FOUserAgent
  126. size.setResolution(context.getSourceResolution());
  127. size.calcPixelsFromSize();
  128. info.setSize(size);
  129. //The whole image had to be loaded for this, so keep it
  130. ImageXMLDOM xmlImage = new ImageXMLDOM(info,
  131. doc, SVGDOMImplementation.SVG_NAMESPACE_URI);
  132. info.getCustomObjects().put(ImageInfo.ORIGINAL_IMAGE, xmlImage);
  133. return info;
  134. } catch (NoClassDefFoundError ncdfe) {
  135. try {
  136. in.reset();
  137. } catch (IOException ioe) {
  138. // we're more interested in the original exception
  139. }
  140. batikAvailable = false;
  141. log.warn("Batik not in class path", ncdfe);
  142. return null;
  143. } catch (IOException e) {
  144. // If the svg is invalid then it throws an IOException
  145. // so there is no way of knowing if it is an svg document
  146. log.debug("Error while trying to load stream as an SVG file: "
  147. + e.getMessage());
  148. // assuming any exception means this document is not svg
  149. // or could not be loaded for some reason
  150. try {
  151. in.reset();
  152. } catch (IOException ioe) {
  153. // we're more interested in the original exception
  154. }
  155. return null;
  156. }
  157. }
  158. }
  159. }