1 13 package info.magnolia.cms.taglibs.util; 14 15 import info.magnolia.cms.core.Content; 16 import info.magnolia.cms.core.ItemType; 17 18 import java.awt.FontFormatException ; 19 import java.awt.Graphics2D ; 20 import java.awt.Image ; 21 import java.awt.image.BufferedImage ; 22 import java.io.File ; 23 import java.io.FileNotFoundException ; 24 import java.io.IOException ; 25 import java.util.StringTokenizer ; 26 27 import javax.imageio.ImageIO ; 28 import javax.jcr.AccessDeniedException; 29 import javax.jcr.PathNotFoundException; 30 import javax.jcr.RepositoryException; 31 import javax.servlet.http.HttpServletRequest ; 32 import javax.servlet.jsp.JspException ; 33 import javax.servlet.jsp.JspWriter ; 34 import javax.servlet.jsp.PageContext ; 35 36 import org.apache.commons.lang.SystemUtils; 37 import org.slf4j.Logger; 38 import org.slf4j.LoggerFactory; 39 40 41 72 public class TextToImageTag extends BaseImageTag { 73 74 79 private static final double SCALE_FACTOR = SystemUtils.IS_OS_WINDOWS ? 4 : 1; 80 81 84 private static final String CSS_CHARACTER_IMAGE = "character-image"; 85 86 89 private static final String CSS_WORD_IMAGE = "word-image"; 90 91 94 private static final String CSS_TEXT_IMAGE = "text-image"; 95 96 99 private static final String TEXT_SPLIT_NONE = "none"; 100 101 104 private static final String TEXT_SPLIT_WORDS = "words"; 105 106 109 private static final String TEXT_SPLIT_CHARACTERS = "characters"; 110 111 114 private static Logger log = LoggerFactory.getLogger(BaseImageTag.class); 115 116 119 private String text; 120 121 124 private String textFontFace; 125 126 129 private int textFontSize; 130 131 134 private String textFontColor; 135 136 139 private String textBackColor; 140 141 144 private String textSplit; 145 146 150 private String divCSS; 151 152 156 public void setText(String text) { 157 this.text = text; 158 } 159 160 164 public void setImageContentNodeName(String imageContentNodeName) { 165 this.imageContentNodeName = imageContentNodeName; 166 } 167 168 172 public void setParentContentNodeName(String parentContentNodeName) { 173 this.parentContentNodeName = parentContentNodeName; 174 } 175 176 180 public void setTextFontFace(String textFontFace) { 181 this.textFontFace = textFontFace; 182 } 183 184 188 public void setTextFontSize(int textFontSize) { 189 this.textFontSize = textFontSize; 190 } 191 192 196 public void setTextFontColor(String textFontColor) { 197 this.textFontColor = textFontColor; 198 } 199 200 204 public void setTextBackColor(String textBackColor) { 205 this.textBackColor = textBackColor; 206 } 207 208 212 public void setTextSplit(String textSplit) { 213 this.textSplit = textSplit; 214 } 215 216 220 public void setDivCSS(String divCSS) { 221 this.divCSS = divCSS; 222 } 223 224 227 protected String getFilename() { 228 return "textImage"; 229 } 230 231 234 public void setUp() { 235 236 if (this.text == null) { 238 this.text = "Test Test Test"; 239 } 240 if (this.textFontFace == null) { 241 this.textFontFace = SystemUtils.IS_OS_WINDOWS ? "Arial" : "Helvetica"; 242 } 243 if (this.textFontSize == 0) { 244 this.textFontSize = 12; 245 } 246 if (this.textFontColor == null) { 247 this.textFontColor = "000000"; 248 } 249 if (this.textBackColor == null) { 250 this.textBackColor = "ffffff"; 251 } 252 if (this.textSplit == null) { 253 this.textSplit = TEXT_SPLIT_NONE; 254 } 255 else if (!((this.textSplit.equals(TEXT_SPLIT_WORDS)) || (this.textSplit.equals(TEXT_SPLIT_CHARACTERS)))) { 256 this.textSplit = TEXT_SPLIT_NONE; 257 } 258 if (this.divCSS == null) { 259 this.divCSS = "text-box"; 260 } 261 } 262 263 266 public void doTag() throws JspException { 267 268 this.setUp(); 269 270 try { 271 Content imageContentNode = getImageContentNode(); 272 273 String [] subStrings = this.getTextSubStrings(this.text); 274 String [] imageURIs = this.getImageURIs( 275 subStrings, 276 (HttpServletRequest ) ((PageContext ) this.getJspContext()).getRequest(), 277 imageContentNode); 278 this.drawTextImages(imageURIs, subStrings); 279 } 280 catch (PathNotFoundException e) { 281 log.error("PathNotFoundException occured during text-to-image conversion: " + e.getMessage(), e); 282 } 283 catch (AccessDeniedException e) { 284 log.error("AccessDeniedException occured during text-to-image conversion: " + e.getMessage(), e); 285 } 286 catch (RepositoryException e) { 287 log.error("RepositoryException occured during text-to-image conversion: " + e.getMessage(), e); 288 } 289 catch (FileNotFoundException e) { 290 log.error("FileNotFoundException occured during text-to-image conversion: " + e.getMessage(), e); 291 } 292 catch (IOException e) { 293 log.error("IOException occured during text-to-image conversion: " + e.getMessage(), e); 294 } 295 catch (FontFormatException e) { 296 log.error("FontFormatException occured during text-to-image conversion: " + e.getMessage(), e); 297 } 298 this.cleanUp(); 299 } 300 301 304 public void cleanUp() { 305 this.parentContentNodeName = null; 306 this.imageContentNodeName = null; 307 this.text = null; 308 this.textFontFace = null; 309 this.textFontSize = 0; 310 this.textFontColor = null; 311 this.textBackColor = null; 312 this.textSplit = null; 313 this.divCSS = null; 314 } 315 316 322 private void drawTextImages(String [] imageURIs, String [] subStrings) throws IOException { 323 JspWriter out = this.getJspContext().getOut(); 324 325 if (this.divCSS != null) { 326 out.print("<div class=\""); 327 out.print(this.divCSS); 328 out.print("\">"); 329 } 330 331 for (int i = 0; i < imageURIs.length; i++) { 332 out.print("<img class=\""); 333 if (this.textSplit.equals(TEXT_SPLIT_CHARACTERS)) { 334 out.print(CSS_CHARACTER_IMAGE); 335 } 336 else if (this.textSplit.equals(TEXT_SPLIT_WORDS)) { 337 out.print(CSS_WORD_IMAGE); 338 } 339 else { 340 out.print(CSS_TEXT_IMAGE); 341 } 342 out.print("\" SRC=\""); 343 out.print(imageURIs[i]); 344 out.print("\" alt=\""); 345 out.print(subStrings[i]); 346 out.print("\" />"); 347 } 348 349 if (this.divCSS != null) { 350 out.print("</div>"); 351 } 352 } 353 354 360 private String [] getTextSubStrings(String text) { 361 String [] subStrings = null; 362 if (this.textSplit.equals(TEXT_SPLIT_CHARACTERS)) { 363 subStrings = new String [text.length()]; 364 for (int i = 0; i < text.length(); i++) { 365 subStrings[i] = text.substring(i, i + 1); 366 } 367 } 368 else if (this.textSplit.equals(TEXT_SPLIT_WORDS)) { 369 StringTokenizer st = new StringTokenizer (text, " "); subStrings = new String [st.countTokens()]; 371 for (int i = 0; st.hasMoreTokens(); i++) { 372 subStrings[i] = st.nextToken().trim(); 373 } 374 } 375 else { 376 subStrings = new String []{text}; 377 } 378 return subStrings; 379 } 380 381 386 private String [] getImageURIs(String [] subStrings, HttpServletRequest req, Content imageContentNode) 387 throws PathNotFoundException, AccessDeniedException, RepositoryException, FileNotFoundException , IOException , 388 FontFormatException { 389 390 String [] imageURIs = new String [subStrings.length]; 391 for (int i = 0; i < subStrings.length; i++) { 392 String tmpImgNodeName = subStrings[i] 394 + this.textBackColor 395 + this.textFontColor 396 + this.textFontFace 397 + this.textFontSize; 398 String imageNodeName = this.convertToSimpleString(tmpImgNodeName); 399 if (!imageContentNode.hasContent(imageNodeName)) { 401 File image = createImage(subStrings[i]); 402 403 Content imageNode = imageContentNode.createContent(imageNodeName, ItemType.CONTENTNODE); 405 406 this.createImageNode(image, imageNode); 407 } 408 String contextPath = req.getContextPath(); 410 String handle = imageContentNode.getHandle(); 411 String imageURI = contextPath 412 + handle 413 + "/" 414 + imageNodeName 415 + "/" 416 + getFilename() 417 + "." 418 + PROPERTIES_EXTENSION_VALUE; 419 imageURIs[i] = imageURI; 420 } 421 return imageURIs; 422 } 423 424 429 private File createImage(String subString) throws FileNotFoundException , IOException , FontFormatException { 430 431 File imageFile = File.createTempFile(getClass().getName(), "png"); 433 imageFile.createNewFile(); 434 435 Text2PngFactory tpf = new Text2PngFactory(); 439 tpf.setFontFace(this.textFontFace); 440 tpf.setFontSize((int) (this.textFontSize * SCALE_FACTOR)); 441 int[] textRGB = this.convertHexToRGB(this.textFontColor); 442 int[] backRGB = this.convertHexToRGB(this.textBackColor); 443 tpf.setTextRGB(textRGB[0], textRGB[1], textRGB[2]); 444 tpf.setBackgroundRGB(backRGB[0], backRGB[1], backRGB[2]); 445 tpf.setText(subString); 446 447 BufferedImage bigImgBuff = (BufferedImage ) tpf.createImage(); 448 if (SCALE_FACTOR != 1) { 449 BufferedImage smallImgBuff = this.scaleImage(bigImgBuff, (1.0 / SCALE_FACTOR)); 450 ImageIO.write(smallImgBuff, "png", imageFile); 451 smallImgBuff = null; 452 } 453 else { 454 ImageIO.write(bigImgBuff, "png", imageFile); 455 } 456 bigImgBuff = null; 457 return imageFile; 458 } 459 460 466 private BufferedImage scaleImage(BufferedImage oriImgBuff, double scaleFactor) { 467 468 int oriWidth = oriImgBuff.getWidth(); 470 int oriHeight = oriImgBuff.getHeight(); 471 int newWidth = new Double (oriWidth * scaleFactor).intValue(); 473 int newHeight = new Double (oriHeight * scaleFactor).intValue(); 474 Image newImg = oriImgBuff.getScaledInstance(newWidth, newHeight, Image.SCALE_AREA_AVERAGING); 476 BufferedImage newImgBuff = new BufferedImage ( 477 newImg.getWidth(null), 478 newImg.getHeight(null), 479 BufferedImage.TYPE_INT_RGB); 480 Graphics2D g = newImgBuff.createGraphics(); 481 g.drawImage(newImg, 0, 0, null); 482 g.dispose(); 483 return newImgBuff; 485 } 486 487 } 488 | Popular Tags |