Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

Java2DSVGHandler.java 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 java.awt.geom.AffineTransform;
  19. import org.w3c.dom.Document;
  20. import org.apache.fop.apps.MimeConstants;
  21. import org.apache.fop.render.Renderer;
  22. import org.apache.fop.render.XMLHandler;
  23. import org.apache.fop.render.RendererContext;
  24. import org.apache.fop.render.pdf.PDFRenderer;
  25. import org.apache.fop.svg.SVGUserAgent;
  26. // Commons-Logging
  27. import org.apache.commons.logging.Log;
  28. import org.apache.commons.logging.LogFactory;
  29. import org.apache.batik.bridge.GVTBuilder;
  30. import org.apache.batik.bridge.BridgeContext;
  31. import org.apache.batik.dom.svg.SVGDOMImplementation;
  32. import org.apache.batik.gvt.GraphicsNode;
  33. /**
  34. * Java2D XML handler for SVG (uses Apache Batik).
  35. * This handler handles XML for foreign objects when rendering to Java2D.
  36. * The properties from the Java2D renderer are subject to change.
  37. */
  38. public class Java2DSVGHandler implements XMLHandler, Java2DRendererContextConstants {
  39. /** logging instance */
  40. private static Log log = LogFactory.getLog(Java2DSVGHandler.class);
  41. /**
  42. * Create a new Java2D XML handler for use by the Java2D renderer and its subclasses.
  43. */
  44. public Java2DSVGHandler() {
  45. //nop
  46. }
  47. /** @see org.apache.fop.render.XMLHandler */
  48. public void handleXML(RendererContext context,
  49. Document doc, String ns) throws Exception {
  50. Java2DInfo pdfi = getJava2DInfo(context);
  51. if (SVGDOMImplementation.SVG_NAMESPACE_URI.equals(ns)) {
  52. renderSVGDocument(context, doc, pdfi);
  53. }
  54. }
  55. /**
  56. * Get the pdf information from the render context.
  57. *
  58. * @param context the renderer context
  59. * @return the pdf information retrieved from the context
  60. */
  61. public static Java2DInfo getJava2DInfo(RendererContext context) {
  62. Java2DInfo pdfi = new Java2DInfo();
  63. pdfi.state = (Java2DGraphicsState)context.getProperty(JAVA2D_STATE);
  64. pdfi.width = ((Integer)context.getProperty(WIDTH)).intValue();
  65. pdfi.height = ((Integer)context.getProperty(HEIGHT)).intValue();
  66. pdfi.currentXPosition = ((Integer)context.getProperty(XPOS)).intValue();
  67. pdfi.currentYPosition = ((Integer)context.getProperty(YPOS)).intValue();
  68. return pdfi;
  69. }
  70. /**
  71. * Java2D information structure for drawing the XML document.
  72. */
  73. public static class Java2DInfo {
  74. /** see Java2D_STATE */
  75. public Java2DGraphicsState state;
  76. /** see Java2D_WIDTH */
  77. public int width;
  78. /** see Java2D_HEIGHT */
  79. public int height;
  80. /** see Java2D_XPOS */
  81. public int currentXPosition;
  82. /** see Java2D_YPOS */
  83. public int currentYPosition;
  84. /** @see java.lang.Object#toString() */
  85. public String toString() {
  86. return "Java2DInfo {"
  87. + "state = " + state + ", "
  88. + "width = " + width + ", "
  89. + "height = " + height + ", "
  90. + "currentXPosition = " + currentXPosition + ", "
  91. + "currentYPosition = " + currentYPosition + "}";
  92. }
  93. }
  94. /**
  95. * Render the svg document.
  96. * @param context the renderer context
  97. * @param doc the svg document
  98. * @param info the pdf information of the current context
  99. */
  100. protected void renderSVGDocument(RendererContext context,
  101. Document doc,
  102. Java2DInfo info) {
  103. log.debug("renderSVGDocument(" + context + ", " + doc + ", " + info + ")");
  104. int x = info.currentXPosition;
  105. int y = info.currentYPosition;
  106. float ptom = context.getUserAgent().getSourcePixelUnitToMillimeter();
  107. SVGUserAgent ua = new SVGUserAgent(ptom, new AffineTransform());
  108. GVTBuilder builder = new GVTBuilder();
  109. BridgeContext ctx = new BridgeContext(ua);
  110. GraphicsNode root;
  111. try {
  112. root = builder.build(ctx, doc);
  113. } catch (Exception e) {
  114. log.error("SVG graphic could not be built: " + e.getMessage(), e);
  115. return;
  116. }
  117. // If no viewbox is defined in the svg file, a viewbox of 100x100 is
  118. // assumed, as defined in SVGUserAgent.getViewportSize()
  119. float iw = (float) ctx.getDocumentSize().getWidth() * 1000f;
  120. float ih = (float) ctx.getDocumentSize().getHeight() * 1000f;
  121. float w = (float) info.width;
  122. float h = (float) info.height;
  123. AffineTransform origTransform = info.state.getGraph().getTransform();
  124. // correct integer roundoff
  125. info.state.getGraph().translate(x / 1000f, y / 1000f);
  126. //SVGSVGElement svg = ((SVGDocument) doc).getRootElement();
  127. // Aspect ratio preserved by layout engine, not here
  128. AffineTransform at = AffineTransform.getScaleInstance(w / iw, h / ih);
  129. if (!at.isIdentity()) {
  130. info.state.getGraph().transform(at);
  131. }
  132. try {
  133. root.paint(info.state.getGraph());
  134. } catch (Exception e) {
  135. log.error("Error while painting SVG", e);
  136. }
  137. info.state.getGraph().setTransform(origTransform);
  138. }
  139. /** @see org.apache.fop.render.XMLHandler#supportsRenderer() */
  140. public boolean supportsRenderer(Renderer renderer) {
  141. return (renderer instanceof Java2DRenderer);
  142. }
  143. /** @see org.apache.fop.render.XMLHandler#getNamespace() */
  144. public String getNamespace() {
  145. return SVGDOMImplementation.SVG_NAMESPACE_URI;
  146. }
  147. }