1
2
3
4
5
6
7 package com.octo.captcha.component.image.wordtoimage;
8
9 import com.octo.captcha.CaptchaException;
10
11 import java.awt.*;
12 import java.awt.font.TextAttribute;
13 import java.awt.image.BufferedImage;
14 import java.text.AttributedString;
15
16 /***
17 * <p/>
18 * Implementation skeleton for the WordToImage component </p> Basically this class implements the imageFromWord method
19 * proceding as folow : <ul> <li>Checks the word length</li> <li>Creates an java.text.AttributedString from the
20 * word</li> <li>Apply font to the AttributedString using the abstract method getFont</li> <li>Create an image for the
21 * background using the abstact method getBackround</li> <li>Put the text on the backround using the abstact method
22 * pasteText</li> <li>Return the newly created image</li> </ul> <p/>This class implements the Template method pattern
23 * from the GOF design patterns.
24 *
25 * @author <a href="mailto:mag@jcaptcha.net">Marc-Antoine Garrigue </a>
26 * @version 1.0
27 */
28 public abstract class AbstractWordToImage implements WordToImage {
29
30 private boolean manageFontByCharacter = true;
31
32 protected AbstractWordToImage() {
33 }
34
35 protected AbstractWordToImage(boolean manageFontByCharacter) {
36 this.manageFontByCharacter = manageFontByCharacter;
37 }
38
39 /***
40 * Creates an image of the provided String This method is a skeleton for creation algorithm. it proceeds as folows :
41 * <ul> <li>Checks the word length</li> <li>Creates an java.text.AttributedString from the word</li> <li>Apply font
42 * to the AttributedString using the abstract method getFont</li> <li>Create an image for the background using the
43 * abstact method getBackround</li> <li>Put the text on the backround using the abstact method pasteText</li>
44 * <li>Return the newly created image</li> </ul>
45 *
46 * @return an image representation of the word
47 *
48 * @throws com.octo.captcha.CaptchaException
49 * if word is invalid or if image generation fails.
50 */
51 public BufferedImage getImage(String word) throws CaptchaException {
52 int wordLength;
53
54 wordLength = checkWordLength(word);
55
56 AttributedString attributedWord = getAttributedString(word, wordLength);
57
58
59 BufferedImage background = getBackground();
60
61 return pasteText(background, attributedWord);
62 }
63
64 AttributedString getAttributedString(String word, int wordLength) {
65 AttributedString attributedWord = new AttributedString(word);
66
67 Font font = getFont();
68 for (int i = 0; i < wordLength; i++) {
69
70 attributedWord.addAttribute(TextAttribute.FONT, font, i, i + 1);
71
72 if(manageFontByCharacter)font=getFont();
73 }
74 return attributedWord;
75 }
76
77 int checkWordLength(String word) throws CaptchaException {
78 int wordLength;
79 if (word == null) {
80 throw new CaptchaException("null word");
81 } else {
82 wordLength = word.length();
83 if (wordLength > this.getMaxAcceptedWordLength()
84 || wordLength < getMinAcceptedWordLength()) {
85 throw new CaptchaException("invalid length word");
86 }
87 }
88 return wordLength;
89 }
90
91 /***
92 * Method from imageFromWord method to apply font to String. Implementations must take into account the minFontSize
93 * and the MaxFontSize.
94 *
95 * @return a Font
96 */
97 abstract Font getFont();
98
99 /***
100 * Generates a backround image on wich text will be paste. Implementations must take into account the imageHeigt and
101 * imageWidth.
102 *
103 * @return the background image
104 */
105 abstract BufferedImage getBackground();
106
107 /***
108 * Pastes the attributed string on the backround image and return the final image. Implementation must take into
109 * account the fact that the text must be readable by human and non by programs
110 *
111 * @return the final image
112 *
113 * @throws CaptchaException if any exception accurs during paste routine.
114 */
115 abstract BufferedImage pasteText(final BufferedImage background,
116 final AttributedString attributedWord) throws CaptchaException;
117
118 }