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.fontgenerator;
8   
9   import java.awt.*;
10  import java.awt.geom.AffineTransform;
11  
12  /***
13   * <p>Takes a random font and apply a rotation to it. </p>
14   *
15   * @author <a href="mailto:mga@octo.com">Mathieu Gandin</a>
16   * @version 1.0
17   */
18  public class DeformedRandomFontGenerator extends RandomFontGenerator {
19  
20  
21      public DeformedRandomFontGenerator(Integer minFontSize,
22                                         Integer maxFontSize) {
23          super(minFontSize, maxFontSize);
24      }
25  
26  
27      /***
28       * Provides a way for children class to customize the generated font array
29       *
30       * @param font
31       * @return a customized font
32       */
33      protected Font applyCustomDeformationOnGeneratedFont(Font font) {
34          // rotate each letter by -0.33, +0.33 or about 20 degrees
35          float theta = (myRandom.nextBoolean() ? 1 : -1) * myRandom.nextFloat() / 3;
36  
37          // private DecimalFormat debug_fmt = new DecimalFormat();
38          // System.out.println("Creating " + font + " rotated angle = " + debug_fmt.format(theta * 57.2957));
39  
40          // rotate each letter by this angle
41          AffineTransform at = new AffineTransform();
42          at.rotate(theta, myRandom.nextDouble()/*x*/, myRandom.nextDouble()/*y*/);
43          return font.deriveFont(at);
44      }
45  }