Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

Java2DSVGHandler.java 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*
  2. * Copyright 1999-2005 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /* $Id$ */
  17. package org.apache.fop.render.java2d;
  18. import org.apache.fop.render.XMLHandler;
  19. import org.apache.fop.render.RendererContext;
  20. import org.apache.fop.svg.SVGUserAgent;
  21. // Commons-Logging
  22. import org.apache.commons.logging.Log;
  23. import org.apache.commons.logging.LogFactory;
  24. /* org.w3c.dom.Document is not imported to avoid conflict with
  25. org.apache.fop.apps.Document */
  26. import org.apache.batik.bridge.GVTBuilder;
  27. import org.apache.batik.bridge.BridgeContext;
  28. import org.apache.batik.dom.svg.SVGDOMImplementation;
  29. import org.apache.batik.gvt.GraphicsNode;
  30. import java.awt.geom.AffineTransform;
  31. /**
  32. * Java2D XML handler for SVG (uses Apache Batik).
  33. * This handler handles XML for foreign objects when rendering to Java2D.
  34. * The properties from the Java2D renderer are subject to change.
  35. */
  36. public class Java2DSVGHandler implements XMLHandler {
  37. /**
  38. * logging instance
  39. */
  40. private Log log = LogFactory.getLog(Java2DSVGHandler.class);
  41. /**
  42. * The current Java2DGraphicsState.
  43. */
  44. public static final String JAVA2D_STATE = "state";
  45. /**
  46. * The width of the svg image/document to render.
  47. */
  48. public static final String JAVA2D_WIDTH = "width";
  49. /**
  50. * The height of the svg image/document to render.
  51. */
  52. public static final String JAVA2D_HEIGHT = "height";
  53. /**
  54. * The x position that this is being drawn at.
  55. */
  56. public static final String JAVA2D_XPOS = "xpos";
  57. /**
  58. * The y position that this is being drawn at.
  59. */
  60. public static final String JAVA2D_YPOS = "ypos";
  61. private String mimeType;
  62. /**
  63. * Create a new Java2D XML handler for use by the Java2D renderer.
  64. * @param mime MIME type that this handler is used for
  65. */
  66. public Java2DSVGHandler(String mime) {
  67. this.mimeType = mime;
  68. }
  69. /** @see org.apache.fop.render.XMLHandler */
  70. public void handleXML(RendererContext context,
  71. org.w3c.dom.Document doc, String ns) throws Exception {
  72. Java2DInfo pdfi = getJava2DInfo(context);
  73. if (SVGDOMImplementation.SVG_NAMESPACE_URI.equals(ns)) {
  74. SVGHandler svghandler = new SVGHandler();
  75. svghandler.renderSVGDocument(context, doc, pdfi);
  76. }
  77. }
  78. /**
  79. * Get the pdf information from the render context.
  80. *
  81. * @param context the renderer context
  82. * @return the pdf information retrieved from the context
  83. */
  84. public static Java2DInfo getJava2DInfo(RendererContext context) {
  85. Java2DInfo pdfi = new Java2DInfo();
  86. pdfi.state = (Java2DGraphicsState)context.getProperty(JAVA2D_STATE);
  87. pdfi.width = ((Integer)context.getProperty(JAVA2D_WIDTH)).intValue();
  88. pdfi.height = ((Integer)context.getProperty(JAVA2D_HEIGHT)).intValue();
  89. pdfi.currentXPosition = ((Integer)context.getProperty(JAVA2D_XPOS)).intValue();
  90. pdfi.currentYPosition = ((Integer)context.getProperty(JAVA2D_YPOS)).intValue();
  91. return pdfi;
  92. }
  93. /**
  94. * Java2D information structure for drawing the XML document.
  95. */
  96. public static class Java2DInfo {
  97. /** see Java2D_STATE */
  98. public Java2DGraphicsState state;
  99. /** see Java2D_WIDTH */
  100. public int width;
  101. /** see Java2D_HEIGHT */
  102. public int height;
  103. /** see Java2D_XPOS */
  104. public int currentXPosition;
  105. /** see Java2D_YPOS */
  106. public int currentYPosition;
  107. /** @see java.lang.Object#toString() */
  108. public String toString() {
  109. return "Java2DInfo {"
  110. + "state = " + state + ", "
  111. + "width = " + width + ", "
  112. + "height = " + height + ", "
  113. + "currentXPosition = " + currentXPosition + ", "
  114. + "currentYPosition = " + currentYPosition + "}";
  115. }
  116. }
  117. /**
  118. * This method is placed in an inner class so that we don't get class
  119. * loading errors if batik is not present.
  120. */
  121. protected class SVGHandler {
  122. /**
  123. * Render the svg document.
  124. * @param context the renderer context
  125. * @param doc the svg document
  126. * @param info the pdf information of the current context
  127. */
  128. protected void renderSVGDocument(RendererContext context,
  129. org.w3c.dom.Document doc,
  130. Java2DInfo info) {
  131. log.debug("renderSVGDocument(" + context + ", " + doc + ", " + info + ")");
  132. int x = info.currentXPosition;
  133. int y = info.currentYPosition;
  134. float ptom = context.getUserAgent().getSourcePixelUnitToMillimeter();
  135. SVGUserAgent ua = new SVGUserAgent(ptom, new AffineTransform());
  136. GVTBuilder builder = new GVTBuilder();
  137. BridgeContext ctx = new BridgeContext(ua);
  138. GraphicsNode root;
  139. try {
  140. root = builder.build(ctx, doc);
  141. } catch (Exception e) {
  142. log.error("SVG graphic could not be built: " + e.getMessage(), e);
  143. return;
  144. }
  145. // If no viewbox is defined in the svg file, a viewbox of 100x100 is
  146. // assumed, as defined in SVGUserAgent.getViewportSize()
  147. float iw = (float) ctx.getDocumentSize().getWidth() * 1000f;
  148. float ih = (float) ctx.getDocumentSize().getHeight() * 1000f;
  149. float w = (float) info.width;
  150. float h = (float) info.height;
  151. AffineTransform origTransform = info.state.getGraph().getTransform();
  152. // correct integer roundoff
  153. info.state.getGraph().translate(x / 1000f, y / 1000f);
  154. //SVGSVGElement svg = ((SVGDocument) doc).getRootElement();
  155. // Aspect ratio preserved by layout engine, not here
  156. AffineTransform at = AffineTransform.getScaleInstance(w / iw, h / ih);
  157. if (!at.isIdentity()) {
  158. info.state.getGraph().transform(at);
  159. }
  160. try {
  161. root.paint(info.state.getGraph());
  162. } catch (Exception e) {
  163. log.error("Error while painting SVG", e);
  164. }
  165. info.state.getGraph().setTransform(origTransform);
  166. }
  167. }
  168. /** @see org.apache.fop.render.XMLHandler#getMimeType() */
  169. public String getMimeType() {
  170. return this.mimeType;
  171. }
  172. /** @see org.apache.fop.render.XMLHandler#getNamespace() */
  173. public String getNamespace() {
  174. return SVGDOMImplementation.SVG_NAMESPACE_URI;
  175. }
  176. }