summaryrefslogtreecommitdiffstats
path: root/intl/unicharutil/tools/genSpecialCasingData.pl
blob: 39b2d478eae82613d9bf0bed4120cb95dd54bcea (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
#!/usr/bin/env perl

# 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/.

# This tool is used to extract "special" (one-to-many) case mappings
# into a form that can be used by nsTextRunTransformations.

use strict;

if ($#ARGV != 1) {
  print <<__EOT;
# Run this tool using a command line of the form
#
#     perl genSpecialCasingData.pl UnicodeData.txt SpecialCasing.txt
#
# The nsSpecialCasingData.cpp file will be written to standard output.
#
# This tool will also write up-to-date versions of the test files
#     all-{upper,lower,title}.html
# and corresponding -ref files in the current directory.
#
__EOT
  exit 0;
}

my %allLower;
my %allUpper;
my %allTitle;
my %compositions;
my %gc;
open FH, "< $ARGV[0]" or die "can't open $ARGV[0] (should be UnicodeData.txt)\n";
while (<FH>) {
  chomp;
  my @fields = split /;/;
  next if ($fields[1] =~ /</); # ignore ranges etc
  my $usv = hex "0x$fields[0]";
  $allUpper{$usv} = $fields[12] if $fields[12] ne '';
  $allLower{$usv} = $fields[13] if $fields[13] ne '';
  $allTitle{$usv} = $fields[14] if $fields[14] ne '';
  $gc{$usv} = $fields[2];
  # we only care about non-singleton canonical decomps
  my $decomp = $fields[5];
  next if $decomp eq '' or $decomp =~ /</ or not $decomp =~ / /;
  $compositions{$decomp} = sprintf("%04X", $usv);
}
close FH;

my %specialLower;
my %specialUpper;
my %specialTitle;
my %charName;
my @headerLines;
open FH, "< $ARGV[1]" or die "can't open $ARGV[1] (should be SpecialCasing.txt)\n";
while (<FH>) {
  chomp;
  m/#\s*(.+)$/;
  my $comment = $1;
  if ($comment =~ /^(SpecialCasing-|Date:)/) {
    push @headerLines, $comment;
    next;
  }
  s/#.*//;
  s/;\s*$//;
  next if $_ eq '';
  my @fields = split /; */;
  next unless (scalar @fields) == 4;
  my $usv = hex "0x$fields[0]";
  addIfSpecial(\%specialLower, $usv, $fields[1]);
  addIfSpecial(\%specialTitle, $usv, $fields[2]);
  addIfSpecial(\%specialUpper, $usv, $fields[3]);
  $charName{$usv} = $comment;
}
close FH;

print <<__END__;
/* 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/. */

/* Auto-generated from files in the Unicode Character Database
   by genSpecialCasingData.pl - do not edit! */

#include "nsSpecialCasingData.h"
#include "mozilla/ArrayUtils.h" // for ArrayLength
#include <stdlib.h>       // for bsearch

__END__
map { print "/* $_ */\n" } @headerLines;

print <<__END__;

using mozilla::unicode::MultiCharMapping;

__END__

printMappings('Lower', \%specialLower);
printMappings('Upper', \%specialUpper);
printMappings('Title', \%specialTitle);

print <<__END__;
static int CompareMCM(const void* aKey, const void* aElement)
{
  const uint32_t ch = *static_cast<const uint32_t*>(aKey);
  const MultiCharMapping* mcm = static_cast<const MultiCharMapping*>(aElement);
  return int(ch) - int(mcm->mOriginalChar);
}

#define MAKE_SPECIAL_CASE_ACCESSOR(which) \\
  const MultiCharMapping* \\
  Special##which(uint32_t aChar) \\
  { \\
    const void* p = bsearch(&aChar, CaseSpecials_##which, \\
                            mozilla::ArrayLength(CaseSpecials_##which), \\
                            sizeof(MultiCharMapping), CompareMCM); \\
    return static_cast<const MultiCharMapping*>(p); \\
  }

namespace mozilla {
namespace unicode {

MAKE_SPECIAL_CASE_ACCESSOR(Lower)
MAKE_SPECIAL_CASE_ACCESSOR(Upper)
MAKE_SPECIAL_CASE_ACCESSOR(Title)

} // namespace unicode
} // namespace mozilla
__END__

addSpecialsTo(\%allLower, \%specialLower);
addSpecialsTo(\%allUpper, \%specialUpper);
addSpecialsTo(\%allTitle, \%specialTitle);

my $testFont = "../fonts/dejavu-sans/DejaVuSans.ttf";
genTest('lower', \%allLower);
genTest('upper', \%allUpper);
genTitleTest();

sub printMappings {
  my ($whichMapping, $hash) = @_;
  print "static const MultiCharMapping CaseSpecials_${whichMapping}[] = {\n";
  foreach my $key (sort { $a <=> $b } keys %$hash) {
    my @chars = split(/ /, $hash->{$key});
    printf "  { 0x%04x, {0x%04x, 0x%04x, 0x%04x} }, // %s\n", $key,
           hex "0x0$chars[0]", hex "0x0$chars[1]", hex "0x0$chars[2]",
           "$charName{$key}";
  }
  print "};\n\n";
};

sub addIfSpecial {
  my ($hash, $usv, $mapping) = @_;
  return unless $mapping =~ / /;
  # only do compositions that start with the initial char
  foreach (keys %compositions) {
    $mapping =~ s/^$_/$compositions{$_}/;
  }
  $hash->{$usv} = $mapping;
};

sub addSpecialsTo {
  my ($hash, $specials) = @_;
  foreach my $key (keys %$specials) {
    $hash->{$key} = $specials->{$key};
  }
};

sub genTest {
  my ($whichMapping, $hash) = @_;
  open OUT, "> all-$whichMapping.html";
  print OUT <<__END__;
<!DOCTYPE html>
<html>
 <head>
  <meta http-equiv="Content-type" content="text/html; charset=utf-8">
  <style type="text/css">
   \@font-face { font-family: foo; src: url($testFont); }
   p { font-family: foo; text-transform: ${whichMapping}case; }
  </style>
 </head>
 <body>
  <p>
__END__
  foreach my $key (sort { $a <=> $b } keys %$hash) {
    printf OUT "&#x%04X;", $key;
    print OUT " <!-- $charName{$key} -->" if exists $charName{$key};
    print OUT "\n";
  }
  print OUT <<__END__;
  </p>
 </body>
</html>
__END__
  close OUT;

  open OUT, "> all-$whichMapping-ref.html";
  print OUT <<__END__;
<!DOCTYPE html>
<html>
 <head>
  <meta http-equiv="Content-type" content="text/html; charset=utf-8">
  <style type="text/css">
   \@font-face { font-family: foo; src: url($testFont); }
   p { font-family: foo; }
  </style>
 </head>
 <body>
  <p>
__END__
  foreach my $key (sort { $a <=> $b } keys %$hash) {
    print OUT join('', map { sprintf("&#x%s;", $_) } split(/ /, $hash->{$key}));
    print OUT " <!-- $charName{$key} -->" if exists $charName{$key};
    print OUT "\n";
  }
  print OUT <<__END__;
  </p>
 </body>
</html>
__END__
  close OUT;
};

sub genTitleTest {
  open OUT, "> all-title.html";
  print OUT <<__END__;
<!DOCTYPE html>
<html>
 <head>
  <meta http-equiv="Content-type" content="text/html; charset=utf-8">
  <style type="text/css">
   \@font-face { font-family: foo; src: url($testFont); }
   p { font-family: foo; text-transform: capitalize; }
  </style>
 </head>
 <body>
  <p>
__END__
  foreach my $key (sort { $a <=> $b } keys %allTitle) {
    printf OUT "&#x%04X;x", $key;
    print OUT " <!-- $charName{$key} -->" if exists $charName{$key};
    print OUT "\n";
  }
  print OUT <<__END__;
  </p>
 </body>
</html>
__END__
  close OUT;

  open OUT, "> all-title-ref.html";
  print OUT <<__END__;
<!DOCTYPE html>
<html>
 <head>
  <meta http-equiv="Content-type" content="text/html; charset=utf-8">
  <style type="text/css">
   \@font-face { font-family: foo; src: url($testFont); }
   p { font-family: foo; }
  </style>
 </head>
 <body>
  <p>
__END__
  foreach my $key (sort { $a <=> $b } keys %allTitle) {
    # capitalize is only applied to characters with GC=L* or N*...
    if ($gc{$key} =~ /^[LN]/) {
      # ...and those that are already uppercase are not transformed
      if (exists $allUpper{$key}) {
        print OUT join('', map { sprintf("&#x%s;", $_) } split(/ /, $allTitle{$key}));
      } else {
        printf OUT "&#x%04X;", $key;
      }
      print OUT "x";
    } else {
      printf OUT "&#x%04X;X", $key;
    }
    print OUT " <!-- $charName{$key} -->" if exists $charName{$key};
    print OUT "\n";
  }
  print OUT <<__END__;
  </p>
 </body>
</html>
__END__
  close OUT;
};