forked from iamcal/emoji-data
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild_map.php
1100 lines (836 loc) · 25.9 KB
/
build_map.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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
error_reporting((E_ALL | E_STRICT) ^ E_NOTICE);
$dir = dirname(__FILE__);
include('catalog.php');
include('common.php');
#
# the softbank mapping in the catalog is bad, so use our custom version
#
$softbank_map = array();
$lines = file('data_softbank_map.txt');
foreach ($lines as $line){
$line = trim($line);
if (strlen($line) == 0) continue;
if (substr($line, 0, 1) == '#') continue;
list($softbank, $unified) = explode(' ', $line);
$softbank_map[StrToLower($unified)] = $softbank;
}
#
# load text mappings
#
$text = array(); # smile -> :)
$texts = array(); # smile -> [:) (: :D]
$lines = file('data_text_toascii.txt');
foreach ($lines as $line){
$line = trim($line);
if (strlen($line)){
$bits = preg_split('!\s+!', $line, 2);
$text[$bits[0]] = $bits[1];
}
}
$lines = file('data_text_toemoji.txt');
foreach ($lines as $line){
$line = trim($line);
if (strlen($line)){
$bits = preg_split('!\s+!', $line, 2);
$texts[$bits[1]][] = $bits[0];
}
}
#
# load the names of all simple characters
#
$names_map = array();
$fh = fopen('unicode/UnicodeData.txt', 'r');
while (($line = fgets($fh)) !== false){
list($cp, $name) = explode(';', $line);
$names_map[$cp] = $name;
}
fclose($fh);
#
# load category data and start building fully-qualified map
#
$category_map = array(); # CP => (Category-name, Global-order)
$subcategory_map = array(); # CP => (Subcategory-name, Global-order)
$qualified_map = array(); # non-qualified-CP => fully-qualified-CP
$categories = array(); # Category-name => Subcategory-name => []
$lines = file('unicode/emoji-test.txt');
$last_cat = '?';
$last_subcat = '?';
$p = 1;
foreach ($lines as $line){
if (!strlen(trim($line))) continue;
$line = rtrim($line);
if (substr($line, 0, 9) == '# group: '){
$last_cat = substr($line, 9);
$categories[$last_cat] = array();
}elseif (substr($line, 0, 12) == '# subgroup: '){
$last_subcat = substr($line, 12);
$categories[$last_cat][$last_subcat] = array();
}elseif (substr($line, 0, 1) == '#'){
continue;
}else{
list($cp) = explode(';', $line);
$cp = trim(StrToLower($cp));
$cp = preg_replace('!\s+!', '-', $cp);
$category_map[$cp] = array($last_cat, $p);
$subcategory_map[$cp] = array($last_subcat, $p);
$p++;
$cp_nq = str_replace('-fe0f', '', $cp);
if ($cp != $cp_nq) $qualified_map[$cp_nq] = $cp;
}
}
# patch in some CPs missing from the data file
$qualified_map['0023-20e3'] = '0023-fe0f-20e3';
$qualified_map['002a-20e3'] = '002a-fe0f-20e3';
$qualified_map['0030-20e3'] = '0030-fe0f-20e3';
$qualified_map['0031-20e3'] = '0031-fe0f-20e3';
$qualified_map['0032-20e3'] = '0032-fe0f-20e3';
$qualified_map['0033-20e3'] = '0033-fe0f-20e3';
$qualified_map['0034-20e3'] = '0034-fe0f-20e3';
$qualified_map['0035-20e3'] = '0035-fe0f-20e3';
$qualified_map['0036-20e3'] = '0036-fe0f-20e3';
$qualified_map['0037-20e3'] = '0037-fe0f-20e3';
$qualified_map['0038-20e3'] = '0038-fe0f-20e3';
$qualified_map['0039-20e3'] = '0039-fe0f-20e3';
$rev_qualified_map = array_flip($qualified_map);
#
# Fetching sequence names
#
echo "Fetching sequence names : ";
$sequence_names = array();
parse_unicode_specfile('unicode/emoji-sequences.txt', 'get_sequence_names');
parse_unicode_specfile('unicode/emoji-zwj-sequences.txt', 'get_sequence_names');
function get_sequence_names($fields, $comment){
$uni = StrToLower(str_replace(' ', '-', trim($fields[0])));
$name = trim($fields[2]);
$GLOBALS['sequence_names'][$uni] = $name;
}
echo "DONE\n";
#
# get versions for all emoji
#
echo "Fetching versions : ";
$versions = array();
parse_unicode_specfile('unicode/emoji-data.txt', 'get_versions');
parse_unicode_specfile('unicode/emoji-sequences.txt', 'get_versions');
parse_unicode_specfile('unicode/emoji-zwj-sequences.txt', 'get_versions');
function get_versions($fields, $comment){
$hex_lows = array();
if (strpos($fields[0], '..')){
list($a, $b) = explode('..', $fields[0]);
$a = hexdec($a);
$b = hexdec($b);
$cps = array();
for ($i=$a; $i<=$b; $i++){
$hex_lows[] = sprintf('%04x', $i);
}
}else{
$cps = explode(' ', $fields[0]);
$hex_lows = array(StrToLower(implode('-', $cps)));
}
if (preg_match('!^\s*E?(\d+\.\d+)!', $comment, $m)){
foreach ($hex_lows as $hex_low){
$GLOBALS['versions'][$hex_low] = $m[1];
}
}
}
echo "DONE\n";
#
# load emoji names
#
$short_names = array();
echo "Loading short names : ";
load_short_names('data_emoji_names.txt');
load_short_names('data_emoji_names_more.txt');
load_short_names('data_emoji_names_v4.txt');
load_short_names('data_emoji_names_v5.txt');
load_short_names('data_emoji_names_v11.txt');
load_short_names('data_emoji_names_v12.txt');
load_short_names('data_emoji_names_v13.txt');
load_short_names('data_emoji_names_v13_1.txt');
echo "DONE\n";
function load_short_names($file){
$raw = file($file);
foreach ($raw as $line){
if (strpos($line, '#') === 0) continue;
$line = trim($line);
if (!strlen($line)) continue;
if (preg_match('!{.*?}!', $line)){
$lines = expand_short_name_line($line);
foreach ($lines as $line){
load_short_name($line);
}
}else{
load_short_name($line);
}
}
}
#
# Token expansion in name files:
#
# {MAN/WOMAN} -> 1F468/1F469
# {MALE/FEMALE} -> 2642-FE0F/2640-FE0F
# {GENDER} -> male/female
# {M/W} -> man/woman
#
# {SKIN} - optional skin tone
# {SKIN!} - non-optional skin tone
# {SKIN2} - second (non-optional) skin tone, not allowing matches with the first skin tone
# {SKIN2x} - second (non-optional) skin tone, allowing matches with the first skin tone
#
function expand_short_name_line($line){
# expand gender first
if (preg_match('!{(MAN/WOMAN|MALE/FEMALE|GENDER|M/W)}!', $line)){
$a = str_replace(array('{MAN/WOMAN}', '{MALE/FEMALE}', '{GENDER}', '{M/W}'), array('1F468', '2642-FE0F', 'male', 'man'), $line);
$b = str_replace(array('{MAN/WOMAN}', '{MALE/FEMALE}', '{GENDER}', '{M/W}'), array('1F469', '2640-FE0F', 'female', 'woman'), $line);
return array_merge(
expand_short_name_line($a),
expand_short_name_line($b)
);
}
# expand optional and required skin tones
if (preg_match('#{SKIN(!|2|2x)?}#', $line, $m)){
return expand_skin_choices(array($line));
}
# give up
return array($line);
}
function expand_skin_choices($lines){
# given a list of lines, expand the list my multiplying out skin tones.
# we can take multiple passes if there are multiple matching tags.
$out = array();
foreach ($lines as $line){
if (preg_match('#{SKIN(!|2|2x)?}#', $line, $m)){
$temp = [];
if ($m[0] == '{SKIN}'){
$temp[] = preg_replace('#{SKIN}#', '', $line, 1);
}
if ($m[0] == '{SKIN2}'){
if (strpos($line, '-1F3FB') === false) $temp[] = preg_replace('#{SKIN2}#', '-1F3FB', $line, 1).':skin-2';
if (strpos($line, '-1F3FC') === false) $temp[] = preg_replace('#{SKIN2}#', '-1F3FC', $line, 1).':skin-3';
if (strpos($line, '-1F3FD') === false) $temp[] = preg_replace('#{SKIN2}#', '-1F3FD', $line, 1).':skin-4';
if (strpos($line, '-1F3FE') === false) $temp[] = preg_replace('#{SKIN2}#', '-1F3FE', $line, 1).':skin-5';
if (strpos($line, '-1F3FF') === false) $temp[] = preg_replace('#{SKIN2}#', '-1F3FF', $line, 1).':skin-6';
}elseif ($m[0] == '{SKIN2x}'){
$temp[] = preg_replace('#{SKIN2x}#', '-1F3FB', $line, 1).':skin-2';
$temp[] = preg_replace('#{SKIN2x}#', '-1F3FC', $line, 1).':skin-3';
$temp[] = preg_replace('#{SKIN2x}#', '-1F3FD', $line, 1).':skin-4';
$temp[] = preg_replace('#{SKIN2x}#', '-1F3FE', $line, 1).':skin-5';
$temp[] = preg_replace('#{SKIN2x}#', '-1F3FF', $line, 1).':skin-6';
}else{
$temp[] = preg_replace('#{SKIN!?}#', '-1F3FB', $line, 1).':skin-2';
$temp[] = preg_replace('#{SKIN!?}#', '-1F3FC', $line, 1).':skin-3';
$temp[] = preg_replace('#{SKIN!?}#', '-1F3FD', $line, 1).':skin-4';
$temp[] = preg_replace('#{SKIN!?}#', '-1F3FE', $line, 1).':skin-5';
$temp[] = preg_replace('#{SKIN!?}#', '-1F3FF', $line, 1).':skin-6';
}
$out = array_merge($out, expand_skin_choices($temp));
}else{
$out[] = $line;
}
}
return $out;
}
function load_short_name($line){
list($cp, $names) = explode(';', $line);
if (isset($GLOBALS['short_names'][$cp])){
$val = implode('/', $GLOBALS['short_names'][$cp]);
echo "ignoring def for $line (already set to {$val})\n";
}else{
$GLOBALS['short_names'][$cp] = explode('/', $names);
}
}
#
# load obsolete mappings
#
echo "Loading obsolete mappings : ";
$raw = file('data_obsoleted.txt');
$obsoleted_by = array();
$obsoletes = array();
foreach ($raw as $line){
list($line, $junk) = explode('#', $line);
list($key, $var) = explode(';', mb_strtoupper(trim($line)));
if (strlen($key)){
$obsoleted_by[$key] = $var;
$obsoletes[$var] = $key;
}
}
echo "DONE\n";
#
# list of skin variations
#
$skin_variation_suffixes = array(
1 => '1F3FB',
2 => '1F3FC',
3 => '1F3FD',
4 => '1F3FE',
5 => '1F3FF',
);
#
# build the official list of emoji with skin variations
#
echo "Loading emoji sequences : ";
$skin_variations = array();
parse_unicode_specfile('unicode/emoji-sequences.txt', function($fields, $comment){
$cps = explode(' ', $fields[0]);
$last = array_pop($cps);
if (in_array($last, $GLOBALS['skin_variation_suffixes'])){
$hex_low = StrToLower(implode('-', $cps));
$GLOBALS['skin_variations'][$hex_low] = 1;
}
});
echo "DONE\n";
#
# build the simple ones first
#
echo "Building emoji from base catalog : ";
$out = array();
$out_unis = array();
$out_skins = array();
$c = 0;
foreach ($catalog as $row){
$img_key = StrToLower(encode_points($row['unicode']));
$shorts = $short_names[mb_strtoupper($img_key)];
$name = $row['char_name']['title'];
if (preg_match("!^REGIONAL INDICATOR SYMBOL LETTERS !", $name)){
if (strlen($shorts[0]) == '2' && count($shorts) == 1){
array_unshift($shorts, 'flag-'.$shorts[0]);
}
}
add_row($img_key, $shorts, array(
'name' => $name,
'docomo' => encode_points($row['docomo' ]['unicode']),
'au' => encode_points($row['au' ]['unicode']),
'google' => encode_points($row['google' ]['unicode']),
));
$c++;
}
echo "DONE ($c)\n";
#
# were there any codepoints we have an image for, but no data for?
#
echo "Building emoji we have shortcodes for : ";
$c = 0;
foreach ($short_names as $uid => $names){
$img_key = StrToLower($uid);
if ($out_unis[$img_key]) continue;
if (substr($names[0], 0, 5) == 'flag-'){
add_row($img_key, $names, array(
'unified' => $uid,
'name' => "REGIONAL INDICATOR SYMBOL LETTERS ".mb_strtoupper(substr($names[0], 5)),
));
}else{
add_row($img_key, $names);
}
$c++;
}
echo "DONE ($c)\n";
#
# include everything from emoji-data.txt
#
$GLOBALS['skip_components'] = [];
$GLOBALS['missing_emoji'] = [];
echo "Checking emoji-data.txt for missing emoji : ";
parse_unicode_specfile('unicode/emoji-data.txt', function($fields, $comment){
global $out_unis, $out;
if (strpos($fields[0], '..')){
list($a, $b) = explode('..', $fields[0]);
$a = hexdec($a);
$b = hexdec($b);
$cps = array();
for ($i=$a; $i<=$b; $i++){
$cps[] = $i;
}
}else{
$cp = sprintf('%04x', hexdec($fields[0]));
$cps = array(hexdec($fields[0]));
}
foreach ($cps as $cp){
$hex_low = sprintf('%04x', $cp);
if ($out_unis[$hex_low]) continue;
if ($cp == 0x0023) continue; # number sign
if ($cp == 0x002A) continue; # asterisk
if ($cp >= 0x0030 && $cp <= 0x0039) continue; # 0-9
if ($cp >= 0x1F1E6 && $cp <= 0x1F1FF) continue; # flag letters
if ($fields[1] == 'Extended_Pictographic' || $fields[1] == 'Emoji_Component'){
$GLOBALS['skip_components'][$hex_low] = 1;
continue;
}
$GLOBALS['missing_emoji'][] = $hex_low;
}
});
foreach ($GLOBALS['missing_emoji'] as $hex_low){
if (isset($GLOBALS['skip_components'][$hex_low])) continue;
$hex_up = mb_strtoupper($hex_low);
$line = shell_exec("grep -e ^{$hex_up}\\; unicode/UnicodeData.txt");
$line = trim($line);
echo "\nno data for $cp/$hex_low from emoji-data.txt: $fields[0];$fields[1] : $line\n";
}
echo "DONE\n";
#
# check against emoji-sequences.txt and emoji-zwj-sequences.txt
#
echo "Checking emoji-(zwj-)?sequences.txt for missing emoji : ";
parse_unicode_specfile('unicode/emoji-sequences.txt', 'check_sequence');
parse_unicode_specfile('unicode/emoji-zwj-sequences.txt', 'check_sequence');
function check_sequence($fields, $comment){
$cps = explode(' ', $fields[0]);
$last = $cps[count($cps)-1];
$hex_low = StrToLower(implode('-', $cps));
# single codepoints are not a sequence
if (count($cps) == 1) return;
# is this already on the output list?
if ($GLOBALS['out_unis'][$hex_low]) return;
# is this already on the output list for skin tone variations?
if ($GLOBALS['out_skins'][$hex_low]) return;
# is this an explicit variation we've already added to the output map?
if ($GLOBALS['variations_handled'][$hex_low]) return;
echo "\nFound sequence not supported: $hex_low / {$comment}\n";
}
echo "DONE\n";
#
# for zwj sequences with skin tones in them, attach to non-toned version as variations
#
echo "Mapping skin tones to base versions : ";
$variations = array();
$out = array_filter($out, function($row){
if (strpos($row['short_name'], ':skin-') > 0){
$GLOBALS['variations'][] = $row;
return false;
}
return true;
});
$short_name_map = array();
foreach ($out as $k => $row){
foreach ($row['short_names'] as $n){
$short_name_map[$n] = $k;
}
}
$skin_codepoints_base = array(
'skin-2' => '1F3FB',
'skin-3' => '1F3FC',
'skin-4' => '1F3FD',
'skin-5' => '1F3FE',
'skin-6' => '1F3FF',
);
$skin_codepoints = array();
foreach ($skin_codepoints_base as $k1 => $v1){
$skin_codepoints[$k1] = $v1;
foreach ($skin_codepoints_base as $k2 => $v2){
$skin_codepoints[$k1.':'.$k2] = $v1.'-'.$v2;
}
}
foreach ($variations as $row){
list($name, $skin) = explode(':', $row['short_name'], 2);
$skin_cp = $skin_codepoints[$skin];
$key = $short_name_map[$name];
if (!$skin_cp){
echo "\nERROR: unable to find skin codepoint match for {$skin}\n";
}
if (isset($key) && isset($out[$key])){
$out[$key]['skin_variations'][$skin_cp] = simplify_row($row);
}else{
echo "\nERROR: unable to find parent for {$row['short_name']} / $name / $skin\n";
}
}
function simplify_row($row){
$out = array();
foreach ($row as $k => $v){
if (in_array($k, array('unified', 'non_qualified', 'image', 'sheet_x', 'sheet_y', 'added_in'))){
$out[$k] = $v;
}elseif (substr($k, 0, 8) == 'has_img_'){
$out[$k] = $v;
}
}
return $out;
}
echo "DONE\n";
#
# process obsoletes
#
echo "Attaching obsoletes to their bases : ";
$cp_map = array();
$cp_map_skin = array();
foreach ($out as $k => $row){
$cp_map[$row['unified']] = $k;
if (isset($row['skin_variations'])){
foreach ($row['skin_variations'] as $k2 => $row2){
$cp_map_skin[$row2['unified']] = array($k, $k2);
}
}
}
foreach ($obsoleted_by as $k => $v){
$idx = $cp_map[$k];
if ($idx){
$out[$idx]['obsoleted_by'] = $v;
}else{
$idx = $cp_map_skin[$k];
if (is_array($idx)){
$out[$idx[0]]['skin_variations'][$idx[1]]['obsoleted_by'] = $v;
}else{
echo "\nERROR: unable to find index for cp {$k} while adding obsoletes\n";
}
}
}
foreach ($obsoletes as $k => $v){
$idx = $cp_map[$k];
if ($idx){
$out[$idx]['obsoletes'] = $v;
}else{
$idx = $cp_map_skin[$k];
if (is_array($idx)){
$out[$idx[0]]['skin_variations'][$idx[1]]['obsoletes'] = $v;
}else{
echo "\nERROR: unable to find index for cp {$k} while adding obsoletes\n";
}
}
}
echo "DONE\n";
#
# check for duplicate short names
#
echo "Checking for duplicate shortnames : ";
$uniq = array();
foreach ($out as $k => $row){
if ($row['unified']){
$k = 'U+'.$row['unified'];
}else{
$k = "UNKNOWN/$k";
}
if (count($row['short_names'])){
foreach ($row['short_names'] as $sn){
if ($uniq[$sn]){
echo "\nDuplicate shortname :{$sn}: for {$uniq[$sn]} and $k\n";
}else{
$uniq[$sn] = $k;
}
}
}else{
echo "\nCharacter with no shortname: $k\n";
print_r($row);
exit;
}
}
echo "DONE\n";
#
# core functions
#
function add_row($img_key, $short_names, $props = array()){
# if we get passed a non-qualified version, swap it for the fq version
if (isset($GLOBALS['qualified_map'][$img_key])){
$img_key = $GLOBALS['qualified_map'][$img_key];
}
if (isset($GLOBALS['out_unis'][$img_key])){
echo "\nERROR: trying to set duplicate emoji $img_key\n";
print_r($short_names);
print_r($props);
foreach ($GLOBALS['out'] as $row){
if ($row['image'] == $img_key.'.png'){
echo "Matches:\n";
print_r($row);
}
}
exit;
}
if (!isset($props['name'])){
$props['name'] = $GLOBALS['names_map'][mb_strtoupper($img_key)];
}
if (!isset($props['unified'])){
$props['unified'] = mb_strtoupper($img_key);
}
$row = simple_row($img_key, $short_names, $props);
if ($row != null) {
$GLOBALS['out'][] = $row;
$GLOBALS['out_unis'][$img_key] = 1;
if ($row['non_qualified']) $GLOBALS['out_unis'][StrToLower($row['non_qualified'])] = 1;
}else{
echo "\nERROR: silently discardng $img_key / ".implode('/', $short_names)."\n";
}
}
function simple_row($img_key, $shorts, $props){
if (!is_array($shorts)) $shorts = array();
$short = count($shorts) ? $shorts[0] : null;
$added = $GLOBALS['versions'][$img_key];
$nq = null;
if ($GLOBALS['rev_qualified_map'][$img_key]){
$nq = mb_strtoupper($GLOBALS['rev_qualified_map'][$img_key]);
if (!$added){
$added = $GLOBALS['versions'][StrToLower($nq)];
}
}
$softbank = null;
if ($GLOBALS['softbank_map'][$img_key]){
$softbank = $GLOBALS['softbank_map'][$img_key];
unset($GLOBALS['softbank_map'][$img_key]);
}else{
if ($nq && $GLOBALS['softbank_map'][StrToLower($nq)]){
$softbank = $GLOBALS['softbank_map'][StrToLower($nq)];
unset($GLOBALS['softbank_map'][StrToLower($nq)]);
}
}
$category = $GLOBALS['category_map'][$img_key];
// TODO: Make sure every emoji has a category! Currently man/woman super
// hero/villian do not.
if (!$category) {
print "\nNot in category map! $img_key\n";
return null;
}
$subcategory = $GLOBALS['subcategory_map'][$img_key];
// TODO: Make sure every emoji has a subcategory!
if (!$subcategory) {
print "\nNot in subcategory map! $img_key\n";
return null;
}
if ($props['name']){
if (preg_match("!^REGIONAL INDICATOR SYMBOL LETTERS !", $props['name'])){
if ($GLOBALS['sequence_names'][$img_key]){
if (substr($GLOBALS['sequence_names'][$img_key], 0, 6) == 'flag: '){
$props['name'] = substr($GLOBALS['sequence_names'][$img_key], 6).' Flag';
}else{
$props['name'] = $GLOBALS['sequence_names'][$img_key].' Flag';
}
}
}
}else{
if ($GLOBALS['sequence_names'][$img_key]){
$props['name'] = mb_strtoupper($GLOBALS['sequence_names'][$img_key]);
}
}
$ret = array(
'name' => null,
'unified' => null,
'non_qualified' => $nq,
'docomo' => null,
'au' => null,
'softbank' => $softbank,
'google' => null,
'image' => $img_key.'.png',
'sheet_x' => 0,
'sheet_y' => 0,
'short_name' => $short,
'short_names' => $shorts,
'text' => $GLOBALS['text'][$short],
'texts' => $GLOBALS['texts'][$short],
'category' => is_array($category) ? $category[0] : null,
'subcategory' => is_array($subcategory) ? $subcategory[0] : null,
'sort_order' => is_array($category) ? $category[1] : null,
'added_in' => $added,
);
$ret['has_img_apple'] = file_exists("{$GLOBALS['dir']}/../img-apple-64/{$ret['image']}");
$ret['has_img_google'] = file_exists("{$GLOBALS['dir']}/../img-google-64/{$ret['image']}");
$ret['has_img_twitter'] = file_exists("{$GLOBALS['dir']}/../img-twitter-64/{$ret['image']}");
$ret['has_img_facebook'] = file_exists("{$GLOBALS['dir']}/../img-facebook-64/{$ret['image']}");
foreach ($props as $k => $v) $ret[$k] = $v;
$has_skin_vars = false;
$skin_vars_base = $img_key;
if ($GLOBALS['skin_variations'][$img_key]) $has_skin_vars = true;
if (file_exists("../img-apple-64/{$img_key}-1f3fb.png")) $has_skin_vars = true;
if ($nq){
if ($GLOBALS['skin_variations'][StrTolower($nq)]){
$has_skin_vars = true;
$skin_vars_base = StrTolower($nq);
}
}
if ($has_skin_vars){
$ret['skin_variations'] = array();
foreach ($GLOBALS['skin_variation_suffixes'] as $suffix){
$var_uni = mb_strtoupper($skin_vars_base.'-'.$suffix);
$var_img_key = StrToLower($skin_vars_base.'-'.$suffix);
$var_img = $var_img_key.'.png';
$var_nq = null;
if ($GLOBALS['rev_qualified_map'][$var_img_key]){
$var_nq = mb_strtoupper($GLOBALS['rev_qualified_map'][$var_img_key]);
}
$GLOBALS['out_skins'][$var_img_key] = 1;
$variation = array(
'unified' => $var_uni,
'non_qualified' => $var_nq,
'image' => $var_img,
'sheet_x' => 0,
'sheet_y' => 0,
'added_in' => $GLOBALS['versions'][StrToLower($var_uni)],
'has_img_apple' => file_exists("{$GLOBALS['dir']}/../img-apple-64/{$var_img}"),
'has_img_google' => file_exists("{$GLOBALS['dir']}/../img-google-64/{$var_img}"),
'has_img_twitter' => file_exists("{$GLOBALS['dir']}/../img-twitter-64/{$var_img}"),
'has_img_facebook' => file_exists("{$GLOBALS['dir']}/../img-facebook-64/{$var_img}"),
);
$ret['skin_variations'][$suffix] = $variation;
}
}
return $ret;
}
#
# patch up category and sort order fields - build the current sort maps
#
echo "Building category sort orders : ";
$missing_categories = array();
$shortname_map = array();
foreach ($out as $k => $row){
$shortname_map[$row['short_name']] = $k;
if ($row['category'] && $row['subcategory']){
$sort_key = sprintf('%05d', $row['sort_order']).'_'.$row['short_name'];
$categories[$row['category']][$row['subcategory']][$sort_key] = $row['short_name'];
}else{
$missing_categories[$row['short_name']] = 1;
}
}
foreach ($categories as $k1 => $subcats){
foreach ($subcats as $k2 => $v){
ksort($v);
$categories[$k1][$k2] = array_values($v);
}
}
#
# for obsoleted codepoints, steal category in one direction or another
#
$cp_map = array();
foreach ($out as $k => $row){
$cp_map[$row['unified']] = $k;
}
foreach ($missing_categories as $k => $junk){
$row = $out[$shortname_map[$k]];
if ($row['obsoletes']){
$idx = $cp_map[$row['obsoletes']];
$row2 = $out[$idx];
list($cat, $subcat) = find_assigned_cat($row2['short_name']);
if ($cat && $subcat){
$categories[$cat][$subcat][] = $row['short_name'];
unset($missing_categories[$row['short_name']]);
}
}
if ($row['obsoleted_by']){
$idx = $cp_map[$row['obsoleted_by']];
$row2 = $out[$idx];
list($cat, $subcat) = find_assigned_cat($row2['short_name']);
if ($cat && $subcat){
$categories[$cat][$subcat][] = $row['short_name'];
unset($missing_categories[$row['short_name']]);
}
}
}
function find_assigned_cat($short_name){
global $categories;
foreach ($categories as $cat => $subcats){
foreach ($subcats as $subcat => $ids){
foreach ($ids as $id){
if ($id == $short_name) return array($cat, $subcat);
}
}
}
return array(null, null);
}
echo "DONE\n";
#
# find missing categories
#
echo "Checking for missing categories : ";
foreach (array_keys($missing_categories) as $k){
$row = $out[$shortname_map[$k]];
echo "\nWARNING: no category for U+{$row['unified']} / :{$row['short_name']}:\n";
}
echo "DONE\n";
#
# apply global sort ordering back into the output hash
#
echo "Setting global sort order : ";
$z = 1;
foreach ($categories as $cat => $subcats){
foreach ($subcats as $subcat => $names){
foreach ($names as $p => $name){
$index = $shortname_map[$name];
$out[$index]['sort_order'] = $z++;
}
}
}
echo "DONE\n";
#
# output category info
#
echo "Saving categories : ";
$json_cat = json_encode($categories, JSON_PRETTY_PRINT);
if ($json_cat) {
$fh = fopen('../categories.json', 'w');
fwrite($fh, $json_cat);
fclose($fh);
} else {
$json_err = json_last_error();
echo "\nERROR: json encode error: {$json_err}\n";
}
echo "DONE\n";
#
# sort everything into a nice order
#
echo "Sorting output list : ";