summaryrefslogtreecommitdiff
path: root/luaotfload.dtx
blob: f5e62b1a2b798dc73dace8dd6c6d394ece4481ec (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
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
% \iffalse meta-comment
%
% Copyright (C) 2009 by Elie Roux <elie.roux@telecom-bretagne.eu>
%
% This work is under the CC0 license.
%
% This work consists of the main source file luaotfload.dtx
% and the derived files
%    luaotfload.sty, luaotfload.lua
%
% Unpacking:
%    tex luaotfload.dtx
%
% Documentation:
%    pdflatex luaotfload.dtx
%
%    The class ltxdoc loads the configuration file ltxdoc.cfg
%    if available. Here you can specify further options, e.g.
%    use A4 as paper format:
%       \PassOptionsToClass{a4paper}{article}
%
%
%
%<*ignore>
\begingroup
  \def\x{LaTeX2e}%
\expandafter\endgroup
\ifcase 0\ifx\install y1\fi\expandafter
         \ifx\csname processbatchFile\endcsname\relax\else1\fi
         \ifx\fmtname\x\else 1\fi\relax
\else\csname fi\endcsname
%</ignore>
%<*install>
\input docstrip.tex
\Msg{************************************************************************}
\Msg{* Installation}
\Msg{* Package: luaotfload 2010/01/08 v1.07 ConTeXt font loading system}
\Msg{************************************************************************}

\keepsilent
\askforoverwritefalse

\let\MetaPrefix\relax

\preamble
This is a generated file.

Copyright (C) 2009 by Elie Roux <elie.roux@telecom-bretagne.eu>

This work is under the CC0 license.

This work consists of the main source file luaotfload.dtx
and the derived files
    luaotfload.sty, luaotfload.lua

\endpreamble

\let\MetaPrefix\DoubleperCent


\generate{%
  \usedir{tex/luatex/luaodfload}%
  \file{luaotfload.sty}{\from{luaotfload.dtx}{package}}%
}

% The following hacks are to generate a lua file with lua comments starting by
% -- instead of %%

\def\MetaPrefix{-- }

\def\luapostamble{%
  \MetaPrefix^^J%
  \MetaPrefix\space End of File `\outFileName'.%
}

\def\currentpostamble{\luapostamble}%

\generate{%
  \usedir{tex/luatex/luaotfload}%
  \file{luaotfload.lua}{\from{luaotfload.dtx}{lua}}%%
}

\obeyspaces
\Msg{************************************************************************}
\Msg{*}
\Msg{* To finish the installation you have to move the following}
\Msg{* files into a directory searched by TeX:}
\Msg{*}
\Msg{*     luaotfload.sty, luaotfload.lua}
\Msg{*}
\Msg{* Happy TeXing!}
\Msg{*}
\Msg{************************************************************************}

\endbatchfile
%</install>
%<*ignore>
\fi
%</ignore>
%<*driver>
\NeedsTeXFormat{LaTeX2e}
\ProvidesFile{luaotfload.drv}%
  [2010/01/08 v1.07 ConTeXt font loading system]%
\documentclass{ltxdoc}
\usepackage{metalogo}
\usepackage[colorlinks=true]{hyperref}
\EnableCrossrefs
\CodelineIndex
\begin{document}
  \DocInput{luaotfload.dtx}%
\end{document}
%</driver>
% \fi
%
% \CheckSum{0}
%
% \CharacterTable
%  {Upper-case    \A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z
%   Lower-case    \a\b\c\d\e\f\g\h\i\j\k\l\m\n\o\p\q\r\s\t\u\v\w\x\y\z
%   Digits        \0\1\2\3\4\5\6\7\8\9
%   Exclamation   \!     Double quote  \"     Hash (number) \#
%   Dollar        \$     Percent       \%     Ampersand     \&
%   Acute accent  \'     Left paren    \(     Right paren   \)
%   Asterisk      \*     Plus          \+     Comma         \,
%   Minus         \-     Point         \.     Solidus       \/
%   Colon         \:     Semicolon     \;     Less than     \<
%   Equals        \=     Greater than  \>     Question mark \?
%   Commercial at \@     Left bracket  \[     Backslash     \\
%   Right bracket \]     Circumflex    \^     Underscore    \_
%   Grave accent  \`     Left brace    \{     Vertical bar  \|
%   Right brace   \}     Tilde         \~}
%
% \GetFileInfo{luaotfload.drv}
%
% \title{The \textsf{luaotfload} package}
% \date{2010/01/08 v1.07}
% \author{Elie Roux \\ \texttt{elie.roux@telecom-bretagne.eu}}
%
% \maketitle
%
% \begin{abstract}
% Con\TeX t font loading system, providing the possibility to load OTF fonts
% with a lot of features, and the \XeTeX\ font loading syntax.
% \end{abstract}
%
% \tableofcontents
%
% \section{Documentation}
% 
% \subsection{Introduction}
%
% Font management and installation has always been painful with \TeX\ (and
% even more with \LaTeX ). A lot of files are needed for one font (tfm, pfb,
% map, fd, vf), and they are limited to 256 characters. But the font world has
% evolved since, and new standard types of fonts have appeared, like
% \textsf{truetype} or \textsf{opentype} fonts. These fonts can contain a lot
% of characters, and have some functionalities (ligatures, old-style numbers,
% small capitals, etc.). They are everywhere, as the system fonts and most
% modern text softwares fonts are of this type. Until now the (almost) only
% way to use them with \TeX\ was to use them with \XeTeX .
%
% Unlike \XeTeX , Lua\TeX\ does not provide facilities for these fonts by
% default, but it provides a way to hook lua code in some points of the \TeX\
% algorithm, for instance we can improve the font loading system; this is what
% we do in this package.
%
% This package is quite low-level, and should be loaded directly in the macro
% package, like it is in Con\TeX t. Sadly, Plain and \LaTeX\ are frozen and
% it's even impossible to adapt them to the new engines. 
%
% \subsection{Loading fonts}
%
% \textsf{luaotfload} supports \XeTeX -like font syntax which looks like:
%
% \begin{center}
% |\font\foo="|\meta{prefix}|:|\meta{font name}|:|\meta{font features}|"| \meta{\TeX\ font features}
% \end{center}
%
% \subsubsection{Prefix}
%
% Prefixes are either |file| or |name|; |file| prefix forces searching font
% based on filename, while a |name| prefix will result in consulting name
% database. Name database can be generated with\footnote{|mtxrun| tool and
% scripts are part of Con\TeX t}:
%
% \begin{center}
% |mtxrun --script fonts --names|
% \footnote{|mtxrun --script fonts --reload --simple| in recent
% Con\TeX t releases}
% \end{center}
%
% \noindent Which will generate a file named |luatex-fonts-names.lua| that is
% to be placed somewhere in your \textsc{texmf} tree where \textsf{kpathsea} can
% find it.
%
% \subsubsection{Font name}
%
% Font name can be either a font filename or actual font name. If no prefix is
% specified; it is assumed to be a filename. \XeTeX-like syntax of using square
% brackets to select fonts from \textsc{texmf} tree is olso supported;
% |\font\foo=[foo.ttf]:+liga|.
%
% \subsubsection{Font features}
%
% By default \textsf{luaotfload} doesn't enable any font features, font features
% must be explicitly enabled, |\font\foo=foo.ttf:+liga| will load |foo.ttf| font
% and enable \texttt{liga} feature. In the same way font features can be
% deactivated by prefixing it with minus sign; |\font\foo=foo.ttf:+liga;-liga|
% will load \texttt{foo.ttf} without any features enabled.
%
% \paragraph{Modes}
% \textsf{luaotfload} has two OpenType processing modes; \texttt{base} mode
% which enables only a subset of OpenType features and works by mapping those
% features to traditional TeX ligaturing and kerning mechanisms and is a bit
% faster, and \texttt{node} mode which, hopefully, supports OpenType fully and
% works by direct processing of node list at lua end and is a bit slower.
% Note that, nside math mode only \texttt{base} is supported.
%
% Modes can be selected using \texttt{mode} keyword and it must come before any
% other font feature, e.g. |\font\foo=foo.ttf:mode=node;+liga|.
%
% \paragraph{Additional font features}
% \textsf{luaotfload} defines some additional font feature not defined in
% OpenType, currently three features are defined:
%
% \begin{itemize}
% \item[-] \texttt{anum}: Arabic numbers
% \item[-] \texttt{tlig}: \TeX\ ligatures
% \item[-] \texttt{trep}: \TeX\ replacements
% \end{itemize}
%
% \subsubsection{Examples}
% \begin{verbatim}
% \font\testa=file:lmroman10-regular                  at 12pt \testa \input tufte \par
% \font\testb=file:lmroman12-regular:+liga;           at 24pt \testb effe flink fietsen \par
% \font\testc=file:lmroman12-regular:mode=node;+liga; at 24pt \testc effe flink fietsen \par
% \font\testd=name:lmroman10bold                      at 12pt \testd a bit bold \par
%
% \font\teste=[lmroman12-regular]:+liga at 30pt \teste text
% \font\testf=[lmroman12-regular]       at 40pt \testf more text
% \end{verbatim}
%
% \subsection{Con\TeX t files needed}
%
% This package is a wrapper for several files taken from the Con\TeX t macro
% package. The philosophy is to let Con\TeX t do all the implementation and
% update these files from time to time. To do so we did not modify the files
% taken from Con\TeX t, we only changed their names to prevent name clashes.
% You can thus update the font system of this package simply by updating the
% files taken from Con\TeX t, without (theorically) changing the \texttt{.sty}
% file nor the main \texttt{.lua} file.
%
% The Con\TeX t files are renamed by adding the prefix \texttt{otfl-} to them
% (\texttt{otfl} as \texttt{OTF L}oad).The files are:
%
% \begin{itemize}
% \item \texttt{luat-dum.lua}
% \item \texttt{data-con.lua}
% \item \texttt{node-ini.lua}
% \item \texttt{node-inj.lua}
% \item \texttt{node-fnt.lua}
% \item \texttt{node-dum.lua}
% \item \texttt{font-ini.lua}
% \item \texttt{font-tfm.lua}
% \item \texttt{font-cid.lua}
% \item \texttt{font-ott.lua}
% \item \texttt{font-otf.lua}
% \item \texttt{font-otd.lua}
% \item \texttt{font-oti.lua}
% \item \texttt{font-otb.lua}
% \item \texttt{font-otn.lua}
% \item \texttt{font-ota.lua}
% \item \texttt{font-otc.lua}
% \item \texttt{font-def.lua}
% \item \texttt{font-xtx.lua}
% \item \texttt{font-map.lua}
% \item \texttt{font-dum.lua}
% \end{itemize}
% 
% \subsection{Troubleshooting}
%
% If you encounter problems with some fonts, please first update to the latest version of this package before reporting a bug, as this package is under active development.
%
% A very common problem is the lack of features for some otf fonts even when specified. It can be related to the fact that some fonts do not provide features for the |dflt| script, which is the default one in this package, so you may have to specify the script in the command line, for example:
%
% |\font\myfont = MyFont.otf:script=latn;+liga;|
%
% \section{\texttt{luaotfload.lua}}
%
% \iffalse
%<*lua>
% \fi
%
%    First some usual initializations.
%
%    \begin{macrocode}
luaotfload          = { }

luaotfload.module = {
    name          = "luaotfload",
    version       = 1.06,
    date          = "2009/12/29",
    description   = "ConTeXt font loading system.",
    author        = "Elie Roux & Hans Hagen",
    copyright     = "Elie Roux",
    license       = "CC0"
}

luatextra.provides_module(luaotfload.module)

%    \end{macrocode}
%
%    We load the Con\TeX t files with this function. It automatically adds the
%    \texttt{otfl-} prefix to it, so that we call it with the actual Con\TeX t
%    name.
%
%    Some Con\TeX t files introduce incompatibilities with old Lua\TeX s, thus
%    the old versions of these files are kept under the name |otfl-compat-...|
%    and are called according to the version. The only file in this case
%    is a |font-otf.lua| that, in version 2009.11.26 of Con\TeX t generates
%    invalid PDFs with Lua\TeX{} older than version 0.45.
%
%    \begin{macrocode}


function luaotfload.loadmodule(name, compat)
    local tofind
    if compat then
        if tex.luatexversion < 45 then
            tofind = 'otfl-compat-'..name
        else
            tofind = 'otfl-'..name
        end
    else
        tofind = 'otfl-'..name
    end
    local found = kpse.find_file(tofind,"tex")
    if not found then
      luatextra.module_error('luaotfload', string.format('file %s not found.', tofind))
      return
    end
    luatextra.module_log('luaotfload', "loading file "..found)
    dofile(found)
end

%    \end{macrocode}
%
%    The following functions are made to map Con\TeX t functions to
%    \textsf{luaextra} functions.
%
%    \begin{macrocode}

string.strip = string.stripspaces

file = fpath
file.extname = fpath.suffix

%    \end{macrocode}
%
%    These are small functions that are not already in \textsf{luatextra}.
%
%    \begin{macrocode}

local splitters_s, splitters_m = { }, { }

function lpeg.splitat(separator,single)
    local splitter = (single and splitters_s[separator]) or splitters_m[separator]
    if not splitter then
        separator = lpeg.P(separator)
        if single then
            local other, any = lpeg.C((1 - separator)^0), lpeg.P(1)
            splitter = other * (separator * lpeg.C(any^0) + "")
            splitters_s[separator] = splitter
        else
            local other = lpeg.C((1 - separator)^0)
            splitter = other * (separator * other)^0
            splitters_m[separator] = splitter
        end
    end
    return splitter
end

function table.compact(t)
    if t then
        for k,v in next, t do
            if not next(v) then
                t[k] = nil
            end
        end
    end
end

function table.sortedhashkeys(tab) -- fast one
    local srt = { }
    for key,_ in next, tab do
        srt[#srt+1] = key
    end
    table.sort(srt)
    return srt
end

function table.reverse_hash(h)
    local r = { }
    for k,v in next, h do
        r[v] = string.lower(string.gsub(k," ",""))
    end 
    return r
end 

function table.reverse(t)
    local tt = { }
    if #t > 0 then
        for i=#t,1,-1 do
            tt[#tt+1] = t[i]
        end
    end
    return tt
end

%    \end{macrocode}
%
%    We start loading some lua files. These two are some code not used by
%    Con\TeX t at all that allow other modules to be used, it provides some
%    low-level Con\TeX t functions.
%
%    \begin{macrocode}

luaotfload.loadmodule('luat-dum.lua') -- not used in context at all
luaotfload.loadmodule('data-con.lua') -- maybe some day we don't need this one

%    \end{macrocode}
%
%    This one is for node support.
%
%    \begin{macrocode}

luaotfload.loadmodule('node-ini.lua')

%    \end{macrocode}
%
%    By default Con\TeX t takes some private attributes for internal use. With
%    Plain and \LaTeX\ we can't do so, we use |\newluaattribute|. This
%    functions overrides a function defined in the previous module that
%    returns the number of a private attribute. We allocate new attributes in
%    the \texttt{.sty} file, and this function returns their number. Like this
%    we don't need any private attribute, and this package is compatible with
%    the others. We use the |otfl@| prefix for attributes.
%
%    \begin{macrocode}

function attributes.private(name)
    local number = tex.attributenumber['otfl@'..name]
    if not number then 
        luatextra.module_error('luaotfload', string.format('asking for attribute %s, but not declared. Please report to the maintainer of luaotfload.', name))
    end
    return number
end

%    \end{macrocode}
%
%    Some more modules. We don't load neither \texttt{font-enc.lua} nor
%    \texttt{font-afm.lua} as it will never be used here.
%    
%    We also remove a warning from \texttt{node-fnt.lua} as it not relevant
%    with Lua\TeX{}tra.
%
%    \begin{macrocode}

tex.attribute[0] = 0

luaotfload.loadmodule('node-res.lua')
luaotfload.loadmodule('node-inj.lua')
luaotfload.loadmodule('node-fnt.lua')
luaotfload.loadmodule('node-dum.lua')

luaotfload.loadmodule('font-ini.lua')
luaotfload.loadmodule('font-tfm.lua')
luaotfload.loadmodule('font-cid.lua')
luaotfload.loadmodule('font-ott.lua')
luaotfload.loadmodule('font-otf.lua', 1)
luaotfload.loadmodule('font-otd.lua')
luaotfload.loadmodule('font-oti.lua')
luaotfload.loadmodule('font-otb.lua')
luaotfload.loadmodule('font-otn.lua')
luaotfload.loadmodule('font-ota.lua')
luaotfload.loadmodule('font-otc.lua')

%    \end{macrocode}
%
%    \texttt{font-def.lua} calls the function \texttt{callback.register} to
%    register its callbacks. We override it with a dumb function so that it
%    does not register any callback. We will register the callbacks later.
%
%    \begin{macrocode}

do
  local temp = callback.register
  callback.register = function (...)
    return
  end 
  luaotfload.loadmodule('font-def.lua')
  callback.register = temp
end

luaotfload.loadmodule('font-xtx.lua')
luaotfload.loadmodule('font-map.lua')
luaotfload.loadmodule('font-dum.lua')
luaotfload.loadmodule('font-msc.lua')

%    \end{macrocode}
%
%    This is a small patch that prevents errors in some \LaTeX\ files.
%
%    \begin{macrocode}

fonts.enc.known = {}

%    \end{macrocode}
%
%    We have to register a function in the |find_vf_file| callback in order to make everything work.
%
%    \begin{macrocode}

function luaotfload.find_vf_file(name)
    name = file.removesuffix(file.basename(name))
    local result = kpse.find_file(name, "vf") or ""
    if result == "" then
        result = kpse.find_file(name, "ovf") or ""
    end
    return result
end

%    \end{macrocode}
%
%    Finally two functions 
%
%    \begin{macrocode}

function luaotfload.register_callbacks()
    callback.add('pre_linebreak_filter', nodes.simple_font_handler, 'luaotfload.pre_linebreak_filter')
    callback.add('hpack_filter',         nodes.simple_font_handler, 'luaotfload.hpack_filter')
    callback.reset('define_font')
    callback.add('define_font', fonts.define.read, 'luaotfload.define_font', 1)
    callback.add('find_vf_file', luaotfload.find_vf_file, 'luaotfload.find_vf_file')
end

function luaotfload.unregister_callbacks()
    callback.remove('pre_linebreak_filter', 'luaotfload.pre_linebreak_filter')
    callback.remove('hpack_filter', 'luaotfload.hpack_filter')
    callback.remove('define_font', 'luaotfload.define_font')
    callback.remove('find_vf_file', 'luaotfload.find_vf_file')
end
%    \end{macrocode}
% \iffalse
%</lua>
% \fi
%
% \section{\texttt{luaotfload.sty}}
%
% \iffalse
%<*package>
% \fi
%
%    Classical Plain+\LaTeX\ package initialization.
%
%    \begin{macrocode}
\csname ifluaotfloadloaded\endcsname
\let\ifluaotfloadloaded\endinput

\expandafter\ifx\csname ProvidesPackage\endcsname\relax
  \input luatextra.sty
\else
  \NeedsTeXFormat{LaTeX2e}
  \ProvidesPackage{luaotfload}%
    [2010/01/08 v1.07 ConTeXt font loading system]
  \RequirePackage{luatextra}
\fi

\expandafter\edef\csname otfl@AtEnd\endcsname{%
  \catcode64 \the\catcode64\relax
}

\catcode64 11

%    \end{macrocode}
%
%    The attributes are allocated here. The |otfl@| prefix is added to prevent
%    name collision.
%
%    \begin{macrocode}

\newluatexattribute\otfl@state
\newluatexattribute\otfl@markbase
\newluatexattribute\otfl@markdone
\newluatexattribute\otfl@markmark
\newluatexattribute\otfl@cursbase
\newluatexattribute\otfl@curscurs
\newluatexattribute\otfl@cursdone
\newluatexattribute\otfl@kernpair
\newluatexattribute\otfl@color

%    \end{macrocode}
%
%    Two small macros to register or unregister the callbacks. Without the
%    callbacks this package is totally turned off.
%
%    \begin{macrocode}

\def\otfl@off{
\luadirect{luaotfload.unregister_callbacks()}
}

\def\otfl@on{
\luadirect{luaotfload.register_callbacks()}
}

%    \end{macrocode}
%
%    We load the |lua| file, and we turn the package on.
%
%    \begin{macrocode}

\luatexUseModule{luaotfload}

\otfl@on

\otfl@AtEnd
%    \end{macrocode}
% \iffalse
%</package>
% \fi
% \Finale
\endinput