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 5.9KB

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