| 1 |
% \iffalse meta-comment |
| 2 |
% |
| 3 |
%% File: l3unicode.dtx |
| 4 |
% |
| 5 |
% Copyright (C) 2018-2026 The LaTeX Project |
| 6 |
% |
| 7 |
% It may be distributed and/or modified under the conditions of the |
| 8 |
% LaTeX Project Public License (LPPL), either version 1.3c of this |
| 9 |
% license or (at your option) any later version. The latest version |
| 10 |
% of this license is in the file |
| 11 |
% |
| 12 |
% https://www.latex-project.org/lppl.txt |
| 13 |
% |
| 14 |
% This file is part of the "l3kernel bundle" (The Work in LPPL) |
| 15 |
% and all files in that bundle must be distributed together. |
| 16 |
% |
| 17 |
% ----------------------------------------------------------------------- |
| 18 |
% |
| 19 |
% The development version of the bundle can be found at |
| 20 |
% |
| 21 |
% https://github.com/latex3/latex3 |
| 22 |
% |
| 23 |
% for those people who are interested. |
| 24 |
% |
| 25 |
%<*driver> |
| 26 |
\documentclass[full,kernel]{l3doc} |
| 27 |
\begin{document} |
| 28 |
\DocInput{\jobname.dtx} |
| 29 |
\end{document} |
| 30 |
%</driver> |
| 31 |
% \fi |
| 32 |
% |
| 33 |
% \title{^^A |
| 34 |
% The \pkg{l3unicode} module\\ Unicode support functions^^A |
| 35 |
% } |
| 36 |
% |
| 37 |
% \author{^^A |
| 38 |
% The \LaTeX{} Project\thanks |
| 39 |
% {^^A |
| 40 |
% E-mail: |
| 41 |
% \href{mailto:[email protected]} |
| 42 |
% {[email protected]}^^A |
| 43 |
% }^^A |
| 44 |
% } |
| 45 |
% |
| 46 |
% \date{Released 2026-05-15} |
| 47 |
% |
| 48 |
% \maketitle |
| 49 |
% |
| 50 |
% \begin{documentation} |
| 51 |
% |
| 52 |
% This module provides Unicode-specific functions along with loading data |
| 53 |
% from a range of Unicode Consortium files. Most of the code here is |
| 54 |
% internal, but there are a small set of public functions. These work with |
| 55 |
% Unicode \meta{codepoints} and are designed to give usable results with |
| 56 |
% both Unicode-aware and $8$-bit engines. |
| 57 |
% |
| 58 |
% \begin{function}[EXP, added = 2022-10-09, updated = 2022-11-09] |
| 59 |
% {\codepoint_generate:nn} |
| 60 |
% \begin{syntax} |
| 61 |
% \cs{codepoint_generate:nn} \Arg{codepoint} \Arg{catcode} |
| 62 |
% \end{syntax} |
| 63 |
% Generates one or more character tokens representing the \meta{codepoint}. |
| 64 |
% With Unicode engines, exactly one character token will be generated, and |
| 65 |
% this will have the \meta{catcode} specified as the second argument: |
| 66 |
% \begin{itemize} |
| 67 |
% \item $1$ (begin group) |
| 68 |
% \item $2$ (end group) |
| 69 |
% \item $3$ (math toggle) |
| 70 |
% \item $4$ (alignment) |
| 71 |
% \item $6$ (parameter) |
| 72 |
% \item $7$ (math superscript) |
| 73 |
% \item $8$ (math subscript) |
| 74 |
% \item $10$ (space) |
| 75 |
% \item $11$ (letter) |
| 76 |
% \item $12$ (other) |
| 77 |
% \item $13$ (active) |
| 78 |
% \end{itemize} |
| 79 |
% For $8$-bit engines, between one and four character tokens will be |
| 80 |
% produced: these will be the bytes of the UTF-8 representation of the |
| 81 |
% \meta{codepoint}. For all codepoints outside of the classical ASCII |
| 82 |
% range, the generated character tokens will be active (category code |
| 83 |
% $13$); for codepoints in the ASCII range, the given \meta{catcode} |
| 84 |
% will be used. To allow the result of this function to be used |
| 85 |
% inside an expansion context, the result is protected by \cs{exp_not:n}. |
| 86 |
% |
| 87 |
% \begin{texnote} |
| 88 |
% Users of (u)p\TeX{} note that these engines are treated as $8$-bit in |
| 89 |
% this context. In particular, for up\TeX{}, irrespective of the |
| 90 |
% \tn{kcatcode} of the \meta{codepoint}, any value outside the ASCII range |
| 91 |
% will result in a series of active bytes being generated. |
| 92 |
% \end{texnote} |
| 93 |
% \end{function} |
| 94 |
% |
| 95 |
% \begin{function}[EXP, added = 2022-10-09] |
| 96 |
% {\codepoint_str_generate:n} |
| 97 |
% \begin{syntax} |
| 98 |
% \cs{codepoint_str_generate:n} \Arg{codepoint} |
| 99 |
% \end{syntax} |
| 100 |
% Generates one or more character tokens representing the \meta{codepoint}. |
| 101 |
% With Unicode engines, exactly one character token will be generated. |
| 102 |
% For $8$-bit engines, between one and four character tokens will be |
| 103 |
% produced: these will be the bytes of the UTF-8 representation of the |
| 104 |
% \meta{codepoint}. All of the generated character tokens will be of |
| 105 |
% category code $12$, except any spaces (codepoint $32$), which will be |
| 106 |
% category code $10$. |
| 107 |
% \end{function} |
| 108 |
% |
| 109 |
% \begin{function}[added = 2023-06-19, EXP]{\codepoint_to_category:n} |
| 110 |
% \begin{syntax} |
| 111 |
% \cs{codepoint_to_category:n} \Arg{codepoint} |
| 112 |
% \end{syntax} |
| 113 |
% Expands to the Unicode general category identifier of the \meta{codepoint}. |
| 114 |
% The general category identifier is a string made up of two letter |
| 115 |
% characters, the first uppercase and the second lowercase. The uppercase |
| 116 |
% letters divide codepoints into broader groups, which are then refined |
| 117 |
% by the lowercase letter. For example, codepoints representing letters |
| 118 |
% all have identifiers starting \texttt{L}, for example \texttt{Lu} |
| 119 |
% (uppercase letter), \texttt{Lt} (titlecase letter), etc. |
| 120 |
% Full details are available in the documentation provided by the Unicode |
| 121 |
% Consortium: see |
| 122 |
% \url{https://www.unicode.org/reports/tr44/#General_Category_Values} |
| 123 |
% \end{function} |
| 124 |
% |
| 125 |
% \begin{function}[added = 2022-10-09, EXP]{\codepoint_to_nfd:n} |
| 126 |
% \begin{syntax} |
| 127 |
% \cs{codepoint_to_nfd:n} \Arg{codepoint} |
| 128 |
% \end{syntax} |
| 129 |
% Converts the \meta{codepoint} to the Unicode Normalization |
| 130 |
% Form Canonical Decomposition. The generated character(s) will have |
| 131 |
% the current category code as they would if typed in directly for Unicode |
| 132 |
% engines; for $8$-bit engines, active characters are used for all codepoints |
| 133 |
% outside of the ASCII range. |
| 134 |
% \end{function} |
| 135 |
% |
| 136 |
% \end{documentation} |
| 137 |
% |
| 138 |
% \begin{implementation} |
| 139 |
% |
| 140 |
% \section{\pkg{l3unicode} implementation} |
| 141 |
% |
| 142 |
% \begin{macrocode} |
| 143 |
%<*code> |
| 144 |
% \end{macrocode} |
| 145 |
% |
| 146 |
% \begin{macrocode} |
| 147 |
%<@@=codepoint> |
| 148 |
% \end{macrocode} |
| 149 |
% |
| 150 |
% \subsection{User functions} |
| 151 |
% |
| 152 |
% \begin{macro}[EXP]{\codepoint_str_generate:n} |
| 153 |
% \begin{macro}[EXP]{\@@_str_generate:nnnn} |
| 154 |
% \begin{macro}[EXP]{\codepoint_generate:nn} |
| 155 |
% \begin{macro}[EXP]{\@@_generate:nnnn} |
| 156 |
% \begin{macro}[EXP]{\@@_generate:n} |
| 157 |
% Conversion of a codepoint to a character (Unicode engines) or to one |
| 158 |
% or more bytes ($8$-bit engines) is required. For loading the data, |
| 159 |
% all that is needed is the form which creates strings: these are outside |
| 160 |
% the group as they will also be used when looking up data in the hash |
| 161 |
% table storage at point-of-use. Later, we will also need functions that |
| 162 |
% can generate character tokens for document use: those are defined below, |
| 163 |
% in the data recovery setup. |
| 164 |
% \begin{macrocode} |
| 165 |
\sys_if_engine_opentype:TF |
| 166 |
{ |
| 167 |
\cs_new:Npn \codepoint_str_generate:n #1 |
| 168 |
{ |
| 169 |
\int_compare:nNnTF {#1} = { `\ } |
| 170 |
{ ~ } |
| 171 |
{ \char_generate:nn {#1} { 12 } } |
| 172 |
} |
| 173 |
\cs_new:Npn \codepoint_generate:nn #1#2 |
| 174 |
{ |
| 175 |
\int_compare:nNnTF {#1} = { `\ } |
| 176 |
{ ~ } |
| 177 |
{ |
| 178 |
\__kernel_exp_not:w \exp_after:wN \exp_after:wN \exp_after:wN |
| 179 |
{ \char_generate:nn {#1} {#2} } |
| 180 |
} |
| 181 |
} |
| 182 |
} |
| 183 |
{ |
| 184 |
\cs_new:Npn \codepoint_str_generate:n #1 |
| 185 |
{ |
| 186 |
\int_compare:nNnTF {#1} = { `\ } |
| 187 |
{ ~ } |
| 188 |
{ |
| 189 |
\use:e |
| 190 |
{ |
| 191 |
\exp_not:N \@@_str_generate:nnnn |
| 192 |
\__kernel_codepoint_to_bytes:n {#1} |
| 193 |
} |
| 194 |
} |
| 195 |
} |
| 196 |
\cs_new:Npn \@@_str_generate:nnnn #1#2#3#4 |
| 197 |
{ |
| 198 |
\char_generate:nn {#1} { 12 } |
| 199 |
\tl_if_blank:nF {#2} |
| 200 |
{ |
| 201 |
\char_generate:nn {#2} { 12 } |
| 202 |
\tl_if_blank:nF {#3} |
| 203 |
{ |
| 204 |
\char_generate:nn {#3} { 12 } |
| 205 |
\tl_if_blank:nF {#4} |
| 206 |
{ \char_generate:nn {#4} { 12 } } |
| 207 |
} |
| 208 |
} |
| 209 |
} |
| 210 |
\cs_new:Npn \codepoint_generate:nn #1#2 |
| 211 |
{ |
| 212 |
\int_compare:nNnTF {#1} = { `\ } |
| 213 |
{ ~ } |
| 214 |
{ |
| 215 |
\int_compare:nNnTF {#1} < { "80 } |
| 216 |
{ |
| 217 |
\__kernel_exp_not:w \exp_after:wN \exp_after:wN \exp_after:wN |
| 218 |
{ \char_generate:nn {#1} {#2} } |
| 219 |
} |
| 220 |
{ |
| 221 |
\use:e |
| 222 |
{ |
| 223 |
\exp_not:N \@@_generate:nnnn |
| 224 |
\__kernel_codepoint_to_bytes:n {#1} |
| 225 |
} |
| 226 |
} |
| 227 |
} |
| 228 |
} |
| 229 |
\cs_new:Npn \@@_generate:nnnn #1#2#3#4 |
| 230 |
{ |
| 231 |
\__kernel_exp_not:w \exp_after:wN |
| 232 |
{ |
| 233 |
\tex_expanded:D |
| 234 |
{ |
| 235 |
\@@_generate:n {#1} |
| 236 |
\@@_generate:n {#2} |
| 237 |
\tl_if_blank:nF {#3} |
| 238 |
{ |
| 239 |
\@@_generate:n {#3} |
| 240 |
\tl_if_blank:nF {#4} |
| 241 |
{ \@@_generate:n {#4} } |
| 242 |
} |
| 243 |
} |
| 244 |
} |
| 245 |
} |
| 246 |
\cs_new:Npn \@@_generate:n #1 |
| 247 |
{ |
| 248 |
\__kernel_exp_not:w \exp_after:wN \exp_after:wN \exp_after:wN |
| 249 |
{ \char_generate:nn {#1} { 13 } } |
| 250 |
} |
| 251 |
} |
| 252 |
% \end{macrocode} |
| 253 |
% \end{macro} |
| 254 |
% \end{macro} |
| 255 |
% \end{macro} |
| 256 |
% \end{macro} |
| 257 |
% \end{macro} |
| 258 |
% |
| 259 |
% \begin{macro}[EXP]{\__kernel_codepoint_to_bytes:n} |
| 260 |
% \begin{macro}[EXP]{\@@_to_bytes_auxi:n} |
| 261 |
% \begin{macro}[EXP]{\@@_to_bytes_auxii:Nnn} |
| 262 |
% \begin{macro}[EXP]{\@@_to_bytes_auxiii:n} |
| 263 |
% \begin{macro}[EXP] |
| 264 |
% { |
| 265 |
% \@@_to_bytes_outputi:nw , |
| 266 |
% \@@_to_bytes_outputii:nw , |
| 267 |
% \@@_to_bytes_outputiii:nw , |
| 268 |
% \@@_to_bytes_outputiv:nw |
| 269 |
% } |
| 270 |
% \begin{macro}[EXP] |
| 271 |
% {\@@_to_bytes_output:nnn, \@@_to_bytes_output:fnn} |
| 272 |
% \begin{macro}[EXP]{\@@_to_bytes_end:} |
| 273 |
% This code converts a codepoint into the correct UTF-8 representation. |
| 274 |
% In terms of the algorithm itself, see |
| 275 |
% \url{https://en.wikipedia.org/wiki/UTF-8} for the octet pattern. |
| 276 |
% \begin{macrocode} |
| 277 |
\cs_new:Npn \__kernel_codepoint_to_bytes:n #1 |
| 278 |
{ |
| 279 |
\exp_args:Nf \@@_to_bytes_auxi:n |
| 280 |
{ \int_eval:n {#1} } |
| 281 |
} |
| 282 |
\cs_new:Npn \@@_to_bytes_auxi:n #1 |
| 283 |
{ |
| 284 |
\if_int_compare:w #1 > "80 \exp_stop_f: |
| 285 |
\if_int_compare:w #1 < "800 \exp_stop_f: |
| 286 |
\@@_to_bytes_outputi:nw |
| 287 |
{ \@@_to_bytes_auxii:Nnn C {#1} { 64 } } |
| 288 |
\@@_to_bytes_outputii:nw |
| 289 |
{ \@@_to_bytes_auxiii:n {#1} } |
| 290 |
\else: |
| 291 |
\if_int_compare:w #1 < "10000 \exp_stop_f: |
| 292 |
\@@_to_bytes_outputi:nw |
| 293 |
{ \@@_to_bytes_auxii:Nnn E {#1} { 64 * 64 } } |
| 294 |
\@@_to_bytes_outputii:nw |
| 295 |
{ |
| 296 |
\@@_to_bytes_auxiii:n |
| 297 |
{ \int_div_truncate:nn {#1} { 64 } } |
| 298 |
} |
| 299 |
\@@_to_bytes_outputiii:nw |
| 300 |
{ \@@_to_bytes_auxiii:n {#1} } |
| 301 |
\else: |
| 302 |
\@@_to_bytes_outputi:nw |
| 303 |
{ |
| 304 |
\@@_to_bytes_auxii:Nnn F |
| 305 |
{#1} { 64 * 64 * 64 } |
| 306 |
} |
| 307 |
\@@_to_bytes_outputii:nw |
| 308 |
{ |
| 309 |
\@@_to_bytes_auxiii:n |
| 310 |
{ \int_div_truncate:nn {#1} { 64 * 64 } } |
| 311 |
} |
| 312 |
\@@_to_bytes_outputiii:nw |
| 313 |
{ |
| 314 |
\@@_to_bytes_auxiii:n |
| 315 |
{ \int_div_truncate:nn {#1} { 64 } } |
| 316 |
} |
| 317 |
\@@_to_bytes_outputiv:nw |
| 318 |
{ \@@_to_bytes_auxiii:n {#1} } |
| 319 |
\fi: |
| 320 |
\fi: |
| 321 |
\else: |
| 322 |
\@@_to_bytes_outputi:nw {#1} |
| 323 |
\fi: |
| 324 |
\@@_to_bytes_end: { } { } { } { } |
| 325 |
} |
| 326 |
\cs_new:Npn \@@_to_bytes_auxii:Nnn #1#2#3 |
| 327 |
{ "#10 + \int_div_truncate:nn {#2} {#3} } |
| 328 |
\cs_new:Npn \@@_to_bytes_auxiii:n #1 |
| 329 |
{ \int_mod:nn {#1} { 64 } + 128 } |
| 330 |
\cs_new:Npn \@@_to_bytes_outputi:nw |
| 331 |
#1 #2 \@@_to_bytes_end: #3 |
| 332 |
{ \@@_to_bytes_output:fnn { \int_eval:n {#1} } { } {#2} } |
| 333 |
\cs_new:Npn \@@_to_bytes_outputii:nw |
| 334 |
#1 #2 \@@_to_bytes_end: #3#4 |
| 335 |
{ \@@_to_bytes_output:fnn { \int_eval:n {#1} } { {#3} } {#2} } |
| 336 |
\cs_new:Npn \@@_to_bytes_outputiii:nw |
| 337 |
#1 #2 \@@_to_bytes_end: #3#4#5 |
| 338 |
{ |
| 339 |
\@@_to_bytes_output:fnn |
| 340 |
{ \int_eval:n {#1} } { {#3} {#4} } {#2} |
| 341 |
} |
| 342 |
\cs_new:Npn \@@_to_bytes_outputiv:nw |
| 343 |
#1 #2 \@@_to_bytes_end: #3#4#5#6 |
| 344 |
{ |
| 345 |
\@@_to_bytes_output:fnn |
| 346 |
{ \int_eval:n {#1} } { {#3} {#4} {#5} } {#2} |
| 347 |
} |
| 348 |
\cs_new:Npn \@@_to_bytes_output:nnn #1#2#3 |
| 349 |
{ |
| 350 |
#3 |
| 351 |
\@@_to_bytes_end: #2 {#1} |
| 352 |
} |
| 353 |
\cs_generate_variant:Nn \@@_to_bytes_output:nnn { f } |
| 354 |
\cs_new:Npn \@@_to_bytes_end: { } |
| 355 |
% \end{macrocode} |
| 356 |
% \end{macro} |
| 357 |
% \end{macro} |
| 358 |
% \end{macro} |
| 359 |
% \end{macro} |
| 360 |
% \end{macro} |
| 361 |
% \end{macro} |
| 362 |
% \end{macro} |
| 363 |
% |
| 364 |
% \begin{macro}[EXP]{\codepoint_to_category:n} |
| 365 |
% Get the value and convert back to the string. |
| 366 |
% \begin{macrocode} |
| 367 |
\sys_if_engine_luatex:TF |
| 368 |
{ |
| 369 |
\cs_new:Npn \codepoint_to_category:n #1 |
| 370 |
{ |
| 371 |
\__kernel_codepoint_data:wn \int_eval:n { #1 } { category } |
| 372 |
} |
| 373 |
} |
| 374 |
{ |
| 375 |
\cs_new:Npn \codepoint_to_category:n #1 |
| 376 |
{ |
| 377 |
\cs:w |
| 378 |
c_@@_category_ |
| 379 |
\tex_romannumeral:D |
| 380 |
\__kernel_codepoint_data:nn { category } {#1} |
| 381 |
_str |
| 382 |
\cs_end: |
| 383 |
} |
| 384 |
} |
| 385 |
% \end{macrocode} |
| 386 |
% \end{macro} |
| 387 |
% |
| 388 |
% \begin{macro}[EXP]{\codepoint_to_nfd:n, \@@_to_nfd:n} |
| 389 |
% \begin{macro}[EXP]{\@@_to_nfd:nn} |
| 390 |
% \begin{macro}[EXP]{\@@_to_nfd:nnn} |
| 391 |
% \begin{macro}[EXP]{\@@_to_nfd:nnnn} |
| 392 |
% Converted to NFD is a potentially-recursive process: the key is to |
| 393 |
% check if we get the input codepoint back again. As far as possible, |
| 394 |
% we use the same path for all engines. |
| 395 |
% \begin{macrocode} |
| 396 |
\cs_new:Npn \codepoint_to_nfd:n #1 |
| 397 |
{ \exp_args:Ne \@@_to_nfd:n { \int_eval:n {#1} } } |
| 398 |
\cs_new:Npn \@@_to_nfd:n #1 |
| 399 |
{ \@@_to_nfd:nn {#1} { \char_value_catcode:n {#1} } } |
| 400 |
\sys_if_engine_opentype:F |
| 401 |
{ |
| 402 |
\cs_gset:Npn \@@_to_nfd:n #1 |
| 403 |
{ |
| 404 |
\int_compare:nNnTF {#1} > { "80 } |
| 405 |
{ \@@_to_nfd:nn {#1} { 12 } } |
| 406 |
{ \@@_to_nfd:nn {#1} { \char_value_catcode:n {#1} } } |
| 407 |
} |
| 408 |
} |
| 409 |
\cs_new:Npn \@@_to_nfd:nn #1#2 |
| 410 |
{ |
| 411 |
\exp_args:Ne \@@_to_nfd:nnn |
| 412 |
{ \@@_nfd:n {#1} } {#1} {#2} |
| 413 |
} |
| 414 |
\cs_new:Npn \@@_to_nfd:nnn #1#2#3 { \@@_to_nfd:nnnn #1 {#2} {#3} } |
| 415 |
\cs_new:Npn \@@_to_nfd:nnnn #1#2#3#4 |
| 416 |
{ |
| 417 |
\int_compare:nNnTF {#1} = {#3} |
| 418 |
{ \codepoint_generate:nn {#1} {#4} } |
| 419 |
{ |
| 420 |
\@@_to_nfd:nn {#1} {#4} |
| 421 |
\tl_if_blank:nF {#2} |
| 422 |
{ \@@_to_nfd:nn {#2} {#4} } |
| 423 |
} |
| 424 |
} |
| 425 |
% \end{macrocode} |
| 426 |
% \end{macro} |
| 427 |
% \end{macro} |
| 428 |
% \end{macro} |
| 429 |
% \end{macro} |
| 430 |
% |
| 431 |
% \subsection{Data loader} |
| 432 |
% |
| 433 |
% Text operations requires data from the Unicode Consortium. Data read into |
| 434 |
% Unicode engine formats is at best a small part of what we need, so there |
| 435 |
% is a loader here to set up the appropriate data structures. |
| 436 |
% |
| 437 |
% Where we need data for most or all of the Unicode range, we use the two-stage |
| 438 |
% table approach recommended by the Unicode Consortium and demonstrated in a |
| 439 |
% model implementation in Python in |
| 440 |
% \url{https://www.strchr.com/multi-stage_tables}. This approach uses the |
| 441 |
% \texttt{intarray} (\texttt{fontdimen}-based) data type as it is fast for |
| 442 |
% random access and avoids significant hash table usage. In contrast, where |
| 443 |
% only a small subset of codepoints are required, storage as macros is |
| 444 |
% preferable. There is also some consideration of the effort needed to load |
| 445 |
% data: see for example the grapheme breaking information, which would be |
| 446 |
% problematic to convert into a two-stage table but which can be used with |
| 447 |
% reasonable performance in a small number of comma lists (at the cost that |
| 448 |
% breaking at higher codepoint Hangul characters will be slightly slow). |
| 449 |
% |
| 450 |
% \begin{variable}{\c_@@_block_size_int} |
| 451 |
% Choosing the block size for the blocks in the two-stage approach is |
| 452 |
% non-trivial: depending on the data stored, the optimal size for |
| 453 |
% memory usage will vary. At the same time, for us there is also the |
| 454 |
% question of load-time: larger blocks require longer comma lists |
| 455 |
% as intermediates, so are slower. As this is going to be needed |
| 456 |
% to use the data, we set it up outside of the group for clarity. |
| 457 |
% \begin{macrocode} |
| 458 |
\int_const:Nn \c_@@_block_size_int { 64 } |
| 459 |
% \end{macrocode} |
| 460 |
% \end{variable} |
| 461 |
% |
| 462 |
% Parsing the data files can be the same way for all engines, but where they |
| 463 |
% are stored as character tokens, the construction method depends on whether |
| 464 |
% they are Unicode or $8$-bit internally. Parsing is therefore done by common |
| 465 |
% functions, with some data storage using engine-specific auxiliaries. |
| 466 |
% |
| 467 |
% As only the data needs to remain at the end of this process, everything |
| 468 |
% is set up inside a group. The only thing that is outside is creating a |
| 469 |
% stream: they are global anyway and it is best to force a stream for |
| 470 |
% all engines. |
| 471 |
% |
| 472 |
% Lua\TeX{} does not use the parsing into multi stage tables. While |
| 473 |
% it still stores the data in this format, this is handled by a faster |
| 474 |
% Lua implementation in \pkg{lua-uni-algos} and reused here. |
| 475 |
% |
| 476 |
% \begin{variable}{\g_@@_data_ior} |
| 477 |
% \begin{macrocode} |
| 478 |
\ior_new:N \g_@@_data_ior |
| 479 |
% \end{macrocode} |
| 480 |
% \end{variable} |
| 481 |
% |
| 482 |
% We need some setup for the two-part table approach. The number of blocks we |
| 483 |
% need will be variable, but the resulting size of the stage one table |
| 484 |
% is predictable. For performance reasons, we therefore create the stage one |
| 485 |
% tables now so they can be used immediately, and will later rename them as a |
| 486 |
% constant tables. For each two-stage table construction, we need a comma |
| 487 |
% list to hold the partial block and a couple of integers to track where |
| 488 |
% we are up to. To avoid burning registers, the latter are stored in macros |
| 489 |
% and are \enquote{fake} integers. We also avoid any \texttt{new} functions, |
| 490 |
% keeping as much as possible local. |
| 491 |
% |
| 492 |
% As we need both positive and negative values, case data requires one |
| 493 |
% two-stage table for each transformation. In contrasts, general Unicode |
| 494 |
% properties could be stored in one table with appropriate combination rules: |
| 495 |
% that is not done at present but is likely to be added over time. Here, all |
| 496 |
% that is needed is additional entries into the comma-list to create the |
| 497 |
% structures. |
| 498 |
% |
| 499 |
% Notice that in the standard \pkg{expl3} way we are indexes position not |
| 500 |
% offset: that does mean a little work later. |
| 501 |
% |
| 502 |
% For integer values, everything is done using token lists to avoid losing |
| 503 |
% registers or making global names. |
| 504 |
% \begin{macrocode} |
| 505 |
\sys_if_engine_luatex:F |
| 506 |
{ |
| 507 |
\group_begin: |
| 508 |
\clist_map_inline:nn |
| 509 |
{ category , grapheme , lowercase , uppercase , wordbreak } |
| 510 |
{ |
| 511 |
\cs_set_nopar:cpn { l_@@_ #1 _block_clist } { } |
| 512 |
\cs_set_nopar:cpn { l_@@_ #1 _block_tl } { 1 } |
| 513 |
\cs_set_nopar:cpn { l_@@_ #1 _pos_tl } { 0 } |
| 514 |
\cs_set_nopar:cpn { l_@@_ #1 _next_tl } { 0 } |
| 515 |
\intarray_new:cn { g_@@_ #1 _index_intarray } |
| 516 |
{ \int_div_truncate:nn { "110000 } \c_@@_block_size_int } |
| 517 |
} |
| 518 |
% \end{macrocode} |
| 519 |
% We also need to track the matched block: this is used dynamically so we |
| 520 |
% only require one variable. |
| 521 |
% \begin{macrocode} |
| 522 |
\cs_set_nopar:Npn \l_@@_matched_block_tl { 0 } |
| 523 |
% \end{macrocode} |
| 524 |
% For Unicode general category and the various breaking properties, there needs |
| 525 |
% to be numerical representation of each possible value. As we need to go from |
| 526 |
% string to number here, but the other way elsewhere, we set up fast mappings |
| 527 |
% both ways, but one set local and the other as constants. |
| 528 |
% \begin{macrocode} |
| 529 |
\cs_set_nopar:Npn \l_@@_uppercase_default_tl { 0 } |
| 530 |
\cs_set_nopar:Npn \l_@@_lowercase_default_tl { 0 } |
| 531 |
\cs_set_protected:Npn \@@_data_auxi:w #1#2#3 |
| 532 |
{ |
| 533 |
\quark_if_recursion_tail_stop:n {#3} |
| 534 |
\cs_set_nopar:cpn { l_@@_ #2 _ #3 _tl } {#1} |
| 535 |
\str_const:cn { c_@@_ #2 _ \tex_romannumeral:D #1 _str } {#3} |
| 536 |
\exp_args:Ne \@@_data_auxi:w { \int_eval:n { #1 + 1 } } {#2} |
| 537 |
} |
| 538 |
\@@_data_auxi:w { 1 } { category } |
| 539 |
{ Lu } { Ll } { Lt } { Lm } { Lo } |
| 540 |
{ Mn } { Me } { Mc } |
| 541 |
{ Nd } { Nl } { No } |
| 542 |
{ Zs } { Zl } { Zp } |
| 543 |
{ Cc } { Cf } { Co } { Cs } { Cn } |
| 544 |
{ Pd } { Ps } { Pe } { Pc } { Po } { Pi } { Pf } |
| 545 |
{ Sm } { Sc } { Sk } { So } |
| 546 |
\q_recursion_tail |
| 547 |
\q_recursion_stop |
| 548 |
\cs_set_eq:NN \l_@@_category_default_tl \l_@@_category_Cn_tl |
| 549 |
\@@_data_auxi:w { 1 } { grapheme } |
| 550 |
{ Control } |
| 551 |
{ CR } { LF } { ZWJ } |
| 552 |
{ Extend } |
| 553 |
{ L } { LV } { LVT } { T } { V } |
| 554 |
{ Prepend } |
| 555 |
{ Regional_Indicator } |
| 556 |
{ SpacingMark } |
| 557 |
{ Other } |
| 558 |
\q_recursion_tail |
| 559 |
\q_recursion_stop |
| 560 |
\cs_set_eq:NN \l_@@_grapheme_default_tl \l_@@_grapheme_Other_tl |
| 561 |
\@@_data_auxi:w { 1 } { wordbreak } |
| 562 |
{ Double_Quote } { Single_Quote } |
| 563 |
{ CR } { LF } { Newline } |
| 564 |
{ WSegSpace } { ZWJ } |
| 565 |
{ Extend } { ExtendNumLet } |
| 566 |
{ Regional_Indicator } |
| 567 |
{ Format } |
| 568 |
{ Katakana } |
| 569 |
{ ALetter } { MidLetter } { Hebrew_Letter } |
| 570 |
{ Numeric }{ MidNum } { MidNumLet } |
| 571 |
{ Other } |
| 572 |
\q_recursion_tail |
| 573 |
\q_recursion_stop |
| 574 |
\cs_set_eq:NN \l_@@_wordbreak_default_tl \l_@@_wordbreak_Other_tl |
| 575 |
% \end{macrocode} |
| 576 |
% For the the property fields, we need to use a two-step procedure |
| 577 |
% as the file is ordered by class not codepoint. First, we parse the content |
| 578 |
% into the hash table locally: there are around $1400$ codepoints to handle, |
| 579 |
% which is workable. Reading this is quite easy: we store any end-of-range |
| 580 |
% codepoint and the class that applies. Conversion to a two-stage table |
| 581 |
% is deferred to later. |
| 582 |
% \begin{macrocode} |
| 583 |
\cs_set_protected:Npn \@@_data_auxi:w #1 ;~ #2 ~ #3 \q_stop |
| 584 |
{ \@@_data_auxii:w #1 .. \q_stop {#2} } |
| 585 |
\cs_set_protected:Npn \@@_data_auxii:w #1 .. #2 \q_stop |
| 586 |
{ \@@_data_auxiii:w #1 ~ .. #2 ~ \q_stop } |
| 587 |
\cs_set_protected:Npn \@@_data_auxiii:w #1 ~ #2 .. #3 ~ #4 \q_stop #5#6 |
| 588 |
{ |
| 589 |
\cs_set_nopar:cpe { l_@@_ #6 _ \tex_romannumeral:D "#1 _tl } |
| 590 |
{ |
| 591 |
{#3} |
| 592 |
{ \use:c { l_@@_ #6 _ #5 _tl } } |
| 593 |
} |
| 594 |
} |
| 595 |
\cs_set_protected:Npn \@@_data_auxvi:w #1#2 |
| 596 |
{ |
| 597 |
\ior_open:Nn \g_@@_data_ior {#1} |
| 598 |
\ior_str_map_inline:Nn \g_@@_data_ior |
| 599 |
{ |
| 600 |
\str_if_eq:eeF { \tl_head:w ##1 \c_hash_str \q_stop } { \c_hash_str } |
| 601 |
{ |
| 602 |
\tl_if_blank:nF {##1} |
| 603 |
{ \@@_data_auxi:w ##1 \q_stop {#2} } |
| 604 |
} |
| 605 |
} |
| 606 |
\ior_close:N \g_@@_data_ior |
| 607 |
} |
| 608 |
\@@_data_auxvi:w { GraphemeBreakProperty.txt } { grapheme } |
| 609 |
\@@_data_auxvi:w { WordBreakProperty.txt } { wordbreak } |
| 610 |
% \end{macrocode} |
| 611 |
% The set up for adding property data is now sorted. |
| 612 |
% \begin{macrocode} |
| 613 |
\cs_set_protected:Npn \@@_data_property:nnnn #1#2#3#4 |
| 614 |
{ |
| 615 |
\int_compare:nNnT {#3} > { \use:c { l_@@_ #4 _next_tl } } |
| 616 |
{ |
| 617 |
\@@_range:nnv {#3} {#4} |
| 618 |
{ l_@@_ #4 _default_tl } |
| 619 |
} |
| 620 |
\@@_add:nn {#4} {#2} |
| 621 |
\tl_set:ce { l_@@_ #4 _next_tl } { \int_eval:n { #3 + 1 } } |
| 622 |
\tl_if_blank:nF {#1} |
| 623 |
{ |
| 624 |
\@@_range:nnn {"#1} {#4} {#2} |
| 625 |
\@@_add:nn {#4} {#2} |
| 626 |
\tl_set:ce { l_@@_ #4 _next_tl } { \int_eval:n { "#1 + 1 } } |
| 627 |
} |
| 628 |
} |
| 629 |
% \end{macrocode} |
| 630 |
% Parse the main Unicode data file and pull out the NFD and case changing |
| 631 |
% data. The NFD data is stored on using the hash table approach and can yield |
| 632 |
% a predictable number of codepoints: one or two. We also need the case data, |
| 633 |
% which will be modified further below. To allow for finding ranges, the |
| 634 |
% description of the codepoint needs to be carried forward. |
| 635 |
% \begin{macrocode} |
| 636 |
\cs_set_protected:Npn \@@_data_auxi:w |
| 637 |
#1 ; #2 ; #3 ; #4 ; #5 ; #6 ; #7 ; #8 ; #9 ; |
| 638 |
{ |
| 639 |
\tl_if_blank:nF {#6} |
| 640 |
{ |
| 641 |
\tl_if_head_eq_charcode:nNF {#6} < % > |
| 642 |
{ \@@_data_auxii:w #1 ; #6 ~ \q_stop } |
| 643 |
} |
| 644 |
\@@_data_auxiii:w #1 ; #2 ; #3 ; |
| 645 |
} |
| 646 |
\cs_set_protected:Npn \@@_data_auxii:w #1 ; #2 ~ #3 \q_stop |
| 647 |
{ |
| 648 |
\tl_const:ce |
| 649 |
{ c_@@_nfd_ \codepoint_str_generate:n {"#1} _tl } |
| 650 |
{ |
| 651 |
{"#2} |
| 652 |
{ \tl_if_blank:nF {#3} {"#3} } |
| 653 |
} |
| 654 |
} |
| 655 |
% \end{macrocode} |
| 656 |
% The category data needs to be converted from a string to the numerical |
| 657 |
% equivalent: a simple operation. |
| 658 |
% The case data is going to be stored as an offset from the parent character, |
| 659 |
% rather than an absolute value. We therefore deal with that plus the situation |
| 660 |
% where a codepoint has no mapping data in one shot. |
| 661 |
% \begin{macrocode} |
| 662 |
\cs_set_protected:Npn \@@_data_auxiii:w |
| 663 |
#1 ; #2 ; #3 ; #4 ; #5 ; #6 ; #7 ; #8 ; #9 ~ \q_stop |
| 664 |
{ |
| 665 |
\use:e |
| 666 |
{ |
| 667 |
\@@_data_auxiv:w |
| 668 |
#1 ; #2 ; |
| 669 |
\@@_data_category:n {#3} ; |
| 670 |
\@@_data_offset:nn {#1} {#7} ; |
| 671 |
\@@_data_offset:nn {#1} {#8} ; |
| 672 |
#9; |
| 673 |
} |
| 674 |
} |
| 675 |
\cs_set:Npn \@@_data_category:n #1 |
| 676 |
{ \use:c { l_@@_category_ #1 _tl } } |
| 677 |
\cs_set:Npn \@@_data_offset:nn #1#2 |
| 678 |
{ |
| 679 |
\tl_if_blank:nTF {#2} |
| 680 |
{ 0 } |
| 681 |
{ \int_eval:n { "#2 - "#1 } } |
| 682 |
} |
| 683 |
% \end{macrocode} |
| 684 |
% To deal with ranges, we track the position of the next codepoint expected. |
| 685 |
% If there is a gap, we deal with that separately: it could be a range or |
| 686 |
% an unused part of the Unicode space. As such, we deal with the current |
| 687 |
% codepoint here whether or not there is range to fill in. Upper- and |
| 688 |
% lowercase data go into the two-stage table, any titlecase exception is |
| 689 |
% just stored in a macro. The data for the codepoint is added to the current |
| 690 |
% block, and if that is now complete we move on to save the block. The |
| 691 |
% case exceptions are all stored as codepoints, with a fixed number of |
| 692 |
% balanced text as we know that there are never more than three. |
| 693 |
% \begin{macrocode} |
| 694 |
\cs_set_protected:Npn \@@_data_auxiv:w #1 ; #2 ; #3 ; #4 ; #5 ; #6 ; |
| 695 |
{ |
| 696 |
\int_compare:nNnT {"#1} > \l_@@_category_next_tl |
| 697 |
{ |
| 698 |
\@@_data_auxv:nnnnw {#1} {#3} {#4} {#5} |
| 699 |
#2 Last> \q_stop |
| 700 |
} |
| 701 |
\@@_add:nn { category } {#3} |
| 702 |
\@@_add:nn { uppercase } {#4} |
| 703 |
\@@_add:nn { lowercase } {#5} |
| 704 |
\int_compare:nNnF {#4} = { \@@_data_offset:nn {#1} {#6} } |
| 705 |
{ |
| 706 |
\tl_const:ce |
| 707 |
{ c_@@_titlecase_ \codepoint_str_generate:n {"#1} _tl } |
| 708 |
{ {"#6} { } { } } |
| 709 |
} |
| 710 |
\@@_data_auxvi:nn { grapheme } {"#1} |
| 711 |
\@@_data_auxvi:nn { wordbreak } {"#1} |
| 712 |
% \end{macrocode} |
| 713 |
% To deal with the property data, we need to recover stored information and |
| 714 |
% build the table. Generally we can assume that all codepoints in these |
| 715 |
% files are in |UnicodeData.txt|, but there is one place |
| 716 |
% a bit more work is needed. For Hangul syllables, the main file lists a range |
| 717 |
% but within that we have different classifications for breaking: that needs to |
| 718 |
% be handled with a second loop. The values for this range are hard-coded, |
| 719 |
% skipping the end-of-range as that will be mopped up by the main loop. |
| 720 |
% \begin{macrocode} |
| 721 |
\int_compare:nNnT {"#1} = { "AC00 } |
| 722 |
{ |
| 723 |
\int_step_inline:nnn { "AC01 } { "D7A2 } |
| 724 |
{ \@@_data_auxvi:nn { grapheme } {##1} } |
| 725 |
} |
| 726 |
\tl_set:Ne \l_@@_category_next_tl |
| 727 |
{ \int_eval:n { "#1 + 1 } } |
| 728 |
\tl_set_eq:NN \l_@@_lowercase_next_tl \l_@@_category_next_tl |
| 729 |
\tl_set_eq:NN \l_@@_uppercase_next_tl \l_@@_category_next_tl |
| 730 |
} |
| 731 |
\cs_set_protected:Npn \@@_add:nn #1#2 |
| 732 |
{ |
| 733 |
\clist_put_right:cn { l_@@_ #1 _block_clist } {#2} |
| 734 |
\int_compare:nNnT { \clist_count:c { l_@@_ #1 _block_clist } } |
| 735 |
= \c_@@_block_size_int |
| 736 |
{ \@@_save_blocks:nn {#1} { 1 } } |
| 737 |
} |
| 738 |
% \end{macrocode} |
| 739 |
% Distinguish between a range and a gap, and pass on the appropriate value(s). |
| 740 |
% The general category for unassigned characters is \texttt{Cn}, so we |
| 741 |
% find the correct value once and then use that. |
| 742 |
% \begin{macrocode} |
| 743 |
\cs_set_protected:Npn \@@_data_auxv:nnnnw #1#2#3#4#5 Last> #6 \q_stop |
| 744 |
{ |
| 745 |
\tl_if_blank:nTF {#6} |
| 746 |
{ |
| 747 |
\@@_range:nno {"#1} { category } |
| 748 |
\l_@@_category_default_tl |
| 749 |
\@@_range:nnn {"#1} { uppercase } { 0 } |
| 750 |
\@@_range:nnn {"#1} { lowercase } { 0 } |
| 751 |
} |
| 752 |
{ |
| 753 |
\@@_range:nnn {"#1} { category } {#2} |
| 754 |
\@@_range:nnn {"#1} { uppercase } {#3} |
| 755 |
\@@_range:nnn {"#1} { lowercase } {#4} |
| 756 |
} |
| 757 |
} |
| 758 |
% \end{macrocode} |
| 759 |
% Recover and process grapheme data. |
| 760 |
% \begin{macrocode} |
| 761 |
\cs_set_protected:Npn \@@_data_auxvi:nn #1#2 |
| 762 |
{ |
| 763 |
\cs_if_exist:cT |
| 764 |
{ l_@@_ #1 _ \tex_romannumeral:D #2 _tl } |
| 765 |
{ |
| 766 |
\exp_after:wN \exp_after:wN \exp_after:wN \@@_data_property:nnnn |
| 767 |
\cs:w |
| 768 |
l_@@_ #1 _ \tex_romannumeral:D #2 _tl |
| 769 |
\cs_end: {#2} {#1} |
| 770 |
} |
| 771 |
} |
| 772 |
% \end{macrocode} |
| 773 |
% Calculated the length of the range and the space remaining in the current |
| 774 |
% block. |
| 775 |
% \begin{macrocode} |
| 776 |
\cs_set_protected:Npn \@@_range:nnn #1#2 |
| 777 |
{ |
| 778 |
\exp_args:Nf \@@_range_aux:nnn |
| 779 |
{ \int_eval:n { #1 - \use:c { l_@@_ #2 _next_tl } } } |
| 780 |
{#2} |
| 781 |
} |
| 782 |
\cs_set_protected:Npn \@@_range:nno { \exp_args:Nnno \@@_range:nnn } |
| 783 |
\cs_set_protected:Npn \@@_range:nnv { \exp_args:Nnnv \@@_range:nnn } |
| 784 |
\cs_set_protected:Npn \@@_range_aux:nnn #1#2 |
| 785 |
{ |
| 786 |
\exp_args:Nf \@@_range:nnnn |
| 787 |
{ |
| 788 |
\int_min:nn |
| 789 |
{#1} |
| 790 |
{ |
| 791 |
\c_@@_block_size_int |
| 792 |
- \clist_count:c { l_@@_ #2 _block_clist } |
| 793 |
} |
| 794 |
} |
| 795 |
{#1} {#2} |
| 796 |
} |
| 797 |
% \end{macrocode} |
| 798 |
% Here we want to do three things: add to and possibly complete the current |
| 799 |
% block, add complete blocks quickly, then finish up the range in a final |
| 800 |
% open block. We need to avoid as far as possible avoid dealing with every |
| 801 |
% single codepoint, so the middle step is optimized. |
| 802 |
% \begin{macrocode} |
| 803 |
\cs_set_protected:Npn \@@_range:nnnn #1#2#3#4 |
| 804 |
{ |
| 805 |
\prg_replicate:nn {#1} |
| 806 |
{ \clist_put_right:cn { l_@@_ #3 _block_clist } {#4} } |
| 807 |
\int_compare:nNnT { \clist_count:c { l_@@_ #3 _block_clist } } |
| 808 |
= \c_@@_block_size_int |
| 809 |
{ \@@_save_blocks:nn {#3} { 1 } } |
| 810 |
\int_compare:nNnF |
| 811 |
{ \int_div_truncate:nn { #2 - #1 } \c_@@_block_size_int } = 0 |
| 812 |
{ |
| 813 |
\tl_set:ce { l_@@_ #3 _block_clist } |
| 814 |
{ |
| 815 |
\exp_args:NNe \use:nn \use_none:n |
| 816 |
{ \prg_replicate:nn { \c_@@_block_size_int } { , #4 } } |
| 817 |
} |
| 818 |
\@@_save_blocks:nn {#3} |
| 819 |
{ \int_div_truncate:nn { (#2 - #1) } \c_@@_block_size_int } |
| 820 |
} |
| 821 |
\prg_replicate:nn |
| 822 |
{ \int_mod:nn { #2 - #1 } \c_@@_block_size_int } |
| 823 |
{ \clist_put_right:ce { l_@@_ #3 _block_clist } {#4} } |
| 824 |
} |
| 825 |
% \end{macrocode} |
| 826 |
% To allow rapid comparison, each completed block is stored locally as a |
| 827 |
% comma list: once all of the blocks have been created, they are converted |
| 828 |
% into an \texttt{intarray} in one step. The aim here is to check the current |
| 829 |
% block against those we've already used, and either match to an existing |
| 830 |
% block or save a new block. |
| 831 |
% \begin{macrocode} |
| 832 |
\cs_set_protected:Npn \@@_save_blocks:nn #1#2 |
| 833 |
{ |
| 834 |
\tl_set_eq:Nc \l_@@_matched_block_tl { l_@@_ #1 _block_tl } |
| 835 |
\int_step_inline:nn { \tl_use:c { l_@@_ #1 _block_tl } - 1 } |
| 836 |
{ |
| 837 |
\tl_if_eq:ccT { l_@@_ #1 _block_clist } |
| 838 |
{ l_@@_ #1 _block_ ##1 _clist } |
| 839 |
{ \tl_set:Nn \l_@@_matched_block_tl {##1} } |
| 840 |
} |
| 841 |
\int_compare:nNnT |
| 842 |
{ \tl_use:c { l_@@_ #1 _block_tl } } = \l_@@_matched_block_tl |
| 843 |
{ |
| 844 |
\clist_set_eq:cc |
| 845 |
{ |
| 846 |
l_@@_ #1 _block_ |
| 847 |
\tl_use:c { l_@@_ #1 _block_tl } _clist |
| 848 |
} |
| 849 |
{ l_@@_ #1 _block_clist } |
| 850 |
\tl_set:ce { l_@@_ #1 _block_tl } |
| 851 |
{ \int_eval:n { \tl_use:c { l_@@_ #1 _block_tl } + 1 } } |
| 852 |
} |
| 853 |
% \end{macrocode} |
| 854 |
% Here, we avoid \cs{prg_replicate:nn} as the number of tokens generated would be |
| 855 |
% high: that shows in the format dump (although \TeX{} recovers memory during |
| 856 |
% the subsequent runs). |
| 857 |
% \begin{macrocode} |
| 858 |
\int_step_inline:nnn |
| 859 |
{ \tl_use:c { l_@@_ #1 _pos_tl } + 1 } |
| 860 |
{ \tl_use:c { l_@@_ #1 _pos_tl } + #2 } |
| 861 |
{ |
| 862 |
\exp_args:Nc \__kernel_intarray_gset:Nnn |
| 863 |
{ g_@@_ #1 _index_intarray } |
| 864 |
{##1} |
| 865 |
\l_@@_matched_block_tl |
| 866 |
} |
| 867 |
\tl_set:ce { l_@@_ #1 _pos_tl } |
| 868 |
{ \int_eval:n { \tl_use:c { l_@@_ #1 _pos_tl } + #2 } } |
| 869 |
\clist_clear:c { l_@@_ #1 _block_clist } |
| 870 |
} |
| 871 |
% \end{macrocode} |
| 872 |
% Close out the final block, rename the first stage table, then combine all |
| 873 |
% of the block comma-lists into one large second-stage table with offsets. |
| 874 |
% As we use an index not an offset, there is a little back-and-forward to do. |
| 875 |
% \begin{macrocode} |
| 876 |
\cs_set_protected:Npn \@@_finalize_blocks:n #1 |
| 877 |
{ |
| 878 |
\clist_map_inline:nn {#1} |
| 879 |
{ |
| 880 |
\exp_args:Nnnv \@@_range:nnn { "110000 } {##1} |
| 881 |
{ l_@@_ ##1 _default_tl } |
| 882 |
\@@_finalize_blocks_aux:n {##1} |
| 883 |
} |
| 884 |
} |
| 885 |
\cs_set_protected:Npn \@@_finalize_blocks_aux:n #1 |
| 886 |
{ |
| 887 |
\cs_gset_eq:cc { c_@@_ #1 _index_intarray } { g_@@_ #1 _index_intarray } |
| 888 |
\cs_undefine:c { g_@@_ #1 _index_intarray } |
| 889 |
\intarray_new:cn { g_@@_ #1 _blocks_intarray } |
| 890 |
{ ( \tl_use:c { l_@@_ #1 _block_tl } - 1 ) * \c_@@_block_size_int } |
| 891 |
\int_step_inline:nn { \tl_use:c { l_@@_ #1 _block_tl } - 1 } |
| 892 |
{ |
| 893 |
\exp_args:Nv \@@_finalize_blocks:nnn |
| 894 |
{ l_@@_ #1 _block_ ##1 _clist } |
| 895 |
{##1} {#1} |
| 896 |
} |
| 897 |
\cs_gset_eq:cc { c_@@_ #1 _blocks_intarray } |
| 898 |
{ g_@@_ #1 _blocks_intarray } |
| 899 |
\cs_undefine:c { g_@@_ #1 _blocks_intarray } |
| 900 |
} |
| 901 |
\cs_set_protected:Npn \@@_finalize_blocks:nnn #1#2#3 |
| 902 |
{ |
| 903 |
\exp_args:Nnf \@@_finalize_blocks:nnnw { 1 } |
| 904 |
{ \int_eval:n { ( #2 - 1 ) * \c_@@_block_size_int } } |
| 905 |
{#3} |
| 906 |
#1 , \q_recursion_tail , \q_recursion_stop |
| 907 |
} |
| 908 |
\cs_set_protected:Npn \@@_finalize_blocks:nnnw #1#2#3#4 , |
| 909 |
{ |
| 910 |
\quark_if_recursion_tail_stop:n {#4} |
| 911 |
\intarray_gset:cnn { g_@@_ #3 _blocks_intarray } |
| 912 |
{ #1 + #2 } |
| 913 |
{#4} |
| 914 |
\exp_args:Nf \@@_finalize_blocks:nnnw |
| 915 |
{ \int_eval:n { #1 + 1 } } {#2} {#3} |
| 916 |
} |
| 917 |
% \end{macrocode} |
| 918 |
% With the setup done, read the main data file: it's easiest to do that as |
| 919 |
% a token list with spaces retained. |
| 920 |
% \begin{macrocode} |
| 921 |
\ior_open:Nn \g_@@_data_ior { UnicodeData.txt } |
| 922 |
\char_set_catcode_space:n { `\ }% |
| 923 |
\ior_map_variable:NNn \g_@@_data_ior \l_@@_tmpa_tl |
| 924 |
{% |
| 925 |
\if_meaning:w \l_@@_tmpa_tl \c_space_tl |
| 926 |
\exp_after:wN \ior_map_break: |
| 927 |
\fi: |
| 928 |
\exp_after:wN \@@_data_auxi:w \l_@@_tmpa_tl \q_stop |
| 929 |
}% |
| 930 |
\char_set_catcode_ignore:n { `\ }% |
| 931 |
% \end{macrocode} |
| 932 |
% Blocks need to be finalized once they have been read as this relies on |
| 933 |
% knowing the last real codepoint. So the tables for |UnicodeData.txt| |
| 934 |
% are closed out before reading additional files. |
| 935 |
% \begin{macrocode} |
| 936 |
\@@_finalize_blocks:n |
| 937 |
{ category , grapheme , lowercase , uppercase , wordbreak } |
| 938 |
\group_end: |
| 939 |
% \end{macrocode} |
| 940 |
% |
| 941 |
% \begin{macro}[EXP]{\__kernel_codepoint_data:nn} |
| 942 |
% \begin{macro}[EXP]{\@@_data:nnn} |
| 943 |
% Recover data from a two-stage table: entirely generic as this applies to |
| 944 |
% all tables (as we use the same block size for all of them). Notice that |
| 945 |
% as we use indices not offsets we have to shuffle out-by-one issues. This |
| 946 |
% function is needed \emph{before} loading the special casing data, as there |
| 947 |
% we need to be able to check the standard case mappings. |
| 948 |
% |
| 949 |
% This macro is not defined in Lua\TeX{} where the data is provided in a more |
| 950 |
% preprocessed form in \cs\__kernel_codepoint_data:wn. |
| 951 |
% \begin{macrocode} |
| 952 |
\cs_new:Npn \__kernel_codepoint_data:nn #1#2 |
| 953 |
{ |
| 954 |
\exp_args:Nf \@@_data:nnn |
| 955 |
{ |
| 956 |
\int_eval:n |
| 957 |
{ |
| 958 |
\c_@@_block_size_int * |
| 959 |
( |
| 960 |
\intarray_item:cn { c_@@_ #1 _index_intarray } |
| 961 |
{ |
| 962 |
\int_div_truncate:nn {#2} |
| 963 |
\c_@@_block_size_int |
| 964 |
+ 1 |
| 965 |
} |
| 966 |
- 1 |
| 967 |
) |
| 968 |
} |
| 969 |
} |
| 970 |
{#2} {#1} |
| 971 |
} |
| 972 |
\cs_new:Npn \@@_data:nnn #1#2#3 |
| 973 |
{ |
| 974 |
\intarray_item:cn { c_@@_ #3 _blocks_intarray } |
| 975 |
{ #1 + \int_mod:nn {#2} \c_@@_block_size_int + 1 } |
| 976 |
} |
| 977 |
} |
| 978 |
% \end{macrocode} |
| 979 |
% \end{macro} |
| 980 |
% \end{macro} |
| 981 |
% |
| 982 |
% The remaining files use the \TeX{} based parser also in Lua\TeX{}. |
| 983 |
% |
| 984 |
% The other data files all use C-style comments so we have to worry about |
| 985 |
% |#| tokens (and reading as strings). The set up for case folding is in two |
| 986 |
% parts. For the basic (core) mappings, folding is the same as lower casing in |
| 987 |
% most positions so only store the differences. For the more complex foldings, |
| 988 |
% always store the result, splitting up the two or three code points in the input |
| 989 |
% as required. |
| 990 |
% \begin{macrocode} |
| 991 |
\group_begin: |
| 992 |
\ior_open:Nn \g_@@_data_ior { CaseFolding.txt } |
| 993 |
\cs_set_protected:Npn \@@_data_auxi:w #1 ;~ #2 ;~ #3 ; #4 \q_stop |
| 994 |
{ |
| 995 |
\if:w \tl_head:n { #2 ? } C |
| 996 |
\reverse_if:N \if_int_compare:w |
| 997 |
\@@_lowercase_base_mapping:n { "#1 } |
| 998 |
= "#3 ~ |
| 999 |
\tl_const:ce |
| 1000 |
{ c_@@_casefold_ \codepoint_str_generate:n {"#1} _tl } |
| 1001 |
{ {"#3} { } { } } |
| 1002 |
\fi: |
| 1003 |
\else: |
| 1004 |
\if:w \tl_head:n { #2 ? } F |
| 1005 |
\@@_data_auxii:w #1 ~ #3 ~ \q_stop |
| 1006 |
\fi: |
| 1007 |
\fi: |
| 1008 |
} |
| 1009 |
\sys_if_engine_luatex:TF |
| 1010 |
{ |
| 1011 |
\cs_set:Npn \@@_lowercase_base_mapping:n #1 |
| 1012 |
{ |
| 1013 |
\__kernel_codepoint_data:wn #1 { lowercase } |
| 1014 |
} |
| 1015 |
} |
| 1016 |
{ |
| 1017 |
\cs_set:Npn \@@_lowercase_base_mapping:n #1 |
| 1018 |
{ |
| 1019 |
\int_eval:n { \__kernel_codepoint_data:nn { lowercase } {#1} + #1 } |
| 1020 |
} |
| 1021 |
} |
| 1022 |
% \end{macrocode} |
| 1023 |
% Here, |#4| can have a trailing space, so we tidy up a bit at the cost of |
| 1024 |
% speed for these small number of cases it applies to. |
| 1025 |
% \begin{macrocode} |
| 1026 |
\cs_set_protected:Npn \@@_data_auxii:w #1 ~ #2 ~ #3 ~ #4 \q_stop |
| 1027 |
{ |
| 1028 |
\tl_const:ce { c_@@_casefold_ \codepoint_str_generate:n {"#1} _tl } |
| 1029 |
{ |
| 1030 |
{"#2} |
| 1031 |
{"#3} |
| 1032 |
{ \tl_if_blank:nF {#4} { " \int_to_Hex:n {"#4} } } |
| 1033 |
} |
| 1034 |
} |
| 1035 |
\ior_str_map_inline:Nn \g_@@_data_ior |
| 1036 |
{ |
| 1037 |
\reverse_if:N \if:w \c_hash_str \tl_head:w #1 \c_hash_str \q_stop |
| 1038 |
\@@_data_auxi:w #1 \q_stop |
| 1039 |
\fi: |
| 1040 |
} |
| 1041 |
\ior_close:N \g_@@_data_ior |
| 1042 |
% \end{macrocode} |
| 1043 |
% For upper- and lowercasing special situations, there is a bit more to |
| 1044 |
% do as we also have titlecasing to consider, plus we need to stop part-way |
| 1045 |
% through the file. |
| 1046 |
% \begin{macrocode} |
| 1047 |
\ior_open:Nn \g_@@_data_ior { SpecialCasing.txt } |
| 1048 |
\cs_set_protected:Npn \@@_data_auxi:w |
| 1049 |
#1 ;~ #2 ;~ #3 ;~ #4 ; #5 \q_stop |
| 1050 |
{ |
| 1051 |
\use:n { \@@_data_auxii:w #1 ~ lower ~ #2 ~ } ~ \q_stop |
| 1052 |
\use:n { \@@_data_auxii:w #1 ~ upper ~ #4 ~ } ~ \q_stop |
| 1053 |
\str_if_eq:nnF {#3} {#4} |
| 1054 |
{ \use:n { \@@_data_auxii:w #1 ~ title ~ #3 ~ } ~ \q_stop } |
| 1055 |
} |
| 1056 |
\cs_set_protected:Npn \@@_data_auxii:w |
| 1057 |
#1 ~ #2 ~ #3 ~ #4 ~ #5 \q_stop |
| 1058 |
{ |
| 1059 |
\tl_if_empty:nF {#4} |
| 1060 |
{ |
| 1061 |
\tl_const:ce { c_@@_ #2 case_ \codepoint_str_generate:n {"#1} _tl } |
| 1062 |
{ |
| 1063 |
{"#3} |
| 1064 |
{"#4} |
| 1065 |
{ \tl_if_blank:nF {#5} {"#5} } |
| 1066 |
} |
| 1067 |
} |
| 1068 |
} |
| 1069 |
\ior_str_map_inline:Nn \g_@@_data_ior |
| 1070 |
{ |
| 1071 |
\str_if_eq:eeTF { \tl_head:w #1 \c_hash_str \q_stop } { \c_hash_str } |
| 1072 |
{ |
| 1073 |
\str_if_eq:eeT |
| 1074 |
{#1} |
| 1075 |
{ \c_hash_str \c_space_tl Conditional~Mappings } |
| 1076 |
{ \ior_map_break: } |
| 1077 |
} |
| 1078 |
{ \@@_data_auxi:w #1 \q_stop } |
| 1079 |
} |
| 1080 |
\ior_close:N \g_@@_data_ior |
| 1081 |
\group_end: |
| 1082 |
% \end{macrocode} |
| 1083 |
% |
| 1084 |
% \begin{macro}[EXP]{\__kernel_codepoint_case:nn} |
| 1085 |
% \begin{macro}[EXP]{\@@_case:nnn} |
| 1086 |
% \begin{macro}[EXP] |
| 1087 |
% {\@@_uppercase:n, \@@_lowercase:n, \@@_titlecase:n, \@@_casefold:n} |
| 1088 |
% \begin{macro}[EXP]{\@@_case:nn} |
| 1089 |
% With the core data files loaded, there is now a need to provide access to |
| 1090 |
% this information for other modules. That is done here such that case |
| 1091 |
% folding can also be covered. At this level, all that needs to be returned |
| 1092 |
% is the |
| 1093 |
% \begin{macrocode} |
| 1094 |
\cs_new:Npn \__kernel_codepoint_case:nn #1#2 |
| 1095 |
{ |
| 1096 |
\exp_args:Ne \@@_case:nnn |
| 1097 |
{ \codepoint_str_generate:n {#2} } {#1} {#2} |
| 1098 |
} |
| 1099 |
\cs_new:Npn \@@_case:nnn #1#2#3 |
| 1100 |
{ |
| 1101 |
\cs_if_exist:cTF { c_@@_ #2 _ #1 _tl } |
| 1102 |
{ |
| 1103 |
\tl_use:c |
| 1104 |
{ c_@@_ #2 _ #1 _tl } |
| 1105 |
} |
| 1106 |
{ \use:c { @@_ #2 :n } {#3} } |
| 1107 |
} |
| 1108 |
\cs_new:Npn \@@_uppercase:n { \@@_case:nn { uppercase } } |
| 1109 |
\cs_new:Npn \@@_lowercase:n { \@@_case:nn { lowercase } } |
| 1110 |
\sys_if_engine_luatex:TF |
| 1111 |
{ |
| 1112 |
\cs_new:Npn \@@_titlecase:n { \@@_case:nn { titlecase } } |
| 1113 |
} |
| 1114 |
{ |
| 1115 |
\cs_new:Npn \@@_titlecase:n { \@@_case:nn { uppercase } } |
| 1116 |
} |
| 1117 |
\cs_new:Npn \@@_casefold:n { \@@_case:nn { lowercase } } |
| 1118 |
\sys_if_engine_luatex:TF |
| 1119 |
{ |
| 1120 |
\cs_new:Npn \@@_case:nn #1#2 |
| 1121 |
{ |
| 1122 |
{ \__kernel_codepoint_data:wn \int_eval:n {#2} {#1} } |
| 1123 |
{ } |
| 1124 |
{ } |
| 1125 |
} |
| 1126 |
} |
| 1127 |
{ |
| 1128 |
\cs_new:Npn \@@_case:nn #1#2 |
| 1129 |
{ |
| 1130 |
{ \int_eval:n { \__kernel_codepoint_data:nn {#1} {#2} + #2 } } |
| 1131 |
{ } |
| 1132 |
{ } |
| 1133 |
} |
| 1134 |
} |
| 1135 |
% \end{macrocode} |
| 1136 |
% \end{macro} |
| 1137 |
% \end{macro} |
| 1138 |
% \end{macro} |
| 1139 |
% \end{macro} |
| 1140 |
% |
| 1141 |
% \begin{macro}[EXP] |
| 1142 |
% { |
| 1143 |
% \__kernel_codepoint_to_grapheme_class:n, |
| 1144 |
% \__kernel_codepoint_to_wordbreak_class:n |
| 1145 |
% } |
| 1146 |
% Get the value and convert back to the string: not currently |
| 1147 |
% public but otherwise very similar to \cs{codepoint_to_category:n}. |
| 1148 |
% \begin{macrocode} |
| 1149 |
\sys_if_engine_luatex:TF |
| 1150 |
{ |
| 1151 |
\cs_new:Npn \__kernel_codepoint_to_grapheme_class:n #1 |
| 1152 |
{ |
| 1153 |
\__kernel_codepoint_data:wn \int_eval:n {#1} { grapheme } |
| 1154 |
} |
| 1155 |
\cs_new:Npn \__kernel_codepoint_to_wordbreak_class:n #1 |
| 1156 |
{ |
| 1157 |
\__kernel_codepoint_data:wn \int_eval:n {#1} { wordbreak } |
| 1158 |
} |
| 1159 |
} |
| 1160 |
{ |
| 1161 |
\cs_new:Npn \__kernel_codepoint_to_grapheme_class:n #1 |
| 1162 |
{ |
| 1163 |
\cs:w |
| 1164 |
c_@@_grapheme_ |
| 1165 |
\tex_romannumeral:D |
| 1166 |
\__kernel_codepoint_data:nn { grapheme } {#1} |
| 1167 |
_str |
| 1168 |
\cs_end: |
| 1169 |
} |
| 1170 |
\cs_new:Npn \__kernel_codepoint_to_wordbreak_class:n #1 |
| 1171 |
{ |
| 1172 |
\cs:w |
| 1173 |
c_@@_wordbreak_ |
| 1174 |
\tex_romannumeral:D |
| 1175 |
\__kernel_codepoint_data:nn { wordbreak } {#1} |
| 1176 |
_str |
| 1177 |
\cs_end: |
| 1178 |
} |
| 1179 |
} |
| 1180 |
% \end{macrocode} |
| 1181 |
% \end{macro} |
| 1182 |
% |
| 1183 |
% \begin{macro}[EXP]{\@@_nfd:n} |
| 1184 |
% \begin{macro}[EXP]{\@@_nfd:nn} |
| 1185 |
% A simple interface. |
| 1186 |
% \begin{macrocode} |
| 1187 |
\sys_if_engine_luatex:TF |
| 1188 |
{ |
| 1189 |
\cs_new:Npn \@@_nfd:n #1 |
| 1190 |
{ \@@_nfd:w \__int_eval:w #1 \__int_eval_end: } |
| 1191 |
} |
| 1192 |
{ |
| 1193 |
\cs_new:Npn \@@_nfd:n #1 |
| 1194 |
{ \exp_args:Ne \@@_nfd:nn { \codepoint_str_generate:n {#1} } {#1} } |
| 1195 |
\cs_new:Npn \@@_nfd:nn #1#2 |
| 1196 |
{ |
| 1197 |
\tl_if_exist:cTF { c_@@_nfd_ #1 _tl } |
| 1198 |
{ \tl_use:c { c_@@_nfd_ #1 _tl } } |
| 1199 |
{ {#2} { } } |
| 1200 |
} |
| 1201 |
} |
| 1202 |
% \end{macrocode} |
| 1203 |
% \end{macro} |
| 1204 |
% \end{macro} |
| 1205 |
% |
| 1206 |
% \begin{macrocode} |
| 1207 |
%</code> |
| 1208 |
% \end{macrocode} |
| 1209 |
% |
| 1210 |
% \end{implementation} |
| 1211 |
% |
| 1212 |
% \PrintIndex |