-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass_AtasciiFont.php
More file actions
188 lines (167 loc) · 5.54 KB
/
class_AtasciiFont.php
File metadata and controls
188 lines (167 loc) · 5.54 KB
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
<?
require_once('./_polyfill.php');
require_once('./_string_helpers.php');
const ATASCII_FONT_PATH="./AtasciiFonts/";
const ATASCII_FONT_EXT=".json";
const ENCODE_ANTIC=false;
const ENCODE_ATASCII=true;
class AtasciiFont {
const HPOS_WHOLE="wholeChar";
const HPOS_HALF="halfChar";
public $fn=null;
public $name=null;
private $charDef=null;
public $width,$height; // Character dimentions
public $letterSpace; // Space between letters
public $lineSpace; // Space between lines
public $HPos; // Horizontal positioning - true-WholeChar; false-halfChar
public $dataEncode; // encode method for character data
public function __construct($fontFile) {
$cnt=@file_get_contents(ATASCII_FONT_PATH.$fontFile.ATASCII_FONT_EXT);
if ( $cnt===false ) throw new Exception("Can't open AtasciiFont file");
$font=json_decode($cnt,true);
if (json_last_error()!=0) throw new Exception(json_last_error_msg()." in AtasciiFont file");
$this->fn=$fontFile;
$this->name=@$font['name'] or $fontFile;
if ( !(isset($font['width'])) || !(isset($font['height'])) ) {
throw new Exception('AtasciiFont file dimentions not speciied.');
}
$this->width=$font['width'];
$this->height=$font['height'];
$this->letterSpace=$font['letterSpacing'] or 1;
$this->lineSpace=$font['linesSpacing'] or 1;
$this->spaceWidth=@$font['spaceWidth'] or $this->letterSpace;
switch (@$font['dataEncode']) {
case 'antic': $this->dataEncode=ENCODE_ANTIC; break;
case 'atascii': $this->dataEncode=ENCODE_ATASCII; break;
default:
$this->dataEncode=ENCODE_ANTIC;
}
if ( !isset($font['charsDefinition']) ) {
throw new Exception('No character definition section found in AtasciiFont file');
}
$this->charDef=&$font['charsDefinition'];
switch (@$font['horizontalPositioning']) {
case self::HPOS_WHOLE: $this->HPos=true; break;
case self::HPOS_HALF: $this->HPos=false; break;
default:
$this->HPos=true;
}
if ( !$this->HPos && isset($font['horizontalPositioning']) ) {
if ( !(isset($font['charsDefinition']['even'])) ||
!(isset($font['charsDefinition']['odd'])) ) {
throw new Exception('AtasciiFont file has incorrect characters definition');
}
}
}
public function getCharData($ch,$pos=0) {
// get character definition
if (!$this->HPos) { // for half positionig get...
if ( ($pos % 2)===0 ) {
$subFont='even'; // ...even definitions
} else {
$subFont='odd'; //...odd definitions
}
if ( isset($this->charDef[$subFont][$ch]) ) {
$curFDef=$this->charDef[$subFont][$ch];
} else {
return null;
}
} else {
if ( isset($this->charDef[$ch]) ) {
$curFDef=$this->charDef[$ch];
} else {
return null;
}
}
return [$curFDef['width'],$curFDef['height'],hexString2Data($curFDef['data'])];
}
public function getWidth(&$textLines) {
$width=0;
foreach ($textLines as $nLine => $textLine) {
$len=strlen($textLine);
if ($len>$width) $width=$len;
}
return $width;
}
public function getHeight(&$textLines) {
return count($textLines);
}
public function makeText($str,$encode=null) {
if ($encode!==null) {
if ($encode) $spaceCh=chr(32); else $spaceCh=chr(0);
} else {
if ($this->dataEncode) $spaceCh=chr(32); else $spaceCh=chr(0);
}
$outLines=[]; $textHeight=$this->height;
$strLen=strLen($str); $letterCnt=0; $offsetY=0;
for ($strOfs=0;$strOfs<$strLen;$strOfs++) {
$ch=$str[$strOfs];
if ( $ch==="\n" ) {
$letterCnt=0;
$offsetY+=$textHeight+$this->lineSpace;
}
if ( $ch===chr(32) ) {
for ($line=0;$line<$textHeight;$line++) {
@$outLines[$offsetY+$line].=str_repeat($spaceCh,$this->spaceWidth);
}
} else {
list($curCharWidth,$curCharHeight,$curCharDef)=$this->getCharData($ch,$letterCnt);
if ( $curCharDef===null ) continue;
for ($line=0;$line<$textHeight;$line++) {
if ($line<$curCharHeight) {
$chLine=substr($curCharDef,$line*$curCharWidth,$curCharWidth);
if ($encode!==false) {
if ( $this->dataEncode===ENCODE_ANTIC && $encode===ENCODE_ATASCII ) {
strANTIC2ASCII($chLine);
} elseif ( $this->dataEncode===ENCODE_ATASCII && $encode===ENCODE_ANTIC ) {
strASCII2ANTIC($chLine);
}
}
@$outLines[$offsetY+$line].=$chLine;
} else {
@$outLines[$offsetY+$line].=str_repeat($spaceCh,$curCharWidth);
}
// letter spacing add
if ( $strOfs+1<$strLen ) { // ...but only, if it is not last character in string
if ($this->letterSpace>0) {
@$outLines[$offsetY+$line].=str_repeat($spaceCh,$this->letterSpace);
}
}
}
$letterCnt++;
}
}
return $outLines;
}
public function makeImage($str, $imageFile=null, $fontFile=DEFAULT_FONT_FILE,
$defaultCharWidth=DEFAULT_CHAR_WIDTH,$defaultCharHeight=DEFAULT_CHAR_HEIGHT) {
$Data=$this->makeText($str);
$fnt=@imagecreatefrompng($fontFile);
if ( $fnt===false ) die('Cannot load Atascii Fontset image');
$width=strlen($Data[0]);
$height=count($Data);
$img=@imagecreate(($width*$defaultCharWidth),($height*$defaultCharHeight))
or die("Cannot Initialize new GD image stream");
for ($y=0;$y<$height;$y++) {
$offset=$y*$width;
for ($x=0;$x<$width;$x++) {
$ch=ord($Data[$y][$x]);
$chx=$ch & 0x1f;
$chy=$ch >> 5;
imagecopy($img,$fnt,
$x*$defaultCharWidth,$y*$defaultCharHeight,
$chx*$defaultCharWidth,$chy*$defaultCharHeight,
$defaultCharWidth,$defaultCharHeight);
}
}
if ($imageFile!==null) {
imagepng($img,$imageFile);
} else {
imagepng($img);
}
imagedestroy($img);
imagedestroy($fnt);
}
}
?>