| 1 |
#!/usr/bin/perl |
| 2 |
|
| 3 |
# /****************************************************************************\ |
| 4 |
# Part of the XeTeX typesetting system |
| 5 |
# Copyright (c) 1994-2008 by SIL International |
| 6 |
# Copyright (c) 2009 by Jonathan Kew |
| 7 |
# |
| 8 |
# SIL Author(s): Jonathan Kew |
| 9 |
# |
| 10 |
# Permission is hereby granted, free of charge, to any person obtaining |
| 11 |
# a copy of this software and associated documentation files (the |
| 12 |
# "Software"), to deal in the Software without restriction, including |
| 13 |
# without limitation the rights to use, copy, modify, merge, publish, |
| 14 |
# distribute, sublicense, and/or sell copies of the Software, and to |
| 15 |
# permit persons to whom the Software is furnished to do so, subject to |
| 16 |
# the following conditions: |
| 17 |
# |
| 18 |
# The above copyright notice and this permission notice shall be |
| 19 |
# included in all copies or substantial portions of the Software. |
| 20 |
# |
| 21 |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 22 |
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 23 |
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 24 |
# NONINFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE |
| 25 |
# FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF |
| 26 |
# CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 27 |
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 28 |
# |
| 29 |
# Except as contained in this notice, the name of the copyright holders |
| 30 |
# shall not be used in advertising or otherwise to promote the sale, |
| 31 |
# use or other dealings in this Software without prior written |
| 32 |
# authorization from the copyright holders. |
| 33 |
# \****************************************************************************/ |
| 34 |
|
| 35 |
# create unicode-letters.tex by processing Unicode data files |
| 36 |
# UnicodeData.txt |
| 37 |
# EastAsianWidth.txt |
| 38 |
# LineBreak.txt |
| 39 |
|
| 40 |
# The intent of unicode-letters.tex is to initialize basic properties for most |
| 41 |
# of the "letters" in Unicode, as follows: |
| 42 |
# |
| 43 |
# \catcode = 11 for everything with GC=L* |
| 44 |
# \sfcode = 999 for everything that has a to-lowercase mapping |
| 45 |
# \uccode, \lccode based on the case mapping fields |
| 46 |
# \XeTeXmathcode = (7 1 #) for BMP letters, (0 1 #) for SMP |
| 47 |
# \XeTeXspacingclass values based on a few of the Unicode line-break classes: |
| 48 |
# 'CM' => 256, # combining marks = transparent |
| 49 |
# 'ID' => 1, # ideograph |
| 50 |
# 'OP' => 2, # opener |
| 51 |
# 'CL' => 3, # closer |
| 52 |
# 'NS' => 3, # non-starter |
| 53 |
# 'EX' => 3, # exclamation |
| 54 |
# 'IS' => 3, # infix sep (comma etc) |
| 55 |
# 'QU' => 4, # ambiguous quote |
| 56 |
|
| 57 |
die "usage: perl $0 UnicodeData.txt EastAsianWidth.txt LineBreak.txt > unicode-letters.tex\n" |
| 58 |
unless $#ARGV == 2; |
| 59 |
|
| 60 |
sub parse_unidata { |
| 61 |
my (@u) = @_; |
| 62 |
$lccode{$u[0]} = $u[13] if $u[13] ne ''; |
| 63 |
$lccode{$u[0]} = $u[0] if $u[13] eq '' and ($u[2] =~ /^L/ or $u[12] ne ''); |
| 64 |
$uccode{$u[0]} = $u[12] if $u[12] ne ''; |
| 65 |
$uccode{$u[0]} = $u[0] if $u[12] eq '' and ($u[2] =~ /^L/ or $u[13] ne ''); |
| 66 |
if ($u[2] =~ /^L/) { |
| 67 |
push(@letters, $u[0]); |
| 68 |
} |
| 69 |
elsif ($u[2] =~ /^M/) { |
| 70 |
push(@marks, $u[0]); |
| 71 |
} |
| 72 |
elsif (exists $lccode{$u[0]} or exists $uccode{$u[0]}) { |
| 73 |
push(@casesym, $u[0]); |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
my ($start, $end); |
| 79 |
open Unidata, $ARGV[0] or die "can't read $ARGV[0]"; |
| 80 |
while (<Unidata>) { |
| 81 |
chomp; |
| 82 |
@u = split(/;/); |
| 83 |
|
| 84 |
my $charname = $u[1]; |
| 85 |
if ($charname !~ /</) { |
| 86 |
parse_unidata(@u); |
| 87 |
next; |
| 88 |
} |
| 89 |
|
| 90 |
if ($charname =~ /, First>/) { |
| 91 |
$start = hex($u[0]); |
| 92 |
} elsif ($charname =~ /, Last>/) { |
| 93 |
$end = hex($u[0]); |
| 94 |
shift @u; |
| 95 |
|
| 96 |
for my $codepoint ($start .. $end) { |
| 97 |
parse_unidata(sprintf("%04X", $codepoint), @u); |
| 98 |
} |
| 99 |
} |
| 100 |
} |
| 101 |
close Unidata; |
| 102 |
|
| 103 |
$date = `date`; |
| 104 |
chomp $date; |
| 105 |
print << "__EOT__"; |
| 106 |
% Do not edit this file! |
| 107 |
% Created from UnicodeData.txt, EastAsianWidth.txt, LineBreak.txt |
| 108 |
% by unicode-char-prep.pl on $date. |
| 109 |
% In case of errors, fix the Perl script instead. |
| 110 |
__EOT__ |
| 111 |
|
| 112 |
print << '__EOT__'; |
| 113 |
|
| 114 |
\begingroup |
| 115 |
\catcode`\{=1 \catcode`\}=2 \catcode`\#=6 |
| 116 |
|
| 117 |
\message{loading Unicode properties} |
| 118 |
|
| 119 |
% test whether xetex is new enough to set codes on supplementary-plane chars |
| 120 |
% (requires version > 0.996) |
| 121 |
\let\ifSupplementary=\iffalse |
| 122 |
\def\Supplementarytrue{\let\ifSupplementary=\iftrue} |
| 123 |
\ifnum\XeTeXversion>0 \Supplementarytrue |
| 124 |
\else |
| 125 |
\def\getrevnumber.#1.#2-#3\end{\count255=#1 } |
| 126 |
\expandafter\getrevnumber\XeTeXrevision.-\end |
| 127 |
\ifnum\count255>996 \Supplementarytrue \fi |
| 128 |
\fi |
| 129 |
% definitions for classes and case mappings based on UnicodeData.txt |
| 130 |
\def\C #1 #2 #3 {\global\uccode"#1="#2 \global\lccode"#1="#3 } % case mappings (non-letter) |
| 131 |
\def\L #1 #2 #3 {\global\catcode"#1=11 % category: letter |
| 132 |
\C #1 #2 #3 % with case mappings |
| 133 |
\ifnum"#1="#3 \else \global\sfcode"#1=999 \fi % uppercase letters have sfcode=999 |
| 134 |
\ifnum"#1<"10000 \global\XeTeXmathcode"#1="7"01"#1 % BMP letters default to class 7 (var), fam 1 |
| 135 |
\else \global\XeTeXmathcode"#1="0"01"#1 % SMP letters class 0 (regular), fam 1 |
| 136 |
\fi} |
| 137 |
\def\l #1 {\L #1 #1 #1 } % letter without case mappings |
| 138 |
\let\m=\l % combining mark - treated as uncased letter |
| 139 |
|
| 140 |
__EOT__ |
| 141 |
|
| 142 |
$supp = 0; |
| 143 |
for (@letters) { |
| 144 |
if ((hex $_ > 0xffff) and ($supp == 0)) { |
| 145 |
print "\\ifSupplementary\n"; |
| 146 |
$supp = 1; |
| 147 |
} |
| 148 |
if (exists $uccode{$_} or exists $lccode{$_}) { |
| 149 |
if (($lccode{$_} eq $_) and ($uccode{$_} eq $_)) { |
| 150 |
print "\\l $_\n"; |
| 151 |
} |
| 152 |
else { |
| 153 |
print "\\L $_ "; |
| 154 |
print exists $uccode{$_} ? $uccode{$_} : "0"; |
| 155 |
print " "; |
| 156 |
print exists $lccode{$_} ? $lccode{$_} : "0"; |
| 157 |
print "\n"; |
| 158 |
} |
| 159 |
} |
| 160 |
else { |
| 161 |
print "\\l $_\n"; |
| 162 |
} |
| 163 |
} |
| 164 |
if ($supp == 1) { |
| 165 |
print "\\fi % end ifSupplementary\n"; |
| 166 |
$supp = 0; |
| 167 |
} |
| 168 |
|
| 169 |
for (@casesym) { |
| 170 |
if ((hex $_ > 0xffff) and ($supp == 0)) { |
| 171 |
print "\\ifSupplementary\n"; |
| 172 |
$supp = 1; |
| 173 |
} |
| 174 |
print "\\C $_ "; |
| 175 |
print exists $uccode{$_} ? $uccode{$_} : "0"; |
| 176 |
print " "; |
| 177 |
print exists $lccode{$_} ? $lccode{$_} : "0"; |
| 178 |
print "\n"; |
| 179 |
} |
| 180 |
if ($supp == 1) { |
| 181 |
print "\\fi % end ifSupplementary\n"; |
| 182 |
$supp = 0; |
| 183 |
} |
| 184 |
|
| 185 |
for (@marks) { |
| 186 |
if ((hex $_ > 0xffff) and ($supp == 0)) { |
| 187 |
print "\\ifSupplementary\n"; |
| 188 |
$supp = 1; |
| 189 |
} |
| 190 |
print "\\m $_\n"; |
| 191 |
} |
| 192 |
if ($supp == 1) { |
| 193 |
print "\\fi % end ifSupplementary\n"; |
| 194 |
$supp = 0; |
| 195 |
} |
| 196 |
|
| 197 |
print << '__EOT__'; |
| 198 |
|
| 199 |
% set \sfcode for Unicode closing quotes |
| 200 |
\sfcode"2019=0 |
| 201 |
\sfcode"201D=0 |
| 202 |
|
| 203 |
% check whether the interchar toks features are present |
| 204 |
\ifx\XeTeXinterchartoks\XeTeXcharclass |
| 205 |
\def\next{\endgroup\endinput} |
| 206 |
\else |
| 207 |
\let\next\relax |
| 208 |
\fi |
| 209 |
\next |
| 210 |
|
| 211 |
__EOT__ |
| 212 |
|
| 213 |
open EAW, $ARGV[1] or die "can't read $ARGV[1]"; |
| 214 |
while (<EAW>) { |
| 215 |
chomp; |
| 216 |
s/ *#.*//; |
| 217 |
s/ +$//; |
| 218 |
if (m/([0-9A-F]{4,6})(?:\.\.([0-9A-F]{4,6}))?;(.+)/) { |
| 219 |
$s = $1; |
| 220 |
$e = $2; |
| 221 |
$w = $3; |
| 222 |
$e = $s if $e eq ''; |
| 223 |
for ($i = hex "0x$s"; $i <= hex "0x$e"; ++$i) { |
| 224 |
$width[$i] = $w; |
| 225 |
} |
| 226 |
} |
| 227 |
} |
| 228 |
close EAW; |
| 229 |
|
| 230 |
%lineBreakClass = ( |
| 231 |
'CM' => 256, # combining marks = transparent |
| 232 |
'ID' => 1, # ideograph |
| 233 |
'OP' => 2, # opener |
| 234 |
'CL' => 3, # closer |
| 235 |
'NS' => 3, # non-starter |
| 236 |
'EX' => 3, # exclamation |
| 237 |
'IS' => 3, # infix sep (comma etc) |
| 238 |
); |
| 239 |
|
| 240 |
print << '__EOT__'; |
| 241 |
\message{and character classes} |
| 242 |
|
| 243 |
% definitions for script classes based on LineBreak.txt |
| 244 |
__EOT__ |
| 245 |
|
| 246 |
foreach (keys %lineBreakClass) { |
| 247 |
print "\\def\\$_ #1 #2 {\\set{#1}{#2}{\\global\\XeTeXcharclass\\n=$lineBreakClass{$_}"; |
| 248 |
print " \\global\\catcode\\n=11" if m/ID/; |
| 249 |
print " }}\n"; |
| 250 |
} |
| 251 |
|
| 252 |
print << '__EOT__'; |
| 253 |
\countdef\n=255 |
| 254 |
\def\set#1#2#3{% execute setting "#3" with \n set to each value from #1 to #2 |
| 255 |
\n="#1 \loop #3 \ifnum\n<"#2 \advance\n by 1 \repeat} |
| 256 |
% copy \loop etc from plain.tex |
| 257 |
\def\loop#1\repeat{\def\body{#1}\iterate} |
| 258 |
\def\iterate{\body \let\next\iterate \else\let\next\relax\fi \next} |
| 259 |
\let\repeat=\fi % this makes \loop...\if...\repeat skippable |
| 260 |
|
| 261 |
__EOT__ |
| 262 |
|
| 263 |
open LineBreak, $ARGV[2] or die "can't read $ARGV[2]"; |
| 264 |
while (<LineBreak>) { |
| 265 |
chomp; |
| 266 |
s/ *#.*//; |
| 267 |
s/ +$//; |
| 268 |
if (m/([0-9A-F]{4,6})(?:\.\.([0-9A-F]{4,6}))?;(..)/) { |
| 269 |
$s = $1; |
| 270 |
$e = $2; |
| 271 |
$lb = $3; |
| 272 |
$e = $s if $e eq ''; |
| 273 |
if (exists $lineBreakClass{$lb}) { |
| 274 |
if ($lineBreakClass{$lb} == 1) { |
| 275 |
# ideographs: set whole range to class 1 |
| 276 |
print "\\$lb $s $e\n"; |
| 277 |
} |
| 278 |
else { |
| 279 |
# punctuation, etc: only set these by default for chars with EAW = W, F, H |
| 280 |
for ($i = hex "0x$s"; $i <= hex "0x$e"; ++$i) { |
| 281 |
if ($width[$i] =~ /^[WFH]$/) { |
| 282 |
printf "\\$lb %04X %04X\n", $i, $i; |
| 283 |
} |
| 284 |
} |
| 285 |
} |
| 286 |
} |
| 287 |
} |
| 288 |
} |
| 289 |
close LineBreak; |
| 290 |
|
| 291 |
print << '__EOT__'; |
| 292 |
|
| 293 |
% basic line-breaking support for CJK etc; override for more sophisticated spacing, |
| 294 |
% automatic font changes, etc. |
| 295 |
|
| 296 |
\gdef\xtxHanGlue{\hskip0pt plus 0.1em\relax} % between ideographs |
| 297 |
\gdef\xtxHanSpace{\hskip0.2em plus 0.2em minus 0.1em\relax} % before/after runs of CJK |
| 298 |
|
| 299 |
\global\XeTeXinterchartoks 0 1 = {\xtxHanSpace} |
| 300 |
\global\XeTeXinterchartoks 0 2 = {\xtxHanSpace} |
| 301 |
\global\XeTeXinterchartoks 0 3 = {\nobreak\xtxHanSpace} |
| 302 |
|
| 303 |
\global\XeTeXinterchartoks 1 0 = {\xtxHanSpace} |
| 304 |
\global\XeTeXinterchartoks 2 0 = {\nobreak\xtxHanSpace} |
| 305 |
\global\XeTeXinterchartoks 3 0 = {\xtxHanSpace} |
| 306 |
|
| 307 |
\global\XeTeXinterchartoks 1 1 = {\xtxHanGlue} |
| 308 |
\global\XeTeXinterchartoks 1 2 = {\xtxHanGlue} |
| 309 |
\global\XeTeXinterchartoks 1 3 = {\nobreak\xtxHanGlue} |
| 310 |
|
| 311 |
\global\XeTeXinterchartoks 2 1 = {\nobreak\xtxHanGlue} |
| 312 |
\global\XeTeXinterchartoks 2 2 = {\nobreak\xtxHanGlue} |
| 313 |
\global\XeTeXinterchartoks 2 3 = {\xtxHanGlue} |
| 314 |
|
| 315 |
\global\XeTeXinterchartoks 3 1 = {\xtxHanGlue} |
| 316 |
\global\XeTeXinterchartoks 3 2 = {\xtxHanGlue} |
| 317 |
\global\XeTeXinterchartoks 3 3 = {\nobreak\xtxHanGlue} |
| 318 |
|
| 319 |
\endgroup |
| 320 |
|
| 321 |
__EOT__ |