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
38class phpFiglet
39{
40
41    /*
42     *  Internal variables
43     */
44
45    var $signature;
46    var $hardblank;
47    var $height;
48    var $baseline;
49    var $maxLenght;
50    var $oldLayout;
51    var $commentLines;
52    var $printDirection;
53    var $fullLayout;
54    var $codeTagCount;
55    var $fontFile;
56
57    /*
58     *  Contructor
59     */
60
61    function phpFiglet()
62    {
63
64    }
65
66    /*
67     *  Load an flf font file. Return true on success, false on error.
68     */
69
70    function loadfont($fontfile)
71    {
72        $this->fontFile = @file($fontfile);
73        if (!$this->fontFile) return false;
74
75        $hp = explode(" ", $this->fontFile[0]); // get header
76
77        $this->signature = substr($hp[0], 0, strlen($hp[0]) - 1);
78        $this->hardblank = substr($hp[0], strlen($hp[0]) - 1, 1);
79        $this->height = $hp[1];
80        $this->baseline = $hp[2];
81        $this->maxLenght = $hp[3];
82        $this->oldLayout = $hp[4];
83        $this->commentLines = $hp[5] + 1;
84        $this->printDirection = $hp[6];
85        $this->fullLayout = $hp[7];
86        $this->codeTagCount = $hp[8];
87
88        unset($hp);
89
90        if ($this->signature != "flf2a") {
91            return false;
92        } else {
93            return true;
94        }
95    }
96
97    /*
98     *  Get a character as a string, or an array with one line
99     *  for each font height.
100     */
101
102    function getCharacter($character, $asarray = false)
103    {
104        $asciValue = ord($character);
105        $start = $this->commentLines + ($asciValue - 32) * $this->height;
106        $data = ($asarray) ? array() : "";
107
108        for ($a = 0; $a < $this->height; $a++) {
109            $tmp = $this->fontFile[$start + $a];
110            $tmp = str_replace("@", "", $tmp);
111            //$tmp = trim($tmp);
112            $tmp = str_replace($this->hardblank, " ", $tmp);
113
114            if ($asarray) {
115                $data[] = $tmp;
116            } else {
117                $data .= $tmp;
118            }
119        }
120
121        return $data;
122    }
123
124    /*
125     *  Returns a figletized line of characters.
126     */
127
128    function fetch($line)
129    {
130        $ret = "";
131
132        for ($i = 0; $i < (strlen($line)); $i++) {
133            $data[] = $this->getCharacter($line[$i], true);
134        }
135
136        @reset($data);
137
138        for ($i = 0; $i < $this->height; $i++) {
139            while (list($k, $v) = each($data)) {
140                $ret .= str_replace("\n", "", $v[$i]);
141            }
142            reset($data);
143            $ret .= "\n";
144        }
145
146        return $ret;
147    }
148
149    /*
150     *  Display (print) a figletized line of characters.
151     */
152
153    function display($line)
154    {
155        print $this->fetch($line);
156    }
157
158}
159
160?>
161