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.

PSImageHandlerSVG.java 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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.ps;
  19. import java.awt.Rectangle;
  20. import java.awt.geom.AffineTransform;
  21. import java.io.IOException;
  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.gvt.GraphicsNode;
  26. import org.apache.xmlgraphics.image.loader.Image;
  27. import org.apache.xmlgraphics.image.loader.ImageFlavor;
  28. import org.apache.xmlgraphics.image.loader.impl.ImageXMLDOM;
  29. import org.apache.xmlgraphics.java2d.ps.PSGraphics2D;
  30. import org.apache.xmlgraphics.ps.PSGenerator;
  31. import org.apache.fop.image.loader.batik.BatikImageFlavors;
  32. import org.apache.fop.image.loader.batik.BatikUtil;
  33. import org.apache.fop.render.ImageHandler;
  34. import org.apache.fop.render.RenderingContext;
  35. import org.apache.fop.svg.SVGEventProducer;
  36. import org.apache.fop.svg.SVGUserAgent;
  37. import org.apache.fop.svg.font.FOPFontFamilyResolverImpl;
  38. /**
  39. * Image handler implementation which handles SVG images for PostScript output.
  40. */
  41. public class PSImageHandlerSVG implements ImageHandler {
  42. private static final ImageFlavor[] FLAVORS = new ImageFlavor[] {
  43. BatikImageFlavors.SVG_DOM
  44. };
  45. /** {@inheritDoc} */
  46. public void handleImage(RenderingContext context, Image image, Rectangle pos)
  47. throws IOException {
  48. PSRenderingContext psContext = (PSRenderingContext)context;
  49. PSGenerator gen = psContext.getGenerator();
  50. ImageXMLDOM imageSVG = (ImageXMLDOM)image;
  51. //Controls whether text painted by Batik is generated using text or path operations
  52. boolean strokeText = false;
  53. //TODO Configure text stroking
  54. SVGUserAgent ua = new SVGUserAgent(context.getUserAgent(),
  55. new FOPFontFamilyResolverImpl(psContext.getFontInfo()), new AffineTransform());
  56. PSGraphics2D graphics = new PSGraphics2D(strokeText, gen);
  57. graphics.setGraphicContext(new org.apache.xmlgraphics.java2d.GraphicContext());
  58. BridgeContext ctx = new PSBridgeContext(ua,
  59. (strokeText ? null : psContext.getFontInfo()),
  60. context.getUserAgent().getImageManager(),
  61. context.getUserAgent().getImageSessionContext());
  62. //Cloning SVG DOM as Batik attaches non-thread-safe facilities (like the CSS engine)
  63. //to it.
  64. Document clonedDoc = BatikUtil.cloneSVGDocument(imageSVG.getDocument());
  65. GraphicsNode root;
  66. try {
  67. GVTBuilder builder = new GVTBuilder();
  68. root = builder.build(ctx, clonedDoc);
  69. } catch (Exception e) {
  70. SVGEventProducer eventProducer = SVGEventProducer.Provider.get(
  71. context.getUserAgent().getEventBroadcaster());
  72. eventProducer.svgNotBuilt(this, e, image.getInfo().getOriginalURI());
  73. return;
  74. }
  75. // get the 'width' and 'height' attributes of the SVG document
  76. float w = (float)ctx.getDocumentSize().getWidth() * 1000f;
  77. float h = (float)ctx.getDocumentSize().getHeight() * 1000f;
  78. float sx = pos.width / w;
  79. float sy = pos.height / h;
  80. ctx = null;
  81. gen.commentln("%FOPBeginSVG");
  82. gen.saveGraphicsState();
  83. final boolean clip = false;
  84. if (clip) {
  85. /*
  86. * Clip to the svg area.
  87. * Note: To have the svg overlay (under) a text area then use
  88. * an fo:block-container
  89. */
  90. gen.writeln("newpath");
  91. gen.defineRect(pos.getMinX() / 1000f, pos.getMinY() / 1000f,
  92. pos.width / 1000f, pos.height / 1000f);
  93. gen.writeln("clip");
  94. }
  95. // transform so that the coordinates (0,0) is from the top left
  96. // and positive is down and to the right. (0,0) is where the
  97. // viewBox puts it.
  98. gen.concatMatrix(sx, 0, 0, sy, pos.getMinX() / 1000f, pos.getMinY() / 1000f);
  99. AffineTransform transform = new AffineTransform();
  100. // scale to viewbox
  101. transform.translate(pos.getMinX(), pos.getMinY());
  102. gen.getCurrentState().concatMatrix(transform);
  103. try {
  104. root.paint(graphics);
  105. } catch (Exception e) {
  106. SVGEventProducer eventProducer = SVGEventProducer.Provider.get(
  107. context.getUserAgent().getEventBroadcaster());
  108. eventProducer.svgRenderingError(this, e, image.getInfo().getOriginalURI());
  109. }
  110. gen.restoreGraphicsState();
  111. gen.commentln("%FOPEndSVG");
  112. }
  113. /** {@inheritDoc} */
  114. public int getPriority() {
  115. return 400;
  116. }
  117. /** {@inheritDoc} */
  118. public Class getSupportedImageClass() {
  119. return ImageXMLDOM.class;
  120. }
  121. /** {@inheritDoc} */
  122. public ImageFlavor[] getSupportedImageFlavors() {
  123. return FLAVORS;
  124. }
  125. /** {@inheritDoc} */
  126. public boolean isCompatible(RenderingContext targetContext, Image image) {
  127. if (targetContext instanceof PSRenderingContext) {
  128. PSRenderingContext psContext = (PSRenderingContext)targetContext;
  129. return !psContext.isCreateForms()
  130. && (image == null || (image instanceof ImageXMLDOM
  131. && image.getFlavor().isCompatible(BatikImageFlavors.SVG_DOM)));
  132. }
  133. return false;
  134. }
  135. }