View Javadoc

1   /*
2    * JCaptcha, the open source java framework for captcha definition and integration
3    * Copyright (c)  2007 jcaptcha.net. All Rights Reserved.
4    * See the LICENSE.txt file distributed with this package.
5    */
6   
7   package com.octo.captcha.component.image.wordtoimage;
8   
9   import com.octo.captcha.CaptchaException;
10  import com.octo.captcha.component.image.backgroundgenerator.BackgroundGenerator;
11  import com.octo.captcha.component.image.fontgenerator.FontGenerator;
12  import com.octo.captcha.component.image.textpaster.TextPaster;
13  
14  import java.awt.*;
15  import java.awt.image.BufferedImage;
16  import java.text.AttributedString;
17  
18  /***
19   * <p>Base class for composed WordToImage</p> It extends the AbstractWord to image and uses three others Components :
20   * <ul> <li>a FontGenerator to implement the getFont() method</li> <li>a BackgroundGenerator to implement the
21   * getBackround() method</li> <li>a TextParser to implement the pasteText() method</li> </ul>
22   * <p/>
23   *
24   * @author <a href="mailto:mag@jcaptcha.net">Marc-Antoine Garrigue</a>
25   * @version 1.0
26   */
27  public class ComposedWordToImage extends AbstractWordToImage {
28  
29      private FontGenerator fontGenerator;
30      private BackgroundGenerator background;
31      private TextPaster textPaster;
32  
33      /***
34       * @param fontGenerator a AbstractFontGenerator to implement the getFont() method
35       * @param background    a AbstractBackgroundGenerator to implement the getBackround() method
36       * @param textPaster    a AbstractTextParser to implement the pasteText() method
37       */
38      public ComposedWordToImage(FontGenerator fontGenerator,
39                                 BackgroundGenerator background,
40                                 TextPaster textPaster) {
41          this.background = background;
42          this.fontGenerator = fontGenerator;
43          this.textPaster = textPaster;
44      }
45  
46      public ComposedWordToImage(boolean manageFontByCharacter, FontGenerator fontGenerator, BackgroundGenerator background, TextPaster textPaster) {
47          super(manageFontByCharacter);
48          this.fontGenerator = fontGenerator;
49          this.background = background;
50          this.textPaster = textPaster;
51      }
52  
53      /***
54       * @deprecated
55       * @return the max word length accepted by this word2image service
56       */
57      public int getMaxAcceptedWordLenght() {
58          return textPaster.getMaxAcceptedWordLength();
59      }
60  
61      /***
62       * @deprecated
63       * @return the min word length accepted by this word2image service
64       */
65      public int getMinAcceptedWordLenght() {
66          return textPaster.getMinAcceptedWordLength();
67      }
68  
69  
70  
71      /***
72       * @return the max word length accepted by this word2image service
73       */
74      public int getMaxAcceptedWordLength() {
75          return textPaster.getMaxAcceptedWordLength();
76      }
77  
78      /***
79       * @return the min word length accepted by this word2image service
80       */
81      public int getMinAcceptedWordLength() {
82          return textPaster.getMinAcceptedWordLength();
83      }
84  
85      /***
86       * @return the generated image height
87       */
88      public int getImageHeight() {
89          return background.getImageHeight();
90      }
91  
92      /***
93       * @return teh generated image width
94       */
95      public int getImageWidth() {
96          return background.getImageWidth();
97      }
98  
99      /***
100      * @return the min font size for the generated image
101      */
102     public int getMinFontSize() {
103         return fontGenerator.getMinFontSize();
104     }
105 
106     /***
107      * Method from imageFromWord method to apply font to String. Implementations must take into account the minFontSize
108      * and the MaxFontSize.
109      *
110      * @return a Font
111      */
112     Font getFont() {
113         return fontGenerator.getFont();
114     }
115 
116     /***
117      * Generates a backround image on wich text will be paste. Implementations must take into account the imageHeigt and
118      * imageWidth.
119      *
120      * @return the background image
121      */
122     BufferedImage getBackground() {
123         return background.getBackground();
124     }
125 
126     /***
127      * Pastes the attributed string on the backround image and return the final image. Implementation must take into
128      * account the fact that the text must be readable by human and non by programs
129      *
130      * @return the final image
131      *
132      * @throws CaptchaException if any exception accurs during paste routine.
133      */
134     BufferedImage pasteText(BufferedImage background,
135                             AttributedString attributedWord)
136             throws CaptchaException {
137         return textPaster.pasteText(background, attributedWord);
138     }
139 }