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

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