summaryrefslogtreecommitdiffstats
path: root/intl/uconv/tools/jamap.pl
blob: 7316d891f349339dba41c474e0162cb54dbc23fe (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
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
#!/usr/local/bin/perl
use strict;

my @source_files;

my @sjis_h;
$sjis_h[0] = -1;
@sjis_h[0x81..0x9f] = map { 0x2100 + $_ * 0x200 } (0 .. 30);
@sjis_h[0xe0..0xef] = map { 0x5F00 + $_ * 0x200 } (0 .. 15);
@sjis_h[0xf0..0xf9] = (-2) x 10;
my @sjis_l;
@sjis_l[0x40..0x7e] = (0x21..0x5f);
@sjis_l[0x80..0xfc] = (0x60..0x7e, 0x121..0x17e);

sub sjis_to_jis {
  my ($s) = @_;
  my $j;
  my $type;

  my $h = $sjis_h[($s>>8)&0xff];

  if ( $h > 0 ) { # jis0208

    my $l = $sjis_l[$s&0xff];
    if ( $l == 0 ) {
      $j = $s;
      $type = 'sjis2undef';
    } else {
      $j = $h + $l;
      if ( $j >= 0x3000 && $j < 0x7500 ) { # jis0208 kanji
        $type = 'jis0208';
      } elsif ( $j < 0x2900 ) { # jis0208
        $type = 'jis0208';
      } else {
        $type = 'jis0208undef';
      }
    }

  } elsif ( $h == -1 ) { # single byte

    $j = $s;
    if ( $s <= 0x7f ) { # jis0201 roman
      $type = 'jis0201';
    } elsif ( $s >= 0xa1 && $s <= 0xdf ) { # jis0201 kana
      $type = 'jis0201';
    } else { # sjis single byte undefined
      $type = 'sjis1undef';
    }

  } elsif ( $h == -2 ) { # private use
    $j = $s;
    $type = 'private';

  } else { # sjis undefined
    $j = $s;
    $type = 'sjis2undef';
  }

  return ($j, $type);
}


sub read_sjis_map {
  my ($filename, $s_col, $u_col) = @_;
  my %map;
  open MAP, $filename or die $!;
  while (<MAP>) {
    my @cols = split /\s+/;
    my ($s, $u) = @cols[$s_col, $u_col];
    $s =~ /^0x[0-9A-Fa-f]+$/ && $u =~ /^0x[0-9A-Fa-f]+$/ or next;

    $s = oct($s);
    $u = oct($u);

    my ($j, $type) = sjis_to_jis($s);
    push @{$map{$type}}, [$j, $s, $u];

  }
  close MAP or warn $!;
  push @source_files, $filename;
  return %map;
}

sub read_0212_map {
  my ($filename, $j_col, $u_col) = @_;
  my $map;
  open MAP, $filename or die $!;
  while (<MAP>) {
    my @cols = split /\s+/;
    my ($j, $u) = @cols[$j_col, $u_col];
    $j =~ /^0x[0-9A-Fa-f]+$/ && $u =~ /^0x[0-9A-Fa-f]+$/ or next;

    $j = oct($j);
    $u = oct($u);
    $u = 0xff5e if $u == 0x007e;

    push @$map, [$j, 0, $u];
  }
  close MAP or warn $!;
  push @source_files, $filename;
  return $map;
}


my %printed;
sub write_fromu_map {
  my ($filename, $code, @maps) = @_;
  open MAP, ">$filename" or die $!;
  foreach my $map (@maps) {
    foreach my $pair (@$map) {
      my ($j, $s, $u) = @$pair;
      if ( $code eq 'sjis' ) {
        $j = $s;
      }
      if ( defined($printed{$u}) ) {
        if ( $printed{$u} ne $j ) {
          printf "conflict 0x%04x to 0x%04x, 0x%04x\n", $u, $printed{$u}, $j;
        }
      } else {
        if ( $j < 0x100 ) {
          printf MAP "0x%02X\t0x%04X\n", $j, $u;
        } else {
          printf MAP "0x%04X\t0x%04X\n", $j, $u;
        }
        $printed{$u} = $j;
      }
    }
  }
  close MAP or warn $!;
}

my @table;
my %table;
my $table_next_count = 0;

sub get_94table_index {
  my ($map_table) = @_;
  my $key = join ',', map {int($map_table->[$_])} (0 .. 93);
  my $table_index = $table{$key};
  if ( !defined($table_index) ) {
    $table_index = $table_next_count;
    $table_next_count += 94;
    $table[$table_index] = $map_table;
    $table{$key} = $table_index;
  }
  return $table_index;
}

sub get_188table_index {
  my ($map_table) = @_;
  my $map_table1 = [ @{$map_table}[0 .. 93] ];
  my $map_table2 = [ @{$map_table}[94 .. 187] ];
  my $key = join ',', map {int($map_table->[$_])} (0 .. 187);
  my $key1 = join ',', map {int($map_table1->[$_])} (0 .. 93);
  my $key2 = join ',', map {int($map_table2->[$_])} (0 .. 93);
  my $table_index = $table{$key};
  if ( !defined($table_index) ) {
    $table_index = $table_next_count;
    $table_next_count += 188;
    $table[$table_index] = $map_table1;
    $table[$table_index + 94] = $map_table2;
    $table{$key} = $table_index;
    $table{$key1} = $table_index unless defined($table{$key1});
    $table{$key2} = $table_index + 94 unless defined($table{$key2});
  }
  return $table_index;
}

get_188table_index([]);

sub print_sjis_table_index {
  my @maps = @_;
  my %map_table;
  foreach my $map (@maps) {
    foreach my $pair (@$map) {
      my ($j, $s, $u) = @$pair;
      my $row = $s >> 8;
      my $cell = $s&0xff;
      if ( $cell >= 0x40 && $cell <= 0x7e ) {
        $cell -= 0x40;
      } elsif ( $cell >= 0x80 && $cell <= 0xfc ) {
        $cell -= 0x41;
      } else {
        next;
      }
      if ( defined($map_table{$row}->[$cell]) && $map_table{$row}->[$cell] != $u ) {
         print "conflict!\n";
      }
      $map_table{$row}->[$cell] = $u;
    }
  }

  for ( my $i = 0x80; $i < 0x100; $i++ ) {
    if ( ($i & 0x7) == 0 ) {
      print MAP "\n ";
    }
    if ( $i >= 0xa1 && $i <= 0xdf ) {
      printf MAP " 0x%04X,", $i + 0xfec0;
    } elsif ( $i >= 0xf0 && $i <= 0xf9 ) {
      printf MAP " 0x%04X,", 0xe000 + ($i - 0xf0) * 188;
    } elsif ( $i == 0x80 ) {
      print MAP " 0xFFFD,";
    } elsif ( $i == 0xa0 ) {
      print MAP " 0xF8F0,";
    } elsif ( $i >= 0xfd ) {
      printf MAP " 0x%04X,", $i + (0xf8f1 - 0xfd);
    } else {
      my $table_index = get_188table_index($map_table{$i});
      printf MAP " %6d,", $table_index;
    }
  }
}

sub print_jis_table_index {
  my @maps = @_;
  my %map_table;
  foreach my $map (@maps) {
    foreach my $pair (@$map) {
      my ($j, $s, $u) = @$pair;
      my $row = $j >> 8;
      my $cell = ($j&0xff) - 0x21;
      if ( defined($map_table{$row}->[$cell]) && $map_table{$row}->[$cell] != $u ) {
         print "conflict!\n";
      }
      $map_table{$row}->[$cell] = $u;
    }
  }

  for ( my $i = 0; $i < 0x80; $i++ ) {
    if ( ($i & 0x7) == 0 ) {
      print MAP "\n ";
    }
    if ( $i >= 0x21 && $i <= 0x7e ) {
      my $table_index = get_94table_index($map_table{$i});
      printf MAP " %6d,", $table_index;
    } else {
      print MAP " 0xFFFD,";
    }
  }
}

sub print_table_index {
  my ($map_name, @maps) = @_;
  print MAP "static const uint16_t g${map_name}IndexShiftJis[] = {";
  print_sjis_table_index(@maps);
  print MAP "\n};\n";
  print MAP "static const uint16_t g${map_name}IndexJis0208[] = {";
  print_jis_table_index(@maps);
  print MAP "\n};\n";
  print MAP "static const uint16_t * const g${map_name}Index[] = {";
  print MAP "\n  g${map_name}IndexShiftJis, g${map_name}IndexJis0208";
  print MAP "\n};\n\n";
}

sub print_0212_table_index {
  my ($map_name, @maps) = @_;
  print MAP "static const uint16_t g${map_name}Index[] = {";
  print_jis_table_index(@maps);
  print MAP "\n};\n\n";
}


sub print_table {
  print MAP "static const uint16_t gJapaneseMap[] = {";
  for ( my $i = 0; $i < $table_next_count; $i += 94 ) {
    my $index = $i;
    print MAP "\n  /* index $index */\n         ";
    my $map_table = $table[$i];
    my $print_count = 1;
    for ( my $j = 0; $j < 94; $j++ ) {
      my $u = $map_table->[$j];
      if ( $u == 0 ) { $u = 0xfffd; }
      printf MAP " 0x%04X,", $u;
      if ( ++$print_count == 8 ) {
        print MAP "\n ";
        $print_count = 0;
      }
    }
  }
  print MAP "\n};\n";
}


my %cp932 = read_sjis_map('CP932.TXT', 0, 1);
my $jis0212 = read_0212_map('JIS0212.TXT', 0, 1);

%printed = ();
write_fromu_map('jis0201-uf-unify', 'jis',
  $cp932{jis0201},
);
write_fromu_map('jis0208-uf-unify', 'jis',
  $cp932{jis0208},
);

%printed = ();
write_fromu_map('jis0208ext-uf-unify', 'jis',
  $cp932{jis0208undef},
);

%printed = ();
write_fromu_map('sjis-uf-unify', 'sjis',
  @cp932{'jis0201', 'jis0208', 'jis0208undef', 'sjis1undef', 'sjis2undef'},
);

open MAP, ">japanese.map" or die $!;
binmode MAP;

while (<DATA>) {
  if ( /^!/ ) { last; }
  print MAP;
}
print MAP "/* generated by jamap.pl @source_files */\n\n";
print MAP <<EOM;
// IE-compatible handling of undefined codepoints:
// 0x80 --> U+0080
// 0xa0 --> U+F8F0
// 0xfd --> U+F8F1
// 0xfe --> U+F8F2
// 0xff --> U+F8F3
EOM

print_table_index('CP932', @cp932{'jis0208', 'jis0208undef', 'sjis2undef'});
print_0212_table_index('JIS0212', $jis0212);
print_table();

close MAP or warn $!;

__DATA__
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

!