/
Unicode.php
233 lines (193 loc) · 8.1 KB
/
Unicode.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
<?php
// `! lic=cc0`, con=<norkov@soletude.ca>`, rev=$Id$
// Used in Stagsi Cookbook: `@https://go.soletude.ca/stagsi/ex/YD`@
set_error_handler(function ($severity, $msg, $file, $line) {
throw new ErrorException($msg, 0, $severity, $file, $line);
}, -1);
if (!class_exists('IntlChar')) {
die('php_intl module is required.');
}
// `! +REPLACEME='C:.*'
copy('C:/Program Files/Stagsi/Skeleton/Stagsi.sqlite', 'Stagsi.sqlite');
// `! +REPLACEME='C:.*'
copy('C:/Program Files/Stagsi/Skeleton/Settings.json', 'Settings.json');
$settings = json_decode(file_get_contents('Settings.json'));
$settings->BitmapScalingMode = 'Fant';
$settings->ThumbFormat = 'png';
$perDir = $settings->FolderSize;
file_put_contents('Settings.json', json_encode($settings, JSON_PRETTY_PRINT));
$bom = "\xEF\xBB\xBF";
$rootTagID = 1;
$assoc = [
[
"CallMode" => 1,
"ExecutableList" => [
// `! +REPLACEME=nir\w*?
"nircmd clipboard readfile \"%1\"",
],
"Extension" => ".unicode"
],
];
file_put_contents('Associations.json', json_encode($assoc, JSON_PRETTY_PRINT));
$db = new PDO('sqlite:Stagsi.sqlite');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$selectTag = $db->prepare('SELECT RowId, * FROM Tags WHERE Title = :title AND ParentRowId = :parent');
$selectRootTag = $db->prepare("SELECT RowId, * FROM Tags WHERE Title = :title AND ParentRowId = $rootTagID");
$insertTag = $db->prepare("INSERT INTO Tags (Title, CreationTime, \"Order\", ParentRowId, IconPosition) VALUES (:title, STRFTIME('%s','NOW'), 0, :parent, 0)");
$insertObject = $db->prepare("INSERT INTO Objects (CreationTime, Random, Hash, Title, Format, ThumbCropX, ThumbCropY, ThumbCropWidth, ThumbCropHeight, FileSize, Codepoint) VALUES (STRFTIME('%s','NOW'), RANDOM() % 2147483647, :hash, :title, :format, 0, 0, 0, 0, :size, :codepoint)");
$insertObjectTag = $db->prepare('INSERT INTO ObjectTags (DataObject_RowId, Tag_RowId) VALUES (:object, :tag)');
$ver = join('.', IntlChar::getUnicodeVersion());
$db->exec("INSERT OR REPLACE INTO Stags SET Key = 'Unicode', Value = '$ver'");
try {
$db->exec("ALTER TABLE Objects ADD COLUMN Codepoint INT NOT NULL DEFAULT 0");
} catch (PDOException $e) {
// Already added, ignore.
}
list($formatTagID) = createTag(['Unicode File']);
$db->exec("UPDATE Tags SET System = '_unicode' WHERE RowId = $formatTagID");
$codepoints = [];
// Generation of 0x0000..0xFFFF (55346 codepoints) takes 30 minutes.
IntlChar::enumCharNames(0x0000, 0x0FFF, function ($codepoint) use (&$codepoints) {
$codepoints[] = $codepoint;
});
$count = 0;
foreach ($codepoints as $codepoint) {
if (++$count % 500 == 0) {
printf('%s / %s (%.1f%%)...%s', $count, count($codepoints),
$count / count($codepoints) * 100,
PHP_EOL);
}
$text = IntlChar::chr($codepoint);
$insertObject->bindValue(':hash', md5($bom.$text));
$title = IntlChar::charName($codepoint, IntlChar::UNICODE_CHAR_NAME);
$insertObject->bindValue(':title', $title);
$insertObject->bindValue(':format', 'unicode');
$insertObject->bindValue(':size', $bom.$text);
$insertObject->bindValue(':codepoint', $codepoint);
$insertObject->execute();
$objectID = $db->lastInsertId();
$objectGroup = floor($objectID / $perDir);
linkTagsTo($objectID, $formatTagID);
is_dir($objectGroup) or mkdir($objectGroup);
file_put_contents("$objectGroup/$objectID.unicode", $bom.$text);
drawText("$objectGroup/u$objectID.png", $text);
$age = IntlChar::charAge($codepoint);
$age[count($age) - 1] = 'Unicode v'.join('.', $age);
createTagFor($objectID, array_merge(['Age'], $age));
$digit = IntlChar::charDigitValue($codepoint);
$digit < 0 or createTagFor($objectID, ['Digit', $digit]);
$num = IntlChar::getNumericValue($codepoint);
$num === ((float) -123456789) or createTagFor($objectID, ['Numeric', $num]);
$dir = IntlChar::charDirection($codepoint);
createTagFor($objectID, ['Direction', prettyConst($dir, 'CHAR_DIRECTION_')]);
$names = [
'Name' => IntlChar::UNICODE_CHAR_NAME,
'Name/Alias' => IntlChar::CHAR_NAME_ALIAS,
'Name/Choice Count' => IntlChar::CHAR_NAME_CHOICE_COUNT,
];
foreach ($names as $tag => $const) {
$name = IntlChar::charName($codepoint, $const);
if ($name) {
createTagFor($objectID, array_merge(explode('/', $tag), [$name]));
}
}
$name = IntlChar::charName($codepoint, IntlChar::EXTENDED_CHAR_NAME);
if ($name !== IntlChar::charName($codepoint)) {
createTagFor($objectID, ['Name', 'Extended', $name]);
}
$cat = IntlChar::charType($codepoint);
createTagFor($objectID, ['Category', prettyConst($cat, 'CHAR_CATEGORY_')]);
$pair = IntlChar::getBidiPairedBracket($codepoint);
$pair === $codepoint and $pair = false;
$lower = IntlChar::tolower($codepoint);
$lower === $codepoint and $lower = false;
$upper = IntlChar::toupper($codepoint);
$upper === $codepoint and $upper = false;
if ($pair !== false or $lower !== false or $upper !== false) {
$apath = "Attachments/$objectID";
is_dir($apath) or mkdir($apath, 0750, true);
$pair === false or file_put_contents("$apath/Pair.unicode", $bom.IntlChar::chr($pair));
$lower === false or file_put_contents("$apath/Lower.unicode", $bom.IntlChar::chr($lower));
$upper === false or file_put_contents("$apath/Upper.unicode", $bom.IntlChar::chr($upper));
}
$block = IntlChar::getBlockCode($codepoint);
// E.g. block ID 279 has no constant.
createTagFor($objectID, ['Block', prettyConst($block, 'BLOCK_CODE_') ?: "#$block"]);
$class = IntlChar::getCombiningClass($codepoint);
$class and createTagFor($objectID, ['Combining Class', $class]);
$closure = IntlChar::getFC_NFKC_Closure($codepoint);
strlen($closure) and createTagFor($objectID, ['FC NFKC Closure', $closure]);
}
echo "Wrote $count codepoint objects.", PHP_EOL;
function createTagFor($objectID, array $path) {
$ids = createTag($path);
linkTagsTo($objectID, end($ids));
}
function createTag(array $path) {
global $rootTagID, $db, $selectTag, $selectRootTag, $insertTag;
$ids = [];
foreach ($path as $i => $title) {
if (!$i) {
$selectRootTag->bindValue(':title', $title);
$selectRootTag->execute();
$row = $selectRootTag->fetch();
} else {
$selectTag->bindValue(':title', $title);
$selectTag->bindValue(':parent', end($ids));
$selectTag->execute();
$row = $selectTag->fetch();
}
if ($row) {
$ids[] = $row['RowId'];
} else {
$insertTag->bindValue(':title', $title);
$insertTag->bindValue(':parent', end($ids) ?: $rootTagID);
$insertTag->execute();
$ids[] = $db->lastInsertId();
}
}
return $ids;
}
function linkTagsTo($objectID, $tagIDs) {
global $insertObjectTag;
foreach ((array) $tagIDs as $tagID) {
$insertObjectTag->bindValue(':object', $objectID);
$insertObjectTag->bindValue(':tag', $tagID);
$insertObjectTag->execute();
}
}
function prettyConst($value, $prefix) {
static $consts;
if (!$consts) {
$consts = (new ReflectionClass(IntlChar::class))->getConstants();
}
foreach ($consts as $cname => $cvalue) {
if (!strncmp($cname, $prefix, strlen($prefix)) and $cvalue === $value) {
return ucwords(strtolower(strtr(substr($cname, strlen($prefix)), '_', ' ')));
}
}
}
function drawText($file, $text) {
$im = imagecreatetruecolor($w = 128, $h = 128);
imagesavealpha($im, true);
// Windows Explorer would show 100% transparent pixels as fully opaque.
// E.g. if 127 is changed to 126, the image would appear truly transparent.
// But it's an Explorer's quirk - Photoshop does show proper 100% transparency.
imagefill($im, 0, 0, imagecolorallocatealpha($im, 255, 255, 255, 127));
$font = 'c:/windows/fonts/ARIALUNI.TTF';
$size = 48;
$box = imagettfbbox($size, 0, $font, $text);
// 64x64
// 1:7 --- 32:7
// | '@' |
// 1:37 --- 32:37
$x = round(($w - $box[2] + $box[0]) / 2);
$y = $h - round(($h - $box[1] + $box[7]) / 2);
//if ($text==='@') {
// foreach ($box as $i => &$ref) { $i % 2 and $ref += 30; }
//var_dump($text,$box,$x,$y);
//imagerectangle($im, $box[6], $box[7], $box[2], $box[3], imagecolorallocate($im, 255, 0, 0));
//}
imagettftext($im, $size, 0, $x, $y, imagecolorallocate($im, 0, 0, 0), $font, $text);
imagepng($im, $file);
}