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