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