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.

AbstractGenericSVGHandler.java 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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;
  19. // Java
  20. import java.awt.Dimension;
  21. import java.awt.geom.AffineTransform;
  22. import java.io.IOException;
  23. import org.w3c.dom.Document;
  24. import org.apache.batik.bridge.BridgeContext;
  25. import org.apache.batik.bridge.GVTBuilder;
  26. import org.apache.batik.dom.AbstractDocument;
  27. import org.apache.batik.dom.svg.SVGDOMImplementation;
  28. import org.apache.batik.gvt.GraphicsNode;
  29. import org.apache.xmlgraphics.java2d.Graphics2DImagePainter;
  30. import org.apache.fop.apps.FOUserAgent;
  31. import org.apache.fop.events.EventBroadcaster;
  32. import org.apache.fop.image.loader.batik.BatikUtil;
  33. import org.apache.fop.image.loader.batik.Graphics2DImagePainterImpl;
  34. import org.apache.fop.render.RendererContext.RendererContextWrapper;
  35. import org.apache.fop.svg.SVGEventProducer;
  36. import org.apache.fop.svg.SVGUserAgent;
  37. /**
  38. * Generic XML handler for SVG. Uses Apache Batik for SVG processing and simply paints to
  39. * a Graphics2DAdapter and thus ultimatively to Graphics2D interface that is presented.
  40. * <p>
  41. * To use this class, subclass it and implement the missing methods (supportsRenderer, for example).
  42. */
  43. public abstract class AbstractGenericSVGHandler implements XMLHandler, RendererContextConstants {
  44. /** {@inheritDoc} */
  45. public void handleXML(RendererContext context,
  46. Document doc, String ns) throws Exception {
  47. if (SVGDOMImplementation.SVG_NAMESPACE_URI.equals(ns)) {
  48. renderSVGDocument(context, doc);
  49. }
  50. }
  51. /**
  52. * Creates a graphics 2D image painter implementation
  53. *
  54. * @param root the batik graphics node root
  55. * @param ctx the batik bridge context
  56. * @param imageSize the image size
  57. * @return a new graphics 2D image painter implementation
  58. */
  59. protected Graphics2DImagePainter createGraphics2DImagePainter(
  60. GraphicsNode root, BridgeContext ctx, Dimension imageSize) {
  61. return new Graphics2DImagePainterImpl(root, ctx, imageSize);
  62. }
  63. /**
  64. * Builds the GVT root.
  65. *
  66. * @param userAgent the user agent
  67. * @param ctx the batik bridge context
  68. * @param doc the document
  69. * @return a built GVT root tree
  70. */
  71. protected GraphicsNode buildGraphicsNode(
  72. FOUserAgent userAgent, BridgeContext ctx, Document doc) {
  73. GVTBuilder builder = new GVTBuilder();
  74. final GraphicsNode root;
  75. try {
  76. root = builder.build(ctx, doc);
  77. } catch (Exception e) {
  78. EventBroadcaster eventBroadcaster
  79. = userAgent.getEventBroadcaster();
  80. SVGEventProducer eventProducer = SVGEventProducer.Provider.get(eventBroadcaster);
  81. final String uri = getDocumentURI(doc);
  82. eventProducer.svgNotBuilt(this, e, uri);
  83. return null;
  84. }
  85. return root;
  86. }
  87. /**
  88. * Returns the image size
  89. * @param wrappedContext renderer context wrapper
  90. *
  91. * @return the image size
  92. */
  93. protected Dimension getImageSize(RendererContextWrapper wrappedContext) {
  94. final int width = wrappedContext.getWidth();
  95. final int height = wrappedContext.getHeight();
  96. return new Dimension(width, height);
  97. }
  98. /**
  99. * Render the SVG document.
  100. *
  101. * @param rendererContext the renderer context
  102. * @param doc the SVG document
  103. * @throws IOException In case of an I/O error while painting the image
  104. */
  105. protected void renderSVGDocument(final RendererContext rendererContext,
  106. final Document doc) throws IOException {
  107. updateRendererContext(rendererContext);
  108. //Prepare
  109. FOUserAgent userAgent = rendererContext.getUserAgent();
  110. SVGUserAgent svgUserAgent = new SVGUserAgent(userAgent, new AffineTransform());
  111. //Create Batik BridgeContext
  112. final BridgeContext bridgeContext = new BridgeContext(svgUserAgent);
  113. //Cloning SVG DOM as Batik attaches non-thread-safe facilities (like the CSS engine)
  114. //to it.
  115. Document clonedDoc = BatikUtil.cloneSVGDocument(doc);
  116. //Build the GVT tree
  117. final GraphicsNode root = buildGraphicsNode(userAgent, bridgeContext, clonedDoc);
  118. // Create Graphics2DImagePainter
  119. final RendererContextWrapper wrappedContext = RendererContext.wrapRendererContext(
  120. rendererContext);
  121. Dimension imageSize = getImageSize(wrappedContext);
  122. final Graphics2DImagePainter painter = createGraphics2DImagePainter(
  123. root, bridgeContext, imageSize);
  124. //Let the painter paint the SVG on the Graphics2D instance
  125. Graphics2DAdapter g2dAdapter = rendererContext.getRenderer().getGraphics2DAdapter();
  126. //Paint the image
  127. final int x = wrappedContext.getCurrentXPosition();
  128. final int y = wrappedContext.getCurrentYPosition();
  129. final int width = wrappedContext.getWidth();
  130. final int height = wrappedContext.getHeight();
  131. g2dAdapter.paintImage(painter, rendererContext, x, y, width, height);
  132. }
  133. /**
  134. * Gets the document URI from a Document instance if possible.
  135. *
  136. * @param doc the Document
  137. * @return the URI or null
  138. */
  139. protected String getDocumentURI(Document doc) {
  140. String docURI = null;
  141. if (doc instanceof AbstractDocument) {
  142. AbstractDocument level3Doc = (AbstractDocument)doc;
  143. docURI = level3Doc.getDocumentURI();
  144. }
  145. return docURI;
  146. }
  147. /**
  148. * Override this method to update the renderer context if it needs special settings for
  149. * certain conditions.
  150. *
  151. * @param context the renderer context
  152. */
  153. protected void updateRendererContext(RendererContext context) {
  154. //nop
  155. }
  156. /** {@inheritDoc} */
  157. public String getNameSpace() {
  158. return SVGDOMImplementation.SVG_NAMESPACE_URI;
  159. }
  160. }