1 13 package info.magnolia.cms.taglibs.util; 14 15 import java.awt.Color ; 16 import java.awt.Font ; 17 import java.awt.FontFormatException ; 18 import java.awt.FontMetrics ; 19 import java.awt.Graphics2D ; 20 import java.awt.RenderingHints ; 21 import java.awt.font.FontRenderContext ; 22 import java.awt.font.TextLayout ; 23 import java.awt.geom.Rectangle2D ; 24 import java.awt.image.BufferedImage ; 25 import java.awt.image.RenderedImage ; 26 import java.io.File ; 27 import java.io.FileInputStream ; 28 import java.io.IOException ; 29 import java.io.InputStream ; 30 31 import javax.imageio.ImageIO ; 32 33 34 40 public class Text2PngFactory { 41 42 43 private String fontname; 44 45 46 private int fontsize; 47 48 49 private String text = ""; 50 51 52 private int r = 0; 53 54 private int g = 0; 55 56 private int b = 0; 57 58 59 private int br = 0xff; 60 61 private int bg = 0xff; 62 63 private int bb = 0xff; 64 65 66 private Graphics2D g2; 67 68 69 private Font cachedFont; 70 71 74 public Text2PngFactory() { 75 this.g2 = new BufferedImage (1, 1, BufferedImage.TYPE_3BYTE_BGR).createGraphics(); 77 setOptimalRenderQuality(this.g2); 79 } 80 81 88 public Text2PngFactory(String fontname, int fontsize) throws IOException , FontFormatException { 89 this(fontname, fontsize, ""); 90 } 91 92 100 public Text2PngFactory(String fontname, int fontsize, String text) throws IOException , FontFormatException { 101 102 this.g2 = new BufferedImage (1, 1, BufferedImage.TYPE_3BYTE_BGR).createGraphics(); 104 setOptimalRenderQuality(this.g2); 106 this.setFontFace(fontname); 107 this.setFontSize(fontsize); 108 this.setText(text); 109 } 110 111 116 public void createPngFile(String location) throws IOException { 117 createPngFile(new File (location)); 118 } 119 120 125 public void createPngFile(File location) throws IOException { 126 ImageIO.write(createImage(), "png", location); 127 } 128 129 134 public RenderedImage createImage() throws IOException { 135 136 if (this.fontname == null) { 137 throw new IOException ("No font name given!"); 138 } 139 140 FontRenderContext frc = this.g2.getFontRenderContext(); 142 TextLayout layout = new TextLayout (this.text, this.cachedFont, frc); 143 Rectangle2D bounds = layout.getBounds(); 144 145 int stringWidth = (int) (Math.ceil(bounds.getWidth())) + 2; 148 149 FontMetrics fm = this.g2.getFontMetrics(); 153 int stringHeight = fm.getHeight(); 154 155 BufferedImage im = new BufferedImage (stringWidth, stringHeight, BufferedImage.TYPE_3BYTE_BGR); 157 158 Graphics2D graphics = im.createGraphics(); 160 161 setOptimalRenderQuality(graphics); 163 164 graphics.setBackground(new Color (this.br, this.bg, this.bb)); 166 graphics.setColor(new Color (this.r, this.g, this.b)); 167 graphics.clearRect(0, 0, stringWidth, stringHeight); 168 169 graphics.setFont(getFont()); 171 172 layout.draw(graphics, -(float) Math.floor(bounds.getX()), fm.getMaxAscent()); 175 176 return im; 178 } 179 180 184 public void setText(String text) { 185 this.text = text; 186 } 187 188 194 public void setTextRGB(int r, int g, int b) { 195 this.r = r; 196 this.g = g; 197 this.b = b; 198 } 199 200 206 public void setBackgroundRGB(int r, int g, int b) { 207 this.br = r; 208 this.bg = g; 209 this.bb = b; 210 } 211 212 216 public void setFontFace(String fontname) throws IOException , FontFormatException { 217 218 if (!fontname.equals(this.fontname)) { 219 this.fontname = fontname; 220 updateFace(); 221 } 222 } 223 224 228 public void setFontSize(int fontsize) { 229 if (fontsize != this.fontsize) { 230 this.fontsize = fontsize; 231 updateSize(); 232 } 233 } 234 235 240 private void updateFace() throws IOException , FontFormatException { 241 Font createdFont = null; 242 243 String fontpath = "fonts/" + this.fontname + ".ttf"; 245 246 InputStream fontStream = this.getClass().getClassLoader().getResourceAsStream(fontpath); 247 if (fontStream != null) { 248 createdFont = Font.createFont(Font.TRUETYPE_FONT, fontStream); 249 fontStream.close(); 250 } 251 if (createdFont == null) { 253 Font tempFont = new Font (this.fontname, Font.PLAIN, 1); 254 255 if (tempFont.getFamily().equals(this.fontname)) { 257 createdFont = tempFont; 259 } 260 } 261 if (createdFont == null) { 263 fontStream = new FileInputStream (this.fontname); 264 265 if (fontStream != null) { 266 createdFont = Font.createFont(Font.TRUETYPE_FONT, fontStream); 267 fontStream.close(); 268 } 269 } 270 if (createdFont == null) { 272 throw new IOException ("Can't locate font: " + this.fontname); 273 } 274 275 this.cachedFont = createdFont.deriveFont((float) this.fontsize); 277 278 this.g2.setFont(this.cachedFont); 280 } 281 282 285 private void updateSize() { 286 287 if (this.cachedFont == null) { 288 return; 289 } 290 291 this.cachedFont = this.cachedFont.deriveFont((float) this.fontsize); 293 294 this.g2.setFont(this.cachedFont); 296 } 297 298 302 private FontMetrics getFontMetrics() { 303 304 return this.g2.getFontMetrics(); 305 } 306 307 311 private Font getFont() { 312 313 return this.cachedFont; 314 } 315 316 320 private void setOptimalRenderQuality(Graphics2D graphics) { 321 322 graphics.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); 323 graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 324 graphics.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON); 325 graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC); 326 } 327 328 public String toString() { 329 return this.fontname + ", " + this.fontsize + "pt: " + this.text; 330 } 331 } | Popular Tags |