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.

Graphics2DImagePainterImpl.java 2.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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.image.loader.batik;
  19. import java.awt.Dimension;
  20. import java.awt.Graphics2D;
  21. import java.awt.geom.Rectangle2D;
  22. import org.apache.batik.bridge.BridgeContext;
  23. import org.apache.batik.gvt.GraphicsNode;
  24. import org.apache.xmlgraphics.java2d.Graphics2DImagePainter;
  25. /**
  26. * A graphics 2D image painter implementation for painting SVG images using Batik.
  27. */
  28. public class Graphics2DImagePainterImpl implements Graphics2DImagePainter {
  29. private final GraphicsNode root;
  30. /** the Batik bridge context */
  31. protected final BridgeContext ctx;
  32. /** the intrinsic size of the image */
  33. protected final Dimension imageSize;
  34. /**
  35. * Main constructor
  36. *
  37. * @param root the graphics node root
  38. * @param ctx the bridge context
  39. * @param imageSize the image size
  40. */
  41. public Graphics2DImagePainterImpl(GraphicsNode root, BridgeContext ctx, Dimension imageSize) {
  42. this.root = root;
  43. this.imageSize = imageSize;
  44. this.ctx = ctx;
  45. }
  46. /** {@inheritDoc} */
  47. public Dimension getImageSize() {
  48. return imageSize;
  49. }
  50. private void prepare(Graphics2D g2d, Rectangle2D area) {
  51. // If no viewbox is defined in the svg file, a viewbox of 100x100 is
  52. // assumed, as defined in SVGUserAgent.getViewportSize()
  53. double tx = area.getX();
  54. double ty = area.getY();
  55. if (tx != 0 || ty != 0) {
  56. g2d.translate(tx, ty);
  57. }
  58. float iw = (float) ctx.getDocumentSize().getWidth();
  59. float ih = (float) ctx.getDocumentSize().getHeight();
  60. float w = (float) area.getWidth();
  61. float h = (float) area.getHeight();
  62. float sx = w / iw;
  63. float sy = h / ih;
  64. if (sx != 1.0 || sy != 1.0) {
  65. g2d.scale(sx, sy);
  66. }
  67. }
  68. /** {@inheritDoc} */
  69. public void paint(Graphics2D g2d, Rectangle2D area) {
  70. prepare(g2d, area);
  71. root.paint(g2d);
  72. }
  73. }