summaryrefslogtreecommitdiff
path: root/language/japanese/japanese-otf-uptex/script/mkutf32list.pl
blob: 377659d3983d6bda0e93ff7856478c8094d62730 (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
#!/usr/bin/perl -n -s

=head1 NAME

mkutf32list.pl

=head1 USAGE

mkutf32list.pl cid2code.txt > sp_jp_text.tex
mkutf32list.pl -style=utf cid2code.txt > sp_jp_utf.tex
mkutf32list.pl -style=kchar cid2code.txt > sp_jp_kchar.tex
mkutf32list.pl -style=list cid2code.txt > sp_list_j.txt
mkutf32list.pl -style=list-wo-collec cid2code.txt > sp_list_ja.txt
mkutf32list.pl -allrange cid2code.txt > sp_jp_text.tex

=head1 AUTHOR

Takuji Tanaka

=head1 NOTE

This software is a part of otfbeta-uptex (a.k.a. japanese-otf-uptex).

=cut

use strict;
binmode(STDOUT, ":utf8");

our ($style, $allrange);
our (@count, %reset_ch, $icollec, $cid2code, $line);
our ($col_utf32, @out);
our (@cid_max, $collection_n, $collection, $utfmac, $cmap, $source);

BEGIN{
    $line = 0;
    @count = ();
    %reset_ch = ();
    $icollec = 0;
    @out = ();
}

if (/cid2code/) {
    chomp;
    s/^# /# in /;
    $cid2code=$_;
    $cid2code=~s/^#/%/;
}

if ($.<8 && /((Adobe-(?:Japan|CNS|GB|Korea).*)-\d)\s/) {
    $collection_n=$1;
    $collection=$2;
    if ($collection =~ /cns/i)    {
	@cid_max = qw/-1 14098 17407 17600 18845 18964 19087 19155 19178/;
	$utfmac="UTFT"; $cmap="UniCNS-UTF32";
	$source="Adobe-CNS1-7/cid2code.txt"; }
    elsif ($collection =~ /gb/i)  {
	@cid_max = qw/-1 7716 9896 22126 22352 29063 30283/;
	$utfmac="UTFC"; $cmap="UniGB-UTF32";
	$source="Adobe-GB1-5/cid2code.txt"; }
    elsif ($collection =~ /kor/i) {
	@cid_max = qw/-1 9332 18154 18351/;
	$utfmac="UTFK"; $cmap="UniKS-UTF32";
	$source="Adobe-Korea1-2/cid2code.txt"; }
    else                          {
	@cid_max = qw/-1 8283 8358 8719 9353 15443 20316 23057 23059/;
	$utfmac="UTF";  $cmap="UniJIS-UTF32";
	$source="Adobe-Japan1-7/cid2code.txt"; }
}

next if (/^#/);
$line++;
if ($line == 1) {
    print <<END;
%
% This file is generated from the data of $cmap
$cid2code
% for $collection_n
%
% Reference:
%   https://github.com/adobe-type-tools/cmap-resources/
%   $source
%
% A newer CMap may be required for some code points.
%
END
}
if (/^CID/) {
    my @header = split;
    my $i=0;
    foreach (@header) {
	if (/^Uni(JIS|KS|CNS|GB)-UTF32$/) {
	    $col_utf32 = $i;
	    last;
	}
	$i++
    }
    next;
}

my @list = split;
my $cid = $list[0];
my @utf32 = split ',', $list[$col_utf32];

foreach (@utf32) {
    s/^0+//;

    next if ($_ eq '*');
    next if ($_ =~ '^[1-7][0-9a-f]$|^.$');
    next if ($_ =~ 'v');
    tr/a-z/A-Z/;
    my $ch=hex($_);
    next if ($ch < 0x10000 && !$allrange);

    while(!($cid_max[$icollec+1]>=$cid && $cid>$cid_max[$icollec])) {
	$icollec++;
	if ($icollec>@cid_max) {
	    die "CID:$cid (Character $_) is out of range!!\n";
	}
    }
    if ($count[$icollec]==0) {
	$reset_ch{$ch}=$icollec;
    }
    $count[$icollec]++;
    push @out, $ch;
}


END {
    my ($i, $out, $ch);

    if ($style eq "list-wo-collec") { @out = sort(@out); }

    foreach $ch (@out) {
	if ($style eq "list-wo-collec") {}
	elsif (defined($reset_ch{$ch})) {
	    $i=0;
	    print "\n\n";
	    print "%" if ($style =~ /list/);
	    print "$collection-$reset_ch{$ch}";
	    print "\\\\" if ($style !~ 'list');
	    print "\n";
	}

	$i++;
	if    ($style =~ /utf/)   { $out=sprintf "\\${utfmac}{%X}", $ch; }
	elsif ($style =~ /kchar/) { $out=sprintf "\\kchar\"%X", $ch; }
	elsif ($style =~ /list/)  { $out=sprintf "%X", $ch; }
	else                      { $out=chr($ch); }
	my ($newline);
	$newline = $allrange ? 25 : 10;
	if ($i % $newline != 1) {
	    print "," if ($style =~ /list/);
	}
	print $out;
	if ($i % $newline == 0) {
	    print "%" if ($style =~ /utf/);
	    print "\n" ;
	}
    }

    print "\n\n% end\n";
}