-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathcjk-gs-integrate.pl
executable file
·2907 lines (2656 loc) · 86.9 KB
/
cjk-gs-integrate.pl
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
#!/usr/bin/env perl
#
# cjk-gs-integrate - setup Ghostscript for CID/TTF CJK fonts
#
# Copyright 2015-2021 by Norbert Preining
# Copyright 2016-2021 by Japanese TeX Development Community
#
# This work is based on research and work by (in alphabetical order)
# Masamichi Hosoda
# Yusuke Kuroki
# Yusuke Terada
# Bruno Voisin
# Munehiro Yamamoto
# Hironobu Yamashita
# and the Japanese TeX Q&A wiki page
#
# This file is licensed under GPL version 3 or any later version.
# For copyright statements see end of file.
#
# For development see
# https://github.com/texjporg/cjk-gs-support
#
# LIMITATIONS:
# - Running the script (with default mode = actual setup/removing operations)
# always overwrites "cidfmap.local" and "cidfmap.aliases" without asking,
# whose file names might be common enough. If you choose to run the script,
# leave these files untouched. (Do NOT edit these files by yourself!)
# (This note also applies to MacTeX pre-shipped configuration files.)
#
# TODO:
# - interoperability with kanji-config-updmap
#
# Note that symlink names should be consistent with ptex-fontmaps!
use Getopt::Long qw(:config no_autoabbrev ignore_case_always);
use File::Basename;
use File::Path qw(make_path);
use Cwd 'abs_path';
use strict;
use warnings;
use utf8;
use feature 'state';
use Encode;
use Encode::Alias;
my $debug_msg_before_init;
init_encode_locale ();
binmode (STDIN, ':encoding(console_in)');
binmode (STDOUT, ':encoding(console_out)');
binmode (STDERR, ':encoding(console_out)');
@ARGV = map{ decode('locale', $_) }@ARGV;
(my $prg = basename(decode('locale', $0))) =~ s/\.pl$//;
my $version = '$VER$';
if (win32()) {
# some perl functions (symlink, -l test) does not work
print_warning("Sorry, we have only partial support for Windows!\n");
require Win32::API;
Win32::API->import ();
require File::Compare;
File::Compare->import ();
}
# The followings are installed by ptex-fontmaps (texjporg):
# * 2004-H
# * 2004-V
# The followings are created by Adobe but not considered official
# (see https://forums.adobe.com/thread/537415)
# * GB-RKSJ-H
# * GBT-RKSJ-H
# * KSC-RKSJ-H
# All others are provided in the latest Adobe CMap Resources:
# https://github.com/adobe-type-tools/cmap-resources
my %encode_list = (
Japan => [ qw/
2004-H
2004-V
78-EUC-H
78-EUC-V
78-H
78-RKSJ-H
78-RKSJ-V
78-V
78ms-RKSJ-H
78ms-RKSJ-V
83pv-RKSJ-H
90ms-RKSJ-H
90ms-RKSJ-V
90msp-RKSJ-H
90msp-RKSJ-V
90pv-RKSJ-H
90pv-RKSJ-V
Add-H
Add-RKSJ-H
Add-RKSJ-V
Add-V
Adobe-Japan1-0
Adobe-Japan1-1
Adobe-Japan1-2
Adobe-Japan1-3
Adobe-Japan1-4
Adobe-Japan1-5
Adobe-Japan1-6
Adobe-Japan1-7
EUC-H
EUC-V
Ext-H
Ext-RKSJ-H
Ext-RKSJ-V
Ext-V
H
Hankaku
Hiragana
Identity-H
Identity-V
Katakana
NWP-H
NWP-V
RKSJ-H
RKSJ-V
Roman
UniJIS-UCS2-H
UniJIS-UCS2-HW-H
UniJIS-UCS2-HW-V
UniJIS-UCS2-V
UniJIS-UTF16-H
UniJIS-UTF16-V
UniJIS-UTF32-H
UniJIS-UTF32-V
UniJIS-UTF8-H
UniJIS-UTF8-V
UniJIS2004-UTF16-H
UniJIS2004-UTF16-V
UniJIS2004-UTF32-H
UniJIS2004-UTF32-V
UniJIS2004-UTF8-H
UniJIS2004-UTF8-V
UniJISPro-UCS2-HW-V
UniJISPro-UCS2-V
UniJISPro-UTF8-V
UniJISX0213-UTF32-H
UniJISX0213-UTF32-V
UniJISX02132004-UTF32-H
UniJISX02132004-UTF32-V
V
WP-Symbol
/ ],
GB => [ qw/
Adobe-GB1-0
Adobe-GB1-1
Adobe-GB1-2
Adobe-GB1-3
Adobe-GB1-4
Adobe-GB1-5
GB-EUC-H
GB-EUC-V
GB-H
GB-RKSJ-H
GB-V
GBK-EUC-H
GBK-EUC-V
GBK2K-H
GBK2K-V
GBKp-EUC-H
GBKp-EUC-V
GBT-EUC-H
GBT-EUC-V
GBT-H
GBT-RKSJ-H
GBT-V
GBTpc-EUC-H
GBTpc-EUC-V
GBpc-EUC-H
GBpc-EUC-V
Identity-H
Identity-V
UniGB-UCS2-H
UniGB-UCS2-V
UniGB-UTF16-H
UniGB-UTF16-V
UniGB-UTF32-H
UniGB-UTF32-V
UniGB-UTF8-H
UniGB-UTF8-V
/ ],
CNS => [ qw/
Adobe-CNS1-0
Adobe-CNS1-1
Adobe-CNS1-2
Adobe-CNS1-3
Adobe-CNS1-4
Adobe-CNS1-5
Adobe-CNS1-6
Adobe-CNS1-7
B5-H
B5-V
B5pc-H
B5pc-V
CNS-EUC-H
CNS-EUC-V
CNS1-H
CNS1-V
CNS2-H
CNS2-V
ETHK-B5-H
ETHK-B5-V
ETen-B5-H
ETen-B5-V
ETenms-B5-H
ETenms-B5-V
HKdla-B5-H
HKdla-B5-V
HKdlb-B5-H
HKdlb-B5-V
HKgccs-B5-H
HKgccs-B5-V
HKm314-B5-H
HKm314-B5-V
HKm471-B5-H
HKm471-B5-V
HKscs-B5-H
HKscs-B5-V
Identity-H
Identity-V
UniCNS-UCS2-H
UniCNS-UCS2-V
UniCNS-UTF16-H
UniCNS-UTF16-V
UniCNS-UTF32-H
UniCNS-UTF32-V
UniCNS-UTF8-H
UniCNS-UTF8-V
/ ],
Korea => [ qw/
Adobe-Korea1-0
Adobe-Korea1-1
Adobe-Korea1-2
Identity-H
Identity-V
KSC-EUC-H
KSC-EUC-V
KSC-H
KSC-Johab-H
KSC-Johab-V
KSC-RKSJ-H
KSC-V
KSCms-UHC-H
KSCms-UHC-HW-H
KSCms-UHC-HW-V
KSCms-UHC-V
KSCpc-EUC-H
KSCpc-EUC-V
UniKS-UCS2-H
UniKS-UCS2-V
UniKS-UTF16-H
UniKS-UTF16-V
UniKS-UTF32-H
UniKS-UTF32-V
UniKS-UTF8-H
UniKS-UTF8-V
/ ],
KR => [ qw/
Adobe-KR-0
Adobe-KR-1
Adobe-KR-2
Adobe-KR-3
Adobe-KR-4
Adobe-KR-5
Adobe-KR-6
Adobe-KR-7
Adobe-KR-8
Adobe-KR-9
Identity-H
Identity-V
UniAKR-UTF16-H
UniAKR-UTF32-H
UniAKR-UTF8-H
/ ] );
#
# location where links to fonts in texmf are created, relative to TEXMF
my $otf_pathpart = "fonts/opentype/cjk-gs-integrate";
my $ttf_pathpart = "fonts/truetype/cjk-gs-integrate";
# location where cidfmap, cidfmap.local and cidfmap.aliases are placed
# when found gs is tlgs (win32), then files will be placed in lib/ instead of Resource/Init/
my $cidfmap_pathpart = "Init/cidfmap";
my $cidfmap_local_pathpart = "Init/cidfmap.local";
my $cidfmap_aliases_pathpart = "Init/cidfmap.aliases";
# support for ps2otfps by Akira Kakuto
my $akotfps_pathpart = "dvips/ps2otfps";
my $akotfps_datafilename = "psnames-for-otf";
my $akotfps_datacontent = '';
# dump output for data file (for easy editing for users)
my $dump_datafile = "$prg-data.dat";
my $opt_output;
my $opt_fontdef;
my @opt_fontdef_add;
my @opt_aliases;
my $opt_filelist;
my $opt_texmflink;
my $opt_akotfps;
my $opt_force = 0;
my $opt_remove = 0;
my $opt_cleanup = 0;
my $opt_hardlink = 0;
my $opt_winbatch;
my $opt_dump_data;
my $opt_only_aliases = 0;
my $opt_listaliases = 0;
my $opt_listallaliases = 0;
my $opt_listfonts = 0;
my $opt_info = 0;
my $opt_machine = 0;
my $opt_strictpsname = 0;
my $dry_run = 0;
my $opt_quiet = 0;
my $opt_debug = 0;
my $opt_help = 0;
my $opt_markdown = 0;
if (! GetOptions(
"o|output=s" => \$opt_output,
"f|fontdef=s" => \$opt_fontdef,
"fontdef-add=s" => \@opt_fontdef_add,
"a|alias=s" => \@opt_aliases,
"filelist=s" => \$opt_filelist,
"link-texmf:s" => \$opt_texmflink,
"otfps:s" => \$opt_akotfps,
"force" => \$opt_force,
"remove" => \$opt_remove,
"cleanup" => \$opt_cleanup,
"hardlink" => \$opt_hardlink,
"winbatch:s" => \$opt_winbatch,
"dump-data:s" => \$opt_dump_data,
"only-aliases" => \$opt_only_aliases,
"list-aliases" => \$opt_listaliases,
"list-all-aliases" => \$opt_listallaliases,
"list-fonts" => \$opt_listfonts,
"info" => \$opt_info,
"machine-readable" => \$opt_machine,
"strict-psname" => \$opt_strictpsname, # hidden option for debugging
"n|dry-run" => \$dry_run,
"q|quiet" => \$opt_quiet,
"d|debug+" => \$opt_debug,
"h|help" => \$opt_help,
"markdown" => \$opt_markdown,
"v|version" => sub { print &version(); exit(0); }, ) ) {
die "Try \"$0 --help\" for more information.\n";
}
if ($debug_msg_before_init) {
print_debug ($debug_msg_before_init);
}
sub win32 { return ($^O=~/^MSWin(32|64)$/i); }
sub macosx { return ($^O=~/^darwin$/i); }
my $nul = (win32() ? 'nul' : '/dev/null') ;
my $sep = (win32() ? ';' : ':');
my %fontdb;
my %aliases;
my %user_aliases;
if ($opt_help || $opt_markdown) {
Usage();
exit(0);
}
# check for the existence of kpsewhich, otherwise we cannot do anything
if (system("kpsewhich --version >$nul 2>&1 <$nul" ) != 0) {
print_error("We need `kpsewhich' being installed! Exiting.\n");
exit(1);
}
if ($opt_debug >= 2) {
require Data::Dumper;
no warnings qw(once);
$Data::Dumper::Indent = 1;
}
my $zrlistttc = kpse_miscfont("zrlistttc.lua");
my $zrlistttc_available;
my $cmdl = "texlua $zrlistttc 2>$nul";
$cmdl = encode('locale', $cmdl);
chomp(my $zrlistttc_help = `$cmdl`);
if ($?) {
if ($opt_strictpsname) {
print_error("The script 'zrlistttc.lua' not found, cannot proceed!\n");
exit(1);
}
# show info only for debugging
print_debug("The script 'zrlistttc.lua' not found.\n");
print_debug("Sorry, we can't be safe enough to distinguish\n");
print_debug("uppercase / lowercase file names.\n");
$zrlistttc_available = 0;
} else {
$zrlistttc_available = 1;
}
if (macosx()) {
# due to frequent incompatible changes in font file names by Apple,
# our built-in database doesn't support OS X 10.11 El Capitan or
# later versions
my $macos_ver = `sw_vers -productVersion`;
my $macos_ver_major = $macos_ver;
$macos_ver_major =~ s/^(\d+)\.(\d+).*/$1/;
my $macos_ver_minor = $macos_ver;
$macos_ver_minor =~ s/^(\d+)\.(\d+).*/$2/;
if ($macos_ver_major>=11 || ($macos_ver_major==10 && $macos_ver_minor>=8)) {
if (!$opt_cleanup && !$opt_fontdef && !@opt_fontdef_add) { # if built-in only
print_warning("Our built-in database does not support recent\n");
print_warning("versions of Mac OS (10.8 Mountain Lion or later)!\n");
print_warning("If you want to use Hiragino fonts bundled with\n");
print_warning("your OS, obtain external database file and\n");
print_warning("specify it with --fontdef-add option!\n");
print_warning("I'll continue with my built-in database ...\n");
}
}
}
if (defined($opt_texmflink)) {
my $foo;
if ($opt_texmflink eq '') {
# option was passed but didn't receive a value
# -> use TEXMFLOCAL
$foo = `kpsewhich -var-value=TEXMFLOCAL`;
# We assume that the output of kpsewhich is
# the same as perl's locale (or active code page).
decode('locale', $foo);
chomp($foo);
} else {
# option was passed with an argument
# -> use it
$foo = $opt_texmflink;
}
$opt_texmflink = $foo;
}
if (defined($opt_akotfps)) {
my $foo;
if ($opt_akotfps eq '') {
if (defined($opt_texmflink)) {
$foo = $opt_texmflink;
} else {
$foo = `kpsewhich -var-value=TEXMFLOCAL`;
# We assume that the output of kpsewhich is
# the same as perl's locale (or active code page).
decode('locale', $foo);
chomp($foo);
}
} else {
$foo = $opt_akotfps;
}
$opt_akotfps = $foo;
}
if (defined($opt_winbatch)) {
print_warning("ignoring --winbatch option due to being obsolete\n");
}
if ($opt_hardlink) {
print_warning("ignoring --hardlink option due to being obsolete\n");
}
if (defined($opt_dump_data)) {
if ($opt_dump_data ne '') {
$dump_datafile = $opt_dump_data;
}
$opt_dump_data = 1;
unlink encode('locale_fs', $dump_datafile)
if (-f encode('locale_fs', $dump_datafile));
} else {
$opt_dump_data = 0;
}
if ($opt_cleanup) {
$opt_remove = 1;
}
if ($opt_info) {
$opt_listfonts = 1;
$opt_listaliases = 1;
}
# check exclusive options; unsafe due to make_all_available()
if ($opt_listallaliases && $opt_listaliases) {
print_error("Both --list-all-aliases and --list-aliases!? I'm confused!\n");
exit(1);
}
if ($opt_listallaliases && $opt_listfonts) {
print_error("Options --list-all-aliases and --list-fonts cannot be used at the same time!\n");
exit(1);
}
if ($opt_cleanup && $opt_listfonts) {
print_error("Options --cleanup and --list-fonts cannot be used at the same time!\n");
exit(1);
}
if ($opt_cleanup && $opt_listaliases) {
print_error("Options --cleanup and --list-aliases cannot be used at the same time!\n");
exit(1);
}
main(@ARGV);
#
# only sub definitions from here on
#
sub main {
# first, read font database to obtain %fontdb
print_info("reading font database ...\n");
read_font_database();
if ($opt_dump_data) {
# with --dump-data, dump only effective database and exit
dump_font_database();
if (-f encode('locale_fs', $dump_datafile)) {
print_info("*** Data dumped to $dump_datafile ***\n");
exit(0);
} else {
print_error("Failed to dump data to $dump_datafile!\n");
exit(1);
}
}
# second, determine non-otf link name
# this is actually required only by info_found_fonts() and do_nonotf_fonts()
# operations, but it does no harm for other cases too
determine_nonotf_link_name(); # see comments there
# set 'available' flags and 'type' by kpsewhich search
# if $opt_cleanup or $opt_listallaliases is given, treat all files
# in the database as if they were actually available as OTF
if (!$opt_cleanup && !$opt_listallaliases) {
print_info("checking for files ...\n");
check_for_files();
} else {
make_all_available();
}
# obtain %aliases and %user_aliases
compute_aliases();
# informative operations
if ($opt_listfonts) {
info_found_fonts();
}
if ($opt_listaliases || $opt_listallaliases) {
info_list_aliases();
}
exit(0) if ($opt_listfonts || $opt_listaliases || $opt_listallaliases);
# if $opt_machine is still alive after the above exit(0), it's useless
if ($opt_machine) {
print_error("Option --machine-readable should be used with at least one of the followings:\n");
print_error(" --list-aliases, --list-all-aliases, --list-fonts, --info\n");
print_error("terminating.\n");
exit(1);
}
# do actual setup/removing operations
if (!$opt_output) {
print_info("searching for Ghostscript resource\n");
my $gsres = find_gs_resource();
if (!$gsres) {
print_error("Cannot find Ghostscript, terminating!\n");
exit(1);
} else {
$opt_output = $gsres;
}
}
if (! -d encode('locale_fs', $opt_output)) {
$dry_run || mkdir(encode('locale_fs', $opt_output)) ||
die("Cannot create directory $opt_output: $!");
}
if ($opt_cleanup) {
print_info("going to clean up $opt_output\n");
} else {
print_info("output is going to $opt_output\n");
}
if (!$opt_only_aliases) {
if ($opt_cleanup) {
# all font types are handled at the same time
print_info("cleaning up all links, snippets and cidfmap.local ...\n");
do_all_fonts();
} else {
# OTF and TTF/TTC/OTC must be handled separately, depending on the found files
print_info(($opt_remove ? "removing" : "generating") . " links and snippets for CID fonts ...\n");
do_otf_fonts();
print_info(($opt_remove ? "removing" : "generating") . " links, snippets and cidfmap.local for non-CID fonts ...\n");
do_nonotf_fonts();
}
}
print_info(($opt_remove ? "removing" : "generating") . " snippets and cidfmap.aliases for font aliases ...\n");
do_aliases();
do_cmaps();
write_akotfps_datafile() if $opt_akotfps;
if ($opt_texmflink && !$dry_run) {
print_info("running mktexlsr ...\n");
system("mktexlsr");
}
print_info("finished\n");
}
sub do_all_fonts {
# try to clean up all possible links/snippets/cidfmap which could have been
# generated in the previous runs
# previous versions of cjk-gs-integrate included following bugs:
# * the database sometimes identified GB/CNS classes wrongly
# * symlink names were sometimes invalid (some of which contained
# white-spaces, due to the absence of proper database entry) or
# inconsistent with ptex-fontmaps (Name <-> PSName or redundant Filename)
# * confused symlinks between TTF/TTC/OTC (including ttf <-> ttc links)
# also, current version generates OTC links into $otf_pathpart instead of
# $ttf_pathpart, which was not true in the older versions
# we'd like to clean up all such files
my $fontdest = "$opt_output/Font";
my $ciddest = "$opt_output/CIDFont";
my $cidfsubst = "$opt_output/CIDFSubst";
for my $k (sort keys %fontdb) {
#
# remove snippets: note that $class = $fontdb{$k}{'class'} is not enough
# due to previous bugs
for my $class (%encode_list) {
for my $enc (@{$encode_list{$class}}) {
unlink encode('locale_fs', "$fontdest/$k-$enc")
if (-f encode('locale_fs', "$fontdest/$k-$enc"));
}
}
#
# remove links; borrow link_font operation here for convenience
# since we don't need target in cleanup mode, initialize with stub "none"
#
# for OTF/TTF fonts, first remove both $k[.otf,ttf] and $fontdb{$k}{'origname'}[.otf]
# the links $k.otf and $fontdb{$k}{'origname'} are coming from previous bugs,
# and the links $k.ttf are (sometimes) coming from inconsistency
if ($fontdb{$k}{'origname'}) { # this test skips alias-only fonts (e.g. Ryumin-Light)
link_font("none", $ciddest, $k);
link_font("none", $cidfsubst, "$k.ttf");
link_font("none", "$opt_texmflink/$otf_pathpart", "$k.otf")
if $opt_texmflink;
link_font("none", "$opt_texmflink/$ttf_pathpart", "$k.ttf")
if $opt_texmflink;
link_font("none", $ciddest, $fontdb{$k}{'origname'});
link_font("none", "$opt_texmflink/$otf_pathpart", "$fontdb{$k}{'origname'}.otf")
if $opt_texmflink;
}
# for OTF/TTF/TTC/OTC fonts, loop through all file candidates
for my $f (keys %{$fontdb{$k}{'files'}}) {
# check for subfont extension
my $foo = $f;
$foo =~ s/^(.*)\(\d*\)$/$1/;
link_font("none", $cidfsubst, "$foo");
link_font("none", "$opt_texmflink/$otf_pathpart", "$foo") if $opt_texmflink;
link_font("none", "$opt_texmflink/$ttf_pathpart", "$foo") if $opt_texmflink;
}
}
update_master_cidfmap('cidfmap.local');
# we are in cleanup mode, also remove cidfmap.local itself
if (-f encode('locale_fs', "$opt_output/$cidfmap_local_pathpart")) {
unlink encode('locale_fs', "$opt_output/$cidfmap_local_pathpart");
}
}
sub do_otf_fonts {
my $fontdest = "$opt_output/Font";
my $ciddest = "$opt_output/CIDFont";
make_dir($fontdest, "cannot create CID snippets there!");
make_dir($ciddest, "cannot link CID fonts there!");
make_dir("$opt_texmflink/$otf_pathpart",
"cannot link fonts to it!")
if $opt_texmflink;
for my $k (sort keys %fontdb) {
if ($fontdb{$k}{'available'} && $fontdb{$k}{'type'} eq 'OTF') {
generate_font_snippet($fontdest,
$k, $fontdb{$k}{'class'}, $fontdb{$k}{'target'});
link_font($fontdb{$k}{'target'}, $ciddest, $k);
link_font($fontdb{$k}{'target'}, "$opt_texmflink/$otf_pathpart", "$fontdb{$k}{'origname'}.otf")
if $opt_texmflink;
}
}
}
sub do_nonotf_fonts {
my $fontdest = "$opt_output/Font";
my $cidfsubst = "$opt_output/CIDFSubst";
my $outp = '';
make_dir($fontdest, "cannot create CID snippets there!");
make_dir($cidfsubst, "cannot link TTF fonts there!");
make_dir("$opt_texmflink/$ttf_pathpart",
"cannot link fonts to it!")
if $opt_texmflink;
for my $k (sort keys %fontdb) {
if ($fontdb{$k}{'available'} && $fontdb{$k}{'type'} eq 'TTF') {
$outp .= generate_cidfmap_entry($k, $fontdb{$k}{'class'}, $fontdb{$k}{'ttfname'}, -1);
link_font($fontdb{$k}{'target'}, $cidfsubst, $fontdb{$k}{'ttfname'});
link_font($fontdb{$k}{'target'}, "$opt_texmflink/$ttf_pathpart", $fontdb{$k}{'ttfname'})
if $opt_texmflink;
} elsif ($fontdb{$k}{'available'} && $fontdb{$k}{'type'} eq 'TTC') {
$outp .= generate_cidfmap_entry($k, $fontdb{$k}{'class'}, $fontdb{$k}{'ttcname'}, $fontdb{$k}{'subfont'});
link_font($fontdb{$k}{'target'}, $cidfsubst, $fontdb{$k}{'ttcname'});
link_font($fontdb{$k}{'target'}, "$opt_texmflink/$ttf_pathpart", $fontdb{$k}{'ttcname'})
if $opt_texmflink;
} elsif ($fontdb{$k}{'available'} && $fontdb{$k}{'type'} eq 'OTC') {
# currently Ghostscript does not have OTC support; we don't know what to do
print_debug("gs does not support OTC, not creating gs resource for $k\n");
link_font($fontdb{$k}{'target'}, "$opt_texmflink/$otf_pathpart", $fontdb{$k}{'otcname'})
if $opt_texmflink;
}
}
return if $dry_run;
if ($outp) {
if (! -d encode('locale_fs', "$opt_output/Init")) {
mkdir(encode('locale_fs', "$opt_output/Init")) ||
die("Cannot create directory $opt_output/Init: $!");
}
# It seems that tlgs can handle UTF-8 TTF filename in cidfmap.local.
open(FOO, ">:encoding(UTF-8)",
encode('locale_fs', "$opt_output/$cidfmap_local_pathpart")) ||
die("Cannot open $opt_output/$cidfmap_local_pathpart: $!");
print FOO $outp;
close(FOO);
}
update_master_cidfmap('cidfmap.local');
}
sub do_aliases {
my $fontdest = "$opt_output/Font";
my $ciddest = "$opt_output/CIDFont"; # required for Heisei* check only
my $cidfsubst = "$opt_output/CIDFSubst";
my $outp = '';
#
# alias handling
# we use two levels of aliases
# * one is for the default generic names (these are not actual fonts)
# Ryumin-Light, GothicBBB-Medium, ... etc.
# * the second level of aliases is for Morisawa OTF font names
# RyuminPro-Light, GothicBBBPro-Medium, ... etc.
# the order of fonts selected is
# defined in the Provides(Priority): Name in the font definiton
#
$outp .= "\n\n% Aliases\n";
#
my (@jal, @kal, @kral, @tal, @sal, @ai0al);
#
for my $al (sort keys %aliases) {
my $target;
my $class;
if ($user_aliases{$al}) {
$target = $user_aliases{$al};
# determine class
if ($fontdb{$target}{'available'}) {
$class = $fontdb{$target}{'class'};
} else {
# must be an aliases, we checked this when initializing %user_aliases
# reset the $al value
# and since $class is still undefined we will use the next code below
$al = $target;
}
}
if (!$class) {
if (!%{$aliases{$al}}) {
print_warning("Alias candidate for $al is empty, skipping!\n");
next;
}
# search lowest number
my @ks = keys(%{$aliases{$al}});
my $first = (sort { $a <=> $b} @ks)[0];
$target = $aliases{$al}{$first};
$class = $fontdb{$target}{'class'};
}
# we also need to create font snippets in Font (or add configuration)
# for the aliases!
generate_font_snippet($fontdest, $al, $class, $target);
if ($class eq 'Japan') {
push @jal, "/$al /$target ;";
} elsif ($class eq 'Korea') {
push @kal, "/$al /$target ;";
} elsif ($class eq 'KR') {
push @kral, "/$al /$target ;";
} elsif ($class eq 'GB') {
push @sal, "/$al /$target ;";
} elsif ($class eq 'CNS') {
push @tal, "/$al /$target ;";
} elsif ($class eq 'AI0') {
push @ai0al, "/$al /$target ;";
} else {
print STDERR "unknown class $class for $al\n";
}
}
# special case for native CID fonts in ancient days
# if not readable, add aliases for substitution
push @jal, "/HeiseiMin-W3 /Ryumin-Light ;"
if (! -r encode('locale_fs', "$ciddest/HeiseiMin-W3"));
push @jal, "/HeiseiKakuGo-W5 /GothicBBB-Medium ;"
if (! -r encode('locale_fs', "$ciddest/HeiseiKakuGo-W5"));
#
$outp .= "\n% Japanese fonts\n" . join("\n", @jal) . "\n" if @jal;
$outp .= "\n% Korean (AK1) fonts\n" . join("\n", @kal) . "\n" if @kal;
$outp .= "\n% Korean (AKR) fonts\n" . join("\n", @kral) . "\n" if @kral;
$outp .= "\n% Traditional Chinese fonts\n" . join("\n", @tal) . "\n" if @tal;
$outp .= "\n% Simplified Chinese fonts\n" . join("\n", @sal) . "\n" if @sal;
$outp .= "\n% Adobe-Identity-0 fonts\n" . join("\n", @ai0al) . "\n" if @ai0al;
#
return if $dry_run;
if ($outp && !$opt_remove) {
if (! -d encode('locale_fs', "$opt_output/Init")) {
mkdir(encode('locale_fs', "$opt_output/Init")) ||
die("Cannot create directory $opt_output/Init: $!");
}
open(FOO, ">:encoding(UTF-8)",
encode('locale_fs', "$opt_output/$cidfmap_aliases_pathpart")) ||
die("Cannot open $opt_output/$cidfmap_aliases_pathpart: $!");
print FOO $outp;
close(FOO);
}
update_master_cidfmap('cidfmap.aliases');
# if we are in cleanup mode, also remove cidfmap.aliases itself
if (-f encode('locale_fs', "$opt_output/$cidfmap_aliases_pathpart")) {
unlink encode('locale_fs', "$opt_output/$cidfmap_aliases_pathpart")
if $opt_cleanup;
}
}
sub do_cmaps {
# add symlinking CMaps
# for which we generate snippets but gs does not provide
my $cmapdest = "$opt_output/CMap";
return if $dry_run;
if ($opt_remove) {
# we remove only if both of the following conditions are met:
# (1) it is a link
# (2) the link target is the same as kpsewhich result
# otherwise it's unsafe to remove, as it may have been added
# by others or distributed by gs itself
for my $class (%encode_list) {
for my $enc (@{$encode_list{$class}}) {
if (-l encode('locale_fs', "$cmapdest/$enc")) {
my $linkt = decode('locale_fs',
readlink(encode('locale_fs',
"$cmapdest/$enc")));
if ($linkt) {
if ($linkt eq search_cmap($enc)) {
unlink(encode('locale_fs', "$cmapdest/$enc"));
}
}
}
}
}
return;
}
# add mode
if (! -d encode('locale_fs', "$cmapdest")) {
print_debug("Creating directory $cmapdest ...\n");
make_dir("$cmapdest", "cannot create CMap directory");
}
for my $class (%encode_list) {
if ($class =~ m/^AI0-(.*)$/) {
# skip AI0 font-specific CMap when the real font is unavailable
next if (!$fontdb{$1}{'available'});
}
for my $enc (@{$encode_list{$class}}) {
if (! -f encode('locale_fs', "$cmapdest/$enc")) {
print_debug("CMap $enc is not found in gs resource directory\n");
my $dest = search_cmap($enc);
if ($dest) {
print_debug("Symlinking CMap $dest ...\n");
link_font($dest, "$cmapdest", $enc);
} else {
print_debug("CMap $enc is not found by kpsewhich\n");
}
}
}
}
}
my %cmap_cache;
sub search_cmap {
my ($cmap) = @_;
# search CMap with kpsewhich and cache
if (! exists $cmap_cache{$cmap}) {
my $cmdl = "kpsewhich -format=cmap $cmap";
$cmdl = encode('locale', $cmdl);
$cmap_cache{$cmap} = `$cmdl`;
# We assume that the output of kpsewhich is
# the same as perl's locale (or active code page).
$cmap_cache{$cmap} = decode('locale', $cmap_cache{$cmap});
chomp($cmap_cache{$cmap});
$cmap_cache{$cmap} =~ s/[\r\n]+\z//; # perl's chomp() on git-bash cannot strip CR of CRLF ??
}
return $cmap_cache{$cmap};
}
sub update_master_cidfmap {
# what we have to do is:
# in add mode:
# * add an entry for the given argument
# * for tlgs.win32 pre-shipped cidfmap, prepend '%' to override
# the default of "(cidfmap.TeXLive) .runlibfile",
# in remove mode:
# * remove an entry for the given argument
# * for tlgs.win32 pre-shipped cidfmap, remove '%' to restore the default
my $add = shift;
my $cidfmap_master = "$opt_output/$cidfmap_pathpart";
print_info(sprintf("%s $add %s cidfmap file ...\n",
($opt_remove ? "removing" : "adding"), ($opt_remove ? "from" : "to")));
if (-r encode('locale_fs', $cidfmap_master)) {
open(FOO, "<:encoding(UTF-8)", encode('locale_fs', $cidfmap_master)) ||
die("Cannot open $cidfmap_master for reading: $!");
my $found = 0;
my $found_tl = 0;
my $newmaster = "";
# in add mode: just search for the entry and set $found
# in remove mode: collect all lines that do not match
# also, we handle "cidfmap.TeXLive" now
while(<FOO>) {
if (m/^\s*\(\Q$add\E\)\s\s*\.runlibfile\s*$/) {
$found = 1;
} elsif (m/^\s*\(cidfmap\.TeXLive\)\s\s*\.runlibfile\s*$/) {
# if found, it has to be disabled in add mode in a way in which it can
# be detected in the (future) remove mode
next if $found_tl; # skip it as duplicate (though unlikely to happen)
$found_tl = 1;
$newmaster .= "\%" if (!$opt_remove); # in add mode, disable it
$newmaster .= $_; # pass it as-is
} elsif (m/^\s*\%\%*\s*\(cidfmap\.TeXLive\)\s\s*\.runlibfile\s*$/) {
# if found, it should be the one disabled by myself in the previous run;
# restore it in remove mode
next if $found_tl; # skip it as duplicate (though unlikely to happen)
$found_tl = 1;
$_ =~ s/\%//g if $opt_remove; # in remove mode, enable it
$newmaster .= $_; # pass it
} else {
$newmaster .= $_;
}
}
close(FOO);
# if the original master cidfmap has a new line at end of file,
# then $newmaster should end with "\n".
# otherwise we add a new line, since there is a possibility of %EOF comment
# without trailing new line (e.g. TL before r44039)
$newmaster =~ s/\n$//;
$newmaster =~ s/$/\n/;
if ($opt_remove) {
if ($found || $found_tl) {
return if $dry_run;
open(FOO, ">:encoding(UTF-8)", encode('locale_fs', $cidfmap_master)) ||
die("Cannot clean up $cidfmap_master: $!");
print FOO $newmaster;
close FOO;
}
} else {
if ($found && !$found_tl) {
print_info("$add already loaded in $cidfmap_master, no changes\n");
} else {
return if $dry_run;
open(FOO, ">:encoding(UTF-8)", encode('locale_fs', $cidfmap_master)) ||
die("Cannot open $cidfmap_master for appending: $!");
print FOO $newmaster;
print FOO "($add) .runlibfile\n";
close(FOO);
}
}
} else {
return if $dry_run;
return if $opt_remove;
open(FOO, ">:encoding(UTF-8)", encode('locale_fs', $cidfmap_master)) ||
die("Cannot open $cidfmap_master for writing: $!");
print FOO "($add) .runlibfile\n";
close(FOO);
}
}
sub generate_cidfmap_entry {
my ($n, $c, $f, $sf) = @_;
return "" if $opt_remove;
# $f is already the link target name 'ttfname' (or 'ttcname' or 'otcname')
# as determined by minimal priority number
# extract subfont
my $s = "/$n << /FileType /TrueType
/Path pssystemparams /GenericResourceDir get
(CIDFSubst/$f) concatstrings\n";
if ($sf >= 0) { # in this script, $sf < 0 represents TTF
$s .= " /SubfontID $sf\n";
}
$s .= " /CSI [($c";
if ($c eq "Japan") {
$s .= "1) 6]";
} elsif ($c eq "GB") {
$s .= "1) 5]";
} elsif ($c eq "CNS") {
$s .= "1) 5]";
} elsif ($c eq "Korea") {
$s .= "1) 2]";
} elsif ($c eq "KR") {
$s .= ") 9]";
} elsif ($c eq "AI0") {
print_warning("cannot use class AI0 for non-OTF $n, skipping.\n");