| 1 |
% \iffalse meta-comment |
| 2 |
% |
| 3 |
%% Copyright (C) 2020-2024 by Marcel Krueger |
| 4 |
%% |
| 5 |
%% This file may be distributed and/or modified under the |
| 6 |
%% conditions of the LaTeX Project Public License, either |
| 7 |
%% version 1.3c of this license or (at your option) any later |
| 8 |
%% version. The latest version of this license is in: |
| 9 |
%% |
| 10 |
%% http://www.latex-project.org/lppl.txt |
| 11 |
%% |
| 12 |
%% and version 1.3 or later is part of all distributions of |
| 13 |
%% LaTeX version 2005/12/01 or later. |
| 14 |
% |
| 15 |
%<*batch> |
| 16 |
%<*gobble> |
| 17 |
\ifx\jobname\relax\let\documentclass\undefined\fi |
| 18 |
\ifx\documentclass\undefined |
| 19 |
\csname fi\endcsname |
| 20 |
%</gobble> |
| 21 |
\input docstrip.tex |
| 22 |
\keepsilent |
| 23 |
\let\MetaPrefix\relax |
| 24 |
\preamble |
| 25 |
\endpreamble |
| 26 |
\postamble |
| 27 |
\endpostamble |
| 28 |
\def\MetaPrefix{--} |
| 29 |
\generate{% |
| 30 |
\file{lua-ul.lua}{\from{lua-ul.dtx}{luacode}}% |
| 31 |
\file{pre_append_to_vlist_filter.lua}{\from{lua-ul.dtx}{callback}}% |
| 32 |
\file{lua-ul-patches-preserve-attr.lua}{\from{lua-ul.dtx}{preserve-attr}}% |
| 33 |
} |
| 34 |
\let\MetaPrefix\DoubleperCent |
| 35 |
\generate{\file{lua-ul.sty}{\from{lua-ul.dtx}{package}}} |
| 36 |
\endbatchfile |
| 37 |
%</batch> |
| 38 |
%<*gobble> |
| 39 |
\fi |
| 40 |
\expandafter\ifx\csname @currname\endcsname\empty |
| 41 |
\csname fi\endcsname |
| 42 |
%</gobble> |
| 43 |
%<*driver> |
| 44 |
\documentclass{article} |
| 45 |
\usepackage{luacolor,lua-ul} |
| 46 |
\usepackage{csquotes,doc,framed,metalogo,tikzducks,pict2e,hyperref} |
| 47 |
\RecordChanges |
| 48 |
\begin{document} |
| 49 |
\DocInput{lua-ul.dtx} |
| 50 |
\PrintIndex |
| 51 |
\PrintChanges |
| 52 |
\end{document} |
| 53 |
%</driver> |
| 54 |
%<*gobble> |
| 55 |
\fi |
| 56 |
%</gobble> |
| 57 |
% \fi |
| 58 |
% |
| 59 |
% |
| 60 |
% \catcode`\§=14 |
| 61 |
% \newunderlinetype\beginUnderDuck{\cleaders\hbox{§ |
| 62 |
% \begin{tikzpicture}[x=.5ex,y=.5ex,baseline=.8ex]§ |
| 63 |
% \duck |
| 64 |
% \end{tikzpicture}§ |
| 65 |
% }} |
| 66 |
% \NewDocumentCommand\underDuck{+m}{{\beginUnderDuck#1}} |
| 67 |
% \newunderlinetype\beginUnderWavy[\number\dimexpr1ex]{\cleaders\hbox{§ |
| 68 |
% \setlength\unitlength{.3ex}§ |
| 69 |
% \begin{picture}(4,0)(0,1) |
| 70 |
% \thicklines |
| 71 |
% \color{red}§ |
| 72 |
% \qbezier(0,0)(1,1)(2,0) |
| 73 |
% \qbezier(2,0)(3,-1)(4,0) |
| 74 |
% \end{picture}§ |
| 75 |
% }} |
| 76 |
% \NewDocumentCommand\underWavy{+m}{{\beginUnderWavy#1}} |
| 77 |
% \newunderlinetype\beginStrikeThough{\leaders\hbox{§ |
| 78 |
% \normalfont\bfseries/§ |
| 79 |
% }} |
| 80 |
% \NewDocumentCommand\StrikeThough{+m}{{\beginStrikeThough#1}} |
| 81 |
% |
| 82 |
% \newcommand\Luaul{Lua-UL} |
| 83 |
% \newcommand\luaul{Lua-UL} |
| 84 |
% \GetFileInfo{lua-ul.sty} |
| 85 |
% \title{The \luaul\ package\thanks{This document |
| 86 |
% corresponds to \luaul~\fileversion, dated~\filedate.}} |
| 87 |
% \author{Marcel Kr\"uger \\ \href{mailto:[email protected]}{[email protected]}} |
| 88 |
% |
| 89 |
% \maketitle |
| 90 |
% \section{User-level interface} |
| 91 |
% \Luaul\ uses new capabilities of the LuaTeX engine to provide |
| 92 |
% underlining/\hspace{0pt}strikethrough/\hspace{0pt}highlighting |
| 93 |
% etc.\ support without breaking ligatures, kerning or restricting input. |
| 94 |
% The predefined user-level commands are \verb+\underLine+, \verb+\highLight+, and \verb+\strikeThrough+. |
| 95 |
% (\verb+\highLight+ will only work correctly if the \texttt{luacolor} package is loaded) |
| 96 |
% They are used as |
| 97 |
% |
| 98 |
% \begin{verbatim} |
| 99 |
% \documentclass{article} |
| 100 |
% \usepackage{lua-ul} |
| 101 |
% \begin{document} |
| 102 |
% This package is \strikeThrough{useless}\underLine{awesome}! |
| 103 |
% \end{document} |
| 104 |
% \end{verbatim} |
| 105 |
% |
| 106 |
% \begin{framed} |
| 107 |
% This package is \strikeThrough{useless}\underLine{awesome}! |
| 108 |
% \end{framed} |
| 109 |
% |
| 110 |
% For limited compatibility with \texttt{soul}, the \texttt{soul} package option allows you |
| 111 |
% to use the traditional macro names from \texttt{soul} instead: |
| 112 |
% |
| 113 |
% \begin{verbatim} |
| 114 |
% \documentclass{article} |
| 115 |
% \usepackage[soul]{lua-ul} |
| 116 |
% \begin{document} |
| 117 |
% This package is \st{useless}\ul{awesome}! |
| 118 |
% \end{document} |
| 119 |
% \end{verbatim} |
| 120 |
% |
| 121 |
% The \verb+\highLight+ command highlights the argument in yellow by default. This color can be changed |
| 122 |
% either by providing a color as optional argument or by changing the default through \verb+\LuaULSetHighLightColor+: |
| 123 |
% |
| 124 |
% \begin{verbatim} |
| 125 |
% \documentclass{article} |
| 126 |
% \usepackage{xcolor,luacolor,lua-ul} |
| 127 |
% \LuaULSetHighLightColor{green} |
| 128 |
% \begin{document} |
| 129 |
% Lots of stuff is \highLight{important enough to be highlighted}, |
| 130 |
% but only few things are dangerous enough to deserve |
| 131 |
% \highLight[red]{red highlighting.} |
| 132 |
% |
| 133 |
% \LuaULSetHighLightColor{yellow} |
| 134 |
% Let's go back to traditional \highLight{highlighting}. |
| 135 |
% \end{document} |
| 136 |
% \end{verbatim} |
| 137 |
% |
| 138 |
% \begin{framed} |
| 139 |
% \LuaULSetHighLightColor{green} |
| 140 |
% Lots of stuff is \highLight{important enough to be highlighted}, but only few things |
| 141 |
% are dangerous enough to deserve \highLight[red]{red highlighting.} |
| 142 |
% |
| 143 |
% \LuaULSetHighLightColor{yellow} |
| 144 |
% Let's go back to traditional \highLight{highlighting}. |
| 145 |
% \end{framed} |
| 146 |
% |
| 147 |
% The \verb+\underLine+ allows an optional argument accepting keyval options to customize the underline. |
| 148 |
% Currently four keys are implemented: |
| 149 |
% |
| 150 |
% \begin{description} |
| 151 |
% \item[textcolor] Active by default. The underline is colored in the current text color. |
| 152 |
% \item[color] Allows setting the color of the underline according to the syntax the xcolor manual |
| 153 |
% documents for PSTricks. |
| 154 |
% \item[top/bottom/height] At most two of these three options should be set. Specifies the height of the upper/lower |
| 155 |
% edge of the underline or the linewidth. |
| 156 |
% \end{description} |
| 157 |
% |
| 158 |
% \section{Expert interface} |
| 159 |
% \DescribeMacro{\newunderlinetype}§ |
| 160 |
% Sometimes, you might try to solve more interesting problems than boring underlining, |
| 161 |
% strikethrough or highlighting. Maybe you always wanted \underWavy{to be your own |
| 162 |
% spell checker}, want to demonstrate \underDuck{your love for ducks} or you think |
| 163 |
% that the traditional \verb+\strikeThrough+ \StrikeThough{is not visible enough}. |
| 164 |
% For all these cases, you can define your own \enquote{underlining} command based on a \TeX\ |
| 165 |
% \verb+\[cxg]leaders+ command. |
| 166 |
% |
| 167 |
% For this, use\\ |
| 168 |
% \verb+\newunderlinetype+\meta{macro name}\verb+[+\meta{context specifier}\verb+]{+\meta{\texttt{\string\leaders} command}\verb+}+ |
| 169 |
% |
| 170 |
% First, you have to pass the name of the command which should enable your new kind of underlining. |
| 171 |
% (If you want to have an additional command which takes an argument and underlines this argument, |
| 172 |
% you will have to define this manually.) |
| 173 |
% The optional argument provides a \enquote{context}: This \enquote{context} has to expand to a string |
| 174 |
% (something that can appear in a csname) which changes if the leaders box should be recalculated. |
| 175 |
% The leader will be cached and reused whenever the context evaluates to the same string. |
| 176 |
% So if your leaders should depend on the fontsize, |
| 177 |
% the expansion of the context should contain the font size. If you leaders contain text in the current font, |
| 178 |
% your context should include \verb+\fontname+. The default context includes the current size of \texttt{1ex} |
| 179 |
% and the current color if luacolor is loaded. |
| 180 |
% |
| 181 |
% The final argument contains the actual leader command. You should omit the final glue normally passed to \verb+\leaders+, |
| 182 |
% so e.g.\ write \verb+\leaders\hbox{ . }+ without appending \verb+\hfill+ or \verb+\hskip1pt+ etc. |
| 183 |
% In most cases, the height and depth of your underlines is passed on to TeX to ensure that a deep underline does not intercept other lines. |
| 184 |
% On the other hand, running dimensions work fine if you use a rule. |
| 185 |
% |
| 186 |
% For example, the special underline commands demonstrated above are implemented as |
| 187 |
% |
| 188 |
% \begin{verbatim} |
| 189 |
% \usepackage{luacolor,tikzducks,pict2e} |
| 190 |
% \newunderlinetype\beginUnderDuck{\cleaders\hbox{% |
| 191 |
% \begin{tikzpicture}[x=.5ex,y=.5ex,baseline=.8ex]% |
| 192 |
% \duck |
| 193 |
% \end{tikzpicture}% |
| 194 |
% }} |
| 195 |
% \NewDocumentCommand\underDuck{+m}{{\beginUnderDuck#1}} |
| 196 |
% \newunderlinetype\beginUnderWavy[\number\dimexpr1ex]{\cleaders\hbox{% |
| 197 |
% \setlength\unitlength{.3ex}% |
| 198 |
% \begin{picture}(4,0)(0,1) |
| 199 |
% \thicklines |
| 200 |
% \color{red}% |
| 201 |
% \qbezier(0,0)(1,1)(2,0) |
| 202 |
% \qbezier(2,0)(3,-1)(4,0) |
| 203 |
% \end{picture}% |
| 204 |
% }} |
| 205 |
% \NewDocumentCommand\underWavy{+m}{{\beginUnderWavy#1}} |
| 206 |
% \newunderlinetype\beginStrikeThough{\leaders\hbox{% |
| 207 |
% \normalfont\bfseries/% |
| 208 |
% }} |
| 209 |
% \NewDocumentCommand\StrikeThough{+m}{{\beginStrikeThough#1}} |
| 210 |
% \end{verbatim} |
| 211 |
% |
| 212 |
% Here \verb+\underWavy+ uses a custom context because it doesn't change depending on the current font color. |
| 213 |
% |
| 214 |
% If you only want to use \verb+\newunderlinetype+ and do not want to use the predefined underline types, you |
| 215 |
% can use the \texttt{minimal} package option to disable them. |
| 216 |
% |
| 217 |
% \StopEventually{} |
| 218 |
% \section{The implementation} |
| 219 |
% \changes{0.0.1}{2020-03-12}{Initial release} |
| 220 |
% \subsection{Helper modules} |
| 221 |
% First we need a separate Lua module \verb+pre_append_to_vlist_filter+ which |
| 222 |
% provides a variant of the \verb+append_to_vlist_filter+ callback which can be |
| 223 |
% used by multiple packages. This ensures that we are compatible with other packages |
| 224 |
% implementing \verb+append_to_vlist_filter+. |
| 225 |
% \iffalse |
| 226 |
%<*gobble> |
| 227 |
\RequirePackage{docstrip-luacode} |
| 228 |
\begin{docstrip-luacode}{pre_append_to_vlist_filter} |
| 229 |
%</gobble> |
| 230 |
%<*callback> |
| 231 |
% \fi |
| 232 |
% First check if an equivalent to \verb+pre_append_to_vlist_filter+ already exists. |
| 233 |
% The idea is that this might eventually get added to the kernel directly. |
| 234 |
% \begin{macrocode} |
| 235 |
if luatexbase.callbacktypes.pre_append_to_vlist_filter then |
| 236 |
return |
| 237 |
end |
| 238 |
|
| 239 |
local call_callback = luatexbase.call_callback |
| 240 |
local flush_node = node.flush_node |
| 241 |
local prepend_prevdepth = node.prepend_prevdepth |
| 242 |
local callback_define |
| 243 |
|
| 244 |
% \end{macrocode} |
| 245 |
% HACK: Do not do this at home! We need to define the engine callback directly, |
| 246 |
% so we use the debug library to get the ``real'' \verb+callback.define+: |
| 247 |
% \changes{0.2.1}{2024-02-26}{Reimplement callback patching to be TL 2024 compatible} |
| 248 |
% \begin{macrocode} |
| 249 |
do |
| 250 |
local saved_luatexbase, saved_callback_register = luatexbase, callback.register |
| 251 |
luatexbase.uninstall() |
| 252 |
callback_define = callback.register |
| 253 |
luatexbase, callback.register = saved_luatexbase, saved_callback_register |
| 254 |
end |
| 255 |
|
| 256 |
local function filtered_append_to_vlist_filter(box, |
| 257 |
locationcode, |
| 258 |
prevdepth, |
| 259 |
mirrored) |
| 260 |
local current = call_callback("pre_append_to_vlist_filter", |
| 261 |
box, locationcode, prevdepth, |
| 262 |
mirrored) |
| 263 |
if not current then |
| 264 |
flush_node(box) |
| 265 |
return |
| 266 |
elseif current == true then |
| 267 |
current = box |
| 268 |
end |
| 269 |
return call_callback("append_to_vlist_filter", |
| 270 |
current, locationcode, prevdepth, mirrored) |
| 271 |
end |
| 272 |
|
| 273 |
callback_define('append_to_vlist_filter', |
| 274 |
filtered_append_to_vlist_filter) |
| 275 |
luatexbase.callbacktypes.append_to_vlist_filter = nil |
| 276 |
luatexbase.create_callback('append_to_vlist_filter', 'exclusive', |
| 277 |
function(n, _, prevdepth) |
| 278 |
return prepend_prevdepth(n, prevdepth) |
| 279 |
end) |
| 280 |
luatexbase.create_callback('pre_append_to_vlist_filter', |
| 281 |
'list', false) |
| 282 |
% \end{macrocode} |
| 283 |
% \iffalse |
| 284 |
%</callback> |
| 285 |
%<*gobble> |
| 286 |
\end{docstrip-luacode} |
| 287 |
%</gobble> |
| 288 |
% \fi |
| 289 |
% |
| 290 |
% \changes{0.1.4}{2021-05-12}{Add \texttt{lua-ul-patches-preserve-attr}} |
| 291 |
% \changes{0.1.4}{2021-05-12}{Patch \texttt{\protect\string\protect\sw@slant}} |
| 292 |
% Additionally we have a module \verb+lua-ul-patches-preserve-attr+ for patches of |
| 293 |
% external code to make it more compatible with attribute usage. |
| 294 |
% |
| 295 |
% Currently this only caontains redefinitions of kernel commands. |
| 296 |
% \iffalse |
| 297 |
%<*gobble> |
| 298 |
\begin{docstrip-luacode}{lua-ul-patches-preserve-attr} |
| 299 |
%</gobble> |
| 300 |
%<*preserve-attr> |
| 301 |
% \fi |
| 302 |
% First some local definitions |
| 303 |
% \begin{macrocode} |
| 304 |
local getfont = font.getfont |
| 305 |
|
| 306 |
local direct = node.direct |
| 307 |
|
| 308 |
local getattr = direct.getattributelist |
| 309 |
local getid = direct.getid |
| 310 |
local getpenalty = direct.getpenalty |
| 311 |
local getprev = direct.getprev |
| 312 |
local getwidth = direct.getwidth |
| 313 |
|
| 314 |
local setattr = direct.setattributelist |
| 315 |
local setkern = direct.setkern |
| 316 |
|
| 317 |
local insert_after = direct.insert_after |
| 318 |
local is_glyph = direct.is_glyph |
| 319 |
local newnode = direct.new |
| 320 |
local todirect = direct.todirect |
| 321 |
local tonode = direct.tonode |
| 322 |
|
| 323 |
local glue_id = node.id'glue' |
| 324 |
local kern_t = node.id'kern' |
| 325 |
local penalty_id = node.id'penalty' |
| 326 |
|
| 327 |
local italcorr_sub |
| 328 |
for i, n in next, node.subtypes'kern' do |
| 329 |
if n == 'italiccorrection' then italcorr_sub = i break end |
| 330 |
end |
| 331 |
assert(italcorr_sub) |
| 332 |
|
| 333 |
local nests = tex.nest |
| 334 |
|
| 335 |
% \end{macrocode} |
| 336 |
% |
| 337 |
% Now we come to the interesting part: We redefine \verb+\sw@slant+ from the |
| 338 |
% \LaTeX\ kernel. The original definition uses \verb+\unskip+ and |
| 339 |
% \verb+\unpenalty+ to remove glue and penalties and then inserts italic |
| 340 |
% correction before them. Then it inserts the removed penalty and skip again. |
| 341 |
% This looses the right subtype, attributes and properties of the removed |
| 342 |
% nodes, so we insert the italic correction kern directly at the right position |
| 343 |
% instead. When the character does not exists we still add a 0pt italic |
| 344 |
% correction to stay as compatible as possible with the \verb+\/+ primitive |
| 345 |
% used in the original implementation. |
| 346 |
% \begin{macrocode} |
| 347 |
local funcid = luatexbase.new_luafunction'sw@slant' |
| 348 |
token.set_lua('sw@slant', funcid, 'protected') |
| 349 |
lua.get_functions_table()[funcid] = function() |
| 350 |
local nest = nests.top |
| 351 |
local tail, after = todirect(nest.tail), nil |
| 352 |
local id = getid(tail) |
| 353 |
if id == glue_id then |
| 354 |
if getwidth(tail) == 0 then return end |
| 355 |
tail, after = getprev(tail), tail |
| 356 |
id = getid(tail) |
| 357 |
end |
| 358 |
if id == penalty_id then |
| 359 |
if getpenalty(tail) == 0 then return end |
| 360 |
tail, after = getprev(tail), tail |
| 361 |
end |
| 362 |
local cid, fontid = is_glyph(tail) |
| 363 |
if not cid then return end |
| 364 |
local fontdir = getfont(fontid) |
| 365 |
local characters = fontdir and fontdir.characters |
| 366 |
local char = characters and characters[cid] |
| 367 |
local kern = newnode(kern_t, italcorr_sub) |
| 368 |
setkern(kern, char and char.italic or 0) |
| 369 |
setattr(kern, getattr(tail)) |
| 370 |
% We lie about the head and ignore the return value since tail is never nil |
| 371 |
insert_after(tail, tail, kern) |
| 372 |
if not after then nest.tail = tonode(kern) end |
| 373 |
end |
| 374 |
% \end{macrocode} |
| 375 |
% \iffalse |
| 376 |
%</preserve-attr> |
| 377 |
%<*gobble> |
| 378 |
\end{docstrip-luacode} |
| 379 |
%</gobble> |
| 380 |
% \fi |
| 381 |
% \subsection{Lua module} |
| 382 |
% Now we can define our main Lua module: |
| 383 |
% \iffalse |
| 384 |
%<*gobble> |
| 385 |
\begin{docstrip-luacode}{lua-ul} |
| 386 |
%</gobble> |
| 387 |
%<*luacode> |
| 388 |
% \fi |
| 389 |
% \begin{macrocode} |
| 390 |
local unset_t = node.id'unset' |
| 391 |
local hlist_t = node.id'hlist' |
| 392 |
local vlist_t = node.id'vlist' |
| 393 |
local kern_t = node.id'kern' |
| 394 |
local glue_t = node.id'glue' |
| 395 |
|
| 396 |
local properties = node.direct.get_properties_table() |
| 397 |
|
| 398 |
% \end{macrocode} |
| 399 |
% \verb+current_attr+ is not \texttt{.direct} since it's used in |
| 400 |
% place of a node callback argument. |
| 401 |
% \begin{macrocode} |
| 402 |
local current_attr = node.current_attr |
| 403 |
local has_attribute = node.direct.has_attribute |
| 404 |
local set_attribute = node.direct.set_attribute |
| 405 |
local dimensions = node.direct.dimensions |
| 406 |
local flush_node = node.direct.flush_node |
| 407 |
local getattr = node.direct.getattributelist |
| 408 |
local getboth = node.direct.getboth |
| 409 |
local getfield = node.direct.getfield |
| 410 |
local getglue = node.direct.getglue |
| 411 |
local getleader = node.direct.getleader |
| 412 |
local getlist = node.direct.getlist |
| 413 |
local setheight = node.direct.setheight |
| 414 |
local setdepth = node.direct.setdepth |
| 415 |
local getheight = node.direct.getheight |
| 416 |
local getdepth = node.direct.getdepth |
| 417 |
local getnext = node.direct.getnext |
| 418 |
local getshift = node.direct.getshift |
| 419 |
local insert_after = node.direct.insert_after |
| 420 |
local insert_before = node.direct.insert_before |
| 421 |
local nodecopy = node.direct.copy |
| 422 |
local nodenew = node.direct.new |
| 423 |
local setboth = node.direct.setboth |
| 424 |
local setlink = node.direct.setlink |
| 425 |
local hpack = node.direct.hpack |
| 426 |
local setfield = node.direct.setfield |
| 427 |
local slide = node.direct.slide |
| 428 |
local setattr = node.direct.setattributelist |
| 429 |
local setglue = node.direct.setglue |
| 430 |
local setnext = node.direct.setnext |
| 431 |
local setshift = node.direct.setshift |
| 432 |
local todirect = node.direct.todirect |
| 433 |
local tonode = node.direct.tonode |
| 434 |
local traverse = node.direct.traverse |
| 435 |
local traverse_id = node.direct.traverse_id |
| 436 |
local traverse_list = node.direct.traverse_list |
| 437 |
% The following two are needed to deal with unset nodes |
| 438 |
local getList = function(n) return getfield(n, 'list') end |
| 439 |
local setList = function(n, h) return setfield(n, 'list', h) end |
| 440 |
|
| 441 |
local tokennew = token.new |
| 442 |
local set_lua = token.set_lua |
| 443 |
local scan_keyword = token.scan_keyword |
| 444 |
local scan_list = token.scan_list |
| 445 |
local scan_int = token.scan_int |
| 446 |
local scan_toks = token.scan_toks |
| 447 |
local put_next = token.put_next |
| 448 |
local texerror = tex.error |
| 449 |
|
| 450 |
local functions = lua.get_functions_table() |
| 451 |
local char_given = token.command_id'char_given' |
| 452 |
|
| 453 |
local underlineattrs = {} |
| 454 |
local underline_types = {} |
| 455 |
local underline_strict_flag = {} |
| 456 |
local underline_over_flag = {} |
| 457 |
|
| 458 |
local vmode do |
| 459 |
for k, v in pairs(tex.getmodevalues()) do |
| 460 |
if v == "vertical" then |
| 461 |
vmode = k |
| 462 |
break |
| 463 |
end |
| 464 |
end |
| 465 |
end |
| 466 |
local texnest = tex.nest |
| 467 |
|
| 468 |
% \end{macrocode} |
| 469 |
% \changes{0.1.0}{2020-07-13}{Ensure that \texttt{\protect\string\protect\everyhbox} doesn't interfere with hlist scanning} |
| 470 |
% To avoid influence from \texttt{\string\everyhbox} we reset \texttt{\string\everyhbox} to an empty token list directly before scanning. |
| 471 |
% As an added complication, we need to use a name which is guaranteed to be the primitive tokenlist and we |
| 472 |
% might have to restore it before reading the actual argument (There might be a reason why \texttt{\string\everyhbox} was sat after all. |
| 473 |
% Also we have to ensure that braces swallowed by LuaTeX are balanced, otherwise we get hard to trace errors in alignment contexts. |
| 474 |
% \begin{macrocode} |
| 475 |
local scan_raw_hlist do |
| 476 |
local create = token.create |
| 477 |
local lbrace, rbrace = token.new(0x7B, 1), token.new(0x7D, 2) |
| 478 |
tex.enableprimitives('luaul@', {'everyhbox'}) |
| 479 |
local set_everyhbox do |
| 480 |
local set_toks1, set_toks2 = {create'immediateassignment', |
| 481 |
create'luaul@everyhbox', lbrace}, |
| 482 |
{rbrace, create'relax'} |
| 483 |
function set_everyhbox(t) |
| 484 |
token.put_next(set_toks2) |
| 485 |
token.put_next(t) |
| 486 |
token.put_next(set_toks1) |
| 487 |
token.scan_token() |
| 488 |
end |
| 489 |
end |
| 490 |
local func = luatexbase.new_luafunction"luaul.restore_everyhbox" |
| 491 |
local everyhbox_saved |
| 492 |
functions[func] = function() set_everyhbox(everyhbox_saved) end |
| 493 |
local toks = {rbrace, -- Housekeeping, only for balance reasons |
| 494 |
lbrace, create'the', create'luaul@everyhbox', rbrace, |
| 495 |
create'hpack', lbrace, |
| 496 |
token.new(func, token.command_id'lua_call')} |
| 497 |
function scan_raw_hlist() |
| 498 |
assert(token.get_next().command == 1) |
| 499 |
put_next(toks) |
| 500 |
token.get_next() -- Scan a corresponding brace to keep TeX's brace tracking happy |
| 501 |
local saved_toks = scan_toks(false, true) |
| 502 |
everyhbox_saved = saved_toks |
| 503 |
set_everyhbox{} |
| 504 |
local list = scan_list() |
| 505 |
set_everyhbox(saved_toks) |
| 506 |
return list |
| 507 |
end |
| 508 |
end |
| 509 |
|
| 510 |
local saved_values = {} |
| 511 |
local function new_underline_type() |
| 512 |
for i=1,#underlineattrs do |
| 513 |
local attr = underlineattrs[i] |
| 514 |
saved_values[i] = tex.attribute[attr] |
| 515 |
tex.attribute[attr] = -0x7FFFFFFF |
| 516 |
end |
| 517 |
local strict_flag = scan_keyword'strict' |
| 518 |
local over_flag = scan_keyword'over' |
| 519 |
local b = todirect(scan_raw_hlist()) |
| 520 |
for i=1,#underlineattrs do |
| 521 |
tex.attribute[underlineattrs[i]] = saved_values[i] |
| 522 |
end |
| 523 |
local lead = getlist(b) |
| 524 |
if not getleader(lead) then |
| 525 |
texerror("Leader required", {"An underline type has to \z |
| 526 |
be defined by leader. You should use one of the", "commands \z |
| 527 |
\\leaders, \\cleaders, or \\xleader, or \\gleaders here."}) |
| 528 |
else |
| 529 |
local after = getnext(lead) |
| 530 |
if after then |
| 531 |
texerror("Too many nodes", {"An underline type can only be \z |
| 532 |
defined by a single leaders specification,", "not by \z |
| 533 |
multiple nodes. Maybe you supplied an additional glue?", |
| 534 |
"Anyway, the additional nodes will be ignored"}) |
| 535 |
setnext(lead, nil) |
| 536 |
end |
| 537 |
table.insert(underline_types, lead) |
| 538 |
setList(b, after) |
| 539 |
flush_node(b) |
| 540 |
end |
| 541 |
put_next(tokennew(#underline_types, char_given)) |
| 542 |
underline_strict_flag[#underline_types] = strict_flag |
| 543 |
underline_over_flag[#underline_types] = over_flag |
| 544 |
end |
| 545 |
% \end{macrocode} |
| 546 |
% In \verb+append_to_vlist_filter+ we can not access the list attributes, |
| 547 |
% so we just take the current ones. They might be incorrect if the attribute |
| 548 |
% changes in the vlist, so we record the original value in a property then. |
| 549 |
% \begin{macrocode} |
| 550 |
local function set_underline() |
| 551 |
local j, props |
| 552 |
for i=texnest.ptr,0,-1 do |
| 553 |
local mode = texnest[i].mode |
| 554 |
if mode == vmode or mode == -vmode then |
| 555 |
local head = todirect(texnest[i].head) |
| 556 |
local head_props = properties[head] |
| 557 |
if not head_props then |
| 558 |
head_props = {} |
| 559 |
properties[head] = head_props |
| 560 |
end |
| 561 |
props = head_props.luaul_attributes |
| 562 |
if not props then |
| 563 |
props = {} |
| 564 |
head_props.luaul_attributes = props |
| 565 |
break |
| 566 |
end |
| 567 |
end |
| 568 |
end |
| 569 |
for i=1,#underlineattrs do |
| 570 |
local attr = underlineattrs[i] |
| 571 |
if tex.attribute[attr] == -0x7FFFFFFF then |
| 572 |
j = attr |
| 573 |
break |
| 574 |
end |
| 575 |
end |
| 576 |
if not j then |
| 577 |
j = luatexbase.new_attribute( |
| 578 |
"luaul" .. tostring(#underlineattrs+1)) |
| 579 |
underlineattrs[#underlineattrs+1] = j |
| 580 |
end |
| 581 |
props[j] = props[j] or -0x7FFFFFFF |
| 582 |
tex.attribute[j] = scan_int() |
| 583 |
end |
| 584 |
% \end{macrocode} |
| 585 |
% \changes{0.0.2}{2020-03-15}{Add command to disable active underlining} |
| 586 |
% \begin{macrocode} |
| 587 |
local function reset_underline() |
| 588 |
local reset_all = scan_keyword'*' |
| 589 |
local j |
| 590 |
for i=1,#underlineattrs do |
| 591 |
local attr = underlineattrs[i] |
| 592 |
if tex.attribute[attr] ~= -0x7FFFFFFF then |
| 593 |
if reset_all then |
| 594 |
tex.attribute[attr] = -0x7FFFFFFF |
| 595 |
else |
| 596 |
j = attr |
| 597 |
end |
| 598 |
end |
| 599 |
end |
| 600 |
if not j then |
| 601 |
if not reset_all then |
| 602 |
texerror("No underline active", {"You tried to disable \z |
| 603 |
underlining but underlining was not active in the first", |
| 604 |
"place. Maybe you wanted to ensure that \z |
| 605 |
no underling can be active anymore?", "Then you should \z |
| 606 |
append a *."}) |
| 607 |
end |
| 608 |
return |
| 609 |
end |
| 610 |
tex.attribute[j] = -0x7FFFFFFF |
| 611 |
end |
| 612 |
local new_underline_type_func = |
| 613 |
luatexbase.new_luafunction"luaul.new_underline_type" |
| 614 |
local set_underline_func = |
| 615 |
luatexbase.new_luafunction"luaul.set_underline_func" |
| 616 |
local reset_underline_func = |
| 617 |
luatexbase.new_luafunction"luaul.reset_underline_func" |
| 618 |
set_lua("LuaULNewUnderlineType", new_underline_type_func) |
| 619 |
set_lua("LuaULSetUnderline", set_underline_func, "protected") |
| 620 |
set_lua("LuaULResetUnderline", reset_underline_func, "protected") |
| 621 |
functions[new_underline_type_func] = new_underline_type |
| 622 |
functions[set_underline_func] = set_underline |
| 623 |
functions[reset_underline_func] = reset_underline |
| 624 |
|
| 625 |
% \end{macrocode} |
| 626 |
% \changes{0.0.3}{2020-03-17}{Use glue instead of kern to allow handling |
| 627 |
% unset lists} |
| 628 |
% A little helper to measure box contents and creating a glue |
| 629 |
% node with inverted dimensions. |
| 630 |
% \begin{macrocode} |
| 631 |
local stretch_fi = {} |
| 632 |
local shrink_fi = {} |
| 633 |
local function fil_levels(n) |
| 634 |
for i=0,4 do |
| 635 |
stretch_fi[i], shrink_fi[i] = 0, 0 |
| 636 |
end |
| 637 |
for n in traverse_id(glue_t, n) do |
| 638 |
local w, st, sh, sto, sho = getglue(n) |
| 639 |
stretch_fi[sto] = stretch_fi[sto] + st |
| 640 |
shrink_fi[sho] = shrink_fi[sho] + sh |
| 641 |
end |
| 642 |
local stretch, shrink = 0, 0 |
| 643 |
for i=0,4 do |
| 644 |
if stretch_fi[i] ~= 0 then |
| 645 |
stretch = i |
| 646 |
end |
| 647 |
if shrink_fi[i] ~= 0 then |
| 648 |
shrink = i |
| 649 |
end |
| 650 |
end |
| 651 |
return stretch, shrink |
| 652 |
end |
| 653 |
local function new_glue_neg_dimensions(n, t, |
| 654 |
stretch_order, shrink_order) |
| 655 |
local g = nodenew(glue_t) |
| 656 |
local w = -dimensions(n, t) |
| 657 |
setglue(g, w) |
| 658 |
% setglue(g, -dimensions(n, t), 0, 0, stretch_order, shrink_order) |
| 659 |
setnext(g, n) |
| 660 |
setglue(g, w, -dimensions(1, 1, stretch_order, g, t), |
| 661 |
dimensions(1, 2, shrink_order, g, t), |
| 662 |
stretch_order, shrink_order) |
| 663 |
setnext(g, nil) |
| 664 |
return g |
| 665 |
end |
| 666 |
|
| 667 |
% \end{macrocode} |
| 668 |
% \changes{0.0.3}{2020-03-17}{Make streight lines over hboxes} |
| 669 |
% \changes{0.0.4}{2020-03-31}{Consistently respect height and depth} |
| 670 |
% Now the actual underlining |
| 671 |
% \begin{macrocode} |
| 672 |
local add_underline_hlist, add_underline_hbox, add_underline_vbox |
| 673 |
local function add_underline_vlist(head, attr, outervalue) |
| 674 |
local iter, state, n = traverse_list(head) -- FIXME: unset nodes |
| 675 |
local t |
| 676 |
n, t = iter(state, n) |
| 677 |
while n ~= nil do |
| 678 |
local real_new_value = has_attribute(n, attr) |
| 679 |
local new_value = real_new_value ~= outervalue |
| 680 |
and real_new_value or nil |
| 681 |
if underline_strict_flag[new_value] or not new_value then |
| 682 |
if t == hlist_t then |
| 683 |
add_underline_hbox(n, attr, real_new_value) |
| 684 |
elseif t == vlist_t then |
| 685 |
add_underline_vbox(n, attr, real_new_value) |
| 686 |
end |
| 687 |
n, t = iter(state, n) |
| 688 |
elseif real_new_value <= 0 then |
| 689 |
n, t = iter(state, n) |
| 690 |
else |
| 691 |
local nn |
| 692 |
nn, t = iter(state, n) |
| 693 |
local prev, next = getboth(n) |
| 694 |
setboth(n, nil, nil) |
| 695 |
local shift = getshift(n) |
| 696 |
setshift(n, 0) |
| 697 |
local new_list = hpack((add_underline_hlist(n, attr))) |
| 698 |
setheight(new_list, getheight(n)) |
| 699 |
setdepth(new_list, getdepth(n)) |
| 700 |
setshift(new_list, shift) |
| 701 |
setlink(prev, new_list, next) |
| 702 |
set_attribute(new_list, attr, 0) |
| 703 |
if n == head then |
| 704 |
head = new_list |
| 705 |
end |
| 706 |
n = nn |
| 707 |
end |
| 708 |
end |
| 709 |
return head |
| 710 |
end |
| 711 |
function add_underline_vbox(head, attr, outervalue) |
| 712 |
if outervalue and outervalue <= 0 then return end |
| 713 |
setList(head, add_underline_vlist(getList(head), attr, outervalue)) |
| 714 |
set_attribute(head, attr, outervalue and -outervalue or 0) |
| 715 |
end |
| 716 |
function add_underline_hlist(head, attr, outervalue) |
| 717 |
local max_height, max_depth |
| 718 |
slide(head) |
| 719 |
local last_value |
| 720 |
local first |
| 721 |
local shrink_order, stretch_order |
| 722 |
for n, id, subtype in traverse(head) do |
| 723 |
local real_new_value = has_attribute(n, attr) |
| 724 |
local new_value |
| 725 |
if real_new_value then |
| 726 |
if real_new_value > 0 then |
| 727 |
set_attribute(n, attr, -real_new_value) |
| 728 |
new_value = real_new_value ~= outervalue |
| 729 |
and real_new_value or nil |
| 730 |
end |
| 731 |
else |
| 732 |
set_attribute(n, attr, 0) |
| 733 |
end |
| 734 |
if id == hlist_t then |
| 735 |
if underline_strict_flag[new_value] |
| 736 |
or subtype == 3 or not new_value then |
| 737 |
add_underline_hbox(n, attr, real_new_value) |
| 738 |
new_value = nil |
| 739 |
end |
| 740 |
elseif id == vlist_t then |
| 741 |
if underline_strict_flag[new_value] or not new_value then |
| 742 |
add_underline_vbox(n, attr, real_new_value) |
| 743 |
new_value = nil |
| 744 |
end |
| 745 |
elseif id == kern_t and subtype == 0 then |
| 746 |
local after = getnext(n) |
| 747 |
if after then |
| 748 |
local next_value = has_attribute(after, attr) |
| 749 |
if next_value == outervalue or not next_value then |
| 750 |
new_value = nil |
| 751 |
else |
| 752 |
new_value = last_value |
| 753 |
end |
| 754 |
else |
| 755 |
new_value = last_value |
| 756 |
end |
| 757 |
elseif id == glue_t and ( |
| 758 |
subtype == 8 or |
| 759 |
subtype == 9 or |
| 760 |
subtype == 15 or |
| 761 |
false) then |
| 762 |
new_value = nil |
| 763 |
end |
| 764 |
if last_value ~= new_value then |
| 765 |
if not stretch_order then |
| 766 |
stretch_order, shrink_order = fil_levels(head) |
| 767 |
end |
| 768 |
if last_value then |
| 769 |
% \end{macrocode} |
| 770 |
% \changes{0.1.1}{2021-02-02}{Set attributes for negative glue node} |
| 771 |
% If the value changed and the old one wasn't \texttt{nil}, then we reached |
| 772 |
% the end of the previous underlined segment and therefore know it's length. |
| 773 |
% Therefore we can finally insert the underline. |
| 774 |
% |
| 775 |
% Currently both the underline and the corresponding negative glue inherit |
| 776 |
% the attributes from when the underline was defined. This makes sure that |
| 777 |
% these nodes get consistent attributes (avoiding e.g. that only one of the |
| 778 |
% nodes being picked up in a later pass and therefore interfering with the |
| 779 |
% underlining) and that these are as much as possible under use control. |
| 780 |
% |
| 781 |
% We can't really predict what the most sensible value for attributes we |
| 782 |
% don't control is, but by using this way any issues should be fixable with |
| 783 |
% by adjusting the context argument. |
| 784 |
% |
| 785 |
% Currently this block gets duplicated a few lines down for the end of the |
| 786 |
% list. This should get refactored into it's own function, but I have to be |
| 787 |
% careful to handle all the special cases there. |
| 788 |
% |
| 789 |
% \begin{macrocode} |
| 790 |
local glue = new_glue_neg_dimensions(first, n, |
| 791 |
stretch_order, shrink_order) |
| 792 |
local w, st, sh = getglue(glue) |
| 793 |
local lead = nodecopy(underline_types[last_value]) |
| 794 |
setglue(lead, -w, -st, -sh, stretch_order, shrink_order) |
| 795 |
setattr(glue, getattr(lead)) |
| 796 |
if underline_over_flag[last_value] then |
| 797 |
head = insert_before(head, n, glue) |
| 798 |
insert_after(head, glue, lead) |
| 799 |
else |
| 800 |
head = insert_before(head, first, lead) |
| 801 |
insert_after(head, lead, glue) |
| 802 |
end |
| 803 |
end |
| 804 |
if new_value then |
| 805 |
first = n |
| 806 |
local box = getleader(underline_types[new_value]) |
| 807 |
if not max_height or getheight(box) > max_height then |
| 808 |
max_height = getheight(box) |
| 809 |
end |
| 810 |
if not max_depth or getdepth(box) > max_depth then |
| 811 |
max_depth = getdepth(box) |
| 812 |
end |
| 813 |
end |
| 814 |
last_value = new_value |
| 815 |
end |
| 816 |
end |
| 817 |
if last_value then |
| 818 |
local glue = new_glue_neg_dimensions(first, nil, |
| 819 |
stretch_order, shrink_order) |
| 820 |
local w, st, sh = getglue(glue) |
| 821 |
local lead = nodecopy(underline_types[last_value]) |
| 822 |
setglue(lead, -w, -st, -sh, stretch_order, shrink_order) |
| 823 |
setattr(glue, getattr(lead)) |
| 824 |
if underline_over_flag[last_value] then |
| 825 |
insert_before(head, nil, glue) |
| 826 |
insert_after(head, glue, lead) |
| 827 |
else |
| 828 |
head = insert_before(head, first, lead) |
| 829 |
insert_after(head, lead, glue) |
| 830 |
end |
| 831 |
end |
| 832 |
return head, max_height, max_depth |
| 833 |
end |
| 834 |
function add_underline_hbox(head, attr, outervalue, set_height_depth) |
| 835 |
if outervalue and outervalue <= 0 then return end |
| 836 |
local new_head, new_height, new_depth |
| 837 |
= add_underline_hlist(getList(head), attr, outervalue) |
| 838 |
setList(head, new_head) |
| 839 |
if set_height_depth then |
| 840 |
if new_height and getheight(head) < new_height then |
| 841 |
setheight(head, new_height) |
| 842 |
end |
| 843 |
if new_depth and getdepth(head) < new_depth then |
| 844 |
setdepth(head, new_depth) |
| 845 |
end |
| 846 |
end |
| 847 |
set_attribute(head, attr, outervalue and -outervalue or 0) |
| 848 |
end |
| 849 |
require'pre_append_to_vlist_filter' |
| 850 |
luatexbase.add_to_callback('pre_append_to_vlist_filter', |
| 851 |
function(b, loc, prev, mirror) |
| 852 |
local props = properties[todirect(texnest.top.head)] |
| 853 |
props = props and props.luaul_attributes |
| 854 |
b = todirect(b) |
| 855 |
if loc == "post_linebreak" then |
| 856 |
for i = 1, #underlineattrs do |
| 857 |
local attr = underlineattrs[i] |
| 858 |
local current = props and props[attr] or tex.attribute[attr] |
| 859 |
if current == -0x7FFFFFFF then |
| 860 |
current = nil |
| 861 |
end |
| 862 |
add_underline_hbox(b, underlineattrs[i], current, true) |
| 863 |
end |
| 864 |
else |
| 865 |
for i = 1, #underlineattrs do |
| 866 |
local attr = underlineattrs[i] |
| 867 |
local current = props and props[attr] or tex.attribute[attr] |
| 868 |
local b_attr = has_attribute(b, attr) |
| 869 |
if b_attr and b_attr ~= current then |
| 870 |
local shift = getshift(b) |
| 871 |
setshift(b, 0) |
| 872 |
b = hpack((add_underline_hlist(b, attr))) |
| 873 |
setshift(b, shift) |
| 874 |
set_attribute(b, attr, 0) |
| 875 |
end |
| 876 |
end |
| 877 |
end |
| 878 |
return tonode(b) |
| 879 |
end, 'add underlines to list') |
| 880 |
luatexbase.add_to_callback('hpack_filter', |
| 881 |
function(head, group, size, pack, dir, attr) |
| 882 |
% \end{macrocode} |
| 883 |
% \changes{0.1.3}{2021-04-25}{Correctly detect attributes in alignments} |
| 884 |
% When \verb+hpack_filter+ is called as part of an alignment, no attributes |
| 885 |
% are passed. It seems like a bug, but we will just substitute with the |
| 886 |
% current attributes. Since the callbacks are called after the group for the |
| 887 |
% cell ended, these should always be right. |
| 888 |
% \begin{macrocode} |
| 889 |
if group == 'align_set' or group == 'fin_row' then |
| 890 |
attr = current_attr() |
| 891 |
end |
| 892 |
head = todirect(head) |
| 893 |
for i = 1, #underlineattrs do |
| 894 |
local ulattr = underlineattrs[i] |
| 895 |
local current |
| 896 |
for n in node.traverse(attr) do |
| 897 |
if n.number == ulattr then |
| 898 |
current = n.value |
| 899 |
end |
| 900 |
end |
| 901 |
head = add_underline_hlist(head, ulattr, current) |
| 902 |
end |
| 903 |
return tonode(head) |
| 904 |
end, 'add underlines to list') |
| 905 |
luatexbase.add_to_callback('vpack_filter', |
| 906 |
function(head, group, size, pack, maxdepth, dir, attr) |
| 907 |
% if true then return head end |
| 908 |
head = todirect(head) |
| 909 |
for i = 1, #underlineattrs do |
| 910 |
local ulattr = underlineattrs[i] |
| 911 |
local current |
| 912 |
for n in node.traverse(attr) do |
| 913 |
if n.number == ulattr then |
| 914 |
current = n.value |
| 915 |
end |
| 916 |
end |
| 917 |
head = add_underline_vlist(head, ulattr, current) |
| 918 |
end |
| 919 |
return tonode(head) |
| 920 |
end, 'add underlines to list') |
| 921 |
% \end{macrocode} |
| 922 |
% \changes{0.1.4}{2021-05-12}{Load \texttt{lua-ul-patches-preserve-attr}} |
| 923 |
% Finally load \texttt{lua-ul-patches-preserve-attr}. |
| 924 |
% \begin{macrocode} |
| 925 |
require'lua-ul-patches-preserve-attr' |
| 926 |
% \end{macrocode} |
| 927 |
% \iffalse |
| 928 |
%</luacode> |
| 929 |
%<*gobble> |
| 930 |
\end{docstrip-luacode} |
| 931 |
%</gobble> |
| 932 |
% \fi |
| 933 |
% \subsection{\TeX\ support package} |
| 934 |
% \changes{0.2.0}{2022-05-31}{Move to expl3} |
| 935 |
% Now only some \LaTeX\ glue code is still needed |
| 936 |
% \iffalse |
| 937 |
%<*package> |
| 938 |
\NeedsTeXFormat{LaTeX2e} |
| 939 |
\ProvidesExplPackage |
| 940 |
{lua-ul} |
| 941 |
{2024-02-26}{0.2.1}{Underlining and related functionality for LuaTeX} |
| 942 |
% \fi |
| 943 |
% First define some messages. |
| 944 |
% \begin{macrocode} |
| 945 |
\msg_new:nnnn {lua-ul} {luatex-required} {LuaLaTeX~required} { |
| 946 |
Lua-UL~requires~LuaLaTeX.~ |
| 947 |
Maybe~you~forgot~to~switch~the~engine~in~your~editor? |
| 948 |
} |
| 949 |
\msg_new:nnnn {lua-ul} {luacolor-required} { |
| 950 |
Color~support~in~lua-ul~requires~luacolor. |
| 951 |
}{ |
| 952 |
This~error~can~be~fixed~by~adding~\token_to_str:N \usepackage{luacolor}~in~your~preamble. |
| 953 |
} |
| 954 |
% \end{macrocode} |
| 955 |
% |
| 956 |
% Only \LuaLaTeX{} is supported. |
| 957 |
% For other engines we show an error. |
| 958 |
% \begin{macrocode} |
| 959 |
\sys_if_engine_luatex:F { |
| 960 |
\msg_error:nn {lua-ul} {luatex-required} |
| 961 |
} |
| 962 |
\lua_load_module:n {lua-ul} |
| 963 |
% |
| 964 |
% \end{macrocode} |
| 965 |
% We support some options. Especially \verb+minimal+ will disable the predefined commands \verb+\underLine+ and \verb+\strikeThrough+ and allow you to define similar commands with your custom settings instead, \verb+soul+ tries to replicate names of the \verb+soul+ package. |
| 966 |
% \begin{macrocode} |
| 967 |
\bool_new:N \l__luaul_predefined_bool |
| 968 |
\bool_new:N \l__luaul_soulnames_bool |
| 969 |
\bool_set_true:N \l__luaul_predefined_bool |
| 970 |
\DeclareOption {minimal} { \bool_set_false:N \l__luaul_predefined_bool } |
| 971 |
\DeclareOption {soul} { \bool_set_true:N \l__luaul_soulnames_bool } |
| 972 |
\ProcessOptions\relax |
| 973 |
% \end{macrocode} |
| 974 |
% |
| 975 |
% Just one more tiny helper. |
| 976 |
% \begin{macrocode} |
| 977 |
\cs_new_protected:Npn \__luaul_maybe_define_use:Nx #1#2 { |
| 978 |
\cs_if_free:NT #1 { |
| 979 |
\cs_gset:Npx #1 {#2} |
| 980 |
} |
| 981 |
#1 |
| 982 |
} |
| 983 |
\cs_generate_variant:Nn \__luaul_maybe_define_use:Nx {c} |
| 984 |
% \end{macrocode} |
| 985 |
% The default for the context argument. Give that most stuff should |
| 986 |
% scale vertically with the font size, we expect most arguments |
| 987 |
% to be given in \texttt{ex}. Additionally especially traditional |
| 988 |
% underlines will use the currently active text color, so especially |
| 989 |
% when luacolor is loaded we have to include the color attribute too. |
| 990 |
% \begin{macrocode} |
| 991 |
\cs_new:Npn \__luaul_default_context: { |
| 992 |
\dim_to_decimal_in_sp:n {1ex} |
| 993 |
@ |
| 994 |
\cs_if_exist:NT \LuaCol@Attribute { |
| 995 |
\int_use:N \LuaCol@Attribute |
| 996 |
} |
| 997 |
} |
| 998 |
% \end{macrocode} |
| 999 |
% The main macro. |
| 1000 |
% \begin{macrocode} |
| 1001 |
\NewDocumentCommand \newunderlinetype |
| 1002 |
{ E{*}{{}} m O{\__luaul_default_context:} m } { |
| 1003 |
\cs_new_protected:Npn #2 { |
| 1004 |
\__luaul_maybe_define_use:cx { |
| 1005 |
__luaul_set_underline_ \cs_to_str:N #2 _ #3 : |
| 1006 |
} { |
| 1007 |
\LuaULSetUnderline |
| 1008 |
\LuaULNewUnderlineType #1 { #4 \hskip 0pt } |
| 1009 |
} |
| 1010 |
} |
| 1011 |
} |
| 1012 |
|
| 1013 |
\bool_if:NT \l__luaul_predefined_bool { |
| 1014 |
\msg_new:nnnn {lua-ul} {height-inconsistent} {Specified~height~is~inconsistent~with~specified~top~and~bottom}{} |
| 1015 |
\msg_new:nnnn {lua-ul} {deprecated} {Used~deprecated~command~`\token_to_str:N #1'.~Please~replace~this~with~`\token_to_str:N #2`.}{} |
| 1016 |
\cs_new:Npn \__luaul_deprecated:NN #1#2 { |
| 1017 |
\cs_new_protected:Npn #1 { |
| 1018 |
\msg_warning:nnnn {lua-ul} {deprecated} {#1} {#2} |
| 1019 |
\cs_gset_eq:NN #1 #2 |
| 1020 |
#1 |
| 1021 |
} |
| 1022 |
} |
| 1023 |
% \end{macrocode} |
| 1024 |
% |
| 1025 |
% \changes{0.0.2}{2020-03-15}{Allow \texttt{\protect\string\protect\highLight} color customization} |
| 1026 |
% \changes{0.1.1}{2021-02-02}{Make color mechanism more generic} |
| 1027 |
% \changes{0.1.5}{2021-12-01}{Better error handling if luacolor is not loaded} |
| 1028 |
% For \verb+\highLight+, the color should be customizable. |
| 1029 |
% There are two cases: If \verb+xcolor+ is not loaded, we just accept |
| 1030 |
% a simple color name. Otherwise, we accept color as documented in |
| 1031 |
% xcolor for PSTricks: Either a color name, a color expression or a |
| 1032 |
% combination of colormodel and associated values. |
| 1033 |
% \begin{macrocode} |
| 1034 |
\tl_new:N \l__luaul_current_color_tl |
| 1035 |
\cs_new:Npn \__luaul_set_color_strip:w \xcolor@ #1#2 {} |
| 1036 |
\cs_new_protected:Npn \luaul_set_color:n #1 { |
| 1037 |
\cs_if_free:NTF \XC@getcolor { |
| 1038 |
\tl_set:Nn \l__luaul_current_color_tl {#1} |
| 1039 |
} { |
| 1040 |
\group_begin: |
| 1041 |
\XC@getcolor {#1} \l_tmpa_tl |
| 1042 |
\use:e { \group_end: |
| 1043 |
\tl_set:Nn \exp_not:N \l__luaul_current_color_tl { |
| 1044 |
\exp_after:wN \__luaul_set_color_strip:w \l_tmpa_tl |
| 1045 |
} |
| 1046 |
} |
| 1047 |
} |
| 1048 |
} |
| 1049 |
\__luaul_deprecated:NN \luaul@setcolor \luaul_set_color:n |
| 1050 |
\cs_new_protected:Npn \luaul_apply_color: { |
| 1051 |
\IfPackageLoadedTF {luacolor} { |
| 1052 |
\cs_if_free:NTF \XC@getcolor { |
| 1053 |
\color {\l__luaul_current_color_tl} |
| 1054 |
}{ |
| 1055 |
\exp_after:wN \XC@undeclaredcolor \l__luaul_current_color_tl |
| 1056 |
} |
| 1057 |
}{ |
| 1058 |
\msg_error:nn {lua-ul} {luacolor-required} |
| 1059 |
} |
| 1060 |
} |
| 1061 |
\__luaul_deprecated:NN \luaul@applycolor \luaul_apply_color: |
| 1062 |
\cs_new:Npn \luaul_context_color: { |
| 1063 |
\l__luaul_current_color_tl |
| 1064 |
} |
| 1065 |
\cs_new:Npn \__luaul_default_colored_context: { |
| 1066 |
\dim_to_decimal_in_sp:n {1ex} |
| 1067 |
@ |
| 1068 |
\luaul_context_color: |
| 1069 |
} |
| 1070 |
% \end{macrocode} |
| 1071 |
% Now a user-level command to set the default color for highlights. |
| 1072 |
% \begin{macrocode} |
| 1073 |
\tl_new:N \l__luaul_highlight_color_tl |
| 1074 |
\tl_set:Nn \l__luaul_highlight_color_tl {yellow} |
| 1075 |
\NewDocumentCommand \LuaULSetHighLightColor {om} { |
| 1076 |
\tl_set:Nx \l__luaul_highlight_color_tl { |
| 1077 |
\IfValueTF {#1} { |
| 1078 |
[#1]{#2} |
| 1079 |
}{ |
| 1080 |
#2 |
| 1081 |
} |
| 1082 |
} |
| 1083 |
} |
| 1084 |
% \end{macrocode} |
| 1085 |
% \changes{0.1.1}{2021-02-02}{Use \texttt{xparse} for all user commands} |
| 1086 |
% \changes{0.1.1}{2021-02-02}{Optionally color \texttt{\protect\string\protect\strikeThrough}} |
| 1087 |
% \changes{0.1.2}{2021-02-15}{Allow \texttt{long} arguments (again)} |
| 1088 |
% \changes{0.2.0}{2022-05-31}{Add keyval for \underLine} |
| 1089 |
% The sizes for the predefined commands are stolen from the \enquote{soul} |
| 1090 |
% default values. |
| 1091 |
% \begin{macrocode} |
| 1092 |
|
| 1093 |
\dim_new:N \l__luaul_top_dim |
| 1094 |
\dim_new:N \l__luaul_bottom_dim |
| 1095 |
\dim_new:N \l__luaul_height_dim |
| 1096 |
\bool_new:N \l__luaul_top_set_bool |
| 1097 |
\bool_new:N \l__luaul_bottom_set_bool |
| 1098 |
\bool_new:N \l__luaul_height_set_bool |
| 1099 |
\bool_new:N \l__luaul_color_set_bool |
| 1100 |
|
| 1101 |
\keys_define:nn {luaul / generic} { |
| 1102 |
top .code:n = { |
| 1103 |
\bool_set_true:N \l__luaul_top_set_bool |
| 1104 |
\dim_set:Nn \l__luaul_top_dim {#1} |
| 1105 |
}, |
| 1106 |
bottom .code:n = { |
| 1107 |
\bool_set_true:N \l__luaul_bottom_set_bool |
| 1108 |
\dim_set:Nn \l__luaul_bottom_dim {(#1)*-1} |
| 1109 |
}, |
| 1110 |
height .code:n = { |
| 1111 |
\bool_set_true:N \l__luaul_height_set_bool |
| 1112 |
\dim_set:Nn \l__luaul_height_dim {#1} |
| 1113 |
}, |
| 1114 |
color .code:n = { |
| 1115 |
\bool_set_true:N \l__luaul_color_set_bool |
| 1116 |
\luaul_set_color:n {#1} |
| 1117 |
}, |
| 1118 |
textcolor .code:n = { |
| 1119 |
\bool_set_false:N \l__luaul_color_set_bool |
| 1120 |
}, |
| 1121 |
textcolor .value_forbidden:n = true, |
| 1122 |
} |
| 1123 |
|
| 1124 |
\cs_new:Npn \__luaul_parse_generic_keys:n #1 { |
| 1125 |
\bool_set_false:N \l__luaul_top_set_bool |
| 1126 |
\bool_set_false:N \l__luaul_bottom_set_bool |
| 1127 |
\bool_set_false:N \l__luaul_height_set_bool |
| 1128 |
|
| 1129 |
\keys_set:nn {luaul / generic} {#1} |
| 1130 |
|
| 1131 |
\bool_if:NTF \l__luaul_bottom_set_bool { |
| 1132 |
\bool_if:NTF \l__luaul_top_set_bool { |
| 1133 |
\bool_if:NTF \l__luaul_height_set_bool { |
| 1134 |
\dim_compare:nNnF { \l__luaul_top_dim + \l__luaul_bottom_dim } = {\l__luaul_height_dim} { |
| 1135 |
\msg_error:nn {lua-ul} {height-inconsistent} |
| 1136 |
} |
| 1137 |
} { |
| 1138 |
\dim_set:Nn \l__luaul_height_dim { \l__luaul_top_dim + \l__luaul_bottom_dim } |
| 1139 |
} |
| 1140 |
} { |
| 1141 |
\dim_set:Nn \l__luaul_top_dim { \l__luaul_height_dim - \l__luaul_bottom_dim } |
| 1142 |
} |
| 1143 |
} { |
| 1144 |
\dim_set:Nn \l__luaul_bottom_dim { \l__luaul_height_dim - \l__luaul_top_dim } |
| 1145 |
} |
| 1146 |
} |
| 1147 |
|
| 1148 |
\newunderlinetype \@@underLine [ |
| 1149 |
\dim_to_decimal_in_sp:n { \l__luaul_top_dim } |
| 1150 |
@ |
| 1151 |
\dim_to_decimal_in_sp:n { \l__luaul_bottom_dim } |
| 1152 |
@ |
| 1153 |
\bool_if:NTF \l__luaul_color_set_bool { |
| 1154 |
colored @ \luaul_context_color: |
| 1155 |
} { |
| 1156 |
\cs_if_exist:NT \LuaCol@Attribute { |
| 1157 |
\int_use:N \LuaCol@Attribute |
| 1158 |
} |
| 1159 |
} |
| 1160 |
] { |
| 1161 |
\bool_if:NT \l__luaul_color_set_bool { |
| 1162 |
\luaul_apply_color: |
| 1163 |
} |
| 1164 |
\leaders\vrule height \l__luaul_top_dim depth \l__luaul_bottom_dim |
| 1165 |
} |
| 1166 |
\newunderlinetype \@basicUnderLine { |
| 1167 |
\leaders \vrule height -.65ex depth .75ex |
| 1168 |
} |
| 1169 |
|
| 1170 |
\NewDocumentCommand \@underLine {o} { |
| 1171 |
\IfValueTF {#1} { |
| 1172 |
\__luaul_parse_generic_keys:n { |
| 1173 |
top = -.65ex, |
| 1174 |
bottom = -.75ex, |
| 1175 |
} |
| 1176 |
\__luaul_parse_generic_keys:n { |
| 1177 |
#1 |
| 1178 |
} |
| 1179 |
\@@underLine |
| 1180 |
} { |
| 1181 |
\@basicUnderLine |
| 1182 |
} |
| 1183 |
} |
| 1184 |
\NewDocumentCommand \underLine {o +m} { |
| 1185 |
\group_begin: |
| 1186 |
\@underLine [#1] #2 |
| 1187 |
\group_end: |
| 1188 |
} |
| 1189 |
|
| 1190 |
\newunderlinetype \@@strikeThrough { |
| 1191 |
\leaders \vrule height .55ex depth -.45ex |
| 1192 |
} |
| 1193 |
\newunderlinetype \@colored@strikeThrough [\__luaul_default_colored_context:] { |
| 1194 |
\luaul_apply_color: |
| 1195 |
\leaders\vrule height .55ex depth -.45ex |
| 1196 |
} |
| 1197 |
\NewDocumentCommand \@strikeThrough {o} { |
| 1198 |
\IfValueTF {#1} { |
| 1199 |
\luaul_set_color:n {#1} |
| 1200 |
\@colored@strikeThrough |
| 1201 |
} |
| 1202 |
\@@strikeThrough |
| 1203 |
} |
| 1204 |
\NewDocumentCommand \strikeThrough {o +m} { |
| 1205 |
\group_begin: |
| 1206 |
\@strikeThrough[#1] #2 |
| 1207 |
\group_end: |
| 1208 |
} |
| 1209 |
|
| 1210 |
\newunderlinetype \@@highLight [\__luaul_default_colored_context:] { |
| 1211 |
\luaul_apply_color: |
| 1212 |
\leaders\vrule height 1.75ex depth .75ex |
| 1213 |
} |
| 1214 |
\NewDocumentCommand \@highLight {O{\l__luaul_highlight_color_tl}} { |
| 1215 |
\luaul_set_color:n {#1} |
| 1216 |
\@@highLight |
| 1217 |
} |
| 1218 |
\NewDocumentCommand \highLight {O{\l__luaul_highlight_color_tl} +m} { |
| 1219 |
\group_begin: |
| 1220 |
\@highLight [#1] #2 |
| 1221 |
\group_end: |
| 1222 |
} |
| 1223 |
\bool_if:NT \l__luaul_soulnames_bool { |
| 1224 |
\cs_new_eq:NN \textul \underLine \cs_new_eq:NN \ul \textul |
| 1225 |
\cs_new_eq:NN \textst \strikeThrough \cs_new_eq:NN \st \textst |
| 1226 |
\cs_new_eq:NN \texthl \highLight \cs_new_eq:NN \hl \texthl |
| 1227 |
} |
| 1228 |
} |
| 1229 |
% \end{macrocode} |
| 1230 |
% \changes{0.0.2}{2020-03-15}{Patch \texttt{\protect\string\protect\reset@font}} |
| 1231 |
% Finally patch \verb+\reset@font+ to ensure that underlines do not propagate |
| 1232 |
% into unexpected places. |
| 1233 |
% \begin{macrocode} |
| 1234 |
\ifx \reset@font \normalfont |
| 1235 |
\let \reset@font \relax |
| 1236 |
\DeclareRobustCommand \reset@font {% |
| 1237 |
\normalfont |
| 1238 |
\LuaULResetUnderline*% |
| 1239 |
} |
| 1240 |
\else |
| 1241 |
\MakeRobust \reset@font |
| 1242 |
\begingroup |
| 1243 |
\expandafter \let |
| 1244 |
\expandafter \helper |
| 1245 |
\csname reset@font \endcsname |
| 1246 |
\expandafter \endgroup |
| 1247 |
\expandafter \gdef |
| 1248 |
\csname reset@font \expandafter \endcsname |
| 1249 |
\expandafter {% |
| 1250 |
\helper% |
| 1251 |
\LuaULResetUnderline*% |
| 1252 |
} |
| 1253 |
\fi |
| 1254 |
% \end{macrocode} |
| 1255 |
% \changes{0.1.1}{2021-02-02}{Patch \texttt{\protect\string\protect\output}} |
| 1256 |
% In the output routine, the page box is repacked before \verb+\reset@font+ is |
| 1257 |
% called, so we have to ensure to reset the attributes before that. This will |
| 1258 |
% use some awesome output routine hook as soon as that's in the kernel, until |
| 1259 |
% then manual patching it is. |
| 1260 |
% |
| 1261 |
% At the time I am writing this the remaining code of the package contains |
| 1262 |
% exactly ten times \verb+\expandafter+. Interestingly, that's also exactly |
| 1263 |
% the number of \verb+\expandafter+s we use here. |
| 1264 |
% \begin{macrocode} |
| 1265 |
\output\expandafter\expandafter\expandafter{% |
| 1266 |
\expandafter\expandafter\expandafter\LuaULResetUnderline |
| 1267 |
\expandafter\expandafter\expandafter*% |
| 1268 |
\expandafter\@firstofone\the\output% |
| 1269 |
} |
| 1270 |
% \end{macrocode} |
| 1271 |
% \iffalse |
| 1272 |
%</package> |
| 1273 |
% \fi |
| 1274 |
% \Finale |