-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathlib.php
157 lines (148 loc) · 4.5 KB
/
lib.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
<?php
include_once __DIR__."/samples/_php/encoding.php";
include_once __DIR__."/samples/_php/unicode.php";
include_once __DIR__."/samples/_php/convertKana.php";
if( !function_exists('mb_str_split')){
// some old php do not have it
function mb_str_split( $string = '', $length = 1 , $encoding = "UTF-8" ){
if(!empty($string)){
$split = array();
$mb_strlen = mb_strlen($string,$encoding);
for($pi = 0; $pi < $mb_strlen; $pi += $length){
$substr = mb_substr($string, $pi,$length,$encoding);
if( !empty($substr)){
$split[] = $substr;
}
}
}
return $split;
}
}
function my_json_decode($line)
{
// some old php do not have json_encode() and json_decode
// decode a line from graphicsXxx.txt or dictionaryXxx.txt
$a=new StdClass();
if (preg_match("/^\\{\"character\":\"([^\"]+)\",\"strokes\":\\[\"([^\\]]+)\"\\],\"medians\":\\[(.+)\\]\\}$/",$line,$match))
{
$a->{'character'}=$match[1];
$a->{'strokes'}=explode("\",\"",$match[2]);
$x=explode("]],[[",$match[3]);
$kmx=count($x);
$x[0]=str_replace("[[","",$x[0]);
$x[$kmx-1]=str_replace("]]","",$x[$kmx-1]);
$y=array();
for($kx=0;$kx<$kmx;$kx++)
{
$y=explode("],[",$x[$kx]);
$kmy=count($y);
for($ky=0;$ky<$kmy;$ky++)
{
$y[$ky]=explode(",",$y[$ky]);
}
$x[$kx]=$y;
}
$a->{'medians'}=$x;
}
else if (preg_match("/\"character\":\"([^\"]+)\"/",$line,$match))
{
$a->{'character'}=$match[1];
if (preg_match("/\"set\":\\[\"([^\\]]+)\"\\]/",$line,$match))
$a->{'set'}=explode("\",\"",$match[1]);
if (preg_match("/\"definition\":\"([^\"]+)\"/",$line,$match))
$a->{'definition'}=$match[1];
if (preg_match("/\"pinyin\":\\[\"([^\\]]+)\"\\]/",$line,$match))
$a->{'pinyin'}=explode("\",\"",$match[1]);
if (preg_match("/\"on\":\\[\"([^\\]]+)\"\\]/",$line,$match))
$a->{'on'}=explode("\",\"",$match[1]);
if (preg_match("/\"kun\":\\[\"([^\\]]+)\"\\]/",$line,$match))
$a->{'kun'}=explode("\",\"",$match[1]);
if (preg_match("/\"radical\":\"([^\"]+)\"/",$line,$match))
$a->{'radical'}=$match[1];
if (preg_match("/\"decomposition\":\"([^\"]+)\"/",$line,$match))
$a->{'decomposition'}=$match[1];
if (preg_match("/\"acjk\":\"([^\"]+)\"/",$line,$match))
$a->{'acjk'}=$match[1];
}
return $a;
}
function transformPathFromGraphics($p)
{
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";
return $q;
}
return $p;
}
function buildSvg($a)
{
$u=decUnicode($a->{'character'});
$id="z".$u;
$x="xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\"";
$s="<svg id=\"".$id."\" class=\"acjk\" version=\"1.1\" viewBox=\"0 0 1024 1024\" ".$x.">\n";
// style
$s.="<style>\n<![CDATA[\n";
$s.="@keyframes zk {\n";
$s.="\tto {\n";
$s.="\t\tstroke-dashoffset:0;\n";
$s.="\t}\n";
$s.="}\n";
$s.="svg.acjk path[clip-path] {\n";
$s.="\t--t:0.8s;\n";
$s.="\tanimation:zk var(--t) linear forwards var(--d);\n";
$s.="\tstroke-dasharray:3337;\n"; // more than pathLength + 1
$s.="\tstroke-dashoffset:3339;\n"; // less than 2 * strokeDasharray - pathLength
$s.="\tstroke-width:128;\n"; // acjk.strokeWidthMax + 8 or 16?
$s.="\tstroke-linecap:round;\n";
$s.="\tfill:none;\n";
$s.="\tstroke:#000;\n";
$s.="}\n";
$s.="svg.acjk path[id] {fill:#ccc;}\n";
$s.="]]>\n</style>\n";
// stroke shapes
$k=0;
foreach($a->{'strokes'} as $p)
{
$k++;
$p=str_replace(","," ",$p);
$p=preg_replace("#\s?([MQCLZ])\s?#","$1",$p);
$p=preg_replace("#([^ ])-#","$1 -",$p);
// transform coordinates of path nodes (x2 = x1, y2 = 900-y1)
// don't do this transformation if $_GET["t"] exists and is not 1
if (!isset($_GET["t"])||($_GET["t"]==1)) $p=transformPathFromGraphics($p);
$s.="<path id=\"".$id."d".$k."\" d=\"".$p."\"/>\n";
}
// clip paths
$s.="<defs>\n";
$k=0;
foreach($a->{'strokes'} as $p)
{
$k++;
$s.="\t<clipPath id=\"".$id."c".$k."\">";
$s.="<use xlink:href=\"#".$id."d".$k."\"/>";
$s.="</clipPath>\n";
}
$s.="</defs>\n";
// medians
$k=0;
foreach($a->{'medians'} as $m)
{
$k++;
$z="";
foreach($m as $point) $z.=($z?"L":"M").$point[0]." ".$point[1];
if (!isset($_GET["t"])||($_GET["t"]==1)) $z=transformPathFromGraphics($z);
$s.="<path style=\"--d:".$k."s;\" pathLength=\"3333\" clip-path=\"url(#".$id."c".$k.")\" d=\"".$z."\"/>\n";
}
$s.="</svg>";
return $s;
}
?>