View Javadoc

1   package com.octo.captcha.component.image.textpaster;
2   
3   import com.octo.captcha.CaptchaException;
4   import com.octo.captcha.component.image.color.ColorGenerator;
5   import com.octo.captcha.component.image.textpaster.glyphsvisitor.GlyphsVisitors;
6   import com.octo.captcha.component.image.textpaster.glyphsdecorator.GlyphsDecorator;
7   
8   import java.awt.image.BufferedImage;
9   import java.awt.*;
10  import java.awt.geom.Rectangle2D;
11  import java.awt.font.FontRenderContext;
12  import java.awt.font.TextAttribute;
13  import java.text.AttributedString;
14  import java.text.AttributedCharacterIterator;
15  
16  /***
17   * Use Glyphs to draw, much more powerfull and efficient than using AttributedString
18   *
19   * @author mag
20   * @Date 6 mars 2008
21   */
22  public class GlyphsPaster extends AbstractTextPaster{
23  
24      private GlyphsVisitors[] glyphVisitors;
25      private GlyphsDecorator[] glyphsDecorators;
26  
27  
28      public GlyphsPaster(Integer minAcceptedWordLength, Integer maxAcceptedWordLength) {
29          super(minAcceptedWordLength, maxAcceptedWordLength);
30      }
31  
32      public GlyphsPaster(Integer minAcceptedWordLength, Integer maxAcceptedWordLength, Color textColor) {
33          super(minAcceptedWordLength, maxAcceptedWordLength, textColor);
34      }
35  
36      public GlyphsPaster(Integer minAcceptedWordLength, Integer maxAcceptedWordLength, ColorGenerator colorGenerator) {
37          super(minAcceptedWordLength, maxAcceptedWordLength, colorGenerator);
38      }
39  
40      public GlyphsPaster(Integer minAcceptedWordLength, Integer maxAcceptedWordLength, ColorGenerator colorGenerator, Boolean manageColorPerGlyph) {
41          super(minAcceptedWordLength, maxAcceptedWordLength, colorGenerator, manageColorPerGlyph);
42      }
43  
44      public GlyphsPaster(Integer minAcceptedWordLength, Integer maxAcceptedWordLength, GlyphsVisitors[] glyphVisitors) {
45          super(minAcceptedWordLength, maxAcceptedWordLength);
46          this.glyphVisitors = glyphVisitors;
47      }
48  
49      public GlyphsPaster(Integer minAcceptedWordLength, Integer maxAcceptedWordLength, Color textColor, GlyphsVisitors[] glyphVisitors) {
50          super(minAcceptedWordLength, maxAcceptedWordLength, textColor);
51          this.glyphVisitors = glyphVisitors;
52      }
53  
54      public GlyphsPaster(Integer minAcceptedWordLength, Integer maxAcceptedWordLength, ColorGenerator colorGenerator, GlyphsVisitors[] glyphVisitors) {
55          super(minAcceptedWordLength, maxAcceptedWordLength, colorGenerator);
56          this.glyphVisitors = glyphVisitors;
57      }
58  
59      public GlyphsPaster(Integer minAcceptedWordLength, Integer maxAcceptedWordLength, ColorGenerator colorGenerator, GlyphsVisitors[] glyphVisitors, GlyphsDecorator[] glyphsDecorators) {
60          super(minAcceptedWordLength, maxAcceptedWordLength, colorGenerator);
61          this.glyphVisitors = glyphVisitors;
62          this.glyphsDecorators = glyphsDecorators;
63      }
64  
65      public GlyphsPaster(Integer minAcceptedWordLength, Integer maxAcceptedWordLength, ColorGenerator colorGenerator, Boolean manageColorPerGlyph, GlyphsVisitors[] glyphVisitors) {
66          super(minAcceptedWordLength, maxAcceptedWordLength, colorGenerator, manageColorPerGlyph);
67          this.glyphVisitors = glyphVisitors;
68      }
69  
70  
71      public GlyphsPaster(Integer minAcceptedWordLength, Integer maxAcceptedWordLength, ColorGenerator colorGenerator, Boolean manageColorPerGlyph, GlyphsVisitors[] glyphVisitors, GlyphsDecorator[] glyphsDecorators) {
72          super(minAcceptedWordLength, maxAcceptedWordLength, colorGenerator, manageColorPerGlyph);
73          this.glyphVisitors = glyphVisitors;
74          this.glyphsDecorators = glyphsDecorators;
75      }
76  
77      public BufferedImage pasteText(BufferedImage background, AttributedString attributedWord) throws CaptchaException {
78          BufferedImage out = copyBackground(background);
79          Graphics2D g2 = pasteBackgroundAndSetTextColor(out, background);
80          customizeGraphicsRenderingHints(g2);
81          
82          //build glyphs
83          AttributedCharacterIterator acit = attributedWord.getIterator();
84          Glyphs glyphs = new Glyphs();
85  
86          for(int i = 0 ; i < acit.getEndIndex(); i++){
87              Font font = (Font) acit.getAttribute(TextAttribute.FONT);
88              g2.setFont(font);
89              final FontRenderContext frc = g2.getFontRenderContext();
90              glyphs.addGlyphVector(font.createGlyphVector(frc,new char[]{acit.current()}));
91              acit.next();
92          }
93  
94          Rectangle2D backgroundBounds = new Rectangle2D.Float(0,0,background.getWidth(),background.getHeight());
95  
96          //First decorate
97          if (glyphVisitors != null) {
98              //System.out.println("\n"+glyphs);
99              for (int i = 0; i < glyphVisitors.length; i++) {
100                 //System.out.println(glyphVisitors[i]);
101                 glyphVisitors[i].visit(glyphs,backgroundBounds);
102                 //System.out.println(glyphs);
103             }
104 
105 
106 
107            /* if(!backgroundBounds.contains(glyphs.getBounds())){
108 
109                  StringBuffer error =new StringBuffer("Invalid visitor configuration : " +
110                          "\nTrying to move glyphs out of background\n Visitors:");
111                 for (int i = 0; i < glyphVisitors.length; i++) {
112                     error.append("\n");
113                     error.append(glyphVisitors[i]);
114                 }
115                  error.append("\nResulting string bounds : ");
116                  error.append(glyphs.getBounds());
117                  throw new CaptchaException(error.toString());
118               }  */
119         }
120 
121         for(int i = 0 ; i < glyphs.size(); i++){
122             g2.drawGlyphVector(glyphs.get(i),0,0);
123              if(isManageColorPerGlyph())g2.setColor(getColorGenerator().getNextColor());
124         }
125 
126         //and now decorate
127         if (glyphsDecorators != null) {
128             for (int i = 0; i < glyphsDecorators.length; i++) {
129                 glyphsDecorators[i].decorate(g2, glyphs, background);
130 
131             }
132         }
133 
134         g2.dispose();
135         return out;
136     }
137 
138 
139     
140 
141 }