forked from iamcal/emoji-data
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.php
87 lines (69 loc) · 2.37 KB
/
common.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
<?php
function encode_points($points){
$bits = array();
if (is_array($points)){
foreach ($points as $p){
$bits[] = sprintf('%04X', $p);
}
}
if (!count($bits)) return null;
return implode('-', $bits);
}
function utf8_bytes_to_uni_hex($utf8_bytes){
$bytes = array();
foreach (str_split($utf8_bytes) as $ch){
$bytes[] = ord($ch);
}
$codepoint = 0;
if (count($bytes) == 1) $codepoint = $bytes[0];
if (count($bytes) == 2) $codepoint = (($bytes[0] & 0x1F) << 6) | ($bytes[1] & 0x3F);
if (count($bytes) == 3) $codepoint = (($bytes[0] & 0x0F) << 12) | (($bytes[1] & 0x3F) << 6) | ($bytes[2] & 0x3F);
if (count($bytes) == 4) $codepoint = (($bytes[0] & 0x07) << 18) | (($bytes[1] & 0x3F) << 12) | (($bytes[2] & 0x3F) << 6) | ($bytes[3] & 0x3F);
if (count($bytes) == 5) $codepoint = (($bytes[0] & 0x03) << 24) | (($bytes[1] & 0x3F) << 18) | (($bytes[2] & 0x3F) << 12) | (($bytes[3] & 0x3F) << 6) | ($bytes[4] & 0x3F);
if (count($bytes) == 6) $codepoint = (($bytes[0] & 0x01) << 30) | (($bytes[1] & 0x3F) << 24) | (($bytes[2] & 0x3F) << 18) | (($bytes[3] & 0x3F) << 12) | (($bytes[4] & 0x3F) << 6) | ($bytes[5] & 0x3F);
$str = sprintf('%x', $codepoint);
return str_pad($str, 4, '0', STR_PAD_LEFT);
}
function utf8_bytes_to_hex($str){
mb_internal_encoding('UTF-8');
$out = array();
while (strlen($str)){
$out[] = utf8_bytes_to_uni_hex(mb_substr($str, 0, 1));
$str = mb_substr($str, 1);
}
return implode('-', $out);
}
function parse_unicode_specfile($filename, $callback){
$lines = file($filename);
foreach ($lines as $line){
$p = strpos($line , '#');
$comment = '';
if ($p !== false){
$comment = trim(substr($line, $p+1));
$line = substr($line, 0, $p);
}
$line = trim($line);
if (!strlen($line)) continue;
$bits = explode(';', $line);
$fields = array();
foreach ($bits as $bit){
$fields[] = trim($bit);
}
call_user_func($callback, $fields, $comment);
}
}
function cp_to_utf8_bytes($v){
if ($v < 128){
return chr($v);
}
if ($v < 2048){
return chr(($v >> 6) + 192) . chr(($v & 63) + 128);
}
if ($v < 65536){
return chr(($v >> 12) + 224) . chr((($v >> 6) & 63) + 128) . chr(($v & 63) + 128);
}
if ($v < 2097152){
return chr(($v >> 18) + 240) . chr((($v >> 12) & 63) + 128) . chr((($v >> 6) & 63) + 128) . chr(($v & 63) + 128);
}
die("can't create codepoints for $v");
}