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.6KB

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