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.

AbstractGraphics2DAdapter.java 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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;
  19. import java.awt.Color;
  20. import java.awt.Graphics2D;
  21. import java.awt.Point;
  22. import java.awt.RenderingHints;
  23. import java.awt.Transparency;
  24. import java.awt.color.ColorSpace;
  25. import java.awt.geom.Rectangle2D;
  26. import java.awt.image.BufferedImage;
  27. import java.awt.image.ColorModel;
  28. import java.awt.image.ComponentColorModel;
  29. import java.awt.image.DataBuffer;
  30. import java.awt.image.Raster;
  31. import java.awt.image.WritableRaster;
  32. import java.io.IOException;
  33. import org.apache.fop.render.RendererContext.RendererContextWrapper;
  34. import org.apache.fop.util.UnitConv;
  35. /**
  36. * Abstract base class for Graphics2DAdapter implementations.
  37. */
  38. public abstract class AbstractGraphics2DAdapter implements Graphics2DAdapter {
  39. /**
  40. * Paints the image to a BufferedImage and returns that.
  41. * @param painter the painter which will paint the actual image
  42. * @param context the renderer context for the current renderer
  43. * @param resolution the requested bitmap resolution
  44. * @param gray true if the generated image should be in grayscales
  45. * @param withAlpha true if an alpha channel should be created
  46. * @return the generated BufferedImage
  47. */
  48. protected BufferedImage paintToBufferedImage(
  49. org.apache.xmlgraphics.java2d.Graphics2DImagePainter painter,
  50. RendererContextWrapper context, int resolution, boolean gray, boolean withAlpha) {
  51. int bmw = (int)Math.ceil(UnitConv.mpt2px(context.getWidth(), resolution));
  52. int bmh = (int)Math.ceil(UnitConv.mpt2px(context.getHeight(), resolution));
  53. BufferedImage bi;
  54. if (gray) {
  55. if (withAlpha) {
  56. bi = createGrayBufferedImageWithAlpha(bmw, bmh);
  57. } else {
  58. bi = new BufferedImage(bmw, bmh, BufferedImage.TYPE_BYTE_GRAY);
  59. }
  60. } else {
  61. if (withAlpha) {
  62. bi = new BufferedImage(bmw, bmh, BufferedImage.TYPE_INT_ARGB);
  63. } else {
  64. bi = new BufferedImage(bmw, bmh, BufferedImage.TYPE_INT_RGB);
  65. }
  66. }
  67. Graphics2D g2d = bi.createGraphics();
  68. try {
  69. g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS,
  70. RenderingHints.VALUE_FRACTIONALMETRICS_ON);
  71. setRenderingHintsForBufferedImage(g2d);
  72. g2d.setBackground(Color.white);
  73. g2d.setColor(Color.black);
  74. if (!withAlpha) {
  75. g2d.clearRect(0, 0, bmw, bmh);
  76. }
  77. /* debug code
  78. int off = 2;
  79. g2d.drawLine(off, 0, off, bmh);
  80. g2d.drawLine(bmw - off, 0, bmw - off, bmh);
  81. g2d.drawLine(0, off, bmw, off);
  82. g2d.drawLine(0, bmh - off, bmw, bmh - off);
  83. */
  84. double sx = (double)bmw / context.getWidth();
  85. double sy = (double)bmh / context.getHeight();
  86. g2d.scale(sx, sy);
  87. //Paint the image on the BufferedImage
  88. Rectangle2D area = new Rectangle2D.Double(
  89. 0.0, 0.0, context.getWidth(), context.getHeight());
  90. painter.paint(g2d, area);
  91. } finally {
  92. g2d.dispose();
  93. }
  94. return bi;
  95. }
  96. private static BufferedImage createGrayBufferedImageWithAlpha(int width, int height) {
  97. BufferedImage bi;
  98. boolean alphaPremultiplied = true;
  99. int bands = 2;
  100. int[] bits = new int[bands];
  101. for (int i = 0; i < bands; i++) {
  102. bits[i] = 8;
  103. }
  104. ColorModel cm = new ComponentColorModel(
  105. ColorSpace.getInstance(ColorSpace.CS_GRAY),
  106. bits,
  107. true, alphaPremultiplied,
  108. Transparency.TRANSLUCENT,
  109. DataBuffer.TYPE_BYTE);
  110. WritableRaster wr = Raster.createInterleavedRaster(
  111. DataBuffer.TYPE_BYTE,
  112. width, height, bands,
  113. new Point(0, 0));
  114. bi = new BufferedImage(cm, wr, alphaPremultiplied, null);
  115. return bi;
  116. }
  117. /**
  118. * Sets rendering hints on the Graphics2D created for painting to a BufferedImage. Subclasses
  119. * can modify the settings to customize the behaviour.
  120. * @param g2d the Graphics2D instance
  121. */
  122. protected void setRenderingHintsForBufferedImage(Graphics2D g2d) {
  123. g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
  124. RenderingHints.VALUE_ANTIALIAS_OFF);
  125. g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
  126. RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
  127. }
  128. /** {@inheritDoc} */
  129. public void paintImage(Graphics2DImagePainter painter,
  130. RendererContext context,
  131. int x, int y, int width, int height) throws IOException {
  132. paintImage((org.apache.xmlgraphics.java2d.Graphics2DImagePainter)painter,
  133. context, x, y, width, height);
  134. }
  135. }