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   /*
8    * jcaptcha, the open source java framework for captcha definition and integration
9    * copyright (c)  2007 jcaptcha.net. All Rights Reserved.
10   * See the LICENSE.txt file distributed with this package.
11   */
12  
13  package com.octo.captcha.component.image.textpaster;
14  
15  import com.octo.captcha.CaptchaException;
16  import com.octo.captcha.component.image.color.ColorGenerator;
17  
18  import java.awt.*;
19  import java.awt.image.BufferedImage;
20  import java.text.AttributedString;
21  
22  /***
23   * Pastes characters in differents height lines in the background
24   *
25   * @date 19 mars 2007
26   * @author <a href="mailto:antoine.veret@gmail.com">Antoine Véret</a>
27   * @deprecated  
28   */
29  public class NonLinearTextPaster extends AbstractTextPaster {
30  
31      public NonLinearTextPaster(Integer minAcceptedWordLength, Integer maxAcceptedWordLength,
32                              Color textColor) {
33          super(minAcceptedWordLength, maxAcceptedWordLength, textColor);
34      }
35  
36      public NonLinearTextPaster(Integer minAcceptedWordLength, Integer maxAcceptedWordLength,
37                              ColorGenerator colorGenerator) {
38          super(minAcceptedWordLength, maxAcceptedWordLength, colorGenerator);
39      }
40  
41      public NonLinearTextPaster(Integer minAcceptedWordLength, Integer maxAcceptedWordLength,
42                              ColorGenerator colorGenerator, Boolean manageColorPerGlyph) {
43          super(minAcceptedWordLength, maxAcceptedWordLength, colorGenerator, manageColorPerGlyph);
44      }
45  
46      /***
47       * Pastes the attributed string on the backround image and return the final image. Implementation must take into
48       * account the fact that the text must be readable by human and non by programs.
49       *
50       * @return the final image
51       *
52       * @throws com.octo.captcha.CaptchaException
53       *          if any exception accurs during paste routine.
54       */
55      public BufferedImage pasteText(final BufferedImage background,
56                                     final AttributedString attributedWord) throws CaptchaException {
57  
58          BufferedImage out = copyBackground(background);
59          Graphics2D g2 = pasteBackgroundAndSetTextColor(out, background);
60  
61          // convert string into a series of glyphs we can work with
62          MutableAttributedString newAttrString = new MutableAttributedString(g2,
63                  attributedWord, 2);
64  
65          // space out the glyphs with a little kerning
66          newAttrString.useMinimumSpacing(6);
67  
68          // shift string to a non-linear layout in the output image
69          newAttrString.shiftBoundariesToNonLinearLayout(background.getWidth(), background.getHeight());
70          
71          // now draw each glyph at the appropriate spot on the image.
72          if (isManageColorPerGlyph())
73              newAttrString.drawString(g2, getColorGenerator());
74          else
75              newAttrString.drawString(g2);
76  
77          g2.dispose();
78          return out;
79      }
80  }