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.

ImageConverterG2D2SVG.java 3.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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.Dimension;
  20. import java.awt.geom.Rectangle2D;
  21. import java.util.Map;
  22. import org.w3c.dom.DOMImplementation;
  23. import org.w3c.dom.Document;
  24. import org.w3c.dom.Element;
  25. import org.apache.batik.dom.GenericDOMImplementation;
  26. import org.apache.batik.dom.svg.SVGDOMImplementation;
  27. import org.apache.batik.svggen.SVGGeneratorContext;
  28. import org.apache.batik.svggen.SVGGraphics2D;
  29. import org.apache.xmlgraphics.image.loader.Image;
  30. import org.apache.xmlgraphics.image.loader.ImageException;
  31. import org.apache.xmlgraphics.image.loader.ImageFlavor;
  32. import org.apache.xmlgraphics.image.loader.ImageSize;
  33. import org.apache.xmlgraphics.image.loader.impl.AbstractImageConverter;
  34. import org.apache.xmlgraphics.image.loader.impl.ImageGraphics2D;
  35. import org.apache.xmlgraphics.image.loader.impl.ImageXMLDOM;
  36. /**
  37. * This ImageConverter converts Java2D images into SVG images.
  38. */
  39. public class ImageConverterG2D2SVG extends AbstractImageConverter {
  40. /** {@inheritDoc} */
  41. public Image convert(Image src, Map hints) throws ImageException {
  42. checkSourceFlavor(src);
  43. ImageGraphics2D g2dImage = (ImageGraphics2D)src;
  44. DOMImplementation domImpl = GenericDOMImplementation.getDOMImplementation();
  45. // Create an instance of org.w3c.dom.Document
  46. Document document = domImpl.createDocument(
  47. SVGDOMImplementation.SVG_NAMESPACE_URI, "svg", null);
  48. Element root = document.getDocumentElement();
  49. // Create an SVGGeneratorContext to customize SVG generation
  50. SVGGeneratorContext genCtx = SVGGeneratorContext.createDefault(document);
  51. genCtx.setComment("Generated by Apache Batik's SVGGraphics2D");
  52. genCtx.setEmbeddedFontsOn(true);
  53. // Create an instance of the SVG Generator
  54. SVGGraphics2D g2d = new SVGGraphics2D(genCtx, true);
  55. ImageSize size = src.getSize();
  56. Dimension dim = size.getDimensionMpt();
  57. g2d.setSVGCanvasSize(dim);
  58. //SVGGraphics2D doesn't generate the viewBox by itself
  59. root.setAttribute("viewBox", "0 0 " + dim.width + " " + dim.height);
  60. g2dImage.getGraphics2DImagePainter().paint(g2d,
  61. new Rectangle2D.Float(0, 0, dim.width, dim.height));
  62. //Populate the document root with the generated SVG content.
  63. g2d.getRoot(root);
  64. //Return the generated SVG image
  65. ImageXMLDOM svgImage = new ImageXMLDOM(src.getInfo(), document, BatikImageFlavors.SVG_DOM);
  66. g2d.dispose();
  67. return svgImage;
  68. }
  69. /** {@inheritDoc} */
  70. public ImageFlavor getSourceFlavor() {
  71. return ImageFlavor.GRAPHICS2D;
  72. }
  73. /** {@inheritDoc} */
  74. public ImageFlavor getTargetFlavor() {
  75. return BatikImageFlavors.SVG_DOM;
  76. }
  77. }