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 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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.AffineTransform;
  21. import java.util.Map;
  22. import org.w3c.dom.Document;
  23. import org.apache.batik.bridge.BridgeContext;
  24. import org.apache.batik.bridge.GVTBuilder;
  25. import org.apache.batik.bridge.UserAgent;
  26. import org.apache.batik.dom.svg.SVGDOMImplementation;
  27. import org.apache.batik.gvt.GraphicsNode;
  28. import org.apache.commons.logging.Log;
  29. import org.apache.commons.logging.LogFactory;
  30. import org.apache.xmlgraphics.image.GraphicsConstants;
  31. import org.apache.xmlgraphics.image.loader.Image;
  32. import org.apache.xmlgraphics.image.loader.ImageException;
  33. import org.apache.xmlgraphics.image.loader.ImageFlavor;
  34. import org.apache.xmlgraphics.image.loader.ImageInfo;
  35. import org.apache.xmlgraphics.image.loader.ImageManager;
  36. import org.apache.xmlgraphics.image.loader.ImageProcessingHints;
  37. import org.apache.xmlgraphics.image.loader.ImageSessionContext;
  38. import org.apache.xmlgraphics.image.loader.XMLNamespaceEnabledImageFlavor;
  39. import org.apache.xmlgraphics.image.loader.impl.AbstractImageConverter;
  40. import org.apache.xmlgraphics.image.loader.impl.ImageGraphics2D;
  41. import org.apache.xmlgraphics.image.loader.impl.ImageXMLDOM;
  42. import org.apache.xmlgraphics.java2d.Graphics2DImagePainter;
  43. import org.apache.xmlgraphics.util.UnitConv;
  44. import org.apache.fop.svg.SimpleSVGUserAgent;
  45. /**
  46. * This ImageConverter converts SVG images to Java2D.
  47. * <p>
  48. * Note: The target flavor is "generic" Java2D. No Batik-specific bridges are hooked into the
  49. * conversion process. Specialized renderers may want to provide specialized adapters to profit
  50. * from target-format features (for example with PDF or PS). This converter is mainly for formats
  51. * which only support bitmap images or rudimentary Java2D support.
  52. */
  53. public class ImageConverterSVG2G2D extends AbstractImageConverter {
  54. /** logger */
  55. private static Log log = LogFactory.getLog(ImageConverterSVG2G2D.class);
  56. /** {@inheritDoc} */
  57. public Image convert(final Image src, Map hints) throws ImageException {
  58. checkSourceFlavor(src);
  59. final ImageXMLDOM svg = (ImageXMLDOM)src;
  60. if (!SVGDOMImplementation.SVG_NAMESPACE_URI.equals(svg.getRootNamespace())) {
  61. throw new IllegalArgumentException("XML DOM is not in the SVG namespace: "
  62. + svg.getRootNamespace());
  63. }
  64. //Prepare
  65. float pxToMillimeter = UnitConv.IN2MM / GraphicsConstants.DEFAULT_DPI;
  66. Number ptm = (Number)hints.get(ImageProcessingHints.SOURCE_RESOLUTION);
  67. if (ptm != null) {
  68. pxToMillimeter = (float)(UnitConv.IN2MM / ptm.doubleValue());
  69. }
  70. UserAgent ua = createBatikUserAgent(pxToMillimeter);
  71. GVTBuilder builder = new GVTBuilder();
  72. final ImageManager imageManager = (ImageManager)hints.get(
  73. ImageProcessingHints.IMAGE_MANAGER);
  74. final ImageSessionContext sessionContext = (ImageSessionContext)hints.get(
  75. ImageProcessingHints.IMAGE_SESSION_CONTEXT);
  76. boolean useEnhancedBridgeContext = (imageManager != null && sessionContext != null);
  77. final BridgeContext ctx = (useEnhancedBridgeContext
  78. ? new GenericFOPBridgeContext(ua, null, imageManager, sessionContext)
  79. : new BridgeContext(ua));
  80. Document doc = svg.getDocument();
  81. //Cloning SVG DOM as Batik attaches non-thread-safe facilities (like the CSS engine)
  82. //to it.
  83. Document clonedDoc = BatikUtil.cloneSVGDocument(doc);
  84. //Build the GVT tree
  85. final GraphicsNode root;
  86. try {
  87. root = builder.build(ctx, clonedDoc);
  88. } catch (Exception e) {
  89. throw new ImageException("GVT tree could not be built for SVG graphic", e);
  90. }
  91. //Create the painter
  92. int width = svg.getSize().getWidthMpt();
  93. int height = svg.getSize().getHeightMpt();
  94. Dimension imageSize = new Dimension(width, height);
  95. Graphics2DImagePainter painter = createPainter(ctx, root, imageSize);
  96. //Create g2d image
  97. ImageInfo imageInfo = src.getInfo();
  98. ImageGraphics2D g2dImage = new ImageGraphics2D(imageInfo, painter);
  99. return g2dImage;
  100. }
  101. /**
  102. * Creates a user agent for Batik. Override to provide your own user agent.
  103. * @param pxToMillimeter the source resolution (in px per millimeter)
  104. * @return the newly created user agent
  105. */
  106. protected SimpleSVGUserAgent createBatikUserAgent(float pxToMillimeter) {
  107. return new SimpleSVGUserAgent(
  108. pxToMillimeter,
  109. new AffineTransform()) {
  110. /** {@inheritDoc} */
  111. public void displayMessage(String message) {
  112. //TODO Refine and pipe through to caller
  113. log.info(message);
  114. }
  115. /** {@inheritDoc} */
  116. public void displayError(Exception e) {
  117. log.error("Error converting SVG to a Java2D graphic", e);
  118. }
  119. /** {@inheritDoc} */
  120. public void displayError(String message) {
  121. log.error(message);
  122. }
  123. };
  124. }
  125. /**
  126. * Creates a Graphics 2D image painter
  127. *
  128. * @param ctx the bridge context
  129. * @param root the graphics node root
  130. * @param imageSize the image size
  131. * @return the newly created graphics 2d image painter
  132. */
  133. protected Graphics2DImagePainter createPainter(
  134. BridgeContext ctx, GraphicsNode root, Dimension imageSize) {
  135. return new Graphics2DImagePainterImpl(root, ctx, imageSize);
  136. }
  137. /** {@inheritDoc} */
  138. public ImageFlavor getSourceFlavor() {
  139. return XMLNamespaceEnabledImageFlavor.SVG_DOM;
  140. }
  141. /** {@inheritDoc} */
  142. public ImageFlavor getTargetFlavor() {
  143. return ImageFlavor.GRAPHICS2D;
  144. }
  145. }