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.

ImageConverterSVG2G2D.java 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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.Graphics2D;
  21. import java.awt.geom.AffineTransform;
  22. import java.awt.geom.Rectangle2D;
  23. import java.util.Map;
  24. import org.apache.batik.bridge.BridgeContext;
  25. import org.apache.batik.bridge.GVTBuilder;
  26. import org.apache.batik.bridge.UserAgent;
  27. import org.apache.batik.dom.svg.SVGDOMImplementation;
  28. import org.apache.batik.gvt.GraphicsNode;
  29. import org.apache.commons.logging.Log;
  30. import org.apache.commons.logging.LogFactory;
  31. import org.apache.fop.svg.SimpleSVGUserAgent;
  32. import org.apache.xmlgraphics.image.loader.Image;
  33. import org.apache.xmlgraphics.image.loader.ImageException;
  34. import org.apache.xmlgraphics.image.loader.ImageFlavor;
  35. import org.apache.xmlgraphics.image.loader.ImageProcessingHints;
  36. import org.apache.xmlgraphics.image.loader.XMLNamespaceEnabledImageFlavor;
  37. import org.apache.xmlgraphics.image.loader.impl.AbstractImageConverter;
  38. import org.apache.xmlgraphics.image.loader.impl.ImageGraphics2D;
  39. import org.apache.xmlgraphics.image.loader.impl.ImageXMLDOM;
  40. import org.apache.xmlgraphics.java2d.Graphics2DImagePainter;
  41. import org.apache.xmlgraphics.util.UnitConv;
  42. /**
  43. * This ImageConverter converts SVG images to Java2D.
  44. * <p>
  45. * Note: The target flavor is "generic" Java2D. No Batik-specific bridges are hooked into the
  46. * conversion process. Specialized renderers may want to provide specialized adapters to profit
  47. * from target-format features (for example with PDF or PS). This converter is mainly for formats
  48. * which only support bitmap images or rudimentary Java2D support.
  49. */
  50. public class ImageConverterSVG2G2D extends AbstractImageConverter {
  51. /** logger */
  52. private static Log log = LogFactory.getLog(ImageConverterSVG2G2D.class);
  53. /** {@inheritDoc} */
  54. public Image convert(Image src, Map hints) throws ImageException {
  55. checkSourceFlavor(src);
  56. final ImageXMLDOM svg = (ImageXMLDOM)src;
  57. if (!SVGDOMImplementation.SVG_NAMESPACE_URI.equals(svg.getRootNamespace())) {
  58. throw new IllegalArgumentException("XML DOM is not in the SVG namespace: "
  59. + svg.getRootNamespace());
  60. }
  61. //Prepare
  62. float pxToMillimeter = UnitConv.IN2MM / 72; //default: 72dpi
  63. Number ptm = (Number)hints.get(ImageProcessingHints.SOURCE_RESOLUTION);
  64. if (ptm != null) {
  65. pxToMillimeter = (float)(UnitConv.IN2MM / ptm.doubleValue());
  66. }
  67. UserAgent ua = createBatikUserAgent(pxToMillimeter);
  68. GVTBuilder builder = new GVTBuilder();
  69. final BridgeContext ctx = new BridgeContext(ua);
  70. //Build the GVT tree
  71. final GraphicsNode root;
  72. try {
  73. root = builder.build(ctx, svg.getDocument());
  74. } catch (Exception e) {
  75. throw new ImageException("GVT tree could not be built for SVG graphic", e);
  76. }
  77. //Create the painter
  78. Graphics2DImagePainter painter = new Graphics2DImagePainter() {
  79. public void paint(Graphics2D g2d, Rectangle2D area) {
  80. // If no viewbox is defined in the svg file, a viewbox of 100x100 is
  81. // assumed, as defined in SVGUserAgent.getViewportSize()
  82. float iw = (float) ctx.getDocumentSize().getWidth();
  83. float ih = (float) ctx.getDocumentSize().getHeight();
  84. float w = (float) area.getWidth();
  85. float h = (float) area.getHeight();
  86. g2d.translate(area.getX(), area.getY());
  87. g2d.scale(w / iw, h / ih);
  88. root.paint(g2d);
  89. }
  90. public Dimension getImageSize() {
  91. return new Dimension(svg.getSize().getWidthMpt(), svg.getSize().getHeightMpt());
  92. }
  93. };
  94. ImageGraphics2D g2dImage = new ImageGraphics2D(src.getInfo(), painter);
  95. return g2dImage;
  96. }
  97. /**
  98. * Creates a user agent for Batik. Override to provide your own user agent.
  99. * @param pxToMillimeter the source resolution (in px per millimeter)
  100. * @return the newly created user agent
  101. */
  102. protected SimpleSVGUserAgent createBatikUserAgent(float pxToMillimeter) {
  103. return new SimpleSVGUserAgent(
  104. pxToMillimeter,
  105. new AffineTransform()) {
  106. /** {@inheritDoc} */
  107. public void displayMessage(String message) {
  108. //TODO Refine and pipe through to caller
  109. log.debug(message);
  110. }
  111. };
  112. }
  113. /** {@inheritDoc} */
  114. public ImageFlavor getSourceFlavor() {
  115. return XMLNamespaceEnabledImageFlavor.SVG_DOM;
  116. }
  117. /** {@inheritDoc} */
  118. public ImageFlavor getTargetFlavor() {
  119. return ImageFlavor.GRAPHICS2D;
  120. }
  121. }