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.

BatikGraphics2DImagePainter.java 2.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. package org.apache.fop.image.loader.batik;
  2. import java.awt.Dimension;
  3. import java.awt.Graphics2D;
  4. import java.awt.geom.Rectangle2D;
  5. import org.apache.batik.bridge.BridgeContext;
  6. import org.apache.batik.gvt.GraphicsNode;
  7. import org.apache.xmlgraphics.image.loader.impl.ImageXMLDOM;
  8. import org.apache.xmlgraphics.java2d.Graphics2DImagePainter;
  9. /**
  10. * A generic graphics 2D image painter implementation
  11. */
  12. public class BatikGraphics2DImagePainter implements Graphics2DImagePainter {
  13. protected final ImageXMLDOM svg;
  14. protected final BridgeContext ctx;
  15. protected final GraphicsNode root;
  16. /**
  17. * Constructor
  18. *
  19. * @param svg the svg image dom
  20. * @param ctx the bridge context
  21. * @param root the graphics node root
  22. */
  23. public BatikGraphics2DImagePainter(ImageXMLDOM svg, BridgeContext ctx, GraphicsNode root) {
  24. this.svg = svg;
  25. this.ctx = ctx;
  26. this.root = root;
  27. }
  28. /**
  29. * Initialises the graphics 2d
  30. *
  31. * @param g2d the graphics 2d
  32. * @param area the rectangle drawing area
  33. */
  34. protected void init(Graphics2D g2d, Rectangle2D area) {
  35. // If no viewbox is defined in the svg file, a viewbox of 100x100 is
  36. // assumed, as defined in SVGUserAgent.getViewportSize()
  37. double tx = area.getX();
  38. double ty = area.getY();
  39. if (tx != 0 || ty != 0) {
  40. g2d.translate(tx, ty);
  41. }
  42. float iw = (float) ctx.getDocumentSize().getWidth();
  43. float ih = (float) ctx.getDocumentSize().getHeight();
  44. float w = (float) area.getWidth();
  45. float h = (float) area.getHeight();
  46. float sx = w / iw;
  47. float sy = h / ih;
  48. if (sx != 1.0 || sy != 1.0) {
  49. g2d.scale(sx, sy);
  50. }
  51. }
  52. /** {@inheritDoc} */
  53. public void paint(Graphics2D g2d, Rectangle2D area) {
  54. init(g2d, area);
  55. root.paint(g2d);
  56. }
  57. /** {@inheritDoc} */
  58. public Dimension getImageSize() {
  59. return new Dimension(svg.getSize().getWidthMpt(), svg.getSize().getHeightMpt());
  60. }
  61. /**
  62. * Returns the svg image dom
  63. * @return the svg image dom
  64. */
  65. public ImageXMLDOM getImageXMLDOM() {
  66. return svg;
  67. }
  68. /**
  69. * Returns the bridge context
  70. * @return the bridge context
  71. */
  72. public BridgeContext getBridgeContext() {
  73. return ctx;
  74. }
  75. /**
  76. * Returns the graphics root node
  77. * @return the graphics root node
  78. */
  79. public GraphicsNode getRoot() {
  80. return root;
  81. }
  82. }