forked from Medabots/medarot3_dialog-previewer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prerenderer.php
175 lines (135 loc) · 4.11 KB
/
prerenderer.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
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
<?php
include(dirname(__FILE__).'/functions.php');
$text='';
if(isset($_REQUEST['t'])) {
$text=$_REQUEST['t'].'';
}
if(empty($text)) {
header('Content-Type: text/html; charset=utf-8');
echo '<!DOCTYPE html>';
echo '<html>';
echo '<head>';
echo '<meta http-equiv="Content-Type" content="text/html; charset=utf-8">';
echo '<title>Medarot 3 Text Pre-renderer</title>';
echo '</head>';
echo '<body>';
echo '<form action="" method="get">';
echo '<input name="d" type="hidden" value="1">';
echo '<p><textarea name="t" cols="50" rows="5"></textarea></p>';
echo '<p><select name="f">';
echo '<option value="0" selected>Normal</option>';
echo '<option value="1">Narrow</option>';
echo '<option value="2">Bold</option>';
echo '<option value="3">Robotic</option>';
echo '<option value="4">Robotic Bold</option>';
echo '</select></p>';
echo '<p><input type="submit" value="Download"></p>';
echo '</form>';
echo '</body>';
echo '</html>';
} else {
// Normalise linebreaks.
$text=str_replace("\r\n","\n",$text);
$text=str_replace("\r","\n",$text);
$basedir=dirname(__FILE__);
$cachedir=$basedir.'/cache';
if(!file_exists($cachedir)) {
mkdir($cachedir);
}
$charwidthtable=get_char_width_table();
$fontnum=0;
if(isset($_REQUEST['f'])) {
$fontnum=trim($_REQUEST['f'].'');
$fontnum=clean_fontnum($fontnum);
}
$font=load_vwf_font($fontnum,$cachedir,$basedir);
$lines=explode("\n",$text);
$longestlinelength=8;
$dl=0;
if(isset($_REQUEST['d'])) {
$dl=trim($_REQUEST['d'].'');
$dl=intval($dl,10);
}
$dl=($dl>0?true:false);
$i=0;
$c=strlen($text);
$linestart=0;
$currentline=0;
foreach($lines as $linenum => $linetext) {
// Replace ASCII control characters and tabs with ?.
$linetext=preg_replace('/[\x00-\x1F\x7F]/','?',$linetext);
// Re-encode special characters.
$linetext=reenc_special_chars($linetext);
// Convert any remaining UTF8 characters to ?.
$linetext=mb_convert_encoding($linetext,'ASCII','UTF8');
// Make damn sure there are no stragglers in the $80 to $FF range.
$linetext=preg_replace('/[\x80-\xFF]/','?',$linetext);
// Store modified text.
$lines[$linenum]=$linetext;
// Count text length in pixels and note fonts used.
// Strings can be uses as char arrays in php.
$linelength=0;
$i=0;
$c=strlen($linetext);
while($i<$c) {
$char=$linetext[$i];
$charcode=ord($char);
if($charcode<0x80) {
// Add to pixel length total.
$linelength+=$charwidthtable[$fontnum][$charcode]+1;
}
$i++;
}
if($linelength>$longestlinelength) {
$longestlinelength=$linelength;
}
}
// Calculate the dimensions of our canvas.
$canvaswidth=($longestlinelength-($longestlinelength%8))+8;
$canvasheight=count($lines)*8;
if($canvasheight<8) {
$canvasheight=8;
}
// Create our canvas for compositing images.
$im=imagecreatetruecolor($canvaswidth,$canvasheight);
// Make the background white.
imagealphablending($im,false);
$white=imagecolorallocate($im,255,255,255);
imagefill($im,0,0,$white);
imagealphablending($im,true);
// Draw text.
foreach($lines as $linenum => $linetext) {
$i=0;
$c=strlen($linetext);
$linelength=0;
while($i<$c) {
$char=$linetext[$i];
$charcode=ord($char);
if($charcode<0x80) {
// Get font character width.
$charwidth=$charwidthtable[$fontnum][$charcode]+1;
// Calculate font character x and y positions divided by 8.
$charxindex=$charcode%0x10;
$charyindex=round(($charcode-$charxindex)/0x10);
// Draw font character to canvas image.
imagecopy($im,$font,$linelength,($linenum*8),$charxindex*8,$charyindex*8,$charwidth,8);
$linelength+=$charwidth;
}
$i++;
}
}
// Font is no longer needed. Remove it from memory.
imagedestroy($font);
// Output final image.
header('Content-Type: image/png');
if($dl) {
header('Content-Disposition: attachment; filename=RenderedText.png');
}
header('X-Content-Type-Options: nosniff');
imagesavealpha($im,false);
//imagetruecolortopalette($im,false,2);
//imagecolortransparent($im,imagecolorat($im,$canvaswidth-1,0));
imagepng($im,null,7);
imagedestroy($im);
}
?>