xref: /plugin/captcha/figlet.php (revision 0110319e4cbc960ac8315c21fefff1266233e841)
1*0110319eSAndreas Gohr<?php
2*0110319eSAndreas Gohr/*        _         _____  _       _       _
3*0110319eSAndreas Gohr *   ___ | |_  ___ |   __||_| ___ | | ___ | |_
4*0110319eSAndreas Gohr *  | . ||   || . ||   __|| || . || || -_||  _|
5*0110319eSAndreas Gohr *  |  _||_|_||  _||__|   |_||_  ||_||___||_|
6*0110319eSAndreas Gohr *  |_|       |_|            |___|
7*0110319eSAndreas Gohr *
8*0110319eSAndreas Gohr *  Author   :      Lucas Baltes (lucas@thebobo.com)
9*0110319eSAndreas Gohr *                  $Author: lhb $
10*0110319eSAndreas Gohr *
11*0110319eSAndreas Gohr *  Website  :      http://www.thebobo.com/
12*0110319eSAndreas Gohr *
13*0110319eSAndreas Gohr *  Date     :      $Date: 2003/03/16 10:08:01 $
14*0110319eSAndreas Gohr *  Rev      :      $Revision: 1.0 $
15*0110319eSAndreas Gohr *
16*0110319eSAndreas Gohr *  Copyright:      2003 - Lucas Baltes
17*0110319eSAndreas Gohr *  License  :      GPL - http://www.gnu.org/licenses/gpl.html
18*0110319eSAndreas Gohr *
19*0110319eSAndreas Gohr *  Purpose  :      Figlet font class
20*0110319eSAndreas Gohr *
21*0110319eSAndreas Gohr *  Comments :      phpFiglet is a php class to somewhat recreate the
22*0110319eSAndreas Gohr *                  functionality provided by the original figlet program
23*0110319eSAndreas Gohr *                  (http://www.figlet.org/). It does not (yet) support the
24*0110319eSAndreas Gohr *                  more advanced features like kerning or smushing. It can
25*0110319eSAndreas Gohr *                  use the same (flf2a) fonts as the original figlet program
26*0110319eSAndreas Gohr *                  (see their website for more fonts).
27*0110319eSAndreas Gohr *
28*0110319eSAndreas Gohr *  Usage    :      $phpFiglet = new phpFiglet();
29*0110319eSAndreas Gohr *
30*0110319eSAndreas Gohr *                  if ($phpFiglet->loadFont("fonts/standard.flf")) {
31*0110319eSAndreas Gohr *                      $phpFiglet->display("Hello World");
32*0110319eSAndreas Gohr *                  } else {
33*0110319eSAndreas Gohr *                      trigger_error("Could not load font file");
34*0110319eSAndreas Gohr *                  }
35*0110319eSAndreas Gohr *
36*0110319eSAndreas Gohr */
37*0110319eSAndreas Gohr
38*0110319eSAndreas Gohr
39*0110319eSAndreas Gohrclass phpFiglet
40*0110319eSAndreas Gohr{
41*0110319eSAndreas Gohr
42*0110319eSAndreas Gohr    /*
43*0110319eSAndreas Gohr     *  Internal variables
44*0110319eSAndreas Gohr     */
45*0110319eSAndreas Gohr
46*0110319eSAndreas Gohr    var $signature;
47*0110319eSAndreas Gohr    var $hardblank;
48*0110319eSAndreas Gohr    var $height;
49*0110319eSAndreas Gohr    var $baseline;
50*0110319eSAndreas Gohr    var $maxLenght;
51*0110319eSAndreas Gohr    var $oldLayout;
52*0110319eSAndreas Gohr    var $commentLines;
53*0110319eSAndreas Gohr    var $printDirection;
54*0110319eSAndreas Gohr    var $fullLayout;
55*0110319eSAndreas Gohr    var $codeTagCount;
56*0110319eSAndreas Gohr    var $fontFile;
57*0110319eSAndreas Gohr
58*0110319eSAndreas Gohr
59*0110319eSAndreas Gohr    /*
60*0110319eSAndreas Gohr     *  Contructor
61*0110319eSAndreas Gohr     */
62*0110319eSAndreas Gohr
63*0110319eSAndreas Gohr    function phpFiglet()
64*0110319eSAndreas Gohr    {
65*0110319eSAndreas Gohr
66*0110319eSAndreas Gohr    }
67*0110319eSAndreas Gohr
68*0110319eSAndreas Gohr
69*0110319eSAndreas Gohr    /*
70*0110319eSAndreas Gohr     *  Load an flf font file. Return true on success, false on error.
71*0110319eSAndreas Gohr     */
72*0110319eSAndreas Gohr
73*0110319eSAndreas Gohr    function loadfont($fontfile)
74*0110319eSAndreas Gohr    {
75*0110319eSAndreas Gohr        $this->fontFile = @file($fontfile);
76*0110319eSAndreas Gohr        if (!$this->fontFile) return false;
77*0110319eSAndreas Gohr
78*0110319eSAndreas Gohr        $hp = explode(" ", $this->fontFile[0]); // get header
79*0110319eSAndreas Gohr
80*0110319eSAndreas Gohr        $this->signature = substr($hp[0], 0, strlen($hp[0]) -1);
81*0110319eSAndreas Gohr        $this->hardblank = substr($hp[0], strlen($hp[0]) -1, 1);
82*0110319eSAndreas Gohr        $this->height = $hp[1];
83*0110319eSAndreas Gohr        $this->baseline = $hp[2];
84*0110319eSAndreas Gohr        $this->maxLenght = $hp[3];
85*0110319eSAndreas Gohr        $this->oldLayout = $hp[4];
86*0110319eSAndreas Gohr        $this->commentLines = $hp[5] + 1;
87*0110319eSAndreas Gohr        $this->printDirection = $hp[6];
88*0110319eSAndreas Gohr        $this->fullLayout = $hp[7];
89*0110319eSAndreas Gohr        $this->codeTagCount = $hp[8];
90*0110319eSAndreas Gohr
91*0110319eSAndreas Gohr        unset($hp);
92*0110319eSAndreas Gohr
93*0110319eSAndreas Gohr        if ($this->signature != "flf2a") {
94*0110319eSAndreas Gohr            return false;
95*0110319eSAndreas Gohr        } else {
96*0110319eSAndreas Gohr            return true;
97*0110319eSAndreas Gohr        }
98*0110319eSAndreas Gohr    }
99*0110319eSAndreas Gohr
100*0110319eSAndreas Gohr
101*0110319eSAndreas Gohr    /*
102*0110319eSAndreas Gohr     *  Get a character as a string, or an array with one line
103*0110319eSAndreas Gohr     *  for each font height.
104*0110319eSAndreas Gohr     */
105*0110319eSAndreas Gohr
106*0110319eSAndreas Gohr    function getCharacter($character, $asarray = false)
107*0110319eSAndreas Gohr    {
108*0110319eSAndreas Gohr        $asciValue = ord($character);
109*0110319eSAndreas Gohr        $start = $this->commentLines + ($asciValue - 32) * $this->height;
110*0110319eSAndreas Gohr        $data = ($asarray) ? array() : "";
111*0110319eSAndreas Gohr
112*0110319eSAndreas Gohr        for ($a = 0; $a < $this->height; $a++)
113*0110319eSAndreas Gohr        {
114*0110319eSAndreas Gohr            $tmp = $this->fontFile[$start + $a];
115*0110319eSAndreas Gohr            $tmp = str_replace("@", "", $tmp);
116*0110319eSAndreas Gohr            //$tmp = trim($tmp);
117*0110319eSAndreas Gohr            $tmp = str_replace($this->hardblank, " ", $tmp);
118*0110319eSAndreas Gohr
119*0110319eSAndreas Gohr            if ($asarray) {
120*0110319eSAndreas Gohr                $data[] = $tmp;
121*0110319eSAndreas Gohr            } else {
122*0110319eSAndreas Gohr                $data .= $tmp;
123*0110319eSAndreas Gohr            }
124*0110319eSAndreas Gohr        }
125*0110319eSAndreas Gohr
126*0110319eSAndreas Gohr        return $data;
127*0110319eSAndreas Gohr    }
128*0110319eSAndreas Gohr
129*0110319eSAndreas Gohr
130*0110319eSAndreas Gohr    /*
131*0110319eSAndreas Gohr     *  Returns a figletized line of characters.
132*0110319eSAndreas Gohr     */
133*0110319eSAndreas Gohr
134*0110319eSAndreas Gohr    function fetch($line)
135*0110319eSAndreas Gohr    {
136*0110319eSAndreas Gohr        $ret = "";
137*0110319eSAndreas Gohr
138*0110319eSAndreas Gohr        for ($i = 0; $i < (strlen($line)); $i++)
139*0110319eSAndreas Gohr        {
140*0110319eSAndreas Gohr            $data[] = $this->getCharacter($line[$i], true);
141*0110319eSAndreas Gohr        }
142*0110319eSAndreas Gohr
143*0110319eSAndreas Gohr        @reset($data);
144*0110319eSAndreas Gohr
145*0110319eSAndreas Gohr        for ($i = 0; $i < $this->height; $i++)
146*0110319eSAndreas Gohr        {
147*0110319eSAndreas Gohr            while (list($k, $v) = each($data))
148*0110319eSAndreas Gohr            {
149*0110319eSAndreas Gohr                $ret .= str_replace("\n", "", $v[$i]);
150*0110319eSAndreas Gohr            }
151*0110319eSAndreas Gohr            reset($data);
152*0110319eSAndreas Gohr            $ret .= "\n";
153*0110319eSAndreas Gohr        }
154*0110319eSAndreas Gohr
155*0110319eSAndreas Gohr        return $ret;
156*0110319eSAndreas Gohr    }
157*0110319eSAndreas Gohr
158*0110319eSAndreas Gohr
159*0110319eSAndreas Gohr    /*
160*0110319eSAndreas Gohr     *  Display (print) a figletized line of characters.
161*0110319eSAndreas Gohr     */
162*0110319eSAndreas Gohr
163*0110319eSAndreas Gohr    function display($line)
164*0110319eSAndreas Gohr    {
165*0110319eSAndreas Gohr        print $this->fetch($line);
166*0110319eSAndreas Gohr    }
167*0110319eSAndreas Gohr
168*0110319eSAndreas Gohr}
169*0110319eSAndreas Gohr?>
170