Debian Codegraph @

texk/texlive/linked_scripts/ptex-fontmaps/kanji-config-updmap.pl

raw
1 #!/usr/bin/env perl
2 # kanji-config-updmap: setup Japanese font embedding
3 # Version 20210625.0
4 #
5 # formerly known as updmap-setup-kanji
6 #
7 # Copyright 2004-2006 by KOBAYASHI R. Taizo for the shell version (updmap-otf)
8 # Copyright 2011-2021 by PREINING Norbert
9 # Copyright 2016-2021 by Japanese TeX Development Community
10 #
11 # This file is licensed under GPL version 3 or any later version.
12 # For copyright statements see end of file.
13 #
14 # For development see
15 # https://github.com/texjporg/ptex-fontmaps
16 #
17 # For a changelog see the git log
18 #
19
20 $^W = 1;
21 use Getopt::Long qw(:config no_autoabbrev ignore_case_always);
22 use strict;
23
24 my $prg = "kanji-config-updmap";
25 my $version = '20210625.0';
26
27 my $updmap_real = "updmap";
28 my $updmap = $updmap_real;
29
30 my $dry_run = 0;
31 my $opt_help = 0;
32 my $opt_jis = 0;
33 my $opt_sys = 0;
34 my $opt_user = 0;
35 my $opt_old = 0;
36 my $opt_force = 0;
37 my @opt_mode_list;
38 my $opt_mode_one;
39 my $opt_mode_ja;
40 my $opt_mode_sc;
41 my $opt_mode_tc;
42 my $opt_mode_ko;
43
44 if (! GetOptions(
45 "n|dry-run" => \$dry_run,
46 "h|help" => \$opt_help,
47 "jis2004" => \$opt_jis,
48 "mode=s" => \$opt_mode_one,
49 "ja=s" => \$opt_mode_ja,
50 "sc=s" => \$opt_mode_sc,
51 "tc=s" => \$opt_mode_tc,
52 "ko=s" => \$opt_mode_ko,
53 "sys" => \$opt_sys,
54 "user" => \$opt_user,
55 "old" => \$opt_old,
56 "force" => \$opt_force,
57 "version" => sub { print &version(); exit(0); }, ) ) {
58 die "Try \"$0 --help\" for more information.\n";
59 }
60
61 if ($opt_mode_one) {
62 if (defined($opt_mode_ja) || defined($opt_mode_sc) ||
63 defined($opt_mode_tc) || defined($opt_mode_ko)) {
64 die "Options --ja/--sc/--tc/--ko are invalid with --mode=NN!\n";
65 }
66 # define a corresponding option by empty string
67 if ($opt_mode_one eq "ja") {
68 $opt_mode_ja = '';
69 } elsif ($opt_mode_one eq "sc") {
70 $opt_mode_sc = '';
71 } elsif ($opt_mode_one eq "tc") {
72 $opt_mode_tc = '';
73 } elsif ($opt_mode_one eq "ko") {
74 $opt_mode_ko = '';
75 } else {
76 die "Unknown mode $opt_mode_one!";
77 }
78 }
79 push @opt_mode_list, "ja" if (defined($opt_mode_ja));
80 push @opt_mode_list, "sc" if (defined($opt_mode_sc));
81 push @opt_mode_list, "tc" if (defined($opt_mode_tc));
82 push @opt_mode_list, "ko" if (defined($opt_mode_ko));
83 if (!@opt_mode_list) {
84 # default mode needs to be set, define it by empty string
85 $opt_mode_one = "ja";
86 $opt_mode_ja = '';
87 push @opt_mode_list, "ja";
88 }
89
90 sub win32 { return ($^O=~/^MSWin(32|64)$/i); }
91 my $nul = (win32() ? 'nul' : '/dev/null') ;
92
93 if (defined($ARGV[0]) && $ARGV[0] ne "status") {
94 if (!($opt_user || $opt_sys)) {
95 die "Either -user or -sys mode is required.";
96 }
97 }
98
99 if ($opt_user && $opt_sys) {
100 die "Only one of -user and -sys can be used!";
101 }
102
103 if ($opt_sys) {
104 $updmap = "$updmap --sys" ;
105 $updmap_real = "$updmap_real --sys" ;
106 } else {
107 # TeX Live 2017 requires --user option
108 # try to determine the version of updmap installed
109 my $updver = `updmap --version 2>&1`;
110 if ($updver =~ m/^updmap version r([0-9]*) /) {
111 if ($1 >= 44080) {
112 $updmap = "$updmap --user" ;
113 $updmap_real = "$updmap_real --user" ;
114 } # else nothing to do, already set up for old updmap
115 } else {
116 # not recognized updmap -> assume new updmap unless --old
117 if (!$opt_old) {
118 $updmap = "$updmap --user" ;
119 $updmap_real = "$updmap_real --user" ;
120 }
121 }
122 }
123 if ($dry_run) {
124 $updmap = "echo updmap";
125 }
126
127 if ($opt_help) {
128 Usage();
129 exit(0);
130 }
131
132 #
133 # representatives of support font families
134 #
135 my %representatives;
136 my %ai0flags;
137 my @databaselist = "ptex-fontmaps-data.dat";
138 push @databaselist, "ptex-fontmaps-macos-data.dat" if (macosx_new());
139
140
141 main(@ARGV);
142
143 sub macosx { return ($^O=~/^darwin$/i); }
144
145 sub macosx_new {
146 if (macosx()) {
147 my $macos_ver = `sw_vers -productVersion`;
148 my $macos_ver_major = $macos_ver;
149 $macos_ver_major =~ s/^(\d+)\.(\d+).*/$1/;
150 my $macos_ver_minor = $macos_ver;
151 $macos_ver_minor =~ s/^(\d+)\.(\d+).*/$2/;
152 if ($macos_ver_major==10 && $macos_ver_minor>=11) {
153 return 1; # macOS 10.11 (El Capitan) or later
154 } elsif ($macos_ver_major>=11) {
155 return 1; # macOS 11.0 (Big Sur) or later
156 }
157 }
158 return 0;
159 }
160
161 sub version {
162 my $ret = sprintf "%s version %s\n", $prg, $version;
163 return $ret;
164 }
165
166 sub Usage {
167 my $usage = <<"EOF";
168 $prg $version
169 Set up embedding of Japanese/Chinese/Korean fonts via updmap.cfg.
170
171 This script searches for some of the most common fonts
172 for embedding into pdfs by dvipdfmx.
173
174 In addition it allows to set up arbitrary font families
175 to be embedded into the generated pdf files, as long
176 as at least the representative map file is present.
177 Other map files will be used if available:
178
179 For Japanese:
180 ptex-<family>.map (representative map file)
181 uptex-<family>.map
182 otf-<family>.map
183 otf-up-<family>.map
184
185 For Simplified Chinese, Traditional Chinese and Korean:
186 uptex-<NN>-<family>.map (representative map file)
187 otf-<NN>-<family>.map
188 (NN being: sc, tc, ko)
189
190 Please see the documentation of updmap for details (updmap --help).
191
192 Usage: $prg [OPTION] {<fontname>|auto|nofont|status}
193
194 <family> Embed an arbitrary font family <family>, at least
195 the representative map file has to be available.
196 auto: If the current status is noEmbed or unknown, try to embed
197 one of the supported font families automatically.
198 If none of them is available, fall back to nofont
199 nofont: Embed no fonts (and rely on system fonts when displaying pdfs).
200 If your system does not have any of the supported font
201 families, this target is selected automatically.
202 status: Get information about current environment and usable font maps.
203
204 Options:
205 -n, --dry-run Do not actually run updmap
206 -h, --help Show this message and exit
207 --mode=NN Setup for Japanese (NN=ja), Korean (NN=ko),
208 Simplified Chinese (NN=sc), Traditional Chinese (NN=tc)
209 --NN Shorthand for --mode=NN
210 --jis2004 Use JIS2004 variants for default fonts of (u)pTeX
211 --sys Run in sys mode, i.e., call updmap -sys
212 --user Run in user mode, i.e., call updmap -user or updmap,
213 by checking the version of the updmap script.
214 If a non-parsable output of `updmap --version' is found,
215 a new updmap with --user option is assumed.
216 If this is not the case, explicitly use --old.
217 --old Makes $prg call `updmap' without --user argument in user mode.
218 --force Set up font embedding even if the font is not available.
219 --version Show version information and exit
220
221 EOF
222 ;
223 print $usage;
224 exit(0);
225 }
226
227
228
229 ###
230 ### Collect Database Lines
231 ###
232
233 sub InitDatabase {
234 %representatives = ();
235 %ai0flags = ();
236 }
237
238 sub ReadDatabase {
239 my @curdbl;
240 # open database
241 for my $f (@databaselist) {
242 my $foo = kpse_miscfont($f);
243 if (!open(FDB, "<$foo")) {
244 printf STDERR "Cannot find $f, skipping!\n";
245 next;
246 }
247 @curdbl = <FDB>;
248 close(FDB);
249 # parse lines
250 my $lineno = 0;
251 chomp(@curdbl);
252 push @curdbl, ""; # add a "final empty line" to easy parsing
253 for my $l (@curdbl) {
254 $lineno++;
255 next if ($l =~ m/^\s*$/); # skip empty line
256 next if ($l =~ m/^\s*#/); # skip comment line
257 $l =~ s/\s*#.*$//; # skip comment after '#'
258 if ($l =~ m/^JA\*\((\d+)\):\s*(.*):\s*(.*)$/) { # no -04 map
259 $representatives{'ja'}{$2}{'priority'} = $1;
260 $representatives{'ja'}{$2}{'file'} = $3;
261 $representatives{'ja'}{$2}{'nojis04'} = 1;
262 next;
263 }
264 if ($l =~ m/^JA\((\d+)\):\s*(.*):\s*(.*)$/) {
265 $representatives{'ja'}{$2}{'priority'} = $1;
266 $representatives{'ja'}{$2}{'file'} = $3;
267 next;
268 }
269 if ($l =~ m/^SC\((\d+)\):\s*(.*):\s*(.*)$/) {
270 $representatives{'sc'}{$2}{'priority'} = $1;
271 $representatives{'sc'}{$2}{'file'} = $3;
272 next;
273 }
274 if ($l =~ m/^TC\((\d+)\):\s*(.*):\s*(.*)$/) {
275 $representatives{'tc'}{$2}{'priority'} = $1;
276 $representatives{'tc'}{$2}{'file'} = $3;
277 next;
278 }
279 if ($l =~ m/^KO\((\d+)\):\s*(.*):\s*(.*)$/) {
280 $representatives{'ko'}{$2}{'priority'} = $1;
281 $representatives{'ko'}{$2}{'file'} = $3;
282 next;
283 }
284 if ($l =~ m/^JA-AI0\*:\s*(.*):\s*(.*)$/) { # no -04 map
285 $representatives{'ja'}{$1}{'priority'} = 9999; # lowest
286 $representatives{'ja'}{$1}{'file'} = $2;
287 $representatives{'ja'}{$1}{'nojis04'} = 1;
288 $ai0flags{'ja'}{$1} = 1;
289 next;
290 }
291 if ($l =~ m/^JA-AI0:\s*(.*):\s*(.*)$/) {
292 $representatives{'ja'}{$1}{'priority'} = 9999; # lowest
293 $representatives{'ja'}{$1}{'file'} = $2;
294 $ai0flags{'ja'}{$1} = 1;
295 next;
296 }
297 if ($l =~ m/^SC-AI0:\s*(.*):\s*(.*)$/) {
298 $representatives{'sc'}{$1}{'priority'} = 9999; # lowest
299 $representatives{'sc'}{$1}{'file'} = $2;
300 $ai0flags{'sc'}{$1} = 1;
301 next;
302 }
303 if ($l =~ m/^TC-AI0:\s*(.*):\s*(.*)$/) {
304 $representatives{'tc'}{$1}{'priority'} = 9999; # lowest
305 $representatives{'tc'}{$1}{'file'} = $2;
306 $ai0flags{'tc'}{$1} = 1;
307 next;
308 }
309 if ($l =~ m/^KO-AI0:\s*(.*):\s*(.*)$/) {
310 $representatives{'ko'}{$1}{'priority'} = 9999; # lowest
311 $representatives{'ko'}{$1}{'file'} = $2;
312 $ai0flags{'ko'}{$1} = 1;
313 next;
314 }
315 # we are still here??
316 die "Cannot parse \"$foo\" at line $lineno,
317 exiting. Strange line: >>>$l<<<\n";
318 }
319 }
320 if (!%representatives) {
321 die "Candidate list is empty, cannot proceed!\n";
322 }
323 }
324
325 sub kpse_miscfont {
326 my ($file) = @_;
327 my $foo = '';
328 # first, prioritize GitHub repository diretory structure
329 $foo = "database/$file" if (-f "database/$file");
330 if ($foo eq "") {
331 chomp($foo = `kpsewhich -format=miscfont $file`);
332 }
333 return $foo;
334 }
335
336 ###
337 ### Check Installed Font
338 ###
339
340 sub CheckInstallFont {
341 for my $opt_mode (@opt_mode_list) {
342 for my $k (keys %{$representatives{$opt_mode}}) {
343 my $f = `kpsewhich $representatives{$opt_mode}{$k}{'file'}`;
344 if ($?) {
345 $representatives{$opt_mode}{$k}{'available'} = "";
346 } else {
347 $representatives{$opt_mode}{$k}{'available'} = chomp($f);
348 }
349 }
350 }
351 }
352
353 ###
354 ### GetStatus
355 ###
356
357 sub check_mapfile {
358 my $mapf = shift;
359 my $f = `kpsewhich $mapf 2> $nul`;
360 my $ret = $?;
361 if (wantarray) {
362 return (!$ret, $f);
363 } else {
364 return (!$ret);
365 }
366 }
367
368 sub gen_mapfile {
369 my $opt_mode = shift;
370 my $map_base = shift;
371 # returns a representative map file name
372 # ptex-${map_base}.map also exists for Japanese AI0 fonts,
373 # but it is a stub so we use uptex-${map_base}.map instead
374 return ($opt_mode eq "ja" ?
375 ($ai0flags{$opt_mode}{$map_base} ?
376 "uptex-${map_base}.map" :
377 "ptex-${map_base}.map") :
378 "uptex-${opt_mode}-${map_base}.map");
379 }
380
381 sub GetStatus {
382 my $opt_mode = shift;
383 my $val;
384 my $STATUS = "";
385 my $VARIANT = "";
386
387 # fetch jaEmbed/scEmbed/tcEmbed/koEmbed
388 $val = `$updmap_real --quiet --showoption ${opt_mode}Embed`;
389 if ($val =~ m/^${opt_mode}Embed=([^()\s]*)(\s+\()?/) {
390 $STATUS = $1;
391 } else {
392 die "Cannot find status of current ${opt_mode}Embed setting via updmap --showoption!\n";
393 }
394 # fetch jaVariant
395 if ($opt_mode eq "ja") {
396 $val = `$updmap_real --quiet --showoption ${opt_mode}Variant`;
397 if ($val =~ m/^${opt_mode}Variant=([^()\s]*)(\s+\()?/) {
398 $VARIANT = $1; # should be '' or '-04'
399 } else {
400 die "Cannot find status of current ${opt_mode}Variant setting via updmap --showoption!\n";
401 }
402 }
403
404 my $testmap = gen_mapfile($opt_mode, "$STATUS$VARIANT");
405 $VARIANT = "<empty>" if ($VARIANT eq ""); # for printing
406 if (check_mapfile($testmap)) {
407 print "CURRENT family for $opt_mode: $STATUS";
408 print " (variant: $VARIANT)" if ($opt_mode eq "ja");
409 print " (AI0)" if ($ai0flags{$opt_mode}{$STATUS});
410 print "\n";
411 } else {
412 print STDERR "WARNING: Currently selected map file for $opt_mode cannot be found: $testmap\n";
413 }
414
415 for my $k (sort keys %{$representatives{$opt_mode}}) {
416 my $MAPFILE = gen_mapfile($opt_mode, $k);
417 next if ($MAPFILE eq $testmap);
418 if (check_mapfile($MAPFILE)) {
419 if ($representatives{$opt_mode}{$k}{'available'}) {
420 print "Standby family : $k";
421 print " (AI0)" if ($ai0flags{$opt_mode}{$k});
422 print "\n";
423 }
424 }
425 }
426 return $STATUS;
427 }
428
429 ###
430 ### Setup Map files
431 ###
432
433 sub SetupMapFile {
434 my $opt_mode = shift;
435 my $rep = shift;
436 my $MAPFILE = gen_mapfile($opt_mode, $rep);
437 if (check_mapfile($MAPFILE)) {
438 print "Setting up ... $rep";
439 print " (AI0)" if ($ai0flags{$opt_mode}{$rep});
440 print " for $opt_mode\n";
441 system("$updmap --quiet --nomkmap --nohash --setoption ${opt_mode}Embed $rep");
442 if ($opt_mode eq "ja") {
443 if ($opt_jis && $representatives{'ja'}{$rep}{'nojis04'}) {
444 print STDERR "WARNING: No -04 map available, option --jis2004 ignored!\n";
445 $opt_jis = 0;
446 }
447 if ($opt_jis) {
448 system("$updmap --quiet --nomkmap --nohash --setoption jaVariant -04");
449 } else {
450 system("$updmap --quiet --nomkmap --nohash --setoption jaVariant \"\"");
451 }
452 }
453 } else {
454 die "NOT EXIST $MAPFILE\n";
455 }
456 }
457
458 sub SetupReplacement {
459 my $opt_mode = shift;
460 my $rep = shift;
461 if (defined($representatives{$opt_mode}{$rep})) {
462 if ($representatives{$opt_mode}{$rep}{'available'} || $opt_force) {
463 return SetupMapFile($opt_mode, $rep);
464 } else {
465 printf STDERR "$rep not available, falling back to auto!\n";
466 return SetupReplacement($opt_mode, "auto");
467 }
468 } else {
469 if ($rep eq "nofont") {
470 return SetupMapFile($opt_mode, "noEmbed");
471 } elsif ($rep eq "auto") {
472 my $STATUS = GetStatus($opt_mode);
473 # first check if we have a status set and the font is installed
474 # in this case don't change anything, just make sure
475 if (defined($representatives{$opt_mode}{$STATUS}) &&
476 $representatives{$opt_mode}{$STATUS}{'available'}) {
477 return SetupMapFile($opt_mode, $STATUS);
478 } else {
479 if (!($STATUS eq "noEmbed" || $STATUS eq "")) {
480 # some unknown setting is set up currently, overwrite, but warn
481 print STDERR "Previous setting $STATUS for $opt_mode is unknown, replacing it!\n"
482 }
483 # if we are in the noEmbed or nothing set case,
484 # and if one of the supported fonts are present, then use them
485 for my $i (sort { $representatives{$opt_mode}{$a}{'priority'}
486 <=>
487 $representatives{$opt_mode}{$b}{'priority'} }
488 keys %{$representatives{$opt_mode}}) {
489 if ($representatives{$opt_mode}{$i}{'available'}) {
490 return SetupMapFile($opt_mode, $i);
491 }
492 }
493 # still here, no map file found!
494 return SetupMapFile($opt_mode, "noEmbed");
495 }
496 } else {
497 # anything else is treated as a map file name
498 return SetupMapFile($opt_mode, $rep);
499 }
500 }
501 }
502
503 ###
504 ### MAIN
505 ###
506
507 sub main {
508 # Number of arguments allowed:
509 # 0: should be only --NN=<family> lists ('=' can be omitted)
510 # 1: treated as [--mode=NN] <family> ('=' can be omitted)
511 # 2 or more: I can't handle!
512 my ($a, $b) = @_;
513 if (defined($b)) {
514 die "Number of the arguments should be at most one!\n";
515 }
516 if ($a) {
517 die "Strange argument found! >>>$a<<<\n" if (!$opt_mode_one);
518 # argument is passed to defined-but-empty language mode
519 if (defined($opt_mode_ja) && !$opt_mode_ja) {
520 $opt_mode_ja = $a;
521 } elsif (defined($opt_mode_sc) && !$opt_mode_sc) {
522 $opt_mode_sc = $a;
523 } elsif (defined($opt_mode_tc) && !$opt_mode_tc) {
524 $opt_mode_tc = $a;
525 } elsif (defined($opt_mode_ko) && !$opt_mode_ko) {
526 $opt_mode_ko = $a;
527 }
528 } else {
529 die "No family or operation is specified for $opt_mode_one!\n",
530 "Try \"$0 --help\" for more information.\n" if ($opt_mode_one);
531 }
532
533 InitDatabase();
534 ReadDatabase();
535 CheckInstallFont();
536
537 # if one of <family> arguments is "status", then
538 # all arguments are forced into "status"
539 if (($opt_mode_ja && ($opt_mode_ja eq "status")) ||
540 ($opt_mode_sc && ($opt_mode_sc eq "status")) ||
541 ($opt_mode_tc && ($opt_mode_tc eq "status")) ||
542 ($opt_mode_ko && ($opt_mode_ko eq "status"))) {
543 GetStatus("ja") if ($opt_mode_ja);
544 GetStatus("sc") if ($opt_mode_sc);
545 GetStatus("tc") if ($opt_mode_tc);
546 GetStatus("ko") if ($opt_mode_ko);
547 } else {
548 SetupReplacement("ja", $opt_mode_ja) if ($opt_mode_ja);
549 SetupReplacement("sc", $opt_mode_sc) if ($opt_mode_sc);
550 SetupReplacement("tc", $opt_mode_tc) if ($opt_mode_tc);
551 SetupReplacement("ko", $opt_mode_ko) if ($opt_mode_ko);
552 system("$updmap");
553 }
554 }
555
556 #
557 #
558 # Copyright statements:
559 #
560 # KOBAYASHI Taizo
561 # email to [email protected]
562 # Message-Id: <[email protected]>
563 # Message-Id: <[email protected]>
564 # --------------------------------------------------------
565 # copyright statement は簡単に以下で結構です。
566 #
567 # Copyright 2004-2006 by KOBAYASHI Taizo
568 #
569 # では
570 # GPL version 3 or any later version
571 #
572 # --------------------------------------------------------
573 #
574 # PREINING Norbert
575 # as author and maintainer of the current file
576 # Licensed under GPL version 3 or any later version
577 #
578 ### Local Variables:
579 ### perl-indent-level: 2
580 ### tab-width: 2
581 ### indent-tabs-mode: nil
582 ### End:
583 # vim: set tabstop=2 expandtab autoindent: