1 23 24 package org.infoglue.deliver.util.graphics; 25 26 import java.awt.Color ; 27 import java.awt.Font ; 28 import java.awt.Graphics2D ; 29 import java.awt.RenderingHints ; 30 import java.awt.font.TextAttribute ; 31 import java.awt.font.TextLayout ; 32 import java.awt.image.BufferedImage ; 33 import java.io.File ; 34 import java.net.URL ; 35 import java.text.AttributedCharacterIterator ; 36 import java.text.AttributedString ; 37 import java.util.ArrayList ; 38 import java.util.Hashtable ; 39 import java.util.Iterator ; 40 import java.util.StringTokenizer ; 41 import java.util.Vector ; 42 43 import javax.imageio.ImageIO ; 44 45 46 55 56 public class ImageRenderer { 58 public static final int ALIGN_LEFT = 0; 59 public static final int ALIGN_CENTER = 1; 60 public static final int ALIGN_RIGHT = 2; 61 62 private int canvasWidth = 300; 63 private int canvasHeight = 100; 64 private int textStartPosX = 5; 65 private int textStartPosY = 25; 66 private int textWidth = 300; 67 private int textHeight = 100; 68 69 private String fontName = "Dialog"; 70 private int fontStyle = Font.PLAIN; 71 private int fontSize = 12; 72 73 private int alignment = ALIGN_LEFT; 74 75 private Color backgroundColor = null; 76 private Color foreGroundColor = null; 77 78 private String backgroundImageUrl = null; 79 80 143 144 147 148 public void generateGifImageFromText(String file, String text, String encoding) throws Exception 149 { 150 BufferedImage image = new BufferedImage (this.canvasWidth, this.canvasHeight, BufferedImage.TYPE_INT_ARGB); 151 152 155 drawText((Graphics2D )image.getGraphics(), text); 156 Hashtable arguments = new Hashtable (); 157 arguments.put("encoding", "websafe"); 158 File outputFile = new File (file); 161 javax.imageio.ImageIO.write(image, "PNG", outputFile); 162 } 163 164 165 private void drawText(Graphics2D g2d, String text) throws Exception 166 { 167 Font font = FontSaver.create(this.fontName, this.fontStyle, this.fontSize); 168 169 if(this.backgroundImageUrl != null) 170 { 171 URL url = new URL (this.backgroundImageUrl); 172 BufferedImage bufferedImage = ImageIO.read(url); 173 g2d.drawImage(bufferedImage,0,0, null); 174 } 175 else 176 { 177 g2d.setBackground(this.backgroundColor); 178 g2d.setPaint(this.backgroundColor); 179 g2d.fillRect(0, 0, this.canvasWidth, this.canvasHeight); 180 } 181 182 g2d.setPaint(this.foreGroundColor); 183 g2d.setFont(font); 184 185 g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 186 g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); 187 188 AttributedString as = new AttributedString (text); 189 as.addAttribute(TextAttribute.FONT, font); 190 as.addAttribute(TextAttribute.JUSTIFICATION, font); 191 AttributedCharacterIterator paragraph = as.getIterator(); 192 int paragraphStart = paragraph.getBeginIndex(); 193 int paragraphEnd = paragraph.getEndIndex(); 194 195 float drawPosY = (float)textStartPosY; 196 197 StringTokenizer st = new StringTokenizer (text); 198 Vector v = new Vector (); 199 while(st.hasMoreElements()) 200 { 201 String word = (String )st.nextElement(); 202 v.addElement(word); 203 } 204 205 String testString = ""; 206 String realString = ""; 207 java.util.List lines = new ArrayList (); 208 int offset = 0; 209 for(int i=0; i < v.size(); i++) 210 { 211 testString = realString + (String )v.get(i) + " "; 212 TextLayout testLay = new TextLayout (testString, font, g2d.getFontRenderContext()); 213 214 if(testLay.getBounds().getWidth() > textWidth || i == v.size()-2) 215 { 216 String remainingString = testString; 217 if(v.size() > i + 1) 218 remainingString += (String )v.get(i + 1); 219 if(v.size() > i + 2) 220 remainingString += " " + (String )v.get(i + 2); 221 222 TextLayout fullyFilledLay = new TextLayout (remainingString, font, g2d.getFontRenderContext()); 223 if(fullyFilledLay.getBounds().getWidth() < textWidth) 224 { 225 realString = testString; 226 testString = ""; 227 } 228 else 229 { 230 String row = ""; 231 for(int j=offset; j < i; j++) 232 row = row + (String )v.get(j) + " "; 233 234 lines.add(row); 235 realString = ""; 236 testString = ""; 237 offset = i; 238 realString = (String )v.get(i) + " "; 239 } 240 } 241 else 242 { 243 realString = testString; 244 testString = ""; 245 } 246 } 247 248 if(!realString.equalsIgnoreCase("")) 249 lines.add(realString); 250 251 252 Iterator i = lines.iterator(); 253 while (i.hasNext()) 254 { 255 String word = (String )i.next(); 256 if(word != null && word.length() > 0) 257 { 258 TextLayout layout = new TextLayout (word, font, g2d.getFontRenderContext()); 259 260 int centerX = this.textWidth / 2; 261 int centeredTextStartX = centerX - ((int)layout.getVisibleAdvance() / 2); 262 int rightTextStartX = this.textWidth - (int)layout.getVisibleAdvance(); 263 264 drawPosY += layout.getAscent(); 266 267 float drawPosX; 268 if (layout.isLeftToRight()) 269 { 270 if(this.alignment == ALIGN_CENTER) 271 drawPosX = centeredTextStartX; 272 if(this.alignment == ALIGN_RIGHT) 273 drawPosX = rightTextStartX - textStartPosX; 274 else 275 drawPosX = textStartPosX; 276 } 277 else 278 { 279 drawPosX = textWidth - layout.getAdvance(); 280 } 281 282 layout.draw(g2d, drawPosX, drawPosY); 284 285 drawPosY += layout.getDescent() + layout.getLeading(); 287 } 288 } 289 290 } 291 292 293 public void setCanvasHeight(int canvasHeight) 294 { 295 this.canvasHeight = canvasHeight; 296 } 297 298 public void setCanvasWidth(int canvasWidth) 299 { 300 this.canvasWidth = canvasWidth; 301 } 302 303 public void setTextHeight(int textHeight) 304 { 305 this.textHeight = textHeight; 306 } 307 308 public void setTextStartPosX(int textStartPosX) 309 { 310 this.textStartPosX = textStartPosX; 311 } 312 313 public void setTextStartPosY(int textStartPosY) 314 { 315 this.textStartPosY = textStartPosY; 316 } 317 318 public void setTextWidth(int textWidth) 319 { 320 this.textWidth = textWidth; 321 } 322 323 public void setBackgroundColor(Color backgroundColor) 324 { 325 this.backgroundColor = backgroundColor; 326 } 327 328 public void setForeGroundColor(Color foreGroundColor) 329 { 330 this.foreGroundColor = foreGroundColor; 331 } 332 333 public void setFontName(String fontName) 334 { 335 this.fontName = fontName; 336 } 337 338 public void setFontSize(int fontSize) 339 { 340 this.fontSize = fontSize; 341 } 342 343 public void setFontStyle(int fontStyle) 344 { 345 this.fontStyle = fontStyle; 346 } 347 348 public void setBackgroundImageUrl(String backgroundImageUrl) 349 { 350 this.backgroundImageUrl = backgroundImageUrl; 351 } 352 353 public int getAlignment() 354 { 355 return alignment; 356 } 357 358 public void setAlignment(int alignment) 359 { 360 this.alignment = alignment; 361 } 362 363 } 364 365 366 367 368 | Popular Tags |