Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

PCLSVGHandler.java 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * Copyright 2006 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.pcl;
  18. // Java
  19. import java.awt.Dimension;
  20. import java.awt.Graphics2D;
  21. import java.awt.geom.AffineTransform;
  22. import java.awt.geom.Rectangle2D;
  23. import java.io.IOException;
  24. // DOM
  25. import org.w3c.dom.Document;
  26. // Batik
  27. import org.apache.batik.bridge.GVTBuilder;
  28. import org.apache.batik.bridge.BridgeContext;
  29. import org.apache.batik.dom.svg.SVGDOMImplementation;
  30. import org.apache.batik.gvt.GraphicsNode;
  31. // FOP
  32. import org.apache.fop.render.Graphics2DAdapter;
  33. import org.apache.fop.render.Graphics2DImagePainter;
  34. import org.apache.fop.render.Renderer;
  35. import org.apache.fop.render.RendererContextConstants;
  36. import org.apache.fop.render.XMLHandler;
  37. import org.apache.fop.render.RendererContext;
  38. import org.apache.fop.svg.SVGUserAgent;
  39. // Commons-Logging
  40. import org.apache.commons.logging.Log;
  41. import org.apache.commons.logging.LogFactory;
  42. /**
  43. * PCL XML handler for SVG. Uses Apache Batik for SVG processing.
  44. * This handler handles XML for foreign objects when rendering to HP GL/2.
  45. * It renders SVG to HP GL/2 using the PCLGraphics2D.
  46. *
  47. * @version $Id$
  48. */
  49. public class PCLSVGHandler implements XMLHandler, RendererContextConstants {
  50. /** logging instance */
  51. private static Log log = LogFactory.getLog(PCLSVGHandler.class);
  52. /**
  53. * Create a new XML handler for use by the PCL renderer.
  54. */
  55. public PCLSVGHandler() {
  56. }
  57. /** @see org.apache.fop.render.XMLHandler */
  58. public void handleXML(RendererContext context,
  59. Document doc, String ns) throws Exception {
  60. PCLRendererContext pclContext = PCLRendererContext.wrapRendererContext(context);
  61. if (SVGDOMImplementation.SVG_NAMESPACE_URI.equals(ns)) {
  62. renderSVGDocument(context, doc, pclContext);
  63. }
  64. }
  65. /**
  66. * Render the SVG document.
  67. * @param context the renderer context
  68. * @param doc the SVG document
  69. * @param pclContext the information of the current context
  70. */
  71. protected void renderSVGDocument(final RendererContext context,
  72. final Document doc, final PCLRendererContext pclContext) {
  73. int x = pclContext.getCurrentXPosition();
  74. int y = pclContext.getCurrentYPosition();
  75. Graphics2DImagePainter painter = new Graphics2DImagePainter() {
  76. public void paint(Graphics2D g2d, Rectangle2D area) {
  77. SVGUserAgent ua = new SVGUserAgent(
  78. context.getUserAgent().getSourcePixelUnitToMillimeter(),
  79. new AffineTransform());
  80. GVTBuilder builder = new GVTBuilder();
  81. BridgeContext ctx = new BridgeContext(ua);
  82. GraphicsNode root;
  83. try {
  84. root = builder.build(ctx, doc);
  85. // If no viewbox is defined in the svg file, a viewbox of 100x100 is
  86. // assumed, as defined in SVGUserAgent.getViewportSize()
  87. float iw = (float) ctx.getDocumentSize().getWidth() * 1000f;
  88. float ih = (float) ctx.getDocumentSize().getHeight() * 1000f;
  89. float w = (float) area.getWidth();
  90. float h = (float) area.getHeight();
  91. g2d.scale(w / iw, h / ih);
  92. root.paint(g2d);
  93. } catch (Exception e) {
  94. log.error("SVG graphic could not be built: "
  95. + e.getMessage(), e);
  96. return;
  97. }
  98. }
  99. public Dimension getImageSize() {
  100. return new Dimension(pclContext.getWidth(), pclContext.getHeight());
  101. }
  102. };
  103. try {
  104. Graphics2DAdapter adapter = context.getRenderer().getGraphics2DAdapter();
  105. adapter.paintImage(painter, context,
  106. x, y, pclContext.getWidth(), pclContext.getHeight());
  107. } catch (IOException ioe) {
  108. ((PCLRenderer)context.getRenderer()).handleIOTrouble(ioe);
  109. }
  110. }
  111. /** @see org.apache.fop.render.XMLHandler#supportsRenderer(org.apache.fop.render.Renderer) */
  112. public boolean supportsRenderer(Renderer renderer) {
  113. return (renderer instanceof PCLRenderer);
  114. }
  115. /** @see org.apache.fop.render.XMLHandler#getNamespace() */
  116. public String getNamespace() {
  117. return SVGDOMImplementation.SVG_NAMESPACE_URI;
  118. }
  119. }