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.

SVGDataUrlImageHandler.java 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 java.io.InputStream;
  22. import org.xml.sax.ContentHandler;
  23. import org.xml.sax.SAXException;
  24. import org.xml.sax.helpers.AttributesImpl;
  25. import org.apache.commons.io.IOUtils;
  26. import org.apache.xmlgraphics.image.loader.Image;
  27. import org.apache.xmlgraphics.image.loader.ImageFlavor;
  28. import org.apache.xmlgraphics.image.loader.impl.ImageRawStream;
  29. import org.apache.xmlgraphics.util.QName;
  30. import org.apache.xmlgraphics.util.uri.DataURLUtil;
  31. import org.apache.fop.render.ImageHandler;
  32. import org.apache.fop.render.RenderingContext;
  33. import org.apache.fop.render.intermediate.IFConstants;
  34. /**
  35. * Image handler implementation that embeds JPEG bitmaps as RFC 2397 data URLs in the target SVG
  36. * file.
  37. */
  38. public class SVGDataUrlImageHandler implements ImageHandler, SVGConstants {
  39. /** Constant for the "CDATA" attribute type. */
  40. private static final String CDATA = "CDATA";
  41. /** {@inheritDoc} */
  42. public int getPriority() {
  43. return 500;
  44. }
  45. /** {@inheritDoc} */
  46. public Class getSupportedImageClass() {
  47. return ImageRawStream.class;
  48. }
  49. /** {@inheritDoc} */
  50. public ImageFlavor[] getSupportedImageFlavors() {
  51. return new ImageFlavor[] {
  52. ImageFlavor.RAW_PNG,
  53. ImageFlavor.RAW_JPEG,
  54. };
  55. }
  56. private void addAttribute(AttributesImpl atts, QName attribute, String value) {
  57. atts.addAttribute(attribute.getNamespaceURI(),
  58. attribute.getLocalName(), attribute.getQName(), CDATA, value);
  59. }
  60. /** {@inheritDoc} */
  61. public void handleImage(RenderingContext context, Image image, Rectangle pos)
  62. throws IOException {
  63. SVGRenderingContext svgContext = (SVGRenderingContext)context;
  64. if (!(image instanceof ImageRawStream)) {
  65. throw new IllegalStateException();
  66. }
  67. ImageRawStream raw = (ImageRawStream)image;
  68. InputStream in = raw.createInputStream();
  69. try {
  70. ContentHandler handler = svgContext.getContentHandler();
  71. String url = DataURLUtil.createDataURL(in, raw.getMimeType());
  72. AttributesImpl atts = new AttributesImpl();
  73. addAttribute(atts, IFConstants.XLINK_HREF, url);
  74. atts.addAttribute("", "x", "x", CDATA, Integer.toString(pos.x));
  75. atts.addAttribute("", "y", "y", CDATA, Integer.toString(pos.y));
  76. atts.addAttribute("", "width", "width", CDATA, Integer.toString(pos.width));
  77. atts.addAttribute("", "height", "height", CDATA, Integer.toString(pos.height));
  78. try {
  79. handler.startElement(NAMESPACE, "image", "image", atts);
  80. handler.endElement(NAMESPACE, "image", "image");
  81. } catch (SAXException e) {
  82. throw new IOException(e.getMessage());
  83. }
  84. } finally {
  85. IOUtils.closeQuietly(in);
  86. }
  87. }
  88. /** {@inheritDoc} */
  89. public boolean isCompatible(RenderingContext targetContext, Image image) {
  90. return (image == null || image instanceof ImageRawStream)
  91. && targetContext instanceof SVGRenderingContext;
  92. }
  93. }