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.

Java2DGraphics2DAdapter.java 2.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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.render.java2d;
  19. import java.awt.Color;
  20. import java.awt.Dimension;
  21. import java.awt.Graphics2D;
  22. import java.awt.geom.AffineTransform;
  23. import java.awt.geom.Rectangle2D;
  24. import java.io.IOException;
  25. import org.apache.xmlgraphics.java2d.Graphics2DImagePainter;
  26. import org.apache.fop.render.Graphics2DAdapter;
  27. import org.apache.fop.render.RendererContext;
  28. /**
  29. * Graphics2DAdapter implementation for Java2D.
  30. */
  31. public class Java2DGraphics2DAdapter implements Graphics2DAdapter {
  32. /** {@inheritDoc} */
  33. public void paintImage(Graphics2DImagePainter painter,
  34. RendererContext context,
  35. int x, int y, int width, int height) throws IOException {
  36. float fwidth = width / 1000f;
  37. float fheight = height / 1000f;
  38. float fx = x / 1000f;
  39. float fy = y / 1000f;
  40. // get the 'width' and 'height' attributes of the SVG document
  41. Dimension dim = painter.getImageSize();
  42. float imw = (float)dim.getWidth() / 1000f;
  43. float imh = (float)dim.getHeight() / 1000f;
  44. float sx = fwidth / (float)imw;
  45. float sy = fheight / (float)imh;
  46. Java2DRenderer renderer = (Java2DRenderer)context.getRenderer();
  47. Java2DGraphicsState state = renderer.state;
  48. //Create copy and paint on that
  49. Graphics2D g2d = (Graphics2D)state.getGraph().create();
  50. g2d.setColor(Color.black);
  51. g2d.setBackground(Color.black);
  52. //TODO Clip to the image area.
  53. // transform so that the coordinates (0,0) is from the top left
  54. // and positive is down and to the right. (0,0) is where the
  55. // viewBox puts it.
  56. g2d.translate(fx, fy);
  57. AffineTransform at = AffineTransform.getScaleInstance(sx, sy);
  58. if (!at.isIdentity()) {
  59. g2d.transform(at);
  60. }
  61. Rectangle2D area = new Rectangle2D.Double(0.0, 0.0, imw, imh);
  62. painter.paint(g2d, area);
  63. g2d.dispose();
  64. }
  65. }