-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathpatchEmoji.php
executable file
·197 lines (170 loc) · 4.62 KB
/
patchEmoji.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
#!/usr/bin/php
<?php
$emojiVersion = '15.1';
$ucdVersion = '15.1.0';
$emoji = [];
$file = wget('http://unicode.org/Public/' . $ucdVersion . '/ucd/emoji/emoji-data.txt');
$images = codepointsFromFile($file, 'Emoji_Presentation');
foreach (codepointsFromFile($file, 'Emoji') as $cp)
{
$utf8 = utf8($cp);
if (!isset($images[$cp]))
{
// Append U+FE0F to emoji without Emoji_Presentation=Yes
$utf8 .= "\xEF\xB8\x8F";
}
$emoji[] = $utf8;
}
$file = wget('http://unicode.org/Public/emoji/' . $emojiVersion . '/emoji-sequences.txt');
$file .= wget('http://unicode.org/Public/emoji/' . $emojiVersion . '/emoji-zwj-sequences.txt');
preg_match_all('(^[0-9A-F ]+)m', $file, $matches);
foreach ($matches[0] as $seq)
{
$utf8 = seqToUtf8(trim($seq));
$emoji[] = $utf8;
}
// Add all possible flag combinations. It makes the regexp simpler and covers any missing flag
for ($i = 0x1F1E6; $i <= 0x1F1FF; ++$i)
{
for ($j = 0x1F1E6; $j <= 0x1F1FF; ++$j)
{
$emoji[] = utf8($i) . utf8($j);
}
}
$allText = "\n";
$allXml = "<r>\n";
foreach ($emoji as $utf8)
{
if (strpos($utf8, "\xE2") === false && strpos($utf8, "\xEF") === false && strpos($utf8, "\xF0") === false)
{
echo bin2hex($utf8), " does not contain 0xE2, 0xEF or 0xF0. Parser.php would need to be updated.\n";
}
$hex = utf8ToHex($utf8);
$seq = removeMarks($hex);
$tseq = ltrim($hex, '0');
if (strpos($tseq, '-200d') === false)
{
$tseq = str_replace('-fe0f', '', $tseq);
}
$allText .= $utf8 . "\n";
$allXml .= '<EMOJI seq="' . $seq . '" tseq="' . $tseq . '">' . $utf8 . "</EMOJI>\n";
$allText .= ':' . $hex . ":\n";
$allXml .= '<EMOJI seq="' . $seq . '" tseq="' . $tseq . '">:' . $hex . ":</EMOJI>\n";
}
$allXml .= '</r>';
include __DIR__ . '/../src/Utils.php';
$allXml = s9e\TextFormatter\Utils::encodeUnicodeSupplementaryCharacters($allXml);
file_put_contents(__DIR__ . '/../tests/Plugins/Emoji/all.txt', $allText);
file_put_contents(__DIR__ . '/../tests/Plugins/Emoji/all.xml', $allXml);
include __DIR__ . '/../vendor/autoload.php';
$filepath = realpath(__DIR__ . '/../src/Plugins/Emoji/Parser.php');
file_put_contents(
$filepath,
preg_replace_callback(
'((protected \\$unicodeRegexp = ).*;)',
function ($m) use ($emoji)
{
$builder = new s9e\RegexpBuilder\Builder([
'input' => 'Bytes',
'output' => 'PHP'
]);
$regexp = '(' . $builder->build($emoji) . '(?!\\xEF\\xB8\\x8E)(?:\\xEF\\xB8\\x8F)?)S';
return $m[1] . var_export($regexp, true) . ';';
},
file_get_contents($filepath)
)
);
$filepath = realpath(__DIR__ . '/../src/Plugins/Emoji/Parser.js');
file_put_contents(
$filepath,
preg_replace_callback(
'((const unicodeRegexp = ).*;)',
function ($m) use ($emoji)
{
$builder = new s9e\RegexpBuilder\Builder([
'input' => 'Utf8',
'inputOptions' => ['useSurrogates' => true],
'output' => 'JavaScript',
'outputOptions' => ['case' => 'lower']
]);
$regexp = '/' . $builder->build($emoji) . '(?!\\ufe0e)\\ufe0f?/g';
return $m[1] . $regexp . ';';
},
file_get_contents($filepath)
)
);
die("Done.\n");
function codepointsFromFile($file, $type)
{
$regexp = '(^([0-9A-F]+)(\\..[0-9A-F]+)?\\s*;\\s*' . $type . '(?!\\w))m';
preg_match_all($regexp, $file, $matches, PREG_SET_ORDER);
$codepoints = [];
foreach ($matches as $m)
{
$start = hexdec($m[1]);
$end = (empty($m[2])) ? $start : hexdec(ltrim($m[2], '.'));
$cp = $start;
do
{
$codepoints[$cp] = $cp;
}
while (++$cp <= $end);
}
return $codepoints;
}
function wget($url, $prefix = '')
{
$filepath = sys_get_temp_dir() . '/' . $prefix . basename($url);
if (!file_exists($filepath))
{
copy(
'compress.zlib://' . $url,
$filepath,
stream_context_create(['http' => ['header' => 'Accept-Encoding: gzip']])
);
}
return file_get_contents($filepath);
}
function seqToUtf8($seq)
{
$str = '';
foreach (preg_split('([-_ ])', $seq) as $cp)
{
$str .= utf8(hexdec($cp));
}
return $str;
}
function utf8($cp)
{
return html_entity_decode('&#x' . dechex($cp) . ';', ENT_QUOTES, 'UTF-8');
}
function utf8ToHex($str)
{
$hex = [];
$i = 0;
do
{
$cp = ord($str[$i]);
if ($cp >= 0b11110000)
{
$cp = (($cp & 7) << 18) | ((ord($str[++$i]) & 63) << 12) | ((ord($str[++$i]) & 63) << 6) | (ord($str[++$i]) & 63);
}
elseif ($cp >= 0b11100000)
{
$cp = (($cp & 15) << 12) | ((ord($str[++$i]) & 63) << 6) | (ord($str[++$i]) & 63);
}
elseif ($cp >= 0b11000000)
{
$cp = (($cp & 15) << 6) | (ord($str[++$i]) & 63);
}
$hex[] = sprintf('%04x', $cp);
}
while (++$i < strlen($str));
return implode('-', $hex);
}
function removeMarks($seq)
{
$seq = str_replace('-fe0f', '', $seq);
$seq = str_replace('-200d', '', $seq);
return $seq;
}