| 1 |
% File load-unicode-data.tex |
| 2 |
% |
| 3 |
% Copyright 2015-2025 The LaTeX Project |
| 4 |
% |
| 5 |
% It may be distributed and/or modified under the conditions of |
| 6 |
% the LaTeX Project Public License (LPPL), either version 1.3c of |
| 7 |
% this license or (at your option) any later version. The latest |
| 8 |
% version of this license is in the file |
| 9 |
% http://www.latex-project.org/lppl.txt. |
| 10 |
% |
| 11 |
% Issues with this file should be reported at |
| 12 |
% https://github.com/latex3/unicode-data |
| 13 |
% |
| 14 |
% This file parses a number of data files provided by the Unicode Consortium |
| 15 |
% and when used with used Unicode-capable engine sets up a range of TeX-related |
| 16 |
% parameters based on the extracted information. |
| 17 |
% |
| 18 |
% From the file UnicodeData.txt the following properties are set: |
| 19 |
% - \catcode 11 for all letters (Unicode class "L") |
| 20 |
% - \catcode 11 for all combining marks (Unicode class "M") |
| 21 |
% - \sfcode 999 for all code points of class "Lu" (upper case letters) |
| 22 |
% - \lccode for all of class "Ll" (lower case letters) to the code point |
| 23 |
% itself, and \uccode to the upper case mapping (or if not given |
| 24 |
% to the code point itself) |
| 25 |
% - \uccode for all of class "Lu" (upper case letters) to the code point |
| 26 |
% itself, and \lccode to the lower case mapping (or if not given |
| 27 |
% to the code point itself) |
| 28 |
% - \lccode and \uccode for all of class "Lt" (title case letters) to the |
| 29 |
% lower and upper case mappings (or if not given to the code point itself) |
| 30 |
% - \lccode and \uccode for all other letter code points are set to |
| 31 |
% the code point itself |
| 32 |
% - \lccode and/or \uccode for non-letter code points for which an upper |
| 33 |
% or lower case mapping is given |
| 34 |
% - \sfcode 0 (ignored) for code points of Unicode classes "Pe" (closing |
| 35 |
% punctuation marks) and "Pf" (final quotation marks) |
| 36 |
% - \Umathcode for all letters as math type 7 (var) |
| 37 |
% |
| 38 |
% ============================================================================= |
| 39 |
% |
| 40 |
% The data can only be loaded by Unicode engines. Currently this is limited to |
| 41 |
% XeTeX and LuaTeX, both of which define \Umathcode. |
| 42 |
\ifx\Umathcode\undefined |
| 43 |
\expandafter\endinput |
| 44 |
\fi |
| 45 |
% Just in case, check for the e-TeX extensions. |
| 46 |
\ifx\eTeXversion\undefined |
| 47 |
\expandafter\endinput |
| 48 |
\fi |
| 49 |
% This file can be loaded in IniTeX mode so the category codes of |{|, |}| and |
| 50 |
% |#| may not be correct. Everything is done in a group so that only the |
| 51 |
% settings we want to propagate are made available generally. |
| 52 |
\begingroup |
| 53 |
\catcode`\{=1 % |
| 54 |
\catcode`\}=2 % |
| 55 |
\catcode`\#=6 % |
| 56 |
% Write some basic information to the log. |
| 57 |
\catcode`\^=7 % |
| 58 |
\newlinechar=`\^^J % |
| 59 |
\message{^^J}% |
| 60 |
\message{load-unicode-data.tex v1.19 (2025-09-26)^^J}% |
| 61 |
\message{Reading Unicode data^^J}% |
| 62 |
% The first stage of parsing is dealing with the fact that there are lots of |
| 63 |
% data items separated by |;|. Of those, only a few are needed so they are |
| 64 |
% picked out and everything else is dropped. There is one complication: there |
| 65 |
% are a few cases in the data file of ranges which are marked by the descriptor |
| 66 |
% |First| and a matching |Last|. A separate routine is used to handle these |
| 67 |
% cases. |
| 68 |
\def\parseunicodedataI#1;#2;#3;#4;#5;#6;#7;#8;#9;{% |
| 69 |
\parseunicodedataII#1;#3;#2 First>\relax |
| 70 |
}% |
| 71 |
\def\parseunicodedataII#1;#2;#3 First>#4\relax{% |
| 72 |
\ifx\relax#4\relax |
| 73 |
\expandafter\parseunicodedataIII |
| 74 |
\else |
| 75 |
\expandafter\parseunicodedataVII |
| 76 |
\fi |
| 77 |
#1;#2;% |
| 78 |
}% |
| 79 |
\def\parseunicodedataIII#1;#2;#3;#4;#5;#6;#7;#8\relax{% |
| 80 |
\parseunicodedataIV{#1}{#2}{#6}{#7}% |
| 81 |
}% |
| 82 |
% At this stage we have a `normal' data line with four pieces of information: |
| 83 |
% the code point, the Unicode class and the (possibly empty) upper and lower |
| 84 |
% case mappings. A few utility macros are defined, then we branch based on the |
| 85 |
% Unicode class. Notice that for all letter-like code points we first set the |
| 86 |
% |\lccode| and |\uccode| values to the code point itself then test for the |
| 87 |
% classes where a different setting might be appropriate. For non-letters |
| 88 |
% there is a check to see if any mappings are available, and also for trailing |
| 89 |
% punctuation to set the appropriate |\sfcode|. |
| 90 |
\def\Ll{Ll}% |
| 91 |
\def\Lt{Lt}% |
| 92 |
\def\Lu{Lu}% |
| 93 |
\def\Pe{Pe}% |
| 94 |
\def\Pf{Pf}% |
| 95 |
\def\firsttoken#1#2\relax{#1}% |
| 96 |
\def\parseunicodedataIV#1#2#3#4{% |
| 97 |
\ifnum 0% |
| 98 |
\if L\firsttoken#2?\relax 1\fi |
| 99 |
\if M\firsttoken#2?\relax 1\fi |
| 100 |
>0 % |
| 101 |
\parseunicodedataV{"#1}% |
| 102 |
\def\temp{#2}% |
| 103 |
\ifx\Ll\temp |
| 104 |
\parseunicodedataVI\uccode{#1}{#3}% |
| 105 |
\fi |
| 106 |
\ifx\Lt\temp |
| 107 |
\parseunicodedataVI\uccode{#1}{#3}% |
| 108 |
\parseunicodedataVI\lccode{#1}{#4}% |
| 109 |
\fi |
| 110 |
\ifx\Lu\temp |
| 111 |
\parseunicodedataVI\lccode{#1}{#4}% |
| 112 |
\global\sfcode"#1=999 % |
| 113 |
\fi |
| 114 |
% All letters in math mode should be variables. |
| 115 |
\global\Umathcode"#1="7"01"#1 % |
| 116 |
\else |
| 117 |
\def\temp{#2}% |
| 118 |
\ifnum 0\ifx\temp\Pe 1\fi\ifx\temp\Pf 1\fi>0 % |
| 119 |
\global\sfcode"#1=0 % |
| 120 |
\fi |
| 121 |
\ifx\relax#3\relax |
| 122 |
\else |
| 123 |
\global\uccode"#1="#3 % |
| 124 |
\fi |
| 125 |
\ifx\relax#4\relax |
| 126 |
\else |
| 127 |
\global\lccode"#1="#4 % |
| 128 |
\fi |
| 129 |
\fi |
| 130 |
}% |
| 131 |
% A simple auxiliary for all letter-like code points: the |\lccode| and |
| 132 |
% |\uccode| may get reset for cased letters but this means the initial |
| 133 |
% setting can't be forgotten. |
| 134 |
\def\parseunicodedataV#1{% |
| 135 |
\global\catcode#1=11 % |
| 136 |
\global\lccode#1=#1 % |
| 137 |
\global\uccode#1=#1 % |
| 138 |
}% |
| 139 |
% An auxiliary to deal with the fact that some cased letters don't actually |
| 140 |
% have a case mapping available. |
| 141 |
\def\parseunicodedataVI#1#2#3{% |
| 142 |
\ifx\relax#3\relax |
| 143 |
\else |
| 144 |
\global#1"#2="#3 % |
| 145 |
\fi |
| 146 |
}% |
| 147 |
% For lines that were the |First>| of a range, read the data source again for |
| 148 |
% last line. Lines for letters then trigger a loop over the entire range. These |
| 149 |
% are always non-cased letters. |
| 150 |
\def\parseunicodedataVII#1;#2;#3\relax{% |
| 151 |
\read0 to \unicodedataline |
| 152 |
\expandafter\parseunicodedataXII\unicodedataline\relax#1;#2\relax |
| 153 |
}% |
| 154 |
\def\parseunicodedataXII#1;#2\relax#3;#4\relax{% |
| 155 |
\if L\firsttoken#4?\relax |
| 156 |
\begingroup |
| 157 |
\count0="#3 % |
| 158 |
\loop |
| 159 |
\unless\ifnum\count0>"#1 % |
| 160 |
\parseunicodedataV{\count0 }% |
| 161 |
\advance\count0 by 1 % |
| 162 |
\repeat |
| 163 |
\endgroup |
| 164 |
\fi |
| 165 |
}% |
| 166 |
% From plain: may not be defined (yet). |
| 167 |
\def\loop#1\repeat{\def\body{#1}\iterate}% |
| 168 |
\def\iterate{% |
| 169 |
\body |
| 170 |
\let\next\iterate |
| 171 |
\else |
| 172 |
\let\next\relax |
| 173 |
\fi |
| 174 |
\next |
| 175 |
}% |
| 176 |
\let\repeat\fi |
| 177 |
% There is no version data in |UnicodeData.txt|: log that it is being used with |
| 178 |
% a hard-coded date (the modification date from www.unicode.org/Public/). This obviously |
| 179 |
% needs to be updated when a new download takes place! |
| 180 |
\message{\string# UnicodeData-17.0.0.txt^^J}% |
| 181 |
\message{\string# Modified 2025-09-26 07:00 GMT [JAW]^^J}% |
| 182 |
% Actually loading the file requires an input stream, done directly. |
| 183 |
% There is a blank line at the end of the data source so there is a check |
| 184 |
% here for a |\par|. |
| 185 |
\def\storedpar{\par}% |
| 186 |
\openin0=UnicodeData.txt % |
| 187 |
\loop\unless\ifeof0 % |
| 188 |
\read0 to \unicodedataline |
| 189 |
\unless\ifx\unicodedataline\storedpar |
| 190 |
\expandafter\parseunicodedataI\unicodedataline\relax |
| 191 |
\fi |
| 192 |
\repeat |
| 193 |
\closein0 % |
| 194 |
\endgroup |