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