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 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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.AffineTransform;
  22. import java.awt.geom.Rectangle2D;
  23. import java.math.BigDecimal;
  24. import java.math.RoundingMode;
  25. import org.apache.batik.bridge.BridgeContext;
  26. import org.apache.batik.gvt.GraphicsNode;
  27. import org.apache.xmlgraphics.java2d.AbstractGraphics2D;
  28. import org.apache.xmlgraphics.java2d.Graphics2DImagePainter;
  29. /**
  30. * A graphics 2D image painter implementation for painting SVG images using Batik.
  31. */
  32. public class Graphics2DImagePainterImpl implements Graphics2DImagePainter {
  33. private final GraphicsNode root;
  34. /** the Batik bridge context */
  35. protected final BridgeContext ctx;
  36. /** the intrinsic size of the image */
  37. protected final Dimension imageSize;
  38. /**
  39. * Main constructor
  40. *
  41. * @param root the graphics node root
  42. * @param ctx the bridge context
  43. * @param imageSize the image size
  44. */
  45. public Graphics2DImagePainterImpl(GraphicsNode root, BridgeContext ctx, Dimension imageSize) {
  46. this.root = root;
  47. this.imageSize = imageSize;
  48. this.ctx = ctx;
  49. }
  50. /** {@inheritDoc} */
  51. public Dimension getImageSize() {
  52. return imageSize;
  53. }
  54. private void prepare(Graphics2D g2d, Rectangle2D area) {
  55. // If no viewbox is defined in the svg file, a viewbox of 100x100 is
  56. // assumed, as defined in SVGUserAgent.getViewportSize()
  57. double tx = area.getX();
  58. double ty = area.getY();
  59. if (tx != 0 || ty != 0) {
  60. g2d.translate(tx, ty);
  61. }
  62. float iw = (float) ctx.getDocumentSize().getWidth();
  63. float ih = (float) ctx.getDocumentSize().getHeight();
  64. float w = (float) area.getWidth();
  65. float h = (float) area.getHeight();
  66. float sx = w / iw;
  67. float sy = h / ih;
  68. if (sx != 1.0 || sy != 1.0) {
  69. g2d.scale(sx, sy);
  70. }
  71. normaliseScale(g2d);
  72. }
  73. private void normaliseScale(Graphics2D g2d) {
  74. if (!(g2d instanceof AbstractGraphics2D)) {
  75. AffineTransform old = g2d.getTransform();
  76. double scaleX = BigDecimal.valueOf(old.getScaleX()).setScale(2, RoundingMode.HALF_UP).doubleValue();
  77. double scaleY = BigDecimal.valueOf(old.getScaleY()).setScale(2, RoundingMode.HALF_UP).doubleValue();
  78. AffineTransform newat = new AffineTransform(scaleX, old.getShearY(), old.getShearX(), scaleY,
  79. old.getTranslateX(), old.getTranslateY());
  80. g2d.setTransform(newat);
  81. }
  82. }
  83. /** {@inheritDoc} */
  84. public void paint(Graphics2D g2d, Rectangle2D area) {
  85. prepare(g2d, area);
  86. root.paint(g2d);
  87. }
  88. }