1 17 18 19 20 package org.apache.fop.render; 21 22 import java.awt.Color ; 23 import java.awt.Graphics2D ; 24 import java.awt.Point ; 25 import java.awt.RenderingHints ; 26 import java.awt.Transparency ; 27 import java.awt.color.ColorSpace ; 28 import java.awt.geom.Rectangle2D ; 29 import java.awt.image.BufferedImage ; 30 import java.awt.image.ColorModel ; 31 import java.awt.image.ComponentColorModel ; 32 import java.awt.image.DataBuffer ; 33 import java.awt.image.Raster ; 34 import java.awt.image.WritableRaster ; 35 36 import org.apache.fop.render.Graphics2DAdapter; 37 import org.apache.fop.render.Graphics2DImagePainter; 38 import org.apache.fop.render.RendererContext.RendererContextWrapper; 39 import org.apache.fop.util.UnitConv; 40 41 44 public abstract class AbstractGraphics2DAdapter implements Graphics2DAdapter { 45 46 54 protected BufferedImage paintToBufferedImage(Graphics2DImagePainter painter, 55 RendererContextWrapper context, int resolution, boolean gray, boolean withAlpha) { 56 int bmw = (int)Math.ceil(UnitConv.mpt2px(context.getWidth(), resolution)); 57 int bmh = (int)Math.ceil(UnitConv.mpt2px(context.getHeight(), resolution)); 58 BufferedImage bi; 59 if (gray) { 60 if (withAlpha) { 61 bi = createGrayBufferedImageWithAlpha(bmw, bmh); 62 } else { 63 bi = new BufferedImage (bmw, bmh, BufferedImage.TYPE_BYTE_GRAY); 64 } 65 } else { 66 if (withAlpha) { 67 bi = new BufferedImage (bmw, bmh, BufferedImage.TYPE_INT_ARGB); 68 } else { 69 bi = new BufferedImage (bmw, bmh, BufferedImage.TYPE_INT_RGB); 70 } 71 } 72 Graphics2D g2d = bi.createGraphics(); 73 try { 74 g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, 75 RenderingHints.VALUE_FRACTIONALMETRICS_ON); 76 setRenderingHintsForBufferedImage(g2d); 77 78 g2d.setBackground(Color.white); 79 g2d.setColor(Color.black); 80 if (!withAlpha) { 81 g2d.clearRect(0, 0, bmw, bmh); 82 } 83 90 double sx = (double)bmw / context.getWidth(); 91 double sy = (double)bmh / context.getHeight(); 92 g2d.scale(sx, sy); 93 94 Rectangle2D area = new Rectangle2D.Double ( 96 0.0, 0.0, context.getWidth(), context.getHeight()); 97 painter.paint(g2d, area); 98 } finally { 99 g2d.dispose(); 100 } 101 return bi; 102 } 103 104 private static BufferedImage createGrayBufferedImageWithAlpha(int width, int height) { 105 BufferedImage bi; 106 boolean alphaPremultiplied = true; 107 int bands = 2; 108 int[] bits = new int[bands]; 109 for (int i = 0; i < bands; i++) { 110 bits[i] = 8; 111 } 112 ColorModel cm = new ComponentColorModel ( 113 ColorSpace.getInstance(ColorSpace.CS_GRAY), 114 bits, 115 true, alphaPremultiplied, 116 Transparency.TRANSLUCENT, 117 DataBuffer.TYPE_BYTE); 118 WritableRaster wr = Raster.createInterleavedRaster( 119 DataBuffer.TYPE_BYTE, 120 width, height, bands, 121 new Point (0, 0)); 122 bi = new BufferedImage (cm, wr, alphaPremultiplied, null); 123 return bi; 124 } 125 126 131 protected void setRenderingHintsForBufferedImage(Graphics2D g2d) { 132 g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 133 RenderingHints.VALUE_ANTIALIAS_OFF); 134 g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, 135 RenderingHints.VALUE_TEXT_ANTIALIAS_OFF); 136 } 137 138 } 139 | Popular Tags |