-
Notifications
You must be signed in to change notification settings - Fork 71
/
makeGraphicsFromSvgs4Kana.php
201 lines (196 loc) · 5.91 KB
/
makeGraphicsFromSvgs4Kana.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
<!doctype html>
<html>
<!--
Purpose: make graphicsKana.txt from svgsKana folder content
Usage: run makeGraphicsFromSvgs4Kana.php in a browser
Requirements: svgsKana directory must exist, graphicsKana.txt file must not exist
Clean svg paths (remove decimal, replace "," by " ", ...)
Does nothing if svgsKana doesn't exist
Does nothing if graphicsKana.txt already exists
Note: the server must be "localhost" or the server specified as value of $myServer below
-->
<head>
<meta charset="UTF-8">
<meta name="viewport" content="initial-scale=1.0,user-scalable=yes">
<style>
footer a {text-align:center;color:#000;}
</style>
</head>
<body>
<?php
// if you want to use a server which is not "localhost"
// replace the value of $myServer below by your server domain or ip
// don't let $myServer set to a production server to avoid anybody runs this script
$myServer="192.168.1.23";
$version="Kana";
if (isset($_GET["p"])&&(md5($_GET["p"]."acjk")=="897950b81d960d551df6a6e9e9df9be5"))
$pAcjk=1;
else
$pAcjk=0;
$dir="svgs".$version;
$target="graphics".$version.".txt";
?>
<h1>Make graphics<?php echo $version;?>.txt file
from svgs<?php echo $version;?> directory content</h1>
<?php
include_once "samples/_php/unicode.php";
function transformPathFromSvgs($p)
{
//assume "-" never follows a number
if (preg_match_all("#([MQCLZ, ]+)([0-9.-]+)[ ,]+([0-9.-]+)#",$p,$m))
{
$npm=count($m[0]);
$q="";
for ($np=0;$np<$npm;$np++)
{
$x=intval($m[2][$np]);
$y=-intval($m[3][$np])+900;
$q.=$m[1][$np].$x.",".$y;
}
if (preg_match("/Z/",$p)) $q.="Z";
//print "<br>p=".$p."<br>";
//print "<br>q=".$q."<br>";
return $q;
}
return $p;
}
function replaceVHbyL($p)
{
// assume "-" never follows a number
// assume no ,
// assume no space near a letter
// add V or H if omitted
$q="/([VH])([0-9-]+)[\s]([0-9-]+)/";
while (preg_match($q,$p)) $p=preg_replace($q,"$1$2$1$3",$p);
while (preg_match("/[VH]/",$p))
{
// replace V by L
$q="/([0-9-]+)\s([0-9-]+)V([0-9-]+)/";
if (preg_match($q,$p)) $p=preg_replace($q,"$1 $2L$1 $3",$p);
// replace H by L
$q="/([0-9-]+)\s([0-9-]+)H([0-9-]+)/";
if (preg_match($q,$p)) $p=preg_replace($q,"$1 $2L$3 $2",$p);
}
return $p;
}
function makeGraphics($dir,$target,$version)
{
if (file_exists($target)) unlink($target);
$a=scandir($dir);
$k=0;
foreach ($a as $f)
{
if (substr($f,0,1)==".") echo "Skip ".$f."<br>\n";
else if ($f=="00000.svg") echo "Skip ".$f."<br>\n";
else
{
$dec=intVal($f);
$handle=fopen($dir."/".$f,"r");
$s='{"character":"'.unichr($dec).'","strokes":[';
if ($handle)
{
$k++;
echo $k.": ".unichr($dec)." ".$f."<br>\n";
$n=0;
while (($line=fgets($handle))!==false)
{
$r='#<path[^>]+id="z[0-9]+d[0-9]+[a-z]?"[^>]+d="([^"]+)"[^>]*>#';
if (preg_match($r,$line,$m))
{
if ($n) $s.=",";
$m2=$m[1];
// replace , by space
$m2=preg_replace("/,/"," ",$m2);
// add space before -
$m2=preg_replace("/([0-9])-/","$1 -",$m2);
// remove extra space
$m2=preg_replace("/\s+/"," ",$m2);
// remove decimal
$m2=preg_replace("/\.[0-9]+/","",$m2);
// replace z by Z
$m2=preg_replace("/z/","Z",$m2);
// remove space before and after M, Q, C, L, H, V et Z
$m2=preg_replace("/\s?([MQCLVHZ])\s?/","$1",$m2);
// add C if omitted
$q="/(C([0-9-]+\s){5}[0-9-]+)\s/";
while (preg_match($q,$m2)) $m2=preg_replace($q,"$1C",$m2);
// add Q if omitted
$q="/(Q([0-9-]+\s){3}[0-9-]+)\s/";
while (preg_match($q,$m2)) $m2=preg_replace($q,"$1Q",$m2);
// add L if omitted
$q="/([ML][0-9-]+\s[0-9-]+)\s/";
while (preg_match($q,$m2)) $m2=preg_replace($q,"$1L",$m2);
$m2=replaceVHbyL($m2);
if (!isset($_GET["t"])||($_GET["t"]==1)) $s.='"'.transformPathFromSvgs($m2).'"';
//echo $m2."<br>\n";
$n++;
}
}
$s.='],"medians":[';
rewind($handle);
$n=0;
while (($line=fgets($handle))!==false)
{
$r='#<path[^>]+pathLength[^>]+d="([^"]+)"[^>]*>#';
if (preg_match($r,$line,$m))
{
if ($n) $s.=",";
$m2=$m[1];
// replace , by space
$m2=preg_replace("/,/"," ",$m2);
// add space before -
$m2=preg_replace("/([0-9])-/","$1 -",$m2);
// remove extra space
$m2=preg_replace("/\s+/"," ",$m2);
// remove decimal
$m2=preg_replace("/\.[0-9]+/","",$m2);
// remove space before and after M, L, H, and V
$m2=preg_replace("/\s?([MLVH])\s?/","$1",$m2);
// add L if omitted
$q="/([ML][0-9-]+\s[0-9-]+)\s/";
while (preg_match($q,$m2)) $m2=preg_replace($q,"$1L",$m2);
// replace V and H by L
$m2=replaceVHbyL($m2);
//echo $m2."<br>\n";
if (!isset($_GET["t"])||($_GET["t"]==1)) $m2=transformPathFromSvgs($m2);
$m2=preg_replace("/\s+/",",",$m2);
$m2=preg_replace("/L/",",",$m2);
$m2=preg_replace("/M/","[[",$m2);
$m2=preg_replace("/([0-9-]+,[0-9-]+),/","$1],[",$m2);
$m2.="]]";
$s.=$m2;
//echo $m2."<br>\n";
$n++;
}
}
$s.=']}';
//echo $s."<br>\n";
file_put_contents("graphics".$version.".txt",$s.PHP_EOL,FILE_APPEND|LOCK_EX);
echo "n=".$n."<br>\n";
fclose($handle);
}
else echo "Cannot open \"".$f."\"<br>\n";
}
}
}
echo "<p>Begin<br>\n";
if (($_SERVER['SERVER_NAME']!="localhost")&&($_SERVER['SERVER_NAME']!=$myServer))
echo "Error: not a convenient server<br>\n";
else if (!file_exists($dir)) echo "Error: ".$dir." directory not found<br>\n";
else if (!$pAcjk&&file_exists($target))
{
echo "Error: ".$target." file already exists<br>\n";
echo "Verify if the version is correct";
echo " (you specified ".($version?"\"".$version."\"":"an empty string")." as version)<br>\n";
echo "If the version is correct, rename or delete the existing ".$target." file before retrying<br>\n";
}
else makeGraphics($dir,$target,$version);
echo "End</p>\n";
?>
<footer>
<a href="./">Home</a>
- <a href="licenses/COPYING.txt">Licences</a><br>
Copyright 2016-2022 - FM&SH
</footer>
</body>
</html>