summaryrefslogtreecommitdiff
path: root/macros/luatex/latex/ligtype/ligtype.tex
blob: 8769a94f5799a903470fd4ff83d75215ab3f5e18 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
\documentclass[
ukenglish,%
twoside=false,%
fontsize=10pt,%
bibliography=totoc,%
numbers=autoenddot,%
headings=normalsize,%
overfullrule,%
headings=optiontotoc,%
]
{scrartcl}

\makeatletter
\providecommand\@ix{9.6}
\providecommand\@xi{10.4}
\providecommand\@xiii{13.15}
\def\subfootnotesize{%
    \@setfontsize%
    \subfootnotesize%
    {7.6}{8}%
}
\def\supernormalsize{%
    \@setfontsize%
    \supernormalsize%
    \@xi{12}%
}
\def\subnormalsize{%
    \@setfontsize%
    \subnormalsize%
    \@ix{11}%
}
\def\sublarge{%
    \@setfontsize%
    \sublarge%
    {11.2}{13}%
}
\def\superlarge{%
    \@setfontsize%
    \superlarge%
    \@xiii{16}%
}
\makeatother

\newcommand*{\sectionas}{2.3ex plus .2ex}
\newcommand*{\sectionbs}{-3.5ex plus -1ex minus -.2ex}
\newcommand*{\sectiontbs}{3.6pt}
\newcommand*{\subsectiontbs}{1.2pt}
\newcommand*{\subsectionas}{1.5ex plus .2ex}
\newcommand*{\subsectionbs}{-3.25ex plus -1ex minus -.2ex}
\newcommand*{\subsubsectionas}{\subsectionas}
\newcommand*{\subsubsectionbs}{\subsectionbs}
\newcommand*{\paragraphas}{-1em}
\newcommand*{\paragraphbs}{.8\baselineskip}

\newlength{\tnwsection}
\newlength{\tnwsubsection}
\newlength{\tnwsubsubsection}
\newlength{\tnwparagraph}
\newlength{\tisection}
\newlength{\tisubsection}
\newlength{\tisubsubsection}
\newlength{\tiparagraph}

\setlength{\tnwsection}{1.2em}
\addtolength{\tnwsection}{.6em}
\setlength{\tnwsubsection}{\tnwsection}
\addtolength{\tnwsubsection}{.75em}
\setlength{\tnwsubsubsection}{\tnwsubsection}
\addtolength{\tnwsubsubsection}{.75em}
\setlength{\tnwparagraph}{\tnwsubsubsection}
\addtolength{\tnwparagraph}{.75em}

\setlength{\tisection}{0em}
\setlength{\tisubsection}{\tisection}
\addtolength{\tisubsection}{\tnwsection}
\setlength{\tisubsubsection}{\tisubsection}
\addtolength{\tisubsubsection}{\tnwsubsection}
\setlength{\tiparagraph}{\tisubsubsection}
\addtolength{\tiparagraph}{\tnwsubsubsection}

\RedeclareSectionCommands[%
    tocraggedentrytext=true,%
]%
{section,subsection,subsubsection,paragraph}

\RedeclareSectionCommand[%
    tocindent=\tisection,%
    tocnumwidth=\tnwsection,%
    tocbeforeskip=\sectiontbs,%
    tocentryformat=\supernormalsize\sffamily\bfseries,%
    tocentrynumberformat=\supernormalsize\sffamily\bfseries,%
    tocpagenumberformat=\supernormalsize\sffamily\bfseries%
]%
{section}

\RedeclareSectionCommand[%
    tocindent=\tisubsection,%
    tocnumwidth=\tnwsubsection,%
    tocbeforeskip=\subsectiontbs,%
    tocentryformat=\subnormalsize,%
    tocentrynumberformat=\subnormalsize,%
    tocpagenumberformat=\subnormalsize%
]%
{subsection}

\RedeclareSectionCommand[%
    tocindent=\tisubsubsection,%
    tocnumwidth=\tnwsubsubsection,%
    tocbeforeskip=\sectiontbs%
]%
{subsubsection}

\RedeclareSectionCommand[%
    afterskip=\paragraphas,%
    beforeskip=\paragraphbs,%
    tocindent=\tiparagraph,%
    tocnumwidth=\tnwparagraph,%
    tocbeforeskip=\sectiontbs%
]%
{paragraph}

\linespread{1.1}

\newcommand*{\hs}{\hspace{.45em}}

\usepackage{fontspec}

\directlua{

    fonts.handlers.otf.addfeature {
        name = "onumkern",
        type = "kern",
        data = {
            ["v"] = { ["zero.taboldstyle"] = 80 },
            ["two.taboldstyle"] = { ["zero.taboldstyle"] = 30, ["/"] = 50 },
            ["five.taboldstyle"] = { ["/"] = 20 },
            ["»"] = { ["I"] = 50 },
        },
    }

}

\setmainfont{Tangent}[
    BoldFont = *-Medium,
    ItalicFont = *-Italic,
    Ligatures = {Common, TeX},
    Numbers = OldStyle,
    RawFeature = +onumkern
]

\setsansfont{SegoeUI}[
    BoldFont = seguisb.ttf,
    Ligatures = {Discretionary, TeX},
    Numbers = OldStyle
]

\setmonofont{Consolas}[
    ItalicFont = *-Italic,
    Numbers = OldStyle
]

\newcommand*{\mono}[1]{%
    {\subnormalsize\texttt{#1}}%
}

\newcommand*{\monofn}[1]{%
    {\subfootnotesize\texttt{#1}}%
}

\usepackage{babel}
\usepackage{realscripts}
\usepackage[paper=a4paper,marginratio={2:1,3:4}]{geometry}
\usepackage[noshortf,makemarks]{ligtype}
\usepackage{spacekern}
\usepackage{hyperref}
\usepackage{bookmark}
\usepackage{microtype}

\hypersetup{%
    pdflang=en,%
    unicode=true,%
	pdfborder={0 0 0},%
	bookmarksopen=true,%
    bookmarksopenlevel=0,%
	bookmarksnumbered=true,%
    pdftitle={The ligtype package},
    pdfsubject={Comprehensive ligature suppression functionalities},
    pdfauthor={Thomas Kelkel},
    pdfkeywords={tex, latex, ligatures}
}

\setlength{\parindent}{0pt}
\setlength{\parskip}{.4\baselineskip}

\newcommand*{\q}[1]{%
    ›%
    #1%
    ‹%
}
\newcommand*{\qq}[1]{%
    »%
    #1%
    «%
}

\setlength{\footnotesep}{.8em}

\deffootnote[0em]{0em}{1em}{}

\newcommand*{\fn}[1]{%
    \footnotemark%
    \footnotetext{%
    \textbf{\addfontfeature{Numbers = Lining, BoldFont = Tangent-Bold}\thefootnote}%
        \hs%
        #1%
    }%
}

\newcommand*{\textnote}[1]{%
    {%
        \ttfamily%
        \char174{}%
        #1%
        \char174{}%
    }%
}

\usepackage{luacode}

\begin{luacode}

local FLOOR = math.floor

local function round ( num, dec )
    return FLOOR ( num * 10^dec + 0.5 ) / 10^dec
end

local p_array = {false, false, false, false, false}
local p_counter = 0
local ID = node.id
local NEW = node.new
local COPY = node.copy
local REM = node.remove
local PREV = node.prev
local NEXT = node.next
local TAIL = node.tail
local T_ID = node.traverse_id
local T_GLYPH = node.traverse_glyph
local INS_B = node.insert_before
local GLYPH = ID ( "glyph" )
local GLUE = ID ( "glue" )
local KERN = ID ( "kern" )
local HLIST = ID ( "hlist" )
local SWAPPED = table.swapped
local SUBTYPES = node.subtypes
local SPACESKIP = SWAPPED ( SUBTYPES ("glue") )["spaceskip"]
local WIDTH = round ( tex.sp ( "1.6em" ), 0 )
local ipairs = ipairs
local page_counter = 0
local INS_A = node.insert_after
local p_counter_x = 0
local ATC = luatexbase.add_to_callback

local function make_marks ( head, char, id, array, counter )
    for n in T_GLYPH ( head ) do
        if n.char == char then
            counter = counter + 1
            array[counter] = {false, false, false}
            head = INS_B ( head, n, NEW ( GLYPH ) )
            PREV ( n ).data = id
            local NEXT = n.next
            local inside_counter = 0
            while NEXT.char ~= char do
                inside_counter = inside_counter + 1
                array[counter][inside_counter] = COPY ( NEXT )
                NEXT = NEXT.next
                REM ( head, NEXT.prev )
            end
            head = REM ( head, NEXT )
            head = REM ( head, n )
        end
    end
    return head, array, counter
end

local function mark_notes ( head )
    head, p_array, p_counter = make_marks ( head, 174, 427956, p_array, p_counter )
    return head
end

local function make_margin_notes ( head, id, array, counter )
    for n in T_ID ( HLIST, head ) do
        local first_kern = 0
        local first_width = 0
        for glyph_node in T_GLYPH ( n.head ) do
            if glyph_node.data == id then
                counter = counter + 1
                local tail_node = TAIL ( n.head )
                local glue_node = NEW ( GLUE )
                glue_node.subtype = SPACESKIP
                glue_node.width = WIDTH
                local NEXT
                local switch_counter = 1 -- page_counter
                n.head = INS_A ( n.head, TAIL ( n.head ), glue_node )
                if ( switch_counter % 2 == 0 ) then
                    NEXT = glue_node
                else
                    NEXT = tail_node
                end
                local kern_value = 0
                for _, value in ipairs ( array[counter] ) do
                    if value ~= false then
                        local NUMBER = COPY ( value )
                            n.head = INS_A ( n.head, NEXT, NUMBER)
                            if NEXT.next then
                                NEXT = NEXT.next
                            end
                        if NUMBER.width then
                            kern_value = kern_value - NUMBER.width
                        end
                    end
                end
                local kern_node = NEW ( KERN )
                kern_node.kern = kern_value - glue_node.width
                if switch_counter % 2 ~= 0  then
                    kern_node.kern = kern_node.kern - tex.hsize + first_kern
                    n.head = INS_A ( n.head, tail_node, kern_node )
                end
                if ( switch_counter % 2 == 0 ) and ( first_kern < 0 ) then
                    kern_node.kern = kern_node.kern - first_kern + first_width - kern_value
                    n.head = INS_A ( n.head, tail_node, kern_node )
                end
                first_kern = kern_node.kern
                first_width = kern_value
            end
        end
    end
    return head, counter
end

local function make_text_notes ( head )
    page_counter = page_counter + 1
    head, p_counter_x = make_margin_notes ( head, 427956, p_array, p_counter_x )
    return head
end

    ATC ( "ligaturing", mark_notes , "mark notes" )
    ATC ( "pre_output_filter", make_text_notes , "count pages" )

\end{luacode}

\flushbottom

\nolig{ligtype}{lig|type}

\begin{document}

\title{The ligtype package\vspace{.25\baselineskip}\\\superlarge{}Comprehensive ligature suppression functionalities}%
\author{\sublarge{}Thomas Kelkel\vspace{-.25\baselineskip}\\\sublarge{}kelkel@emaileon.de\vspace{-.25\baselineskip}}%
\date{\addfontfeature{LetterSpace=2}\sublarge{}2022/06/11\quad{}v0.1a}%
\maketitle

\ligtypeoff

\tableofcontents

\addvspace{3em}
\hfill\textit{\qq{I don't think you would ever do this in English}}\par
\addvspace{-.25\baselineskip}
\hfill {\small\textbf{David Carlisle}\quad}
\vspace{-2\baselineskip}

\section{Introduction}

The main feature of this package is the selective suppression of typographic ligatures.

There’s already the \mono{selnolig} package by Mico Loretan providing such capability. However, it has some significant shortcomings that render it barely applicable in many use cases. The \mono{ligtype} package steps in to address these deficiencies. The main improvements are:

\paragraph{Kerning}

The \mono{ligtype} package applies kerning for the glyphs of the suppressed ligatures. Both font and user kerning are applied in the usual way.

\paragraph{Short-armed f}

If available, it automatically replaces the f-glyphs of suppressed ligatures with their short-arm variant.

\paragraph{Speed}

It is about ten times faster on a typical document, thanks to its completely different architecture.\fn{This factor increases with the length of the paragraphs. On a 150-page paragraph, \monofn{ligtype} runs about a hundred times faster.}

\addvspace{\paragraphbs}

Another, rather minor, difference is that it does not require \mono{fontspec}.

\addvspace{\paragraphbs}

\noindent The \mono{ligtype} package provides built-in suppression of inappropriate ligatures for \textbf{\addfontfeature{BoldFont = Tangent-Bold}German language documents.} (\kern.05emFor this purpose it makes use of the corresponding suppression rules provided by the \mono{selnolig} package, which cover all common f-ligatures\fn{Details can be taken from the \monofn{selnolig} documentation.}.) Using the \mono{nodefault} option and the \mono{\textbackslash{}nolig} and \mono{\textbackslash{}keeplig} macros it can also be used for other languages.

Please note that Lua{\addfontfeature{LetterSpace = 2}\LaTeX} is required to use this package.

Finally, a quick word regarding the code: It is optimized for speed throughout. It would have been much easier to provide a significantly shorter, clearer, more straightforward one. Unfortunately, with this software such code would not have been the fastest one.

\section{Basic usage}

To load the package, simply add the following line to the preamble of the document:

\begin{quote}
    \mono{\textbackslash{}usepackage\{ligtype\}}
\end{quote}

If the glyphs forming the ligatures are already properly kerned and a German language document is typeset, loading the package without any options should be sufficient for most use cases.

However, most of the time it will be the case that the corresponding glyphs have to be kerned first. For this task, the \mono{kerntest} option is extremely helpful.

\section{Package options}

Options can be loaded by adding them comma separated within square brackets:

\begin{quote}
    \mono{\textbackslash{}usepackage[\textit{<option1>,<option2>,…}]\{ligtype\}}
\end{quote}

\addcontentsline{toc}{subsection}{kerntest}

\textnote{kerntest}This option prints all glyph combinations that comprise the ligatures \mono{ligtype} is looking for in Regular, Italic, Bold and Italic Bold for both the Roman and the Sans font on the last page of the document. This gives you an overview of all kerning pairs that are relevant when breaking ligatures, and you can inspect the kerning values accordingly.

\addcontentsline{toc}{subsection}{makemarks}

\textnote{makemarks}Marks each point where a ligature was suppressed with a blue triangle below the baseline.

\addcontentsline{toc}{subsection}{noshortf}

\textnote{noshortf}Various use cases are conceivable with this option.

It is primarily intended to suppress the use of short-armed f if they are not desired.

In addition, it may be useful to load this option when using fonts that do not offer short-armed f, since there is a small gain in speed if \mono{ligtype} does not look for them.

Finally, this option could be used to prevent \qq{false positives}. (Even though such are not known to occur.)

\addcontentsline{toc}{subsection}{allshortf}

\textnote{allshortf}In some cases it may be desirable to use short-armed f without having different f-glyphs in the document. This option can be used for such purpose, since it replaces all long-armed f with their short-arm variant (if available).

This option has priority over the \mono{noshortf} option, i.\,e. if both are loaded, \mono{allshortf} is applied.

\addcontentsline{toc}{subsection}{nodefault}

\textnote{nodefault}Disables the built-in (German language) suppression rules. With this option and the \mono{\textbackslash{}nolig} and \mono{\textbackslash{}keeplig} macros, \mono{ligtype} can be used for other languages.

\section{Macros}

\addcontentsline{toc}{subsection}{nolig}

\addcontentsline{toc}{subsection}{keeplig}

\textnote{\textbackslash{}nolig}The first macro defines a rule for suppressing ligatures, the second one for keeping them. Both macros take two arguments.\textnote{\textbackslash{}keeplig} The first one specifies the string to search for. In the second, a marker of the breaking point is added to this string using a vertical bar.\fn{The architecture of the \monofn{ligtype} package differs quite significantly from that of the \monofn{selnolig} package. To allow current users of \monofn{selnolig} to continue using existing macros for creating suppression rules as far as possible without changes, the syntax has been largely adopted. For the \monofn{\textbackslash{}nolig} macro it is identical. However, in contrast to the \monofn{selnolig} syntax, the breakpoint must be specified as a second argument for the \monofn{\textbackslash{}keeplig} macros, since \monofn{ligtype} requires this for every rule application.} For example:

\begin{quote}
    \mono{\textbackslash{}nolig\{flich\}\{f|lich\}}\\
    \mono{\textbackslash{}keeplig\{flicht\}\{f|licht\}}
\end{quote}

A list of alternatives that can be located at the end of the string can be specified within square brackets:

\begin{quote}
    \mono{\textbackslash{}nolig\{Auff[aeiloruyäöü]\}\{Auf|f\}}
\end{quote}

It is important to note that macros are processed in the order in which they are defined, which means that strings defined later have priority over strings defined earlier. Accordingly, strings defined later should not be included in strings defined earlier, otherwise the earlier ones will have no effect.

\addcontentsline{toc}{subsection}{ligtypeon}

\addcontentsline{toc}{subsection}{ligtypeoff}

\textnote{\textbackslash{}ligtypeon}With the help of these macros the features of the \mono{ligtype} package can be switched on and off within the document.\textnote{\textbackslash{}ligtypeoff}

\section{Acknowledgements}

The \mono{ligtype} package makes use of the German language ligature suppression rules of the \mono{selnolig} package by Mico Loretan. The \mono{selnolig} package can be downloaded at

\begin{quote}
    \mono{https://www.ctan.org/pkg/selnolig}
\end{quote}

and may be distributed and/or modified under the conditions of the LaTeX Project Public License.

Please see the \qq{License and acknowledgments} section of the \mono{selnolig} documentation to learn about all the people who contributed to the creation of the suppression rules.

In general, the truly excellent \mono{selnolig} documentation is highly recommended for further information on the subject of this package.

\section{License}

This package is copyright © 2022 Thomas Kelkel. It may be distributed and/or modified under the conditions of the LaTeX Project Public License, either version 1.3c of this license or (at your option) any later version. This work has the LPPL maintenance status \qq{author maintained}.

\end{document}