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.

AFPSVGHandler.java 9.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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.afp;
  19. // FOP
  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.dom.svg.SVGDOMImplementation;
  26. import org.apache.batik.gvt.GraphicsNode;
  27. import org.apache.xmlgraphics.image.loader.ImageManager;
  28. import org.apache.xmlgraphics.image.loader.ImageSessionContext;
  29. import org.apache.xmlgraphics.java2d.Graphics2DImagePainter;
  30. import org.apache.xmlgraphics.util.MimeConstants;
  31. import org.apache.fop.afp.AFPGraphics2D;
  32. import org.apache.fop.afp.AFPGraphicsObjectInfo;
  33. import org.apache.fop.afp.AFPObjectAreaInfo;
  34. import org.apache.fop.afp.AFPPaintingState;
  35. import org.apache.fop.afp.AFPResourceInfo;
  36. import org.apache.fop.afp.AFPResourceManager;
  37. import org.apache.fop.afp.AFPUnitConverter;
  38. import org.apache.fop.afp.svg.AFPBridgeContext;
  39. import org.apache.fop.apps.FOUserAgent;
  40. import org.apache.fop.fonts.FontInfo;
  41. import org.apache.fop.image.loader.batik.BatikUtil;
  42. import org.apache.fop.image.loader.batik.Graphics2DImagePainterImpl;
  43. import org.apache.fop.render.AbstractGenericSVGHandler;
  44. import org.apache.fop.render.Renderer;
  45. import org.apache.fop.render.RendererContext;
  46. import org.apache.fop.render.RendererContext.RendererContextWrapper;
  47. import org.apache.fop.svg.SVGEventProducer;
  48. import org.apache.fop.svg.SVGUserAgent;
  49. /**
  50. * AFP XML handler for SVG. Uses Apache Batik for SVG processing.
  51. * This handler handles XML for foreign objects and delegates to AFPGraphics2DAdapter.
  52. * @see AFPGraphics2DAdapter
  53. */
  54. public class AFPSVGHandler extends AbstractGenericSVGHandler {
  55. private boolean paintAsBitmap = false;
  56. /** {@inheritDoc} */
  57. public void handleXML(RendererContext context,
  58. Document doc, String ns) throws Exception {
  59. if (SVGDOMImplementation.SVG_NAMESPACE_URI.equals(ns)) {
  60. renderSVGDocument(context, doc);
  61. }
  62. }
  63. /**
  64. * Render the SVG document.
  65. *
  66. * @param rendererContext the renderer context
  67. * @param doc the SVG document
  68. * @throws IOException In case of an I/O error while painting the image
  69. */
  70. protected void renderSVGDocument(final RendererContext rendererContext,
  71. final Document doc) throws IOException {
  72. AFPRendererContext afpRendererContext = (AFPRendererContext)rendererContext;
  73. AFPInfo afpInfo = afpRendererContext.getInfo();
  74. this.paintAsBitmap = afpInfo.paintAsBitmap();
  75. FOUserAgent userAgent = rendererContext.getUserAgent();
  76. // fallback paint as bitmap
  77. String uri = getDocumentURI(doc);
  78. if (paintAsBitmap) {
  79. try {
  80. super.renderSVGDocument(rendererContext, doc);
  81. } catch (IOException ioe) {
  82. SVGEventProducer eventProducer = SVGEventProducer.Provider.get(
  83. userAgent.getEventBroadcaster());
  84. eventProducer.svgRenderingError(this, ioe, uri);
  85. }
  86. return;
  87. }
  88. // Create a new AFPGraphics2D
  89. final boolean textAsShapes = afpInfo.strokeText();
  90. AFPGraphics2D g2d = afpInfo.createGraphics2D(textAsShapes);
  91. AFPPaintingState paintingState = g2d.getPaintingState();
  92. paintingState.setImageUri(uri);
  93. // Create an AFPBridgeContext
  94. BridgeContext bridgeContext = createBridgeContext(userAgent, g2d);
  95. //Cloning SVG DOM as Batik attaches non-thread-safe facilities (like the CSS engine)
  96. //to it.
  97. Document clonedDoc = BatikUtil.cloneSVGDocument(doc);
  98. // Build the SVG DOM and provide the painter with it
  99. GraphicsNode root = buildGraphicsNode(userAgent, bridgeContext, clonedDoc);
  100. // Create Graphics2DImagePainter
  101. final RendererContextWrapper wrappedContext
  102. = RendererContext.wrapRendererContext(rendererContext);
  103. Dimension imageSize = getImageSize(wrappedContext);
  104. Graphics2DImagePainter painter
  105. = createGraphics2DImagePainter(bridgeContext, root, imageSize);
  106. // Create AFPObjectAreaInfo
  107. RendererContextWrapper rctx = RendererContext.wrapRendererContext(rendererContext);
  108. int x = rctx.getCurrentXPosition();
  109. int y = rctx.getCurrentYPosition();
  110. int width = afpInfo.getWidth();
  111. int height = afpInfo.getHeight();
  112. int resolution = afpInfo.getResolution();
  113. paintingState.save(); // save
  114. AFPObjectAreaInfo objectAreaInfo
  115. = createObjectAreaInfo(paintingState, x, y, width, height, resolution);
  116. // Create AFPGraphicsObjectInfo
  117. AFPResourceInfo resourceInfo = afpInfo.getResourceInfo();
  118. AFPGraphicsObjectInfo graphicsObjectInfo = createGraphicsObjectInfo(
  119. paintingState, painter, userAgent, resourceInfo, g2d);
  120. graphicsObjectInfo.setObjectAreaInfo(objectAreaInfo);
  121. // Create the GOCA GraphicsObject in the DataStream
  122. AFPResourceManager resourceManager = afpInfo.getResourceManager();
  123. resourceManager.createObject(graphicsObjectInfo);
  124. paintingState.restore(); // resume
  125. }
  126. private AFPObjectAreaInfo createObjectAreaInfo(AFPPaintingState paintingState,
  127. int x, int y, int width, int height, int resolution) {
  128. // set the data object parameters
  129. AffineTransform at = paintingState.getData().getTransform();
  130. at.translate(x, y);
  131. AFPUnitConverter unitConv = paintingState.getUnitConverter();
  132. int rotation = paintingState.getRotation();
  133. int objX = (int) Math.round(at.getTranslateX());
  134. int objY = (int) Math.round(at.getTranslateY());
  135. int objWidth = Math.round(unitConv.mpt2units(width));
  136. int objHeight = Math.round(unitConv.mpt2units(height));
  137. AFPObjectAreaInfo objectAreaInfo = new AFPObjectAreaInfo(objX, objY, objWidth, objHeight,
  138. resolution, rotation);
  139. return objectAreaInfo;
  140. }
  141. private AFPGraphicsObjectInfo createGraphicsObjectInfo
  142. (AFPPaintingState paintingState, Graphics2DImagePainter painter,
  143. FOUserAgent userAgent, AFPResourceInfo resourceInfo, AFPGraphics2D g2d) {
  144. AFPGraphicsObjectInfo graphicsObjectInfo = new AFPGraphicsObjectInfo();
  145. String uri = paintingState.getImageUri();
  146. graphicsObjectInfo.setUri(uri);
  147. graphicsObjectInfo.setMimeType(MimeConstants.MIME_AFP_GOCA);
  148. graphicsObjectInfo.setResourceInfo(resourceInfo);
  149. graphicsObjectInfo.setPainter(painter);
  150. // Set the afp graphics 2d implementation
  151. graphicsObjectInfo.setGraphics2D(g2d);
  152. return graphicsObjectInfo;
  153. }
  154. /**
  155. * @param userAgent a user agent instance
  156. * @param g2d a graphics context
  157. * @return a bridge context
  158. */
  159. public static BridgeContext createBridgeContext(FOUserAgent userAgent, AFPGraphics2D g2d) {
  160. ImageManager imageManager = userAgent.getFactory().getImageManager();
  161. SVGUserAgent svgUserAgent
  162. = new SVGUserAgent(userAgent, new AffineTransform());
  163. ImageSessionContext imageSessionContext = userAgent.getImageSessionContext();
  164. FontInfo fontInfo = g2d.getFontInfo();
  165. return new AFPBridgeContext(svgUserAgent, fontInfo, imageManager, imageSessionContext,
  166. new AffineTransform(), g2d);
  167. }
  168. /** {@inheritDoc} */
  169. public boolean supportsRenderer(Renderer renderer) {
  170. return false;
  171. }
  172. /** {@inheritDoc} */
  173. protected void updateRendererContext(RendererContext context) {
  174. //Work around a problem in Batik: Gradients cannot be done in ColorSpace.CS_GRAY
  175. context.setProperty(AFPRendererContextConstants.AFP_GRAYSCALE, Boolean.FALSE);
  176. }
  177. private Graphics2DImagePainter createGraphics2DImagePainter(BridgeContext ctx,
  178. GraphicsNode root, Dimension imageSize) {
  179. Graphics2DImagePainter painter = null;
  180. if (paintAsBitmap()) {
  181. // paint as IOCA Image
  182. painter = super.createGraphics2DImagePainter(root, ctx, imageSize);
  183. } else {
  184. // paint as GOCA Graphics
  185. painter = new Graphics2DImagePainterImpl(root, ctx, imageSize);
  186. }
  187. return painter;
  188. }
  189. /**
  190. * Returns true if the SVG is to be painted as a bitmap
  191. *
  192. * @return true if the SVG is to be painted as a bitmap
  193. */
  194. private boolean paintAsBitmap() {
  195. return paintAsBitmap;
  196. }
  197. }