summaryrefslogtreecommitdiff
path: root/Build/source/extra/jfontmaps/script/updmap-otf.pl
blob: 7f79fa2a72eb9df6bdb6dad10fbcb59a6f5588b9 (plain)
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
#!/usr/bin/env perl
# updmap-otf: setup Japanese font embedding
#
# Copyright 2004-2006 by KOBAYASHI R. Taizo for the shell version
# Copyright 2011-2012 by PREINING Norbert
#
# This file is licensed under GPL version 3.
# For copyright statements see end of file.
#
# 31 Jan 2012
#    rewrite in Perl, so that the script is usable under Windows
# 27 Jan 2012 by PREINING Norbert <preining@logic.at>  v0.9.2
#    support IPA and IPAex fonts
#    improve and extended documentation
# 11 Nov 2011 by PREINING Norbert <preining@logic.at>  v0.9.1
#    use kpsewhich for finding fonts
#    use updmap-sys --setoption kanjiEmbed to select the font family
#    use current names of map files
#    use different font name for Kozuka font, as used in the map file
#    get state from updmap.cfg, not from some state file
# 27 May 2006 by KOBAYASHI R. Taizo <tkoba965@mac.com> v0.9
#    use noEmbed.map instead of noEmbeddedFont.map
# 10 Jun 2005 by KOBAYASHI R. Taizo <tkoba965@mac.com> v0.8
#    modified to use updmap-sys in teTeX3
# 07 Nov 2004 by KOBAYASHI R. Taizo <tkoba965@mac.com> v0.7
#    do not echo back the message of updmap.
# 17 Oct 2004 by KOBAYASHI R. Taizo <tkoba965@mac.com> v0.6
#    set hiragino map file if nofont is installed and arg is auto.
# 04 Oct 2004 by KOBAYASHI R. Taizo <tkoba965@mac.com> v0.5
#    handl standby map files more strictly
# 20 Sep 2004 by KOBAYASHI R. Taizo <tkoba965@mac.com> v0.4
#    hand over current status to map file installer
# 19 Sep 2004 by KOBAYASHI R. Taizo <tkoba965@mac.com> v0.3
#    handl *-udvips.map in TEXMF/dvipdfm/config/otf/
# 02 Mar 2004 by KOBAYASHI R. Taizo <tkoba@ike-dyn.ritsumei.ac.jp> v0.2
#    added noFont-udvips.map
# 28 Feb 2004 by KOBAYASHI R. Taizo <tkoba@ike-dyn.ritsumei.ac.jp> v0.1

$^W = 1;
use Getopt::Long qw(:config no_autoabbrev ignore_case_always);
use strict;

my $prg = "updmap-otf";
my $version = '$Id:';

my $updmap_real = "updmap-sys";
my $updmap = $updmap_real;

my $dry_run = 0;
my $opt_help = 0;

if (! GetOptions(
        "n|dry-run" => \$dry_run,
        "h|help" => \$opt_help,
        "version" => sub { print &version(); exit(0); }, ) ) {
  die "Try \"$0 --help\" for more information.\n";
}

if ($dry_run) {
  $updmap = "echo updmap-sys"; 
}

if ($opt_help) {
  Usage();
  exit 0;
}

#
# representatives of support font families
#
my %representatives = (
  hiragino => "HiraMinPro-W3.otf",
  morisawa => "A-OTF-RyuminPro-Light.otf",
  kozuka   => "KozMinPro-Regular.otf",
  ipa      => "ipam.ttf",
  ipaex    => "ipaexm.ttf",
);
my %available;


main(@ARGV);

sub version {
  my $ret = sprintf "%s (perl) version %s\n", $prg, $version;
  return $ret;
}

sub Usage {
  my $usage = <<"EOF";
  updmap-otf     Set up embedding of Japanese fonts via updmap.cfg.

                 This script searches for some of the most common fonts
                 for embedding into pdfs by dvipdfmx.

  Usage:  updmap-otf {<fontname>|auto|nofont|status}

     <fontname>  embed fonts as defined by the map file otf-<fontname>.map
                 if it exists.
     auto:       embed one of the following supported font families
                 automatically:
                   hiragino, morisawa, kozuka, ipaex, ipa
     nofont:     embed no fonts (and rely on system fonts when displaying pdfs)
                 If your system does not have any of the supported font 
                 families as specified above, this target is selected 
                 automatically.
     status:     get information about current environment and usable font map

EOF
;
  print $usage;
  exit 0;
}



###
### Check Installed Font
###

sub CheckInstallFont {
  for my $k (keys %representatives) {
    my $f = `kpsewhich $representatives{$k}`;
    if (! $?) {
      $available{$k} = chomp($f);
    }
  }
}

###
### GetStatus
###

sub check_mapfile {
  my $mapf = shift;
  my $f = `kpsewhich $mapf 2>/dev/null`;
  my $ret = $?;
  if (wantarray) {
    return (!$ret, $f);
  } else {
    return (!$ret);
  }
}

sub GetStatus {
  my $val = `$updmap_real --quiet --showoption kanjiEmbed`;
  my $STATUS;
  if ($val =~ m/^kanjiEmbed=(.*)( \()?/) {
    $STATUS = $1;
    $STATUS =~ s/\s*$//;
    $STATUS =~ s/^"(.*)"$/\1/;
  } else {
    printf STDERR "Cannot find status of current kanjiEmbed setting via updmap-sys --showoption!\n";
    exit 1;
  }

  if (check_mapfile("otf-$STATUS.map")) {
    print "CURRENT map file : otf-$STATUS.map\n";
  } else {
    print "WARNING: Currently selected map file cannot be found: otf-$STATUS.map\n";
  }

  for my $k (sort keys %representatives) {
    my $MAPFILE = "otf-$k.map";
    next if ($MAPFILE eq "otf-$STATUS.map");
    if (check_mapfile($MAPFILE)) {
      if ($available{$k}) {
        print "Standby map file : $MAPFILE\n";
      }
    }
  }
  return $STATUS;
}

###
### Setup Map files
###

sub SetupMapFile {
  my $rep = shift;
  my $MAPFILE = "otf-$rep.map";
  if (check_mapfile($MAPFILE)) {
    print "Setting up ... $MAPFILE\n";
    system("$updmap -setoption kanjiEmbed $rep");
    system("$updmap");
  } else {
    print "NOT EXIST $MAPFILE\n";
    exit 1;
  }
}

sub SetupReplacement {
  my $rep = shift;
  if (defined($representatives{$rep})) {
    if ($available{$rep}) {
      return SetupMapFile($rep);
    } else {
      printf STDERR "$rep not available, falling back to auto!\n";
      return SetupReplacement("auto");
    }
  } else {
    if ($rep eq "nofont") {
      return SetupMapFile("noEmbed");
    } elsif ($rep eq "auto") {
      my $STATUS = GetStatus();
      # first check if we have a status set and the font is installed
      # in this case don't change anything, just make sure
      if (defined($representatives{$STATUS}) && $available{$STATUS}) {
        return SetupMapFile($STATUS);
      } else {
        if (!($STATUS eq "noEmbed" || $STATUS eq "")) {
          # some unknown setting is set up currently, overwrite, but warn
          print "Previous setting $STATUS is unknown, replacing it!\n"
        }
        # if we are in the noEmbed or nothing set case, but one
        # of the three fonts hiragino/morisawa/kozuka are present
        # then use them
        for my $i (qw/hiragino morisawa kozuka ipaex ipa/) {
          if ($available{$i}) {
            return SetupMapFile($i);
          }
        }
        # still here, no map file found!
        return SetupMapFile("noEmbed");
      }
    } else {
      # anything else is treated as a map file name
      return SetupMapFile($rep);
    }
  }
}

###
### MAIN
###

sub main {
  my ($a, $b) = @_;

  # mktexlsr 2> /dev/null

  CheckInstallFont();

  if (defined($b)) {
    usage();
    exit 1;
  }

  if ($a eq "status") {
    GetStatus();
    exit 0;
  }

  return SetupReplacement($a);
}

#
#
# Copyright statements:
#
# KOBAYASHI Taizo
# email to preining@logic.at
# Message-Id: <20120130.162953.59640143170594580.tkoba@cc.kyushu-u.ac.jp>
# --------------------------------------------------------
# copyright statement は簡単に以下で結構です。
#
#        Copyright 2004-2006 by KOBAYASHI Taizo
#
# license も特にこだわりはありませんので
#
#        GPL(v3)
#
# で結構です。
# --------------------------------------------------------
#
# PREINING Norbert
# as author and maintainer of the current file
#
#
### Local Variables:
### perl-indent-level: 2
### tab-width: 2
### indent-tabs-mode: nil
### End:
# vim: set tabstop=2 expandtab: