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.

EmbeddedSVGImageHandler.java 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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.render.svg;
  19. import java.awt.Rectangle;
  20. import java.io.IOException;
  21. import javax.xml.transform.Transformer;
  22. import javax.xml.transform.TransformerException;
  23. import javax.xml.transform.TransformerFactory;
  24. import javax.xml.transform.dom.DOMSource;
  25. import javax.xml.transform.sax.SAXResult;
  26. import org.w3c.dom.Document;
  27. import org.w3c.dom.Element;
  28. import org.xml.sax.Attributes;
  29. import org.xml.sax.ContentHandler;
  30. import org.xml.sax.SAXException;
  31. import org.xml.sax.helpers.AttributesImpl;
  32. import org.apache.commons.logging.Log;
  33. import org.apache.commons.logging.LogFactory;
  34. import org.apache.xmlgraphics.image.loader.Image;
  35. import org.apache.xmlgraphics.image.loader.ImageFlavor;
  36. import org.apache.xmlgraphics.image.loader.impl.ImageRawStream;
  37. import org.apache.xmlgraphics.image.loader.impl.ImageXMLDOM;
  38. import org.apache.fop.image.loader.batik.BatikImageFlavors;
  39. import org.apache.fop.render.ImageHandler;
  40. import org.apache.fop.render.RenderingContext;
  41. import org.apache.fop.render.intermediate.DelegatingFragmentContentHandler;
  42. /**
  43. * Image handler implementation that embeds SVG images in the target SVG file.
  44. */
  45. public class EmbeddedSVGImageHandler implements ImageHandler, SVGConstants {
  46. /** logging instance */
  47. private static Log log = LogFactory.getLog(EmbeddedSVGImageHandler.class);
  48. /** Constant for the "CDATA" attribute type. */
  49. private static final String CDATA = "CDATA";
  50. /** {@inheritDoc} */
  51. public int getPriority() {
  52. return 500;
  53. }
  54. /** {@inheritDoc} */
  55. public Class getSupportedImageClass() {
  56. return ImageRawStream.class;
  57. }
  58. /** {@inheritDoc} */
  59. public ImageFlavor[] getSupportedImageFlavors() {
  60. return new ImageFlavor[] {
  61. BatikImageFlavors.SVG_DOM
  62. };
  63. }
  64. /*
  65. private void addAttribute(AttributesImpl atts, QName attribute, String value) {
  66. atts.addAttribute(attribute.getNamespaceURI(),
  67. attribute.getLocalName(), attribute.getQName(), CDATA, value);
  68. }
  69. */
  70. /** {@inheritDoc} */
  71. public void handleImage(RenderingContext context, Image image, final Rectangle pos)
  72. throws IOException {
  73. SVGRenderingContext svgContext = (SVGRenderingContext)context;
  74. if (!(image instanceof ImageXMLDOM)) {
  75. throw new IllegalStateException();
  76. }
  77. ImageXMLDOM svg = (ImageXMLDOM) image;
  78. ContentHandler handler = svgContext.getContentHandler();
  79. AttributesImpl atts = new AttributesImpl();
  80. atts.addAttribute("", "x", "x", CDATA, SVGUtil.formatMptToPt(pos.x));
  81. atts.addAttribute("", "y", "y", CDATA, SVGUtil.formatMptToPt(pos.y));
  82. atts.addAttribute("", "width", "width", CDATA, SVGUtil.formatMptToPt(pos.width));
  83. atts.addAttribute("", "height", "height", CDATA, SVGUtil.formatMptToPt(pos.height));
  84. try {
  85. Document doc = (Document)svg.getDocument();
  86. Element svgEl = (Element)doc.getDocumentElement();
  87. if (svgEl.getAttribute("viewBox").length() == 0) {
  88. log.warn("SVG doesn't have a viewBox. The result might not be scaled correctly!");
  89. }
  90. TransformerFactory tFactory = TransformerFactory.newInstance();
  91. Transformer transformer = tFactory.newTransformer();
  92. DOMSource src = new DOMSource(svg.getDocument());
  93. SAXResult res = new SAXResult(new DelegatingFragmentContentHandler(handler) {
  94. private boolean topLevelSVGFound;
  95. private void setAttribute(AttributesImpl atts, String localName, String value) {
  96. int index;
  97. index = atts.getIndex("", localName);
  98. if (index < 0) {
  99. atts.addAttribute("", localName, localName, CDATA, value);
  100. } else {
  101. atts.setAttribute(index, "", localName, localName, CDATA, value);
  102. }
  103. }
  104. public void startElement(String uri, String localName, String name, Attributes atts)
  105. throws SAXException {
  106. if (!topLevelSVGFound
  107. && SVG_ELEMENT.getNamespaceURI().equals(uri)
  108. && SVG_ELEMENT.getLocalName().equals(localName)) {
  109. topLevelSVGFound = true;
  110. AttributesImpl modAtts = new AttributesImpl(atts);
  111. setAttribute(modAtts, "x", SVGUtil.formatMptToPt(pos.x));
  112. setAttribute(modAtts, "y", SVGUtil.formatMptToPt(pos.y));
  113. setAttribute(modAtts, "width", SVGUtil.formatMptToPt(pos.width));
  114. setAttribute(modAtts, "height", SVGUtil.formatMptToPt(pos.height));
  115. super.startElement(uri, localName, name, modAtts);
  116. } else {
  117. super.startElement(uri, localName, name, atts);
  118. }
  119. }
  120. });
  121. transformer.transform(src, res);
  122. } catch (TransformerException te) {
  123. throw new IOException(te.getMessage());
  124. }
  125. }
  126. /** {@inheritDoc} */
  127. public boolean isCompatible(RenderingContext targetContext, Image image) {
  128. if (targetContext instanceof SVGRenderingContext) {
  129. if (image == null) {
  130. return true;
  131. }
  132. if (image instanceof ImageXMLDOM) {
  133. ImageXMLDOM svg = (ImageXMLDOM)image;
  134. return NAMESPACE.equals(svg.getRootNamespace());
  135. }
  136. }
  137. return false;
  138. }
  139. }