| 1 |
#!/usr/bin/perl -w |
| 2 |
|
| 3 |
=head1 NAME |
| 4 |
|
| 5 |
makeunidef.pl - Generates Unicode data files for B<ucs.sty>. |
| 6 |
|
| 7 |
=head1 SYSNOPSIS |
| 8 |
|
| 9 |
makeunidef.pl [B<--database>=I<file>] [B<--targetdir>=I<dir>] |
| 10 |
[B<--verbose>] [B<--help>] [B<--comments>] [B<--nocomments>] |
| 11 |
[B<--compress>] [B<--nocompress>] [B<--exclude>=I<option>] |
| 12 |
I<configfiles> |
| 13 |
|
| 14 |
=head1 DESCRIPTION |
| 15 |
|
| 16 |
Generate the F<uni-....def>-files for use by B<ucs.sty>. These are |
| 17 |
generated out of one or more config files (with suffix C<.gz> if |
| 18 |
gzipped), whose format is described below (see L<"CONFIG FILES">). |
| 19 |
|
| 20 |
For some characters there will be autogenerated code, if none is |
| 21 |
supplied by the config files, this code will be associated with the |
| 22 |
Unicode option C<autogenerated>. |
| 23 |
|
| 24 |
=head1 OPTIONS |
| 25 |
|
| 26 |
=over 4 |
| 27 |
|
| 28 |
=item B<--comments>, B<--nocomments> |
| 29 |
|
| 30 |
Enable or disable the automatic generation of comments. |
| 31 |
Defaults to B<--comments>. |
| 32 |
|
| 33 |
=item B<--compress>, B<--nocompress> |
| 34 |
Enable or disable the compression of uninames.dat. |
| 35 |
Defaults to B<--compress>. |
| 36 |
|
| 37 |
=item B<-d>, B<--db>, B<--database>=I<file> |
| 38 |
|
| 39 |
Specify the Unicode database, as provided by the Unicode |
| 40 |
Consortium. Defaults to F<UnicodeData.txt>. The file must have the |
| 41 |
suffix C<.gz> if it is gzipped. |
| 42 |
|
| 43 |
=item B<--dir> |
| 44 |
|
| 45 |
See B<--target>. |
| 46 |
|
| 47 |
=item B<--exclude>, B<--ex>=I<option> |
| 48 |
|
| 49 |
Do not include the characters associated with option I<option>. This |
| 50 |
is e.g. useful for saving space by not including all CJK |
| 51 |
characters. When an excluded option is used in documents, an error is |
| 52 |
yielded. |
| 53 |
|
| 54 |
=item B<-h>, B<--help> |
| 55 |
|
| 56 |
Shows help. |
| 57 |
|
| 58 |
=item B<--nocomments> |
| 59 |
|
| 60 |
See B<--comments>. |
| 61 |
|
| 62 |
=item B<--nocompress> |
| 63 |
|
| 64 |
See B<--compress>. |
| 65 |
|
| 66 |
=item B<-t>, B<--dir>, B<--target>, B<--targetdir>=I<dir> |
| 67 |
|
| 68 |
Sets the target directory for the generated files to I<dir>. The default is |
| 69 |
the current directory. |
| 70 |
|
| 71 |
=item B<-v>, B<--verbose> |
| 72 |
|
| 73 |
Be verbose. |
| 74 |
|
| 75 |
=back |
| 76 |
|
| 77 |
=head1 CONFIG FILES |
| 78 |
|
| 79 |
The config files (which may be gzipped) are to be written in a line |
| 80 |
oriented format. C<#> starts a comment which ends at the end of the |
| 81 |
actual line. The C<#> must be preceded by a whitespace, except for |
| 82 |
lines consisting only of a comment. An empty line or a line containing |
| 83 |
only a comment is ignored. |
| 84 |
|
| 85 |
A line can be a command or a character definition. |
| 86 |
For possible commands see L<"CONFIG COMMANDS">. |
| 87 |
A character |
| 88 |
definition has the following syntax: |
| 89 |
|
| 90 |
I<code> I<macro> |
| 91 |
|
| 92 |
where I<code> is the code position and I<macro> a LaTeX-macro to |
| 93 |
render the glyph. I<macro> is stripped of surrounding whitespaces, and |
| 94 |
I<code> has one of the following formats (all case insensitive): |
| 95 |
|
| 96 |
=over 4 |
| 97 |
|
| 98 |
=item U+I<num>, 0xI<num>, $I<num> |
| 99 |
|
| 100 |
I<num> being the hexadecimal representation of the code position. |
| 101 |
|
| 102 |
=item I<num> |
| 103 |
|
| 104 |
I<num> being the decimal representation of the code position, whereby |
| 105 |
I<num> must not start with 0. |
| 106 |
|
| 107 |
=item 0I<num> |
| 108 |
|
| 109 |
I<num> being the octal representation of the code position. |
| 110 |
|
| 111 |
=back |
| 112 |
|
| 113 |
In all the above formats, I<num> can have any number of digits. |
| 114 |
|
| 115 |
=head2 CONFIG COMMANDS |
| 116 |
|
| 117 |
A command line consists of an command name and optional space |
| 118 |
separated command arguments. The following commands are defined: |
| 119 |
|
| 120 |
=over 4 |
| 121 |
|
| 122 |
=item B<AUTOOPTION> |
| 123 |
|
| 124 |
Same as B<OPTION>, by the second argument specifies a LaTeX |
| 125 |
package. If this package is loaded, the option is set automatically. |
| 126 |
|
| 127 |
This option is deprecated. |
| 128 |
|
| 129 |
=item B<BEGIN>, B<{> |
| 130 |
|
| 131 |
This starts a group. All options set after this are only valid up to |
| 132 |
the matching B<END> or B<}>. Config files, which set options, should |
| 133 |
be enclosed in a block, so that they can't have side effects on other |
| 134 |
parts of the config file. Each config file is contained in an implicit |
| 135 |
block. Blocks can be nested. |
| 136 |
|
| 137 |
=item B<END>, B<}> |
| 138 |
|
| 139 |
See B<BEGIN>. |
| 140 |
|
| 141 |
=item B<ENVELOPE> |
| 142 |
|
| 143 |
All macros are wrapped inside the envelope given by this command. The |
| 144 |
original macro is inserted into the envelope instead of every |
| 145 |
occurrence of C<@@@> and the decimal character code is inserted for |
| 146 |
each occurrence of C<$$$>. B<ENVELOPE> will override B<ENVELOPE> |
| 147 |
commands executed before. |
| 148 |
|
| 149 |
=item B<GLOBAL> |
| 150 |
|
| 151 |
Inserts some code into F<uni-global.def>. This file is loaded with |
| 152 |
F<ucs.sty> in the preamble. F<uni-global.def> is not executed in a group, |
| 153 |
so defining globally is not necessary. See also B<PROVIDE>. |
| 154 |
|
| 155 |
=item B<OPTION> |
| 156 |
|
| 157 |
This command takes one argument. It is the name of the Unicode option |
| 158 |
to associate with the macros defined after this command. An option set |
| 159 |
by B<OPTION> is valid until it is overridden by another B<OPTION> |
| 160 |
command or until the block ends. |
| 161 |
|
| 162 |
=item B<PROVIDE> |
| 163 |
|
| 164 |
Associates a piece of code with characters defined afterwards (until |
| 165 |
the block ends). The code is inserted into every B<uni-....def>-file |
| 166 |
containing one of these characters, so the code is guaranteed to have |
| 167 |
been executed before the corresponding characters are executed. No |
| 168 |
assumption should be made whether the code is executed in a group or |
| 169 |
at top level, i.e. declarations should be made globally, but no |
| 170 |
garbage should be defined or catcodes modified without restoring |
| 171 |
them afterwards. The code may be executed several times. See also B<GLOBAL>. |
| 172 |
|
| 173 |
=back |
| 174 |
|
| 175 |
=head1 KNOWN BUGS AND PROBLEMS |
| 176 |
|
| 177 |
There should be a better way to quote whitespaces in command arguments |
| 178 |
and to insert C<#>s. Perhaps I will add this in future, thereby trying |
| 179 |
to preserve backward compatibility. |
| 180 |
|
| 181 |
=head1 REPORTING BUGS |
| 182 |
|
| 183 |
Send bug reports to Wolfgang Jeltsch <I<[email protected]>>, the mails |
| 184 |
containing the words B<bug report: makeunidef.pl> in the subject. |
| 185 |
|
| 186 |
=head1 AUTHOR |
| 187 |
|
| 188 |
Dominique Unruh <I<[email protected]>>. |
| 189 |
|
| 190 |
=head1 MAINTAINER |
| 191 |
|
| 192 |
Wolfgang Jeltsch <I<[email protected]>>. |
| 193 |
|
| 194 |
=head1 FILES |
| 195 |
|
| 196 |
=over 4 |
| 197 |
|
| 198 |
=item F<uni-I<N>.def>, F<uni-global.def>, F<uninames.dat> |
| 199 |
|
| 200 |
Unicode definitions for B<ucs.sty>, created by this program, I<N> |
| 201 |
stands for different decimal numbers. |
| 202 |
|
| 203 |
=item F<UnicodeData.txt> |
| 204 |
|
| 205 |
Unicode database by the Unicode Consortium, read only. This file can be |
| 206 |
found at F<http://www.unicode.org/Public/UNIDATA/UnicodeData.txt> |
| 207 |
|
| 208 |
=back |
| 209 |
|
| 210 |
=head1 SEE ALSO |
| 211 |
|
| 212 |
The LaTeX package B<ucs.sty>. |
| 213 |
|
| 214 |
=cut |
| 215 |
#'; # for emacs |
| 216 |
|
| 217 |
|
| 218 |
use IO::Handle; |
| 219 |
use IO::File; |
| 220 |
use Data::Dumper; |
| 221 |
use Getopt::Long; |
| 222 |
use Carp; |
| 223 |
|
| 224 |
use strict; |
| 225 |
|
| 226 |
# prototypes |
| 227 |
sub dumphuffman($$$); #{} |
| 228 |
|
| 229 |
|
| 230 |
use vars qw/$createcomments $targetdir @configfiles @characters |
| 231 |
%files @unidata $unidata $messagebreak $dump_unidata |
| 232 |
$verbose $generate_uninames $tables_to_dump $tabledir $autogen |
| 233 |
%knownoptions $loadunidata %stats_charsperoption |
| 234 |
%providehandlers $providehandlerseq %provides $huffman_decoder |
| 235 |
%file_attribs $compressnames %excludedoptions %uninames_abbrev @ranges |
| 236 |
$onlyfile $fileswritten $devnull |
| 237 |
/; |
| 238 |
|
| 239 |
$messagebreak = '\MessageBreak '; |
| 240 |
$autogen = 'autogenerated by makeunidef.pl'; |
| 241 |
|
| 242 |
%uninames_abbrev = |
| 243 |
( |
| 244 |
# "\000" => '{Character available with following options:'. |
| 245 |
# '\MessageBreak\space\space\space}', |
| 246 |
"\001" => '{Unicode character \number\uc@got\uc@spc = U+\uc@temp@a:'. |
| 247 |
'\MessageBreak}', |
| 248 |
# "\002" => '{No name found}', |
| 249 |
"\003" => '{Character available with following excluded options:'. |
| 250 |
'\MessageBreak\space\space\space}', |
| 251 |
"\n" => '\MessageBreak', |
| 252 |
" " => '{ }', |
| 253 |
); |
| 254 |
|
| 255 |
sub globalcode ($) { |
| 256 |
my $code = shift; |
| 257 |
return unless $dump_unidata; |
| 258 |
my $filename = "$targetdir/uni-global.def"; |
| 259 |
my $file = openfile($filename, |
| 260 |
"%%% unicode global data for ucs.sty, $autogen", |
| 261 |
"Unicode global data"); |
| 262 |
print $file "$code\n"; |
| 263 |
} |
| 264 |
|
| 265 |
sub getprovidehandler($) { |
| 266 |
my $code = shift; |
| 267 |
return undef unless defined $code; |
| 268 |
my $handler = $providehandlers{$code}; |
| 269 |
unless (defined $handler) { |
| 270 |
# print "NEW HANDLER: '$code'\n"; |
| 271 |
$handler = $providehandlerseq++; |
| 272 |
$providehandlers{$code} = $handler; |
| 273 |
} |
| 274 |
$provides{$handler} = { code => $code }; |
| 275 |
return $handler; |
| 276 |
} |
| 277 |
|
| 278 |
sub openreadfile($) { |
| 279 |
my $filename = shift; |
| 280 |
$filename .= ".gz" if (!-e $filename && -e "$filename.gz"); |
| 281 |
if ($filename =~ /\.gz/) { |
| 282 |
die "Could not read file $filename" unless -r $filename; |
| 283 |
return new IO::File("gzip -cdf ''\Q$filename\E |"); |
| 284 |
} else { |
| 285 |
return new IO::File($filename,"r"); |
| 286 |
} |
| 287 |
} |
| 288 |
|
| 289 |
sub optionname($) { |
| 290 |
my $o = shift; |
| 291 |
return 'default' unless define($o) ne ''; |
| 292 |
return $o; |
| 293 |
} |
| 294 |
|
| 295 |
sub loadconfig ($) { |
| 296 |
my ($filename) = @_; |
| 297 |
my $fh = openreadfile $filename or |
| 298 |
die "Could not open configfile $filename: $!"; |
| 299 |
my %options; |
| 300 |
my @optionstack; |
| 301 |
while (my $line = <$fh>) { |
| 302 |
chomp($line); |
| 303 |
my $linepos = "$filename:$."; |
| 304 |
$line =~ s/(^|\s)\#.*$//; |
| 305 |
$line =~ s/^\s*//; |
| 306 |
$line =~ s/\s*$//; |
| 307 |
next if $line eq ''; |
| 308 |
if ($line =~ /^[0-9]/ || $line =~ /^u\+/i) { |
| 309 |
my ($num,$command) = split ' ',$line,2; |
| 310 |
$num = oct($num) if $num =~ /^0/; |
| 311 |
$num = hex($num) if $num =~ s/^\$//; |
| 312 |
$num = hex($num) if $num =~ s/^u\+//i; |
| 313 |
#print "Line: $line, Num: $num, Command: $command\n"; |
| 314 |
my %o = %options; |
| 315 |
$o{definedat} = $linepos; |
| 316 |
$command = '' if lc($command) eq '<empty>'; |
| 317 |
unless (defined ($command)) { |
| 318 |
warn "$linepos: Use <empty> to declare an empty ". |
| 319 |
"glyph macro.\n"; next; }; |
| 320 |
$o{combining} = 1 |
| 321 |
if (($command =~ /(^|[^\#])\#1/) || |
| 322 |
(defined $o{envelope} && $o{envelope} =~ /(^|[^\#])\#1/)); |
| 323 |
if (isprivate($num) && |
| 324 |
optionname($o{onoption}) !~ /^(private|local)./) { |
| 325 |
warn sprintf "%s: Character U+%04X is in private area, ". |
| 326 |
"but has option '%s' (not 'private...' or 'local...').\n", |
| 327 |
$linepos,$num,optionname($o{onoption}); |
| 328 |
} |
| 329 |
push @{$characters[$num]}, [ $command, \%o ]; |
| 330 |
my $range = findrange2($num); |
| 331 |
if (defined $range) { ${$$range{options}}{optionname($o{onoption})} =1}; |
| 332 |
if ($unidata[$num]) { $ {$ {$unidata[$num]}{options}}{optionname($o{onoption})} =1}; |
| 333 |
$stats_charsperoption{define($o{onoption})}++; |
| 334 |
} else { |
| 335 |
my @cmd = split ' ',$line; |
| 336 |
if ($cmd[0] eq 'BEGIN' || $cmd[0] eq '{') { |
| 337 |
my %t = %options; |
| 338 |
push @optionstack, \%t; |
| 339 |
} elsif ($cmd[0] eq 'END' || $cmd[0] eq '}') { |
| 340 |
if (@optionstack) { |
| 341 |
my $t = pop @optionstack; |
| 342 |
%options = %$t |
| 343 |
} else { |
| 344 |
warn "$linepos: '$cmd[0]' outside a block.\n"; |
| 345 |
} |
| 346 |
} elsif ($cmd[0] eq 'OPTION') { |
| 347 |
$options{onoption} = $cmd[1]; |
| 348 |
unless (defined $knownoptions{$cmd[1]}) { |
| 349 |
$knownoptions{$cmd[1]} = []; |
| 350 |
} |
| 351 |
} elsif ($cmd[0] eq 'AUTOOPTION') { |
| 352 |
$options{onoption} = $cmd[1]; |
| 353 |
unless (defined $knownoptions{$cmd[1]}) { |
| 354 |
$knownoptions{$cmd[1]} = [ $cmd[2] ]; |
| 355 |
} else { |
| 356 |
# ************ DOUBLED |
| 357 |
push @{$knownoptions{$cmd[1]}}, $cmd[2]; |
| 358 |
} |
| 359 |
} elsif ($cmd[0] eq 'ENVELOPE') { |
| 360 |
$options{envelope} = join ' ',@cmd[1..$#cmd]; |
| 361 |
# } elsif ($cmd[0] eq 'GLOBAL') { |
| 362 |
# globalcode(join ' ',@cmd[1..$#cmd]); |
| 363 |
} elsif (($cmd[0] eq 'PROVIDE') || ($cmd[0] eq 'GLOBAL')) { |
| 364 |
my $prov = ''; |
| 365 |
if ($cmd[1] eq 'MULTILINE') { |
| 366 |
my ($e,$l) = (0); |
| 367 |
my $xlinepos = $linepos; |
| 368 |
while (!$e && defined($l = <$fh>)) { |
| 369 |
if ($l =~ /^\s*END\s*$/) { $e = 1 } |
| 370 |
else { $prov .= $l; } |
| 371 |
} |
| 372 |
unless ($e) { |
| 373 |
warn "$xlinepos: Unfinished PROVIDE MULTILINE.\n"; |
| 374 |
$prov = undef; |
| 375 |
} |
| 376 |
} else { |
| 377 |
$prov = join ' ',@cmd[1..$#cmd]; |
| 378 |
} |
| 379 |
chomp $prov; $prov =~ s/%$//s; |
| 380 |
if ($cmd[0] eq 'PROVIDE') { |
| 381 |
$options{provide} = getprovidehandler($prov); |
| 382 |
} else { |
| 383 |
globalcode($prov); } |
| 384 |
} elsif ($cmd[0] eq 'FONTENC') { |
| 385 |
$options{fontenc} = $cmd[1]; |
| 386 |
} elsif ($cmd[0] eq 'FONTFAMILY') { |
| 387 |
$options{fontfamily} = $cmd[1]; |
| 388 |
} elsif ($cmd[0] eq 'RIGHTLEFT') { |
| 389 |
$options{rightleft} = 1; |
| 390 |
} elsif ($cmd[0] eq 'TABLECODE') { |
| 391 |
$options{tablecode} = join ' ',@cmd[1..$#cmd]; |
| 392 |
} elsif ($cmd[0] eq 'CTRLGLYPH') { |
| 393 |
$options{ctrlglyph} = 1; |
| 394 |
} elsif ($cmd[0] eq 'COMBINECHAR') { |
| 395 |
if ($cmd[1] =~ /^U\+[0-9A-F]+$/i) { |
| 396 |
my ($n) = ($cmd[1] =~ /^U\+([0-9A-F]+)$/i); |
| 397 |
$options{combinechar} = hex($n); |
| 398 |
delete $options{combineglyph}; |
| 399 |
delete $options{combineoption}; |
| 400 |
} elsif ($cmd[1] =~ /^[a-z]+\/U\+[0-9A-F]+$/i) { |
| 401 |
my ($o,$n) = ($cmd[1] =~ /^([a-z]+)\/U\+([0-9A-F]+)$/i); |
| 402 |
$options{combinechar} = hex($n); |
| 403 |
$options{combineoption} = $o; |
| 404 |
delete $options{combineglyph}; |
| 405 |
} else { |
| 406 |
$options{combineglyph} = $cmd[1]; |
| 407 |
delete $options{combinechar}; |
| 408 |
delete $options{combineoption}; |
| 409 |
} |
| 410 |
} elsif ($cmd[0] eq 'LOADFONTENC') { |
| 411 |
$options{loadfontenc} = $cmd[1]; |
| 412 |
} elsif ($cmd[0] eq 'PACKAGE') { |
| 413 |
$options{package} = $cmd[1]; |
| 414 |
} elsif ($cmd[0] eq 'TABLEGLYPH') { |
| 415 |
$options{tableglyph} = join ' ',@cmd[1..$#cmd]; |
| 416 |
} elsif ($cmd[0] eq 'TABLEENVELOPE') { |
| 417 |
$options{tableenvelope} = join ' ',@cmd[1..$#cmd]; |
| 418 |
} else { |
| 419 |
warn "$linepos: Malformed line $line"; |
| 420 |
} |
| 421 |
} |
| 422 |
}; |
| 423 |
close $fh or die "Something went wrong when closing $filename: $!"; |
| 424 |
}; |
| 425 |
|
| 426 |
sub openfile ($$$%) { |
| 427 |
my ($filename,$header,$description,%attribs) = @_; |
| 428 |
my $file = $files{$filename}; |
| 429 |
unless (defined $file) { |
| 430 |
my $nowrite = 0; |
| 431 |
$nowrite = 1 if defined($onlyfile) && $filename !~ m@(^|/)$onlyfile$@; |
| 432 |
$fileswritten ++ unless $nowrite; |
| 433 |
$attribs{nowrite} = 1 if $nowrite; |
| 434 |
if (!$nowrite) { |
| 435 |
$file = new IO::File($filename,O_CREAT|O_WRONLY|O_EXCL) or |
| 436 |
die "Could not open $filename for writing: $!"; |
| 437 |
} else { |
| 438 |
print "Not writing file $filename\n" if $verbose; |
| 439 |
$devnull = new IO::File('/dev/null',O_WRONLY) unless ($devnull); |
| 440 |
$file = $devnull; } |
| 441 |
print $file "$header\n"; |
| 442 |
my @date = localtime; |
| 443 |
# fix to match advertised ucs package date |
| 444 |
# my $date = sprintf "%04d/%02d/%02d", |
| 445 |
# $date[5]+1900, $date[4]+1, $date[3] |
| 446 |
my $date="2022/08/07"; |
| 447 |
my $basename = $filename; $basename =~ s@.*/@@; |
| 448 |
print $file "\\ifx\\ProvidesFile\\undefined\\else |
| 449 |
\\ProvidesFile{$basename}[$date UCS: $description]% |
| 450 |
\\fi |
| 451 |
"; |
| 452 |
flush $file; |
| 453 |
$files{$filename} = $file; |
| 454 |
$file_attribs{$filename} = \%attribs; |
| 455 |
} |
| 456 |
return $file; |
| 457 |
} |
| 458 |
|
| 459 |
sub dumpcharacters() { |
| 460 |
for (my $i=0; $i<=$#characters; $i++) { |
| 461 |
next unless defined $characters[$i]; |
| 462 |
my %seen = (); |
| 463 |
for my $j (@{$characters[$i]}) { |
| 464 |
my $val = $$j[0]; |
| 465 |
my $options = $$j[1]; |
| 466 |
my $envelope = $$options{envelope}; |
| 467 |
if (define($envelope) ne '') { |
| 468 |
my $t = $val; |
| 469 |
$val = $envelope; |
| 470 |
$val =~ s/\$\$\$/$i/g; |
| 471 |
$val =~ s/\@\@\@/$t/g; |
| 472 |
} |
| 473 |
$val = "\\uc\@cmb$val" if ($$options{combining}); |
| 474 |
my $comment = define($$options{comment}); |
| 475 |
my $onoption = $$options{onoption}; |
| 476 |
next if $excludedoptions{(defined $onoption)?$onoption:'default'}; |
| 477 |
my $page = int($i/256); |
| 478 |
my $filename = "$targetdir/uni-$page.def"; |
| 479 |
#print "Character $i ($filename):\n"; |
| 480 |
my $range = sprintf("U+%04X..U+%04X", |
| 481 |
$page*256,$page*256+255); |
| 482 |
my $provide = $$options{provide}; |
| 483 |
my $header = "%%% Unicode to TeX mapping, file uni-$page.def, ". |
| 484 |
"$range, autogenerated by makeunidef.pl"; |
| 485 |
my $file = openfile($filename,$header,"Unicode data $range"); |
| 486 |
|
| 487 |
if (defined $provide) { |
| 488 |
$provide = $provides{$provide}; |
| 489 |
unless (defined $$provide{"done:$filename"}) { |
| 490 |
$$provide{"done:$filename"} = 1; |
| 491 |
print $file "$$provide{code}%\n"; |
| 492 |
#print "Providing $$provide{code} to $filename\n"; |
| 493 |
} |
| 494 |
} |
| 495 |
|
| 496 |
my $texcmd = ''; |
| 497 |
if (defined $val) { |
| 498 |
$texcmd = "\\uc\@dclc{$i}{". |
| 499 |
(defined $onoption?$onoption:'default'). |
| 500 |
"}{$val}"; |
| 501 |
} |
| 502 |
$texcmd .= "% $comment"; |
| 503 |
$texcmd .= " (missing)" unless defined $val; |
| 504 |
$texcmd =~ s/\s+$//; |
| 505 |
print $file "$texcmd\n"; |
| 506 |
my $tag = define($onoption); |
| 507 |
if (defined $seen{$tag}) { |
| 508 |
warn sprintf |
| 509 |
"%s: Character U+%04X(%s) redefined. ". |
| 510 |
"(First definition in %s).\n", |
| 511 |
$$options{definedat},$i,optionname($onoption),$seen{$tag}; |
| 512 |
} else { |
| 513 |
$seen{$tag} = define($$options{definedat}); |
| 514 |
} |
| 515 |
#print "$texcmd\n"; |
| 516 |
} |
| 517 |
} |
| 518 |
flushfiles(); |
| 519 |
} |
| 520 |
|
| 521 |
sub loadunidata($) { |
| 522 |
my ($filename) = @_; |
| 523 |
my $f = openreadfile $unidata or |
| 524 |
die "Could not open $unidata for reading: $!"; |
| 525 |
my $inrange = undef; |
| 526 |
my $rangestart; |
| 527 |
while (my $line = <$f>) { |
| 528 |
chomp($line); |
| 529 |
my @line = split ';', $line; |
| 530 |
my %line = (); |
| 531 |
$line{num} = hex($line[0]); |
| 532 |
$line{name} = $line[1]; |
| 533 |
$line{category} = $line[2]; |
| 534 |
$line{combining} = $line[3]; |
| 535 |
$line{flags} = $line[4]; |
| 536 |
my @compose = split ' ',$line[5]; |
| 537 |
my $compflag = undef; |
| 538 |
if (defined $compose[0] && $compose[0] =~ /^</) { |
| 539 |
$compflag = shift @compose; |
| 540 |
$compflag =~ s/^<//; |
| 541 |
$compflag =~ s/>$//; |
| 542 |
} |
| 543 |
$line{compose} = \@compose; |
| 544 |
$line{compflag} = $compflag; |
| 545 |
$line{decdigit} = $line[6]; |
| 546 |
$line{digit} = $line[7]; |
| 547 |
$line{numerical} = $line[8]; |
| 548 |
$line{bracket} = $line[9]; |
| 549 |
$line{alias} = $line[10]; |
| 550 |
$line{comment} = $line[11]; |
| 551 |
$line{upcase} = $line[12]; |
| 552 |
$line{downcase} = $line[13]; |
| 553 |
$line{titlecase} = $line[14]; |
| 554 |
$line{case3} = $line[15]; |
| 555 |
|
| 556 |
my ($rangename,$rangedir) = |
| 557 |
($line{name} =~ /^\<(.*), (First|Last)\>$/); |
| 558 |
if (defined $rangename) { |
| 559 |
if ($rangedir eq 'First') { |
| 560 |
if (defined $inrange) { |
| 561 |
warn "$filename:$.: Range '$rangename' started while ". |
| 562 |
"in range '$inrange'.\n"; |
| 563 |
} |
| 564 |
#warn "Starting: '$rangename'"; |
| 565 |
$inrange = $rangename; |
| 566 |
$rangestart = $line{num}; |
| 567 |
next; |
| 568 |
} elsif ($rangedir eq 'Last') { |
| 569 |
if (defined $inrange && $rangename eq $inrange) { |
| 570 |
#warn "Ending: '$inrange'"; |
| 571 |
$line{rangestart} = $rangestart; |
| 572 |
$line{rangeend} = $line{num}; |
| 573 |
# $line{num} = $line{rangestart}; |
| 574 |
$line{name} = "Contained in range '$rangename'"; |
| 575 |
#printf "Range found: U+%04X..U+%04X '%s'\n", |
| 576 |
#$rangestart, $line{num}, $rangename if $verbose; |
| 577 |
} elsif (defined $inrange) { |
| 578 |
warn "$filename:$.: ". |
| 579 |
"Range '$inrange' ended by '$rangename'"; |
| 580 |
} |
| 581 |
$inrange = undef; |
| 582 |
} else { |
| 583 |
die "Internal error"; |
| 584 |
} |
| 585 |
} else { |
| 586 |
if (defined $inrange) { |
| 587 |
warn "$filename:$.: ". |
| 588 |
"Range '$inrange' not ended on next line.\n"; |
| 589 |
$inrange = undef; |
| 590 |
} |
| 591 |
} |
| 592 |
$unidata[$line{num}] = \%line; |
| 593 |
push @ranges, \%line if $line{rangestart}; |
| 594 |
} |
| 595 |
close $f or die "Something went wrong when closing $filename: $!"; |
| 596 |
} |
| 597 |
|
| 598 |
sub generate_globals() { |
| 599 |
while (my ($option,$pkgs) = each %knownoptions) { |
| 600 |
if (!$excludedoptions{$option}) { |
| 601 |
if (@$pkgs == 1) { |
| 602 |
my $pkg = $$pkgs[0]; |
| 603 |
globalcode("\\DeclareUnicodeOption[$pkg]{$option}\%"); |
| 604 |
} else { |
| 605 |
globalcode("\\DeclareUnicodeOption{$option}\%"); |
| 606 |
for my $pkg (@$pkgs) { |
| 607 |
globalcode("\\LinkUnicodeOptionToPkg{$option}{$pkg}\%"); |
| 608 |
} |
| 609 |
} |
| 610 |
} else { |
| 611 |
globalcode |
| 612 |
("\\XDeclareUnicodeOption{}{$option}{}{". |
| 613 |
"\\PackageError{ucs}{Option $option has not been generated}". |
| 614 |
"{run makeunidef.pl without --exclude $option.}}{}"); |
| 615 |
} |
| 616 |
} |
| 617 |
flushfiles(); |
| 618 |
} |
| 619 |
|
| 620 |
sub define($) { |
| 621 |
my $str = shift; |
| 622 |
return $str if defined $str; |
| 623 |
return ''; |
| 624 |
} |
| 625 |
|
| 626 |
sub define0($) { |
| 627 |
my $str = shift; |
| 628 |
return $str if defined $str; |
| 629 |
return 0; |
| 630 |
} |
| 631 |
|
| 632 |
sub findrange($) { |
| 633 |
my $i = shift; |
| 634 |
for my $r (@ranges) { |
| 635 |
if ($i>=$$r{rangestart} && $i<=$$r{rangeend}) { |
| 636 |
return $$r{name}; |
| 637 |
} |
| 638 |
} |
| 639 |
return undef; |
| 640 |
} |
| 641 |
|
| 642 |
sub findrange2($) { |
| 643 |
my $i = shift; |
| 644 |
for my $r (@ranges) { |
| 645 |
if ($i>=$$r{rangestart} && $i<=$$r{rangeend}) { |
| 646 |
return $r; |
| 647 |
} |
| 648 |
} |
| 649 |
return undef; |
| 650 |
} |
| 651 |
|
| 652 |
sub generate() { |
| 653 |
for (my $i=0; $i<=$#unidata; $i++) { |
| 654 |
my $char = $unidata[$i]; |
| 655 |
my $tex = undef; |
| 656 |
next unless defined $char; |
| 657 |
next unless %$char; |
| 658 |
next if (defined $characters[$i]); |
| 659 |
if (@{$$char{compose}}) { |
| 660 |
my $success = 1; |
| 661 |
for my $comp (@{$$char{compose}}) { |
| 662 |
$success = 0 unless defined $characters[hex($comp)]; |
| 663 |
} |
| 664 |
if ($success) { |
| 665 |
my $compflag = $$char{compflag}; |
| 666 |
my $compmode = 'none'; |
| 667 |
if (define($compflag) eq '') { $compmode = 'normal' } |
| 668 |
elsif ($compflag eq 'fraction') { $compmode = 'normal' } |
| 669 |
elsif ($compflag eq 'compat') { $compmode = 'normal' } |
| 670 |
elsif ($compflag eq 'noBreak') { $compmode = 'normal' } |
| 671 |
elsif ($compflag eq 'super') { $compmode = 'normal.super' } |
| 672 |
elsif ($compflag eq 'sub') { $compmode = 'normal.sub' } |
| 673 |
elsif ($compflag eq 'font') { $compmode = 'normal' } |
| 674 |
elsif ($compflag eq 'circle') { $compmode = 'normal.circle' } |
| 675 |
elsif ($compflag eq 'square') { $compmode = 'normal.square' } |
| 676 |
elsif ($compflag eq 'wide') { $compmode = 'normal.wide' } |
| 677 |
elsif ($compflag eq 'narrow') { $compmode = 'normal.narrow' } |
| 678 |
elsif ($compflag eq 'vertical') { $compmode = 'normal.vertical' } |
| 679 |
elsif ($compflag eq 'small') { $compmode = 'normal.small' } |
| 680 |
else { warn "Unknown composition flag \<$compflag\> ". |
| 681 |
"in character ".sprintf("%04X",$$char{num}); }; |
| 682 |
if ($compmode =~ /^normal/) { |
| 683 |
$tex = join '', map { |
| 684 |
my $a = hex($_); |
| 685 |
"\\unichar{$a}" } @{$$char{compose}}; |
| 686 |
if ($compmode eq 'normal.super') { |
| 687 |
$tex = "\\unicodesuper{$tex}"; |
| 688 |
} elsif ($compmode eq 'normal.sub') { |
| 689 |
$tex = "\\unicodesub{$tex}"; |
| 690 |
} elsif ($compmode eq 'normal.square') { |
| 691 |
$tex = "\\unicodesquare{$tex}"; |
| 692 |
} elsif ($compmode eq 'normal.circle') { |
| 693 |
$tex = "\\unicodecircle{$tex}"; |
| 694 |
} elsif ($compmode eq 'normal.wide') { |
| 695 |
$tex = "\\unicodewide{$tex}"; |
| 696 |
} elsif ($compmode eq 'normal.narrow') { |
| 697 |
$tex = "\\unicodenarrow{$tex}"; |
| 698 |
} elsif ($compmode eq 'normal.vertical') { |
| 699 |
$tex = "\\unicodevertical{$tex}"; |
| 700 |
} elsif ($compmode eq 'normal.small') { |
| 701 |
$tex = "\\unicodesmall{$tex}"; |
| 702 |
} |
| 703 |
} elsif ($compmode eq 'none') { |
| 704 |
} else { |
| 705 |
die "Unknown composition mode $compmode. INTERNAL ERROR"; |
| 706 |
} |
| 707 |
} |
| 708 |
if (!$success && $$char{numerical} ne '') { |
| 709 |
#print "NUMERICAL $i\n"; |
| 710 |
$tex = $$char{numerical}; |
| 711 |
} |
| 712 |
#print "C: $$char{compflag}\n" if defined $$char{compflag}; |
| 713 |
|
| 714 |
if ($tex) { |
| 715 |
push @{$characters[$i]}, |
| 716 |
[ "$tex", #"\\dirtyunicode{$i}{$tex}" |
| 717 |
{ dirty => 1, |
| 718 |
onoption => 'autogenerated' } ]; |
| 719 |
unless (defined $knownoptions{autogenerated}) { |
| 720 |
$knownoptions{autogenerated} = []; |
| 721 |
} |
| 722 |
} |
| 723 |
} |
| 724 |
} |
| 725 |
} |
| 726 |
|
| 727 |
sub initcharacters() { |
| 728 |
# for (my $i=0; $i<128; $i++) { |
| 729 |
# push @{$characters[$i]}, [ "\\char$i\\relax", { comment => 'ASCII' } ]; |
| 730 |
# } |
| 731 |
} |
| 732 |
|
| 733 |
sub makecomments() { |
| 734 |
for (my $i=0; $i<$#unidata; $i++) { |
| 735 |
next unless defined $unidata[$i]; |
| 736 |
push @{$characters[$i]}, [] unless defined $characters[$i]; |
| 737 |
for my $char (@{$characters[$i]}) { |
| 738 |
my $options = $$char[1]; |
| 739 |
next if defined $$options{comment}; |
| 740 |
my $onoption = $$options{onoption}; |
| 741 |
#print "CHAR: $char\n"; |
| 742 |
#print "CHAR2: $$char[1]\n"; |
| 743 |
my $utf8 = utf16toutf8($i); |
| 744 |
my $comment = ''; |
| 745 |
$comment .= "OPTION: $onoption, " if ($onoption); |
| 746 |
$comment .= sprintf "0x%04X = %d (%s) - %s", |
| 747 |
$i ,$i, $utf8, $ {$unidata[$i]}{name}; |
| 748 |
$$options{comment} = $comment; |
| 749 |
$$char[1] = $options; |
| 750 |
} |
| 751 |
} |
| 752 |
} |
| 753 |
|
| 754 |
sub utf16toutf8($) { |
| 755 |
my $char = shift; |
| 756 |
if ($char<0x80) { return $char; } |
| 757 |
if ($char<0x0800) { |
| 758 |
return chr(0xc0+int($char/64)).chr(0x80+($char%64)); } |
| 759 |
return chr(0xe0+int($char/0x1000)). |
| 760 |
chr(0x80+int(($char%0x1000)/64)). |
| 761 |
chr(0x80+($char%64)); |
| 762 |
} |
| 763 |
|
| 764 |
sub flushfiles() { |
| 765 |
for my $file (values %files) { |
| 766 |
flush $file; |
| 767 |
} |
| 768 |
} |
| 769 |
|
| 770 |
sub closefiles() { |
| 771 |
for my $filename (keys %files) { |
| 772 |
my $file = $files{$filename}; |
| 773 |
my $attrib = $file_attribs{$filename}; |
| 774 |
next if $$attrib{nowrite}; |
| 775 |
my $cs = 'utf-8'; |
| 776 |
$cs = $$attrib{coding} if defined $$attrib{coding}; |
| 777 |
print $file <<EOT; |
| 778 |
%%% Local Variables: |
| 779 |
%%% mode: latex |
| 780 |
%%% coding: $cs |
| 781 |
%%% End: |
| 782 |
EOT |
| 783 |
close $file or die "Something went wrong when closing $filename: $!"; |
| 784 |
} |
| 785 |
if (define0($fileswritten)==0) { |
| 786 |
warn "No files written"; } |
| 787 |
} |
| 788 |
|
| 789 |
sub showhelp() { |
| 790 |
print <<EOT; |
| 791 |
Generates uni-....def files for ucs.sty |
| 792 |
|
| 793 |
Options: |
| 794 |
--database <file> Set unicode database (default: UnicodeData.txt; short -d) |
| 795 |
--nocomments Disable comments in generated files |
| 796 |
--targetdir <dir> Set target directory (default: .; short: -t) |
| 797 |
--verbose Be verbose (short: -v) |
| 798 |
--help This page (short: -h) |
| 799 |
<file(s)> Use this configfile(s) |
| 800 |
|
| 801 |
Configfiles consists of lines, each like this |
| 802 |
<unicode number> <LaTeX-Code> |
| 803 |
where <unicode number> may be prefixed by 0x or 0 to denote hex or oct. |
| 804 |
Lines beginning with # denote a comment. |
| 805 |
EOT |
| 806 |
} |
| 807 |
|
| 808 |
sub optioneq($$) { |
| 809 |
my ($a,$b) = @_; |
| 810 |
$a = 'default' if define($a) eq ''; |
| 811 |
$b = 'default' if define($b) eq ''; |
| 812 |
return $a eq $b; |
| 813 |
} |
| 814 |
|
| 815 |
sub getchar($$) { |
| 816 |
my ($c,$option) = @_; |
| 817 |
|
| 818 |
#print "getchar($c,$option)\n"; |
| 819 |
return undef unless $characters[$c]; |
| 820 |
for my $char (@{$characters[$c]}) { |
| 821 |
my $options = $$char[1]; |
| 822 |
return $char if define($$options{onoption}) eq $option; |
| 823 |
return $char if define($$options{onoption}) eq '' && $option eq 'default'; |
| 824 |
} |
| 825 |
return undef; |
| 826 |
} |
| 827 |
|
| 828 |
sub expand_tablespec($); |
| 829 |
|
| 830 |
sub expand_tablespec($) { |
| 831 |
my $tablesize = 256; |
| 832 |
|
| 833 |
my $spec = shift; |
| 834 |
my @specs = (); |
| 835 |
$spec = '*:*' if $spec eq '*'; |
| 836 |
my @spec = ($spec =~ /^([a-z0-9]+|\*):(.*)$/i); |
| 837 |
my $option = $spec[0]; |
| 838 |
my $range = $spec[1]; |
| 839 |
if (define($option) eq '') { |
| 840 |
warn "Invalid table specifier '$spec'. Ignoring"; |
| 841 |
return (); |
| 842 |
} |
| 843 |
if ($option eq '*') { |
| 844 |
push @specs, expand_tablespec("default:$range"); |
| 845 |
for my $o (sort keys %knownoptions) { |
| 846 |
push @specs, expand_tablespec("$o:$range") unless $o eq 'default'; |
| 847 |
} |
| 848 |
} elsif ($range eq '*') { |
| 849 |
for (my $i=0; $i<=$#characters; $i+=$tablesize) { |
| 850 |
for (my $j=0; $j<$tablesize; $j++) { |
| 851 |
if (my $c = getchar($i+$j,$option)) { |
| 852 |
my $options = $$c[1]; |
| 853 |
#next if $$options{dirty}; |
| 854 |
push @specs, expand_tablespec |
| 855 |
(sprintf "%s:%04X-%04X", |
| 856 |
$option,$i,$i+$tablesize-1); |
| 857 |
last; |
| 858 |
} |
| 859 |
} |
| 860 |
} |
| 861 |
} else { |
| 862 |
warn "Unknown option '$option' in table spec '$spec'. Ignoring" |
| 863 |
unless $option eq 'default' || defined $knownoptions{$option}; |
| 864 |
my @range = map { hex } split '-', $range, 2; |
| 865 |
|
| 866 |
push @specs, { option => $option, |
| 867 |
start => $range[0], |
| 868 |
end => $range[1], |
| 869 |
format => 'table', }; |
| 870 |
} |
| 871 |
return @specs; |
| 872 |
} |
| 873 |
|
| 874 |
sub dump_charactertables() { |
| 875 |
my @tables = map { |
| 876 |
expand_tablespec($_) |
| 877 |
} map { |
| 878 |
s/\s+$//; s/^\s+//; $_ |
| 879 |
} split /,/, $tables_to_dump; |
| 880 |
for my $spec (@tables) { |
| 881 |
dump_charactertable($spec); |
| 882 |
} |
| 883 |
flushfiles(); |
| 884 |
} |
| 885 |
|
| 886 |
sub default_entrydata($) { |
| 887 |
my $i = shift; |
| 888 |
return |
| 889 |
( 'code' => $i, |
| 890 |
'hexcode' => sprintf("%04X", $i), |
| 891 |
'hexcodebeforelast' => sprintf("%1X", int($i%256/16)), |
| 892 |
); |
| 893 |
} |
| 894 |
|
| 895 |
sub make_entrydata($$$$); |
| 896 |
sub make_entrydata($$$$) { |
| 897 |
my ($option,$i,$options,$hasctrlglyph) = @_; |
| 898 |
my %entrydata = default_entrydata($i); |
| 899 |
if (defined $$options{tableglyph}) { |
| 900 |
warn sprintf "%s: Character %s/U+%04X has ". |
| 901 |
"TABLEGLYPH specification.\n", |
| 902 |
$$options{definedat},$option,$i; |
| 903 |
} |
| 904 |
if ($$options{ctrlglyph}) { |
| 905 |
if ($hasctrlglyph) { |
| 906 |
%entrydata = %{make_entrydata('ctrlglyphs',$i,$hasctrlglyph,0)}; |
| 907 |
$entrydata{ctrlglyph} = 1; |
| 908 |
$entrydata{option} = 'ctrlglyphs'; |
| 909 |
return \%entrydata; |
| 910 |
} else { |
| 911 |
warn sprintf "%s: Char U+%04X has no control ". |
| 912 |
"glyph.\n",$$options{definedat},$i; |
| 913 |
$entrydata{tableglyph} = |
| 914 |
sprintf('{\tiny %04X}',$i); |
| 915 |
} |
| 916 |
} |
| 917 |
$entrydata{dirty} = ($$options{dirty})?1:0; |
| 918 |
$entrydata{rightleft} = ($$options{rightleft})?1:0; |
| 919 |
$entrydata{combining} = ($$options{combining})?1:0; |
| 920 |
$entrydata{fontfamily} = $$options{fontfamily} |
| 921 |
if defined $$options{fontfamily}; |
| 922 |
$entrydata{combinechar} = $$options{combinechar} |
| 923 |
if defined $$options{combinechar}; |
| 924 |
$entrydata{combineglyph} = $$options{combineglyph} |
| 925 |
if defined $$options{combineglyph}; |
| 926 |
$entrydata{combineoption} = $$options{combineoption} |
| 927 |
if defined $$options{combineoption}; |
| 928 |
$entrydata{fontenc} = $$options{fontenc} |
| 929 |
if defined $$options{fontenc}; |
| 930 |
if (defined $$options{tableglyph}) { |
| 931 |
warn sprintf "Add to ctrlglyphs.ucf:\nU+%04X\t%s\n", |
| 932 |
$i,$$options{tableglyph} |
| 933 |
unless $hasctrlglyph; |
| 934 |
$entrydata{tableglyph} = $$options{tableglyph}; |
| 935 |
} |
| 936 |
if (defined $$options{tableenvelope}) { |
| 937 |
warn sprintf "%s: Character %s/u+%04x has ". |
| 938 |
"TABLEENVELOPE specification.\n", |
| 939 |
$$options{definedat},$option,$i; |
| 940 |
$entrydata{tableenvelope} = $$options{tableenvelope}; |
| 941 |
$entrydata{tableenvelope} =~ s/\@\@\@/\#1/g; |
| 942 |
$entrydata{"tableenvelope.optspec"} = '#1'; |
| 943 |
} |
| 944 |
# $entrydata{fontenc} = 'T1' |
| 945 |
# unless defined $entrydata{fontenc}; |
| 946 |
for my $x (qw/loadfontenc package tablecode/) { |
| 947 |
$entrydata{"$x.option"} = $$options{$x}; } |
| 948 |
return \%entrydata; |
| 949 |
} |
| 950 |
|
| 951 |
sub dump_charactertable($$$$) { |
| 952 |
my $spec = shift; |
| 953 |
my $option = $$spec{option}; |
| 954 |
my $start = $$spec{start}; |
| 955 |
my $end = $$spec{end}; |
| 956 |
my $format = $$spec{format}; |
| 957 |
|
| 958 |
my $hstart = sprintf "%04X", $start; |
| 959 |
my $hend = sprintf "%04X", $end; |
| 960 |
my $filename = "$tabledir/table-$option-$hstart-$hend.tex"; |
| 961 |
|
| 962 |
print "Generating $filename\n" if $verbose; |
| 963 |
|
| 964 |
my $file = openfile("$tabledir/table-$option-$hstart-$hend.tex", |
| 965 |
"%%% table for option $option, U+$hstart..U+$hend, $autogen", |
| 966 |
"Unicode chart U+$hstart..U+$hend"); |
| 967 |
|
| 968 |
print $file <<EOT; |
| 969 |
\\input{tables.inc} |
| 970 |
|
| 971 |
\\def\\gformat{$format}% |
| 972 |
\\def\\goption{$option}% |
| 973 |
\\def\\ghstart{$hstart}% |
| 974 |
\\def\\ghend{$hend}% |
| 975 |
\\def\\gstart{$start}% |
| 976 |
\\def\\gend{$end}% |
| 977 |
EOT |
| 978 |
|
| 979 |
my $table = "\\tstart%\n"; |
| 980 |
my %fontencs = (); |
| 981 |
my %packages = (); |
| 982 |
my %tablecode = (); |
| 983 |
for (my $i=$start; $i<=$end; $i++) { |
| 984 |
my @chars = (); |
| 985 |
# https://stackoverflow.com/a/41983753/1158383 |
| 986 |
@chars = @{$characters[$i]} if @{$characters[$i]}; |
| 987 |
my $entry = ($unidata[$i])?"\\noglyph":"\\unassigned"; |
| 988 |
my $found = 0; |
| 989 |
my $hasctrlglyph = undef; |
| 990 |
for my $char (@chars) { |
| 991 |
my $options = $$char[1]; |
| 992 |
if (define($$char[0]) ne '' || |
| 993 |
define($$options{envelope}) ne '') { |
| 994 |
if (optioneq($$options{onoption},'ctrlglyphs')) { |
| 995 |
$hasctrlglyph = $options; |
| 996 |
} |
| 997 |
} |
| 998 |
} |
| 999 |
|
| 1000 |
my $entrydata = {default_entrydata($i)}; |
| 1001 |
|
| 1002 |
for my $char (@chars) { |
| 1003 |
my $options = $$char[1]; |
| 1004 |
if (define($$char[0]) ne '' || |
| 1005 |
define($$options{envelope}) ne '') { |
| 1006 |
if (optioneq($$options{onoption},$option)) { |
| 1007 |
$entry = '\glyph'; |
| 1008 |
$entrydata = make_entrydata($option,$i,$options, |
| 1009 |
$hasctrlglyph); |
| 1010 |
$fontencs{$$entrydata{fontenc}} = 1 |
| 1011 |
if defined $$entrydata{fontenc} && |
| 1012 |
$$entrydata{fontenc} ne 'T1'; |
| 1013 |
$fontencs{$$entrydata{'loadfontenc.option'}} = 1 |
| 1014 |
if defined $$entrydata{'loadfontenc.option'}; |
| 1015 |
if (defined $$entrydata{"package.option"}) { |
| 1016 |
for my $p (split ';', $$entrydata{'package.option'}) { |
| 1017 |
$packages{$p} = 1; }} |
| 1018 |
$tablecode{$$entrydata{"tablecode.option"}} = 1 |
| 1019 |
if defined $$entrydata{"tablecode.option"}; |
| 1020 |
$found = 1; |
| 1021 |
} elsif (!$found) { |
| 1022 |
if (!$$options{dirty} || $entry eq '') { |
| 1023 |
$entry = '\withoption'; |
| 1024 |
} |
| 1025 |
} |
| 1026 |
} |
| 1027 |
} |
| 1028 |
for my $i (keys %$entrydata) { |
| 1029 |
next if $i =~ /\./; |
| 1030 |
my $os = define($$entrydata{"$i.optspec"}); |
| 1031 |
$table .= "\\gdef\\a$i$os\{$$entrydata{$i}\}%\n"; } |
| 1032 |
my $comment = sprintf "%04X %s %s", $i, utf16toutf8($i), |
| 1033 |
define($unidata[$i]->{name}); |
| 1034 |
$table .= "$entry\% $comment\n"; |
| 1035 |
} |
| 1036 |
$table .= "\\tend%\n"; |
| 1037 |
|
| 1038 |
print $file "\\def\\gfontencs{",join(',',keys %fontencs,'T1'),"}%\n"; |
| 1039 |
for my $p (keys %packages) { |
| 1040 |
$p = "{$p}" unless $p =~ /\}$/; |
| 1041 |
print $file "\\usepackage$p%\n"; |
| 1042 |
} |
| 1043 |
for my $c (keys %tablecode) { |
| 1044 |
print $file "$c%\n"; |
| 1045 |
} |
| 1046 |
print $file $table; |
| 1047 |
flush $file; |
| 1048 |
} |
| 1049 |
|
| 1050 |
|
| 1051 |
sub parseargs() { |
| 1052 |
#Getopt::Long::Configure(qw/bundling/); |
| 1053 |
my %opt = (); |
| 1054 |
unless (GetOptions(\%opt,qw/config|configfile|c=s@ |
| 1055 |
exclude|ex=s@ |
| 1056 |
database|db|d=s |
| 1057 |
comments! |
| 1058 |
names! |
| 1059 |
data! |
| 1060 |
loadunidata! |
| 1061 |
tables=s |
| 1062 |
tabledir=s |
| 1063 |
targetdir|dir|t|target=s |
| 1064 |
help|h |
| 1065 |
verbose|v |
| 1066 |
compress! |
| 1067 |
onlyfile=s |
| 1068 |
/)) { |
| 1069 |
die "Bad command line options, try --help"; |
| 1070 |
} |
| 1071 |
if ($opt{help}) { |
| 1072 |
showhelp(); |
| 1073 |
exit; |
| 1074 |
} |
| 1075 |
unless ($opt{forceasterisk}) { |
| 1076 |
@ARGV = grep { if (/\*/ && !-e $_) { |
| 1077 |
print "Configfile $_ seems to be unmatched wildcard. Ignoring.\n" |
| 1078 |
if $opt{verbose}; |
| 1079 |
0; } else { 1; } |
| 1080 |
} @ARGV; |
| 1081 |
} |
| 1082 |
push @{$opt{config}}, @ARGV if @ARGV; |
| 1083 |
die "You must supply at least one config file" |
| 1084 |
unless defined $opt{config}; |
| 1085 |
$opt{exclude} = [] unless defined $opt{exclude}; |
| 1086 |
@{$opt{exclude}} = split /,/,join ',',@{$opt{exclude}}; |
| 1087 |
$opt{database} = 'UnicodeData.txt' unless defined $opt{database}; |
| 1088 |
$opt{comments} = 1 unless defined $opt{comments}; |
| 1089 |
$opt{names} = 1 unless defined $opt{names}; |
| 1090 |
$opt{data} = 1 unless defined $opt{data}; |
| 1091 |
$opt{targetdir} = '.' unless defined $opt{targetdir}; |
| 1092 |
$opt{loadunidata} = 1 unless defined $opt{loadunidata}; |
| 1093 |
$opt{tables} = '*' if $opt{tabledir} && !defined $opt{tables}; |
| 1094 |
$opt{tabledir} = '.' unless defined $opt{tabledir}; |
| 1095 |
$opt{compress} = 1 unless defined $opt{compress}; |
| 1096 |
@configfiles = @{$opt{config}}; |
| 1097 |
$unidata = $opt{database}; |
| 1098 |
$createcomments = $opt{comments}; |
| 1099 |
$targetdir = $opt{targetdir}; |
| 1100 |
$verbose = $opt{verbose}; |
| 1101 |
$generate_uninames = $opt{names}; |
| 1102 |
$tabledir = $opt{tabledir}; |
| 1103 |
$tables_to_dump = $opt{tables}; |
| 1104 |
$dump_unidata = $opt{data}; |
| 1105 |
$loadunidata = $opt{loadunidata}; |
| 1106 |
$compressnames = $opt{compress}; |
| 1107 |
$onlyfile = $opt{onlyfile}; |
| 1108 |
%excludedoptions = map { $_ => 1 } @{$opt{exclude}}; |
| 1109 |
} |
| 1110 |
|
| 1111 |
sub untaint($) { |
| 1112 |
my $str = shift; |
| 1113 |
my ($unt) = ($str =~ /^(.*)$/); |
| 1114 |
return $unt; |
| 1115 |
} |
| 1116 |
|
| 1117 |
sub unlinkfile($) { |
| 1118 |
my $fullname = shift; |
| 1119 |
return 1 if defined($onlyfile) && $fullname !~ m@(^|/)$onlyfile$@; |
| 1120 |
return 1 unless -e $fullname; |
| 1121 |
my $f = new IO::File($fullname,O_RDONLY) or |
| 1122 |
die "Could not open $fullname for reading: $!"; |
| 1123 |
my $line = <$f>; |
| 1124 |
close $f; |
| 1125 |
unless ($line =~ /^\%\%\%.*autogenerated by makeunidef.pl/) { |
| 1126 |
die "File $fullname was not generated by me, ". |
| 1127 |
"will not overwrite it"; } |
| 1128 |
unlink untaint($fullname) or |
| 1129 |
die "Could not remove $fullname: $!"; |
| 1130 |
#print "FILE: $file\n"; |
| 1131 |
} |
| 1132 |
|
| 1133 |
sub unlinkfiles($$) { |
| 1134 |
my ($pattern,$dir) = @_; |
| 1135 |
my $d = new IO::Handle; |
| 1136 |
opendir $d, $dir or |
| 1137 |
die "Could not open directory $targetdir: $!"; |
| 1138 |
while (my $file = readdir $d) { |
| 1139 |
#print "F: $file\n"; |
| 1140 |
next unless $file =~ /$pattern/x; |
| 1141 |
#print "UF: $file\n"; |
| 1142 |
unlinkfile("$dir/$file") or |
| 1143 |
die "Could not unlink $dir/$file"; |
| 1144 |
} |
| 1145 |
} |
| 1146 |
|
| 1147 |
$huffman_decoder = '\count255=128 |
| 1148 |
\loop\ifnum\count255<256\relax |
| 1149 |
\catcode\count255=13 |
| 1150 |
\advance\count255by1\relax |
| 1151 |
\repeat |
| 1152 |
\catcode`\G=13 |
| 1153 |
\catcode`\H=13 |
| 1154 |
\catcode`\I=13 |
| 1155 |
\def\uncompress{% |
| 1156 |
\def^^80{\0\0\0\0\0\0\0}% |
| 1157 |
\def^^81{\0\0\0\0\0\0\1}% |
| 1158 |
\def^^82{\0\0\0\0\0\1\0}% |
| 1159 |
\def^^83{\0\0\0\0\0\1\1}% |
| 1160 |
\def^^84{\0\0\0\0\1\0\0}% |
| 1161 |
\def^^85{\0\0\0\0\1\0\1}% |
| 1162 |
\def^^86{\0\0\0\0\1\1\0}% |
| 1163 |
\def^^87{\0\0\0\0\1\1\1}% |
| 1164 |
\def^^88{\0\0\0\1\0\0\0}% |
| 1165 |
\def^^89{\0\0\0\1\0\0\1}% |
| 1166 |
\def^^8a{\0\0\0\1\0\1\0}% |
| 1167 |
\def^^8b{\0\0\0\1\0\1\1}% |
| 1168 |
\def^^8c{\0\0\0\1\1\0\0}% |
| 1169 |
\def^^8d{\0\0\0\1\1\0\1}% |
| 1170 |
\def^^8e{\0\0\0\1\1\1\0}% |
| 1171 |
\def^^8f{\0\0\0\1\1\1\1}% |
| 1172 |
\def^^90{\0\0\1\0\0\0\0}% |
| 1173 |
\def^^91{\0\0\1\0\0\0\1}% |
| 1174 |
\def^^92{\0\0\1\0\0\1\0}% |
| 1175 |
\def^^93{\0\0\1\0\0\1\1}% |
| 1176 |
\def^^94{\0\0\1\0\1\0\0}% |
| 1177 |
\def^^95{\0\0\1\0\1\0\1}% |
| 1178 |
\def^^96{\0\0\1\0\1\1\0}% |
| 1179 |
\def^^97{\0\0\1\0\1\1\1}% |
| 1180 |
\def^^98{\0\0\1\1\0\0\0}% |
| 1181 |
\def^^99{\0\0\1\1\0\0\1}% |
| 1182 |
\def^^9a{\0\0\1\1\0\1\0}% |
| 1183 |
\def^^9b{\0\0\1\1\0\1\1}% |
| 1184 |
\def^^9c{\0\0\1\1\1\0\0}% |
| 1185 |
\def^^9d{\0\0\1\1\1\0\1}% |
| 1186 |
\def^^9e{\0\0\1\1\1\1\0}% |
| 1187 |
\def^^9f{\0\0\1\1\1\1\1}% |
| 1188 |
\def^^a0{\0\1\0\0\0\0\0}% |
| 1189 |
\def^^a1{\0\1\0\0\0\0\1}% |
| 1190 |
\def^^a2{\0\1\0\0\0\1\0}% |
| 1191 |
\def^^a3{\0\1\0\0\0\1\1}% |
| 1192 |
\def^^a4{\0\1\0\0\1\0\0}% |
| 1193 |
\def^^a5{\0\1\0\0\1\0\1}% |
| 1194 |
\def^^a6{\0\1\0\0\1\1\0}% |
| 1195 |
\def^^a7{\0\1\0\0\1\1\1}% |
| 1196 |
\def^^a8{\0\1\0\1\0\0\0}% |
| 1197 |
\def^^a9{\0\1\0\1\0\0\1}% |
| 1198 |
\def^^aa{\0\1\0\1\0\1\0}% |
| 1199 |
\def^^ab{\0\1\0\1\0\1\1}% |
| 1200 |
\def^^ac{\0\1\0\1\1\0\0}% |
| 1201 |
\def^^ad{\0\1\0\1\1\0\1}% |
| 1202 |
\def^^ae{\0\1\0\1\1\1\0}% |
| 1203 |
\def^^af{\0\1\0\1\1\1\1}% |
| 1204 |
\def^^b0{\0\1\1\0\0\0\0}% |
| 1205 |
\def^^b1{\0\1\1\0\0\0\1}% |
| 1206 |
\def^^b2{\0\1\1\0\0\1\0}% |
| 1207 |
\def^^b3{\0\1\1\0\0\1\1}% |
| 1208 |
\def^^b4{\0\1\1\0\1\0\0}% |
| 1209 |
\def^^b5{\0\1\1\0\1\0\1}% |
| 1210 |
\def^^b6{\0\1\1\0\1\1\0}% |
| 1211 |
\def^^b7{\0\1\1\0\1\1\1}% |
| 1212 |
\def^^b8{\0\1\1\1\0\0\0}% |
| 1213 |
\def^^b9{\0\1\1\1\0\0\1}% |
| 1214 |
\def^^ba{\0\1\1\1\0\1\0}% |
| 1215 |
\def^^bb{\0\1\1\1\0\1\1}% |
| 1216 |
\def^^bc{\0\1\1\1\1\0\0}% |
| 1217 |
\def^^bd{\0\1\1\1\1\0\1}% |
| 1218 |
\def^^be{\0\1\1\1\1\1\0}% |
| 1219 |
\def^^bf{\0\1\1\1\1\1\1}% |
| 1220 |
\def^^c0{\1\0\0\0\0\0\0}% |
| 1221 |
\def^^c1{\1\0\0\0\0\0\1}% |
| 1222 |
\def^^c2{\1\0\0\0\0\1\0}% |
| 1223 |
\def^^c3{\1\0\0\0\0\1\1}% |
| 1224 |
\def^^c4{\1\0\0\0\1\0\0}% |
| 1225 |
\def^^c5{\1\0\0\0\1\0\1}% |
| 1226 |
\def^^c6{\1\0\0\0\1\1\0}% |
| 1227 |
\def^^c7{\1\0\0\0\1\1\1}% |
| 1228 |
\def^^c8{\1\0\0\1\0\0\0}% |
| 1229 |
\def^^c9{\1\0\0\1\0\0\1}% |
| 1230 |
\def^^ca{\1\0\0\1\0\1\0}% |
| 1231 |
\def^^cb{\1\0\0\1\0\1\1}% |
| 1232 |
\def^^cc{\1\0\0\1\1\0\0}% |
| 1233 |
\def^^cd{\1\0\0\1\1\0\1}% |
| 1234 |
\def^^ce{\1\0\0\1\1\1\0}% |
| 1235 |
\def^^cf{\1\0\0\1\1\1\1}% |
| 1236 |
\def^^d0{\1\0\1\0\0\0\0}% |
| 1237 |
\def^^d1{\1\0\1\0\0\0\1}% |
| 1238 |
\def^^d2{\1\0\1\0\0\1\0}% |
| 1239 |
\def^^d3{\1\0\1\0\0\1\1}% |
| 1240 |
\def^^d4{\1\0\1\0\1\0\0}% |
| 1241 |
\def^^d5{\1\0\1\0\1\0\1}% |
| 1242 |
\def^^d6{\1\0\1\0\1\1\0}% |
| 1243 |
\def^^d7{\1\0\1\0\1\1\1}% |
| 1244 |
\def^^d8{\1\0\1\1\0\0\0}% |
| 1245 |
\def^^d9{\1\0\1\1\0\0\1}% |
| 1246 |
\def^^da{\1\0\1\1\0\1\0}% |
| 1247 |
\def^^db{\1\0\1\1\0\1\1}% |
| 1248 |
\def^^dc{\1\0\1\1\1\0\0}% |
| 1249 |
\def^^dd{\1\0\1\1\1\0\1}% |
| 1250 |
\def^^de{\1\0\1\1\1\1\0}% |
| 1251 |
\def^^df{\1\0\1\1\1\1\1}% |
| 1252 |
\def^^e0{\1\1\0\0\0\0\0}% |
| 1253 |
\def^^e1{\1\1\0\0\0\0\1}% |
| 1254 |
\def^^e2{\1\1\0\0\0\1\0}% |
| 1255 |
\def^^e3{\1\1\0\0\0\1\1}% |
| 1256 |
\def^^e4{\1\1\0\0\1\0\0}% |
| 1257 |
\def^^e5{\1\1\0\0\1\0\1}% |
| 1258 |
\def^^e6{\1\1\0\0\1\1\0}% |
| 1259 |
\def^^e7{\1\1\0\0\1\1\1}% |
| 1260 |
\def^^e8{\1\1\0\1\0\0\0}% |
| 1261 |
\def^^e9{\1\1\0\1\0\0\1}% |
| 1262 |
\def^^ea{\1\1\0\1\0\1\0}% |
| 1263 |
\def^^eb{\1\1\0\1\0\1\1}% |
| 1264 |
\def^^ec{\1\1\0\1\1\0\0}% |
| 1265 |
\def^^ed{\1\1\0\1\1\0\1}% |
| 1266 |
\def^^ee{\1\1\0\1\1\1\0}% |
| 1267 |
\def^^ef{\1\1\0\1\1\1\1}% |
| 1268 |
\def^^f0{\1\1\1\0\0\0\0}% |
| 1269 |
\def^^f1{\1\1\1\0\0\0\1}% |
| 1270 |
\def^^f2{\1\1\1\0\0\1\0}% |
| 1271 |
\def^^f3{\1\1\1\0\0\1\1}% |
| 1272 |
\def^^f4{\1\1\1\0\1\0\0}% |
| 1273 |
\def^^f5{\1\1\1\0\1\0\1}% |
| 1274 |
\def^^f6{\1\1\1\0\1\1\0}% |
| 1275 |
\def^^f7{\1\1\1\0\1\1\1}% |
| 1276 |
\def^^f8{\1\1\1\1\0\0\0}% |
| 1277 |
\def^^f9{\1\1\1\1\0\0\1}% |
| 1278 |
\def^^fa{\1\1\1\1\0\1\0}% |
| 1279 |
\def^^fb{\1\1\1\1\0\1\1}% |
| 1280 |
\def^^fc{\1\1\1\1\1\0\0}% |
| 1281 |
\def^^fd{\1\1\1\1\1\0\1}% |
| 1282 |
\def^^fe{\1\1\1\1\1\1\0}% |
| 1283 |
\def^^ff{\1\1\1\1\1\1\1}% |
| 1284 |
\readline}% |
| 1285 |
\@tempcnta=0 |
| 1286 |
\def\readline#1 |
| 1287 |
{\toks255{}\toks254{}\relax |
| 1288 |
#1% |
| 1289 |
\let\uc@temp@c\uc@temp@a |
| 1290 |
\unicode@numtohex\uc@temp@a\uc@got4% |
| 1291 |
\edef\uc@temp@b{\the\toks254}% |
| 1292 |
\edef\uc@temp@b{\uc@temp@b}% |
| 1293 |
\global\let\uc@temp@a\uc@temp@c |
| 1294 |
\expandafter\info\expandafter{\uc@temp@b}% |
| 1295 |
\endinput}% |
| 1296 |
\def\skipcodes#1G{\advance\@tempcnta by"#1\relax\checkline}% |
| 1297 |
\def\skipline#1 |
| 1298 |
{}% |
| 1299 |
\def\add#1{\toks254\expandafter{\the\toks254 #1}}% |
| 1300 |
\def\checkline{\advance\@tempcnta by1\relax |
| 1301 |
\ifnum\@tempcnta=\uc@got |
| 1302 |
\expandafter\uncompress\else\expandafter\skipline\fi}% |
| 1303 |
\def\checkrange#1I#2G{% |
| 1304 |
\advance\@tempcnta by"#1\relax |
| 1305 |
\@tempcntb\@tempcnta\advance\@tempcntb by-"#2\relax |
| 1306 |
\ifnum\@tempcntb>\uc@got\let\uc@temp@c\skipline\else |
| 1307 |
\ifnum\@tempcnta<\uc@got\let\uc@temp@c\skipline\else |
| 1308 |
\let\uc@temp@c\uncompress |
| 1309 |
\fi\fi |
| 1310 |
\uc@temp@c}% |
| 1311 |
\letG\checkline |
| 1312 |
\letH\skipcodes |
| 1313 |
\letI\checkrange |
| 1314 |
\def\1{\toks255\expandafter{\the\toks255 b}\2}% |
| 1315 |
\def\0{\toks255\expandafter{\the\toks255 a}\2}% |
| 1316 |
\def\2{\expandafter\ifx\csname hc@\the\toks255\endcsname\relax |
| 1317 |
\else\csname hc@\the\toks255\endcsname\toks255{}\fi}%'; |
| 1318 |
|
| 1319 |
sub getname($) { |
| 1320 |
my $i = shift; |
| 1321 |
my $fullname; my $rangeend; my $xoptions; |
| 1322 |
if (defined $unidata[$i]) { |
| 1323 |
my $name = $ {$unidata[$i]}{name}; |
| 1324 |
my $alias = $ {$unidata[$i]}{alias}; |
| 1325 |
my $comment = $ {$unidata[$i]}{comment}; |
| 1326 |
$rangeend = $ {$unidata[$i]}{rangeend}; |
| 1327 |
$fullname = $name; |
| 1328 |
$fullname .= "\n$alias" if $alias; |
| 1329 |
$fullname .= "\n$comment" if $comment; |
| 1330 |
$xoptions = join ', ', grep { $excludedoptions{$_} } |
| 1331 |
keys %{$ {$unidata[$i]}{options}}; |
| 1332 |
$xoptions = undef if $xoptions eq ''; |
| 1333 |
} |
| 1334 |
|
| 1335 |
# if ($characters[$i]) { |
| 1336 |
# $options = grep { defined $_ } join ', ', map { |
| 1337 |
# my $o = $$_[1]; $o = $$o{onoption}; |
| 1338 |
# $o = 'default' if (!defined $o) || ($o eq ''); $o; |
| 1339 |
# $o = undef unless $excludedoptions{$o}; $o; |
| 1340 |
# } @{$characters[$i]}; |
| 1341 |
# if (defined $rangeend) { |
| 1342 |
# $options2 = $options; $options = undef; |
| 1343 |
# $fullname2 = findrange($i); |
| 1344 |
# } else { |
| 1345 |
# $fullname = findrange($i) unless defined $fullname; |
| 1346 |
# } |
| 1347 |
# } |
| 1348 |
|
| 1349 |
return undef unless defined $fullname; |
| 1350 |
|
| 1351 |
my $str = "\001"; |
| 1352 |
if (defined $fullname) { $str .= "$fullname\n" } else { $str .= "\002\n" }; |
| 1353 |
if (defined $xoptions) { $str .= "\003$xoptions\n" }; |
| 1354 |
chomp $str; |
| 1355 |
|
| 1356 |
# my $str2 = undef; |
| 1357 |
# if (defined $options2) { |
| 1358 |
# $str2 = "\001"; |
| 1359 |
# if (defined $fullname2) { $str2 .= "$fullname\n" } |
| 1360 |
# else { $str2 .= "\002\n" }; |
| 1361 |
# { $str2 .= "\000$options2\n" }; |
| 1362 |
# chomp $str2; |
| 1363 |
# } |
| 1364 |
|
| 1365 |
return ($str); |
| 1366 |
} |
| 1367 |
|
| 1368 |
sub dumpnames_compressed() { |
| 1369 |
my $file = openfile("$targetdir/uninames.dat", |
| 1370 |
"%%% unicode name hash for ucs.sty, $autogen", |
| 1371 |
"Unicode character names, compressed", |
| 1372 |
coding => 'no-conversion'); |
| 1373 |
print "Creating Huffman code\n" if $verbose; |
| 1374 |
my (@weight, @tree, %lookup); |
| 1375 |
my $nexthuffman = 0; |
| 1376 |
my $upto = $#unidata; $upto = $#characters if $#characters > $upto; |
| 1377 |
for (my $i=1; $i<=$upto; $i++) { |
| 1378 |
for my $str (getname $i) { |
| 1379 |
next unless defined $str; |
| 1380 |
for my $c (split '',$str) { |
| 1381 |
my $n = $lookup{$c}; |
| 1382 |
unless (defined $n) { $n = $nexthuffman++; $lookup{$c} = $n }; |
| 1383 |
$weight[$n]++; |
| 1384 |
$tree[$n] = $c; |
| 1385 |
} |
| 1386 |
} |
| 1387 |
} |
| 1388 |
|
| 1389 |
my $highnum = 9999999; |
| 1390 |
while (1) { |
| 1391 |
my ($smallest, $smallest2); |
| 1392 |
my $smallestval = $highnum; |
| 1393 |
my $smallest2val = $highnum; |
| 1394 |
for (my $i=0; $i<$nexthuffman; $i++) { |
| 1395 |
my $val = $weight[$i]; |
| 1396 |
my $j = $i; |
| 1397 |
next unless defined $val; |
| 1398 |
# print "0: $j\n"; |
| 1399 |
if ($val < $smallestval) { |
| 1400 |
my $tmp = $val; $val = $smallestval; $smallestval = $tmp; |
| 1401 |
$tmp = $j; $j = $smallest; $smallest = $tmp; |
| 1402 |
} |
| 1403 |
if ($val < $smallest2val) { |
| 1404 |
$smallest2val = $val; $smallest2 = $j; |
| 1405 |
} |
| 1406 |
# print "A: $smallest, $smallest2\n"; |
| 1407 |
} |
| 1408 |
last if ($smallest2val==$highnum); |
| 1409 |
$weight[$smallest] = undef; |
| 1410 |
$weight[$smallest2] = undef; |
| 1411 |
$tree[$nexthuffman] = [$tree[$smallest],$tree[$smallest2]]; |
| 1412 |
$weight[$nexthuffman] = $smallestval+$smallest2val; |
| 1413 |
# print "$smallest + $smallest2 => $nexthuffman\n"; |
| 1414 |
# print Dumper($tree[$nexthuffman]); |
| 1415 |
$nexthuffman++; |
| 1416 |
} |
| 1417 |
|
| 1418 |
dumphuffman($tree[$nexthuffman-1],'',\%lookup); |
| 1419 |
my $padding = undef; |
| 1420 |
for my $i (values %lookup) { |
| 1421 |
if (length($i)>7) { |
| 1422 |
$padding = $i; last; |
| 1423 |
} |
| 1424 |
} |
| 1425 |
unless (defined $padding) { |
| 1426 |
warn "There is no huffman bit sequence longer than 7 bit. ". |
| 1427 |
"Padding with spaces"; |
| 1428 |
$padding = $lookup{' '} x 8; |
| 1429 |
} |
| 1430 |
|
| 1431 |
print "Compressing\n" if $verbose; |
| 1432 |
|
| 1433 |
for my $c (keys %lookup) { |
| 1434 |
my $v = $lookup{$c}; |
| 1435 |
$v =~ s/1/b/g; $v =~ s/0/a/g; |
| 1436 |
$c = $uninames_abbrev{$c} if defined $uninames_abbrev{$c}; |
| 1437 |
print $file "\\def\\hc\@$v\{\\add $c\}\%\n"; |
| 1438 |
} |
| 1439 |
print $file "$huffman_decoder\n"; |
| 1440 |
|
| 1441 |
my $cp = 0; |
| 1442 |
for (my $i=1; $i<=$#unidata; $i++) { |
| 1443 |
for my $str (getname $i) { |
| 1444 |
next unless defined $str; |
| 1445 |
my $rangestart; |
| 1446 |
if ($unidata[$i]) { $rangestart = $ {$unidata[$i]}{rangestart}; }; |
| 1447 |
|
| 1448 |
$str =~ s/./ |
| 1449 |
if (!defined $lookup{$&}) { print "L: '$&'\n"; }; |
| 1450 |
$lookup{$&}; |
| 1451 |
/egs; |
| 1452 |
# $str = join '', map { if ($_) { |
| 1453 |
# pack "B*", sprintf "3%-7s", $_; |
| 1454 |
# } } split /(.{1,7})/, $str; |
| 1455 |
$str =~ s/.{1,7}/ |
| 1456 |
sprintf "3%s%s",$&,substr($padding,0,7-length($&)); |
| 1457 |
/eg; |
| 1458 |
$str = pack("B*",$str); |
| 1459 |
$cp++; |
| 1460 |
if (defined $rangestart) { |
| 1461 |
# printf $file "I%XI%X", $i-$cp+1, $rangeend-$i; |
| 1462 |
# $cp=$rangeend; |
| 1463 |
printf $file "I%XI%X", $i-$cp+1, $i-$rangestart; |
| 1464 |
$cp=$i; |
| 1465 |
} else { |
| 1466 |
if ($i>$cp) { printf $file "H%X", $i-$cp; $cp=$i}; |
| 1467 |
} |
| 1468 |
print $file "G$str\n"; |
| 1469 |
} |
| 1470 |
} |
| 1471 |
|
| 1472 |
flushfiles(); |
| 1473 |
} |
| 1474 |
|
| 1475 |
sub dumphuffman($$$) { |
| 1476 |
my ($tree,$prefix,$lookup) = @_; |
| 1477 |
# print Dumper([$tree]); return; |
| 1478 |
if (ref $tree) { |
| 1479 |
dumphuffman($$tree[0],$prefix.'0',$lookup); |
| 1480 |
dumphuffman($$tree[1],$prefix.'1',$lookup); |
| 1481 |
} else { |
| 1482 |
# print "HUFFMAN: '$tree' => '$prefix'\n"; |
| 1483 |
$$lookup{$tree} = $prefix; |
| 1484 |
} |
| 1485 |
} |
| 1486 |
|
| 1487 |
sub isprivate($) { |
| 1488 |
my $cp = shift; |
| 1489 |
return 1 if ($cp>=0xe000 && $cp<=0xf8ff); |
| 1490 |
return 1 if ($cp>=0xf0000 && $cp<=0xffffd); |
| 1491 |
return 1 if ($cp>=0x100000 && $cp<=0x10fffd); |
| 1492 |
return 0; |
| 1493 |
} |
| 1494 |
|
| 1495 |
sub dumpnames_uncompressed() { |
| 1496 |
my $file = openfile("$targetdir/uninames.dat", |
| 1497 |
"%%% unicode name hash for ucs.sty, $autogen", |
| 1498 |
"Unicode character names"); |
| 1499 |
warn "Dumping uncompressed unicode data, ". |
| 1500 |
"this is no longer maintained and may give unpredicted results.\n"; |
| 1501 |
dump_ranges($file); |
| 1502 |
for (my $i=1; $i<=$#unidata; $i++) { |
| 1503 |
next unless defined $unidata[$i]; |
| 1504 |
my $name = $ {$unidata[$i]}{name}; |
| 1505 |
my $alias = $ {$unidata[$i]}{alias}; |
| 1506 |
my $comment = $ {$unidata[$i]}{comment}; |
| 1507 |
my $str = sprintf "Unicode character %d = 0x%04x:${messagebreak}%s", |
| 1508 |
$i, $i, $name; |
| 1509 |
$str .= "${messagebreak}$alias" if $alias; |
| 1510 |
$str .= "${messagebreak}$comment" if $comment; |
| 1511 |
print $file "\\info{$str}\%\n"; |
| 1512 |
} |
| 1513 |
flushfiles(); |
| 1514 |
} |
| 1515 |
|
| 1516 |
parseargs(); |
| 1517 |
print "PASS: initcharacters\n" if $verbose; |
| 1518 |
initcharacters(); |
| 1519 |
if ($dump_unidata) { |
| 1520 |
print "PASS: unlink unidata files\n" if $verbose; |
| 1521 |
unlinkfiles(' |
| 1522 |
^uni-[0-9]+\.def$| |
| 1523 |
^uninames.dat$| |
| 1524 |
^uni-global\.def$ ',$targetdir); |
| 1525 |
} |
| 1526 |
if ($tables_to_dump) { |
| 1527 |
print "PASS: unlink tables\n" if $verbose; |
| 1528 |
unlinkfiles(' ^table-.*-.*-.*\.tex$ ',$tabledir); |
| 1529 |
} |
| 1530 |
if ($loadunidata) { |
| 1531 |
print "PASS: loadunidata($unidata)\n" if $verbose; |
| 1532 |
loadunidata($unidata); } |
| 1533 |
for my $configfile (@configfiles) { |
| 1534 |
print "PASS: loadconfig($configfile)\n" if $verbose; |
| 1535 |
loadconfig($configfile); } |
| 1536 |
print "PASS: generate\n" if $verbose; |
| 1537 |
generate(); |
| 1538 |
if ($dump_unidata) { |
| 1539 |
print "PASS: generate globals\n" if $verbose; |
| 1540 |
generate_globals(); } |
| 1541 |
if ($createcomments) { |
| 1542 |
print "PASS: makecomments\n" if $verbose; |
| 1543 |
makecomments(); } |
| 1544 |
if ($dump_unidata) { |
| 1545 |
print "PASS: dumpcharacters\n" if $verbose; |
| 1546 |
dumpcharacters(); |
| 1547 |
} |
| 1548 |
if ($tables_to_dump) { |
| 1549 |
print "PASS: dumping character tables to directory $tabledir/\n" |
| 1550 |
if $verbose; |
| 1551 |
dump_charactertables(); |
| 1552 |
} |
| 1553 |
if ($generate_uninames && $dump_unidata && $loadunidata) { |
| 1554 |
if ($compressnames) { |
| 1555 |
print "PASS: dumpnames (compressed)\n" if $verbose; |
| 1556 |
dumpnames_compressed(); |
| 1557 |
} else { |
| 1558 |
print "PASS: dumpnames (uncompressed)\n" if $verbose; |
| 1559 |
dumpnames_uncompressed(); |
| 1560 |
}} |
| 1561 |
print "PASS: closefiles\n" if $verbose; |
| 1562 |
closefiles(); |
| 1563 |
|
| 1564 |
### LocalVariables: |
| 1565 |
### mode: perl |
| 1566 |
### End: |
| 1567 |
### Local IspellDict: british |
| 1568 |
|
| 1569 |
|
| 1570 |
# LocalWords: uni def makeunidef pl de ucs sty AUTOOPTION num configfiles dir |
| 1571 |
# LocalWords: nocomments SYSNOPSIS targetdir db UnicodeData txt uninames dat |
| 1572 |
# LocalWords: UNIDATA org www http unicode |