-
Notifications
You must be signed in to change notification settings - Fork 0
/
lookup.php
299 lines (264 loc) · 8.84 KB
/
lookup.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
<?php
require_once 'char-utils.php';
require_once 'lookup-lib.php';
define('TIMEOUT', 20);
$ignoreComponents = json_decode(file_get_contents('processed.txt'), true);
if (isset($_GET['char'])) {
$char = $_GET['char'];
$base = 'hkcs_m' . strtolower(substr(charToCodepoint($char), 2));
unset($ignoreComponents['u' . strtolower(substr(charToCodepoint($char), 2)) . '.txt']);
} else if (isset($_GET['name'])) {
$char = null;
$base = $_GET['name'];
unset($ignoreComponents[str_replace('hkcs_m', 'hkcs-', $base) . '.txt']);
} else {
$char = '冒';
$base = 'hkcs_m' . strtolower(substr(charToCodepoint($char), 2));
unset($ignoreComponents['u' . strtolower(substr(charToCodepoint($char), 2)) . '.txt']);
}
function getRelease() {
$release = [];
$files = glob('./release/ids-*.txt');
sort($files);
$files = array_reverse($files);
$filename = $files[0];
// $filename = './release/ids-' . date('Ymd') . '.txt';
if (file_exists($filename)) {
$contents = file($filename);
foreach ($contents as $row) {
list($codepoint, $char, $ids) = explode("\t", $row, 3);
$release[$codepoint] = trim($ids);
}
}
return $release;
}
?>
<!doctype html>
<style>
body{font:24px/1.4 sans-serif}
h1{margin:0;font-size:24px}
summary{font-size:16px;-webkit-user-select:none}
.info{font-size:12px;}
.warning{font-size:12px;font-weight:bold}
.diff{background:#ffeecc}
.missing{background:#ffff00;outline:1px solid red}
pre{line-height:1.4;overflow:auto;padding:4px}
</style>
<title><?
if ($char) echo 'Lookup ' . htmlspecialchars($char);
else echo 'Lookup ' . htmlspecialchars($base);
?></title>
<div style="position:fixed;top:0;left:0;width:100%;background:#fff;box-sizing:border-box;padding:10px;height:54px;border-bottom:1px solid #ccc">〓 ⿰ ⿱ ⿲ ⿳ ⿴ ⿵ ⿶ ⿷ ⿸ ⿹ ⿺ ⿻</div>
<div style="margin-top:61px"></div>
<h1><?
if ($char) echo 'Lookup for char: ' . htmlspecialchars($char);
else echo 'Lookup for component: ' . htmlspecialchars($base);
?></h1>
<?
$db = new DB();
$release = getRelease();
$processed = [];
if (empty($_GET['force_children'])) {
foreach ($ignoreComponents as $fileName => $list) {
foreach ($list as $item) {
$processed[$item] = $fileName;
}
}
}
$starttime = time();
$baseGlyph = $db->getGlyph($base);
$results = [];
$results[$char][] = [ $base, $baseGlyph->tryConvertToIDS() ];
?>
<details open>
<summary>Evaluation Log</summary>
<?
enumerateComponents($db, $results, $base, $processed);
?>
</details>
<?
function friendlyName($name) {
try {
$codepoint = nameToCodepoint($name);
$char = codepointToChar($codepoint);
} catch (Exception $e) {
$char = '??';
}
return htmlspecialchars($name . ' (' . $char . ')');
}
function isChar($name) {
try {
if (preg_match('#-p[0-9]{2}-s[0-9]{2}#', $name)) {
return null;
}
$codepoint = nameToCodepoint($name);
$char = codepointToChar($codepoint);
return $char;
} catch (Exception $e) {
return null;
}
}
function enumerateComponents($db, &$results, $base, &$processed) {
global $starttime, $release;
$componentNames = $db->getGlyphNames($base);
$components = [];
if (count($componentNames) == 0) {
$componentNames = [ $base ];
echo '<div class=warning>No positional variants found, forcefully adding base as positional variant.</div>';
}
foreach ($componentNames as $componentName) {
if (isset($processed[$componentName]) && $processed[$componentName] !== true) {
echo '<div class=info><b>Skipped ' . friendlyName($componentName) . '</b> - Existing in ' . $processed[$componentName] . '. <u>Add &force_children=1 to include.</u></div>';
continue;
}
$processed[$componentName] = true;
$list = $db->getGlyphsWithComponent($componentName);
if (count($componentNames) == 1 && count($list) == 0) {
echo '<div class=info>No positional variants found.</div>';
continue;
}
$components[$componentName] = $list;
echo '<div class=info>Found positional variant ' . friendlyName($componentName) . ' used by ' . count($list) . ' glyphs.</div>';
}
$i = 0;
foreach ($components as $componentName => $glyphs) {
foreach ($glyphs as $glyphData) {
if (!empty($_GET['skip_existing'])) {
try {
$codepoint = nameToCodepoint($glyphData[0]);
if (isset($release[$codepoint]) && $release[$codepoint] === $glyphData[1]) {
continue;
}
} catch (Exception $e) {
}
}
if (preg_match('#-p[0-9]{2}-s[0-9]{2}#', $glyphData[0])) {
echo '<div class=info>Skipped disassembling ' . friendlyName($glyphData[0]) . ' to IDS (included by containing ' . friendlyName($glyphData[2]) . ')</div>';
} else {
$i++;
$results[$glyphData[2]][] = $glyphData;
}
}
}
if ($i) {
echo '<div class=info style="margin-bottom:4px">Added ' . $i . ' results.</div>';
}
// Enumerate Glyphs
foreach ($components as $componentName => $glyphs) {
foreach ($glyphs as $glyphData) {
if (!empty($_GET['skip_existing'])) {
try {
$codepoint = nameToCodepoint($glyphData[0]);
if (isset($release[$codepoint]) && $release[$codepoint] === $glyphData[1]) {
continue;
}
} catch (Exception $e) {
}
}
if (isset($processed[$glyphData[0]])) {
try {
$char = codepointToChar(nameToCodepoint($glyphData[0]));
} catch (Exception $e) {
$char = '??';
}
if ($processed[$glyphData[0]] === true) {
echo '<div class=info>Skipped enumerating ' . friendlyName($glyphData[0]) . ' to avoid infinite loop.</div>';
} else {
echo '<div class=info>Skipped enumerating ' . friendlyName($glyphData[0]) . ' because its children exist in another file - ' . htmlspecialchars($processed[$glyphData[0]]) . '.</div>';
}
continue;
}
$processed[$glyphData[0]] = true;
if (preg_match('#^hkcs_m([0-9a-f]{4,5})#', $glyphData[0], $fileMatch) && file_exists('./u' . $fileMatch[1] . '.txt')) {
$skippedGlyphChar = isChar($glyphData[0]);
if ($skippedGlyphChar) {
echo '<div class=info>Skipped enumerating ' . friendlyName($glyphData[0]) . ' because it exists in another file (<a href="?char=' . $skippedGlyphChar . '&force_children=1" target=_blank>Enumerate</a>).</div>';
} else {
echo '<div class=info>Skipped enumerating ' . friendlyName($glyphData[0]) . ' because it exists in another file.</div>';
}
continue;
}
if ((time() - $starttime) > TIMEOUT) {
echo '<div style="color:red">Timed out before enumerating ' . friendlyName($glyphData[0]) . '</div>';
define('TIMED_OUT', 1);
return;
}
echo '<div class=info>Enumerating ' . friendlyName($glyphData[0]) . '.</div>';
echo '<div style="padding-left:10px;margin-bottom:4px;border-left:4px solid #fc9">';
enumerateComponents($db, $results, $glyphData[0], $processed);
echo '</div>';
}
}
}
?>
<? if (!defined('TIMED_OUT')) : ?>
<script>document.querySelector("details").open = false</script>
<? endif; ?>
<script src="page-toast.js"></script>
<page-toast id=toast></page-toast>
<script>
window.addEventListener('click', e => {
if (e.target.closest('pre#source') && window.getSelection().type === "Caret") {
var text = document.getElementById('source').innerText;
var elem = document.createElement("textarea");
document.body.appendChild(elem);
elem.value = text;
elem.select();
document.execCommand("copy");
document.body.removeChild(elem);
document.getElementById('toast').show('Copied to clipboard');
}
})
</script>
<div style="display:grid;grid-template-columns:minmax(20px, 1fr) minmax(20px, 1fr);gap:10px">
<?
echo '<pre id=source>';
foreach ($results as $base => $glyphs) {
echo '# ' . $base . "\r\n";
foreach ($glyphs as $glyphData) {
if (strpos($glyphData[0], 'hkcs_m') !== 0) {
echo $glyphData[0] . "\t" . $glyphData[0] . "\t" . htmlspecialchars($glyphData[1]) . "\r\n";
} else {
try {
$glyphCodepoint = nameToCodepoint($glyphData[0]);
echo $glyphCodepoint . "\t" . codepointToChar($glyphCodepoint) . "\t" . htmlspecialchars($glyphData[1]) . "\r\n";
} catch (Exception $e) {
}
}
}
echo "\r\n";
}
echo '</pre>';
?>
<?
echo '<pre>';
foreach ($results as $base => $glyphs) {
echo '# ' . $base . "\r\n";
foreach ($glyphs as $glyphData) {
if (strpos($glyphData[0], 'hkcs_m') !== 0) {
echo $glyphData[0] . "\t" . $glyphData[0] . "\t" . htmlspecialchars($glyphData[1]) . "\r\n";
} else {
$glyphCodepoint = nameToCodepoint($glyphData[0]);
if (isset($release[$glyphCodepoint]) && $release[$glyphCodepoint] !== $glyphData[1]) {
$class = 'diff';
} else if (!isset($release[$glyphCodepoint])) {
$class = 'missing';
} else {
$class = '';
}
try {
ob_start();
echo '<span class="' . $class . '">';
echo $glyphCodepoint . "\t" . codepointToChar($glyphCodepoint) . "\t" . htmlspecialchars(isset($release[$glyphCodepoint]) ? $release[$glyphCodepoint] : '') . "\r\n";
echo '</span>';
echo ob_get_clean();
} catch (Exception $e) {
ob_clean();
}
}
}
echo "\r\n";
}
echo '</pre>';
?>
</div>