View Javadoc

1   package com.octo.captcha.component.image.textpaster;
2   
3   import java.awt.font.GlyphVector;
4   import java.awt.font.GlyphMetrics;
5   import java.awt.geom.Point2D;
6   import java.awt.geom.Rectangle2D;
7   import java.awt.geom.AffineTransform;
8   import java.awt.*;
9   import java.util.*;
10  import java.util.List;
11  
12  /***
13   * Container class for a list of GlyphVector
14   * @author mag
15   * @Date 6 mars 2008
16   */
17  public class Glyphs {
18  
19    // List<GlyphAbsolutePositionAndRenderContext> vectors = new ArrayList<GlyphAbsolutePositionAndRenderContext>();
20  
21      List<GlyphVector> vectors = new ArrayList<GlyphVector>();
22  
23      public Glyphs() {
24      }
25  
26  
27  
28      public void addGlyphVector(GlyphVector glyph){
29          this.vectors.add(glyph);
30  
31                  //new GlyphAbsolutePositionAndRenderContext(glyph,frc,new Point2D.Float(0,0)));
32      }
33  
34     public int size(){
35          return vectors.size();
36     }
37  
38     public GlyphVector get(int index){
39         return this.vectors.get(index);
40     }
41  
42     public double getBoundsX(int index){
43         return getBounds(index).getX();
44     }
45  
46     public double getBoundsY(int index){
47         return getBounds(index).getY();
48     }
49  
50      public double getBoundsWidth(int index){
51         return getBounds(index).getWidth();
52     }
53  
54     public double getBoundsHeight(int index){
55         return getBounds(index).getHeight();
56     }
57  
58     public double getX(int index){
59         return get(index).getGlyphPosition(0).getX();
60     }
61  
62      public double getY(int index){
63         return get(index).getGlyphPosition(0).getY();
64     }
65  
66  
67  
68  
69  
70  
71     public Shape getOutline(int index){
72         return get(index).getOutline();
73     }
74   
75  
76  
77      public double getBoundsX(){
78         return getBounds().getX();
79     }
80  
81     public double getBoundsY(){
82         return getBounds().getY();
83     }
84  
85      public double getBoundsWidth(){
86         return getBounds().getWidth();
87     }
88  
89     public double getBoundsHeight(){
90         return getBounds().getHeight();
91     }
92  
93  
94  
95      public double getMaxX(int index){
96          return getBounds(index).getMaxX();
97      }
98  
99       public double getMaxY(int index){
100         return getBounds(index).getMaxY();
101     }
102 
103 
104     public double getMinX(int index){
105          return getBounds(index).getMinX();
106     }
107 
108      public double getMinY(int index){
109          return getBounds(index).getMinX();
110     }
111 
112 
113     public GlyphVector getGlyphVector(int index){
114         return this.vectors.get(index);
115    }
116 
117 
118    public Rectangle2D getBounds(int index){
119        return this.vectors.get(index).getVisualBounds();
120    }
121 
122    public Rectangle2D getBounds(){
123        Rectangle2D bounds=size()>0?getBounds(0):new Rectangle2D.Double(0,0,0,0);
124        for(int i=1;i<size();i++){
125              bounds=bounds.createUnion(getBounds(i));
126        }
127       return bounds;
128    }
129 
130    public GlyphMetrics getMetrics(int index){
131       return get(index).getGlyphMetrics(0);
132    }
133 
134     public double getLSB(int index){
135       return getMetrics(index).getLSB();
136    }
137 
138     public double getRSB(int index){
139       return getMetrics(index).getRSB();
140    }
141 
142     public double getAdvance(int index){
143       return getMetrics(index).getAdvance();
144    } 
145 
146    public double getInternalWidth(int index){
147       return getAdvance(index)-getRSB(index)-getLSB(index);
148    }
149 
150    public Rectangle2D getInternalBounds(int index){
151       return getMetrics(index).getBounds2D();
152    }
153 
154    public double getInternalBoundsX(int index){
155       return getInternalBounds(index).getX();
156    }
157 
158 
159 
160    public double getInternalBoundsY(int index){
161       return getInternalBounds(index).getY();
162    }
163 
164    public double getInternalBoundsWidth(int index){
165       return getInternalBounds(index).getWidth();
166    }
167     
168    public double getInternalBoundsHeigth(int index){
169       return getInternalBounds(index).getHeight();
170    }
171 
172    public double getAdvanceX(int index){
173       return getMetrics(index).getAdvanceX();
174    }
175 
176    public double getAdvanceY(int index){
177       return getMetrics(index).getAdvanceY();
178    }
179 
180 
181    public double getMaxHeight(){
182        double max = 0;
183        for(int i=1;i<size();i++){
184              max=Math.max(getBoundsHeight(i),max);
185        }
186        return max;
187    }
188 
189     public double getMaxWidth(){
190        double max = 0;
191        for(int i=1;i<size();i++){
192              max=Math.max(getBoundsWidth(i),max);
193        }
194        return max;
195    }
196 
197 
198    public void translate(double x, double y){
199      for(int i=0;i<size();i++){
200          translate(i,x,y);
201        }
202    }
203 
204 
205    public void translate(int index,double x, double y ){
206        setPosition(index,x+getX(index),y+getY(index));
207    }
208 
209    public void setPosition(int index,double x, double y ){
210        vectors.get(index).setGlyphPosition(0,new Point2D.Double(x,y));
211    }
212 
213 
214    public void addAffineTransform(AffineTransform at){
215      for(int i=0;i<size();i++){
216          addAffineTransform(i,at);
217        }
218    }
219 
220    public void addAffineTransform(int index,AffineTransform at){
221        AffineTransform t =vectors.get(index).getGlyphTransform(0);
222        if(t==null){t=at;}else{t.concatenate(at);}
223        vectors.get(index).setGlyphTransform(0,t);
224    }
225 
226    public void rotate(int index, double angle){
227        get(index).setGlyphTransform(0, AffineTransform.getRotateInstance(angle,getBoundsX(index)+getBoundsWidth(index)/2,getBoundsY(index)+getBoundsHeight(index)/2));
228    }
229 
230     public String toString() {
231         final String R = "\n";
232         final String RS = "\n\t";
233         final String RSS = "\n\t\t";
234 
235         StringBuffer buf = new StringBuffer();
236         buf.append("{Glyphs=");
237         for (int i = 0; i < size(); i++) {
238             buf.append(RS);
239             buf.append("{GlyphVector="+i+" : ");
240 
241            for (int j = 0; j < this.get(i).getNumGlyphs(); j++) {
242 
243                 buf.append("Glyph="+j);
244 
245                 buf.append("; Bounds=");
246                 buf.append(this.get(i).getGlyphVisualBounds(j).getBounds2D());
247                  buf.append("; Font=");
248                 buf.append(this.get(i).getFont());
249             }
250 
251 
252             buf.append("}");
253 
254         }
255         buf.append(R);
256         buf.append("Bounds : ");
257         buf.append(this.getBounds());
258         buf.append("}");
259         return buf.toString();
260     }
261 }