View Javadoc

1   /**
2    * This file Copyright (c) 2003-2010 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.cms.taglibs.util;
35  
36  import info.magnolia.cms.core.Content;
37  import info.magnolia.cms.core.ItemType;
38  
39  import java.awt.FontFormatException;
40  import java.awt.Graphics2D;
41  import java.awt.Image;
42  import java.awt.image.BufferedImage;
43  import java.io.File;
44  import java.io.FileNotFoundException;
45  import java.io.IOException;
46  import java.util.StringTokenizer;
47  
48  import javax.imageio.ImageIO;
49  import javax.jcr.AccessDeniedException;
50  import javax.jcr.PathNotFoundException;
51  import javax.jcr.RepositoryException;
52  import javax.servlet.http.HttpServletRequest;
53  import javax.servlet.jsp.JspException;
54  import javax.servlet.jsp.JspWriter;
55  import javax.servlet.jsp.PageContext;
56  
57  import org.apache.commons.lang.SystemUtils;
58  import org.slf4j.Logger;
59  import org.slf4j.LoggerFactory;
60  
61  
62  /**
63   * Tag that converts text into PNG images, and outputs a div element containing a set of img elements. The font face,
64   * text color, text size and background color can be set via the tag attributes. <br />
65   * <br />
66   * The images are saved under the node specified by the attribute parentContentNodeName. Under this parent node, a new
67   * node is created, with the name specified by the attribute imageContentNodeName. Under this node, each image will have
68   * it own node. The names of the image node are based on the text that they contain. (Special characters such as &, /,
69   * chinese characters, etc. are replaces by codes to ensure that these names are legal.) <br />
70   * <br />
71   * If the images for the specified text do not exist in the repository under the specified parent node, then the this
72   * tag will create the images and save them. If the images for the text already exist, then they will not be recreated.
73   * <br />
74   * <br />
75   * The text to be converted into images can be split in three ways. If the attribute textSplit is null or is set to
76   * TEXT_SPLIT_NONE, then a single image will be created of the text on one line. If textSplit is set to
77   * TEXT_SPLIT_WORDS, then the text is plit into words (i.e. wherever there is a space). Finally, if textSplit is set to
78   * TEXT_SPLIT_CHARACTERS, then a seperate image is created for each letter. <br />
79   * <br />
80   * The tag outputs a div that contains one or more img's. The CSS class applied to the div is specified by the divCSS
81   * attribute. The CSS applied to the images depends on how the text was split. For text that was not split, the CSS
82   * applied is set to CSS_TEXT_IMAGE, for words it is CSS_WORD_IMAGE, and for characters it is CSS_CHARACTER_IMAGE. Any
83   * spacing that is required between images will need to be set in your css stylesheet. <br />
84   * <br />
85   * The textFontFace attribute may either be a font name of a font installed on the server, or it may be a path to a TTF
86   * file. The class to generate PNG images from TrueType font strings is originally by Philip McCarthy -
87   * http://chimpen.com (http://chimpen.com/things/archives/001139.php). I have made a couple of small changes. <br />
88   * <br />
89   *
90   * @jsp.tag name="txt2img" body-content="empty"
91   *
92   * @author Patrick Janssen
93   * @author Fabrizio Giustina
94   * @version 1.0
95   */
96  public class TextToImageTag extends BaseImageTag {
97  
98      /**
99       * The image that is created can first be created at a larger size, and then scaled down. This overcomes kerning
100      * problems on the Windows platform, which results in very irregular spacing between characters. If you are not
101      * using Windows, this can be set to 1.
102      */
103     private static final double SCALE_FACTOR = SystemUtils.IS_OS_WINDOWS ? 4 : 1;
104 
105     /**
106      * The CSS class applied to images of individual characters.
107      */
108     private static final String CSS_CHARACTER_IMAGE = "character-image";
109 
110     /**
111      * The CSS class appled to images of words.
112      */
113     private static final String CSS_WORD_IMAGE = "word-image";
114 
115     /**
116      * The CSS class applied to images of whole sentances, or other text.
117      */
118     private static final String CSS_TEXT_IMAGE = "text-image";
119 
120     /**
121      * The text will not be split
122      */
123     private static final String TEXT_SPLIT_NONE = "none";
124 
125     /**
126      * The text will be split into words
127      */
128     private static final String TEXT_SPLIT_WORDS = "words";
129 
130     /**
131      * The text will be split into characters
132      */
133     private static final String TEXT_SPLIT_CHARACTERS = "characters";
134 
135     private static final Logger log = LoggerFactory.getLogger(BaseImageTag.class);
136 
137     private String text;
138     private String textFontFace;
139     private int textFontSize;
140     private String textFontColor;
141     private String textBackColor;
142     private String textSplit;
143     private String divCSS;
144 
145     /**
146      * The text to be converted.
147      * @jsp.attribute required="true" rtexprvalue="true"
148      */
149     public void setText(String text) {
150         this.text = text;
151     }
152 
153     /**
154      * The name of the new contentNode to create, under which all image nodes will be saved.
155      * @jsp.attribute required="true" rtexprvalue="true"
156      */
157     public void setImageContentNodeName(String imageContentNodeName) {
158         this.imageContentNodeName = imageContentNodeName;
159     }
160 
161     /**
162      * The name of the parent of the new contentNode.
163      * @jsp.attribute required="false" rtexprvalue="true"
164      */
165     public void setParentContentNodeName(String parentContentNodeName) {
166         this.parentContentNodeName = parentContentNodeName;
167     }
168 
169     /**
170      * The font face of the text, e.g. 'Helvetica'. Default is 'Helvetica'.
171      * @jsp.attribute required="false" rtexprvalue="true"
172      */
173     public void setTextFontFace(String textFontFace) {
174         this.textFontFace = textFontFace;
175     }
176 
177     /**
178      * The size of the text, in points, e.g. 12. Default is '12'.
179      * @jsp.attribute required="false" rtexprvalue="true" type="int"
180      */
181     public void setTextFontSize(int textFontSize) {
182         this.textFontSize = textFontSize;
183     }
184 
185     /**
186      * The color of the text in hexadecimal format, e.g. 'ff0044'. Default is '000000' (black).
187      * @jsp.attribute required="false" rtexprvalue="true"
188      */
189     public void setTextFontColor(String textFontColor) {
190         this.textFontColor = textFontColor;
191     }
192 
193     /**
194      * The color of the background in hexadecimal format, e.g. 'ff0044'. Default is 'ffffff' (white).
195      * @jsp.attribute required="false" rtexprvalue="true"
196      */
197     public void setTextBackColor(String textBackColor) {
198         this.textBackColor = textBackColor;
199     }
200 
201     /**
202      * The method used to split the text into sub-strings: 'none', 'words', or 'characters'. Default is 'none'.
203      * @jsp.attribute required="false" rtexprvalue="true"
204      */
205     public void setTextSplit(String textSplit) {
206         this.textSplit = textSplit;
207     }
208 
209     /**
210      * The CSS class that will be applied to the div that contains these text images.
211      * Defaults to "text-box".
212      * @jsp.attribute required="false" rtexprvalue="true"
213      */
214     public void setDivCSS(String divCSS) {
215         this.divCSS = divCSS;
216     }
217 
218     /**
219      * @see info.magnolia.cms.taglibs.util.BaseImageTag#getFilename()
220      */
221     protected String getFilename() {
222         return "textImage";
223     }
224 
225     /**
226      * Initialize settings
227      */
228     public void setUp() {
229 
230         // check that all the necessary attributes are set
231         if (this.text == null) {
232             this.text = "Test Test Test";
233         }
234         if (this.textFontFace == null) {
235             this.textFontFace = SystemUtils.IS_OS_WINDOWS ? "Arial" : "Helvetica";
236         }
237         if (this.textFontSize == 0) {
238             this.textFontSize = 12;
239         }
240         if (this.textFontColor == null) {
241             this.textFontColor = "000000";
242         }
243         if (this.textBackColor == null) {
244             this.textBackColor = "ffffff";
245         }
246         if (this.textSplit == null) {
247             this.textSplit = TEXT_SPLIT_NONE;
248         }
249         else if (!((this.textSplit.equals(TEXT_SPLIT_WORDS)) || (this.textSplit.equals(TEXT_SPLIT_CHARACTERS)))) {
250             this.textSplit = TEXT_SPLIT_NONE;
251         }
252         if (this.divCSS == null) {
253             this.divCSS = "text-box";
254         }
255     }
256 
257     /**
258      * Do this tag
259      */
260     public void doTag() throws JspException {
261 
262         this.setUp();
263 
264         try {
265             Content imageContentNode = getImageContentNode();
266 
267             String[] subStrings = this.getTextSubStrings(this.text);
268             String[] imageURIs = this.getImageURIs(
269                 subStrings,
270                 (HttpServletRequest) ((PageContext) this.getJspContext()).getRequest(),
271                 imageContentNode);
272             this.drawTextImages(imageURIs, subStrings);
273         }
274         catch (PathNotFoundException e) {
275             log.error("PathNotFoundException occured during text-to-image conversion: " + e.getMessage(), e);
276         }
277         catch (AccessDeniedException e) {
278             log.error("AccessDeniedException occured during text-to-image conversion: " + e.getMessage(), e);
279         }
280         catch (RepositoryException e) {
281             log.error("RepositoryException occured during text-to-image conversion: " + e.getMessage(), e);
282         }
283         catch (FileNotFoundException e) {
284             log.error("FileNotFoundException occured during text-to-image conversion: " + e.getMessage(), e);
285         }
286         catch (IOException e) {
287             log.error("IOException occured during text-to-image conversion: " + e.getMessage(), e);
288         }
289         catch (FontFormatException e) {
290             log.error("FontFormatException occured during text-to-image conversion: " + e.getMessage(), e);
291         }
292         this.cleanUp();
293     }
294 
295     /**
296      * Set objects to null
297      */
298     public void cleanUp() {
299         this.parentContentNodeName = null;
300         this.imageContentNodeName = null;
301         this.text = null;
302         this.textFontFace = null;
303         this.textFontSize = 0;
304         this.textFontColor = null;
305         this.textBackColor = null;
306         this.textSplit = null;
307         this.divCSS = null;
308     }
309 
310     /**
311      * Draws a div box that contains the text images.
312      * @param imageURLs an array of urls
313      * @param subStrings an array of strings
314      * @throws IOException jspwriter exception
315      */
316     private void drawTextImages(String[] imageURIs, String[] subStrings) throws IOException {
317         JspWriter out = this.getJspContext().getOut();
318 
319         if (this.divCSS != null) {
320             out.print("<div class=\"");
321             out.print(this.divCSS);
322             out.print("\">");
323         }
324 
325         for (int i = 0; i < imageURIs.length; i++) {
326             out.print("<img class=\"");
327             if (this.textSplit.equals(TEXT_SPLIT_CHARACTERS)) {
328                 out.print(CSS_CHARACTER_IMAGE);
329             }
330             else if (this.textSplit.equals(TEXT_SPLIT_WORDS)) {
331                 out.print(CSS_WORD_IMAGE);
332             }
333             else {
334                 out.print(CSS_TEXT_IMAGE);
335             }
336             out.print("\" src=\"");
337             out.print(imageURIs[i]);
338             out.print("\" alt=\"");
339             out.print(subStrings[i]);
340             out.print("\" />");
341         }
342 
343         if (this.divCSS != null) {
344             out.print("</div>");
345         }
346     }
347 
348     /**
349      * Splits a string into words or characters, depending on the textSplit attribute. For words, spaces at either end
350      * are removed.
351      * @param The string to split
352      * @return An array of words
353      */
354     private String[] getTextSubStrings(String text) {
355         String[] subStrings = null;
356         if (this.textSplit.equals(TEXT_SPLIT_CHARACTERS)) {
357             subStrings = new String[text.length()];
358             for (int i = 0; i < text.length(); i++) {
359                 subStrings[i] = text.substring(i, i + 1);
360             }
361         }
362         else if (this.textSplit.equals(TEXT_SPLIT_WORDS)) {
363             StringTokenizer st = new StringTokenizer(text, " "); // Split sentence into words
364             subStrings = new String[st.countTokens()];
365             for (int i = 0; st.hasMoreTokens(); i++) {
366                 subStrings[i] = st.nextToken().trim();
367             }
368         }
369         else {
370             subStrings = new String[]{text};
371         }
372         return subStrings;
373     }
374 
375     /**
376      * Get an array of image URIs, one URI for each text string.
377      * @param The array of text strings.
378      * @return An array of URIs pointing to the images.
379      */
380     private String[] getImageURIs(String[] subStrings, HttpServletRequest req, Content imageContentNode)
381         throws PathNotFoundException, AccessDeniedException, RepositoryException, FileNotFoundException, IOException,
382         FontFormatException {
383 
384         String[] imageURIs = new String[subStrings.length];
385         for (int i = 0; i < subStrings.length; i++) {
386             // Create a unique image node name
387             String tmpImgNodeName = subStrings[i]
388                 + this.textBackColor
389                 + this.textFontColor
390                 + this.textFontFace
391                 + this.textFontSize;
392             String imageNodeName = this.convertToSimpleString(tmpImgNodeName);
393             // If the image node with this name does not exist, then create it.
394             if (!imageContentNode.hasContent(imageNodeName)) {
395                 File image = createImage(subStrings[i]);
396 
397                 // Create the node that will contain the image
398                 Content imageNode = imageContentNode.createContent(imageNodeName, ItemType.CONTENTNODE);
399 
400                 this.createImageNode(image, imageNode);
401             }
402             // Save the URI for this image in the array
403             String contextPath = req.getContextPath();
404             String handle = imageContentNode.getHandle();
405             String imageURI = contextPath
406                 + handle
407                 + "/"
408                 + imageNodeName
409                 + "/"
410                 + getFilename()
411                 + "."
412                 + PROPERTIES_EXTENSION_VALUE;
413             imageURIs[i] = imageURI;
414         }
415         return imageURIs;
416     }
417 
418     /**
419      * Creates an image from a word. The file is saved in the location specified by TEMP_IMAGE_PATH.
420      * @param subString The text.
421      * @return An input stream.
422      */
423     private File createImage(String subString) throws FileNotFoundException, IOException, FontFormatException {
424 
425         // Create file
426         File imageFile = File.createTempFile(getClass().getName(), "png");
427         imageFile.createNewFile();
428 
429         // create the image
430         // due to kerning problems, the image is being created 4 times to big
431         // then being scaled down to the right size
432         Text2PngFactory tpf = new Text2PngFactory();
433         tpf.setFontFace(this.textFontFace);
434         tpf.setFontSize((int) (this.textFontSize * SCALE_FACTOR));
435         int[] textRGB = this.convertHexToRGB(this.textFontColor);
436         int[] backRGB = this.convertHexToRGB(this.textBackColor);
437         tpf.setTextRGB(textRGB[0], textRGB[1], textRGB[2]);
438         tpf.setBackgroundRGB(backRGB[0], backRGB[1], backRGB[2]);
439         tpf.setText(subString);
440 
441         BufferedImage bigImgBuff = (BufferedImage) tpf.createImage();
442         if (SCALE_FACTOR != 1) {
443             BufferedImage smallImgBuff = this.scaleImage(bigImgBuff, (1.0 / SCALE_FACTOR));
444             ImageIO.write(smallImgBuff, "png", imageFile);
445             smallImgBuff = null;
446         }
447         else {
448             ImageIO.write(bigImgBuff, "png", imageFile);
449         }
450         bigImgBuff = null;
451         return imageFile;
452     }
453 
454     /**
455      * Create an image file that is a scaled version of the original image
456      * @param the original BufferedImage
457      * @param the scale factor
458      * @return the new BufferedImage
459      */
460     private BufferedImage scaleImage(BufferedImage oriImgBuff, double scaleFactor) {
461 
462         // get the dimesnions of the original image
463         int oriWidth = oriImgBuff.getWidth();
464         int oriHeight = oriImgBuff.getHeight();
465         // get the width and height of the new image
466         int newWidth = new Double(oriWidth * scaleFactor).intValue();
467         int newHeight = new Double(oriHeight * scaleFactor).intValue();
468         // create the thumbnail as a buffered image
469         Image newImg = oriImgBuff.getScaledInstance(newWidth, newHeight, Image.SCALE_AREA_AVERAGING);
470         BufferedImage newImgBuff = new BufferedImage(
471             newImg.getWidth(null),
472             newImg.getHeight(null),
473             BufferedImage.TYPE_INT_RGB);
474         Graphics2D g = newImgBuff.createGraphics();
475         g.drawImage(newImg, 0, 0, null);
476         g.dispose();
477         // return the newImgBuff
478         return newImgBuff;
479     }
480 
481 }