summaryrefslogtreecommitdiff
path: root/luaotfload.lua
blob: 70d89a2ba5e20980d2e0b6a060ee208dc2d67b88 (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
module("luaotfload", package.seeall)

luaotfload.module = {
    name          = "luaotfload",
    version       = 2.2,
    date          = "2013/04/15",
    description   = "OpenType layout system.",
    author        = "Elie Roux & Hans Hagen",
    copyright     = "Elie Roux",
    license       = "CC0"
}

local luatexbase = luatexbase

local type, next, dofile = type, next, dofile
local stringfind = string.find
local find_file  = kpse.find_file

local add_to_callback, create_callback =
      luatexbase.add_to_callback, luatexbase.create_callback
local reset_callback, call_callback =
      luatexbase.reset_callback, luatexbase.call_callback

local dummy_function = function () end

--[[doc--
No final decision has been made on how to handle font definition.  At
the moment, there are three candidates: The \identifier{generic}
callback as hard-coded in the font loader, the \identifier{old}
wrapper, and a simplified version of the latter (\identifier{patch})
that does nothing besides applying font patches.
--doc]]--
luaotfload.font_definer = "patch" --- | “generic” | “old”

local error, warning, info, log =
    luatexbase.provides_module(luaotfload.module)

--[[doc--
This is a necessary initalization in order not to rebuild an existing
font.
Maybe 600 should be replaced by \texmacro{pdfpkresolution} %% (why?)
or \luafunction{texconfig.pk_dpi} (and it should be replaced
dynamically), but we don't have access (yet) to the
\identifier{texconfig} table, so we let it be 600.
Anyway, it does still work fine even if \texmacro{pdfpkresolution} is
changed.
--doc]]--

kpse.init_prog("", 600, "/")

--[[doc--
We set the minimum version requirement for \LUATEX to v0.74, as it was
the first version to include version 5.2 of the \LUA interpreter.
--doc]]--

local luatex_version = 74

if tex.luatexversion < luatex_version then
    warning("LuaTeX v%.2f is old, v%.2f is recommended.",
             tex.luatexversion/100,
             luatex_version   /100)
end

--[[doc--
\subsection{Module loading}

We load the files imported from \CONTEXT with this function.
It automatically prepends the prefix \fileent{otfl-} to its argument,
so we can refer to the files with their actual \CONTEXT name.
--doc]]--

local fl_prefix = "otfl" -- “luatex” for luatex-plain
local loadmodule = function (name)
    local tofind = fl_prefix .."-"..name
    local found = find_file(tofind,"tex")
    if found then
        log("loading file %s.", found)
        dofile(found)
    else
        error("file %s not found.", tofind)
    end
end

--[[doc--
Virtual fonts are resolved via a callback.
\luafunction{find_vf_file} derives the name of the virtual font file
from the filename.
(NB: \CONTEXT handles this likewise in \fileent{font-vf.lua}.)
--doc]]--
local Cs, P, lpegmatch = lpeg.Cs, lpeg.P, lpeg.match

local p_dot, p_slash = P".",  P"/"
local p_suffix       = (p_dot * (1 - p_dot - p_slash)^1 * P(-1)) / ""
local p_removesuffix = Cs((p_suffix + 1)^1)

local find_vf_file = function (name)
    local fullname = find_file(name, "ovf")
    if not fullname then
        --fullname = find_file(file.removesuffix(name), "ovf")
        fullname = find_file(lpegmatch(p_removesuffix, name), "ovf")
    end
    if fullname then
        log("loading virtual font file %s.", fullname)
    end
    return fullname
end

--[[-- keep --]]
--- from Hans (all merged):

---   file name              modified  include name
--- × basics-gen.lua         t         luat-basics-gen
--- × font-def -> fonts-def  t         luatex-font-def (there’s also the normal font-def!)
--- × fonts-enc              f         luatex-font-enc
--- × fonts-ext              t         luatex-fonts-ext
--- × fonts-lua              f         luatex-fonts-lua
---   fonts-tfm              f         luatex-fonts-tfm
--- × fonts-cbk              f         luatex-fonts-lua

--- from Hans (unmerged):
---   font-otc.lua -> otfl-font-otc.lua

--- from luaotfload:
---   otfl-luat-ovr.lua    -- override some luat-dum functions
---   otfl-font-clr.lua
---   otfl-font-ltx.lua
---   otfl-font-nms.lua
---   otfl-font-pfb.lua    -- ?

--[[-- new --]]
--- basics-nod          (merged as fonts-nod !)
--- fonts-demo-vf-1.lua
--- fonts-syn           (merged)

--[[-- merged, to be dropped --]]
--- otfl-data-con.lua
--- otfl-font-cid.lua
--- otfl-font-con.lua
--- otfl-font-ini.lua
--- otfl-font-ota.lua
--- otfl-font-otb.lua
--- otfl-font-otf.lua
--- otfl-font-oti.lua
--- otfl-font-otn.lua

--[[doc--

\subsection{Preparing the Font Loader}
We treat the fontloader as a black box so behavior is consistent
between formats.
The wrapper file is \fileent{otfl-fonts.lua} which we imported from
\href{http://standalone.contextgarden.net/current/context/experimental/tex/generic/context/luatex/}{\LUATEX-Plain}.
It has roughly two purposes:

\begin{enumerate}

   \item insert the functionality required for fontloader; and

   \item put it in place via the respective callbacks.

\end{enumerate}

How the first step is executed depends on the presence on the
\emph{merged font loader code}.
In \identifier{luaotfload} this is contained in the file
\fileent{otfl-fonts-merged.lua}.
If this file cannot be found,  the original libraries from \CONTEXT of
which the merged code was composed are loaded instead.

Hans provides two global tables to control the font loader:

  \begin{itemize}
    \item  \luafunction{generic_context}:
           encapsulation mechanism, callback functions
    \item  \luafunction{non generic_context}:
           customized code insertion
  \end{itemize}


With \luafunction{non_generic_context} we can tailor the font loader
insertion to our file naming habits (key \luafunction{load_before}).
Additionally, \luafunction{skip_loading} can be unset to force loading
of the original libraries as though the merged code was absent.
Another key, \luafunction{load_after} is called at the time when the
font loader is actually inserted.
In combination with the option \luafunction{no_callbacks_yet} in
\luafunction{generic_context}, we can insert our own,
\identifier{luatexbase}-style callback handling here.
--doc]]--
if not _G.    generic_context then _G.    generic_context = { } end
if not _G.non_generic_context then _G.non_generic_context = { } end

local     generic_context =    generic_context
local non_generic_context =non_generic_context

generic_context.no_callbacks_yet = true

_G.non_generic_context = { luatex_fonts = {
        load_before     = "otfl-fonts-merged.lua",
        -- load_after      = nil, --- TODO, this is meant for callbacks
        skip_loading    = true,
}}

--[[doc--
The imported font loader will call \luafunction{callback.register} once
while reading \fileent{font-def.lua}.
This is unavoidable unless we modify the imported files, but harmless
if we make it call a dummy instead.
--doc]]--

local trapped_register = callback.register
callback.register      = dummy_function

--[[doc--
Now that things are sorted out we can finally load the fontloader.
--doc]]--

loadmodule"fonts.lua"

--[[doc--
By default, the fontloader requires a number of \emphasis{private
attributes} for internal use.
These must be kept consistent with the attribute handling methods as
provided by \identifier{luatexbase}.
Our strategy is to override the function that allocates new attributes
before we initialize the font loader, making it a wrapper around
\luafunction{luatexbase.new_attribute}.\footnote{%
    Many thanks, again, to Hans Hagen for making this part
    configurable!
}
The attribute identifiers are prefixed “\fileent{otfl@}” to
avoid name clashes.
--doc]]--

do
    local new_attribute = luatexbase.new_attribute
    local the_attributes = luatexbase.attributes

    _G.attributes = _G.attributes or { }

    _G.attributes.private = function (name)
        local attr   = "otfl@" .. name
        local number = the_attributes[attr]
        if not number then
            number = new_attribute(attr)
        end
        return number
    end
end

--[[doc--

\subsection{Callbacks}

After the fontloader is ready we can restore the callback trap from
\identifier{luatexbase}.
--doc]]--

callback.register = trapped_register

--[[doc--
We do our own callback handling with the means provided by luatexbase.

Note: \luafunction{pre_linebreak_filter} and \luafunction{hpack_filter}
are coupled in \CONTEXT in the concept of \emph{node processor}.
--doc]]--

add_to_callback("pre_linebreak_filter",
                generic_context.callback_pre_linebreak_filter,
                "luaotfload.node_processor",
                1)
add_to_callback("hpack_filter",
                generic_context.callback_hpack_filter,
                "luaotfload.node_processor",
                1)
add_to_callback("find_vf_file",
                find_vf_file, "luaotfload.find_vf_file")

loadmodule"font-otc.lua"   -- TODO check what we can drop from otfl-features

loadmodule"lib-dir.lua"    -- required by font-nms
loadmodule"luat-ovr.lua"

if fonts and fonts.readers.tfm then
  --------------------------------------------------------------------
  --- OFM; read this first
  --------------------------------------------------------------------
  --- I can’t quite make out whether this is still relevant
  --- as those ofm fonts always fail, even in the 2011 version
  --- (mktexpk:  don't know how to create bitmap font for omarabb.ofm)
  --- the font loader appears to read ofm like tfm so if this
  --- hack was supposed achieve that, we should excise it anyways
  fonts.readers.ofm  = fonts.readers.tfm
  fonts.handlers.ofm = fonts.handlers.tfm --- empty anyways
  fonts.formats.ofm  = fonts.formats.tfm  --- “type1”
  --- fonts.readers.sequence[#fonts.readers.sequence+1] = "ofm"
  --------------------------------------------------------------------
end

--[[doc--

Now we load the modules written for \identifier{luaotfload}.

--doc]]--
loadmodule"font-pfb.lua"    --- new in 2.0, added 2011
loadmodule"font-nms.lua"
loadmodule"font-clr.lua"
loadmodule"font-ltx.lua"    --- new in 2.0, added 2011

--[[doc--

We create a callback for patching fonts on the fly, to be used by other
packages.
It initially contains the empty function that we are going to override
below.

--doc]]--

create_callback("luaotfload.patch_font", "simple", dummy_function)

--[[doc--

This is a wrapper for the imported font loader.
As of 2013, everything it does appear to be redundand, so we won’t use
it unless somebody points out a cogent reason.
Nevertheless, it has been adapted to work with the current structure of
font data objects and will stay here for reference / until breakage is
reported.

\emphasis{TODO}
This one also enables patching fonts.
The current fontloader apparently comes with a dedicated mechanism for
that already: enhancers.
How those work remains to be figured out.

--doc]]--
local define_font_wrapper = function (...)
    --- we use “tfmdata” (not “fontdata”) for consistency with the
    --- font loader
    local tfmdata = fonts.definers.read(...)
    if type(tfmdata) == "table" and tfmdata.shared then
        local metadata = tfmdata.shared.rawdata.metadata
        local mathdata = metadata.math --- do all fonts have this field?
        if mathdata then
            local mathconstants = { } --- why new hash, not modify in place?
            local units_per_em  = metadata.units_per_em
            local size          = tfmdata.size
            for k,v in next, mathdata do
                --- afaics this is alread taken care of by
                --- definers.read
                if stringfind(k, "Percent") then
                    -- keep percent values as is
                    print(k,v)
                    mathconstants[k] = v
                else
                    mathconstants[k] = v / units_per_em * size
                end
            end
            --- for \overwithdelims
            --- done by definers.read as well
            mathconstants.FractionDelimiterSize             = 1.01 * size
            --- fontloader has 2.4 × size
            mathconstants.FractionDelimiterDisplayStyleSize = 2.39 * size
            tfmdata.MathConstants = mathconstants
        end
        call_callback("luaotfload.patch_font", tfmdata)
    end
    return tfmdata
end

--[[doc--

\subsection{\CONTEXT override}

We provide a simplified version of the original font definition
callback.

--doc]]--

local read_font_file = fonts.definers.read
local patch_defined_font = function (...)
    local tfmdata = read_font_file(...)-- spec -> size -> id -> tmfdata
    if type(tfmdata) == "table" then
        call_callback("luaotfload.patch_font", tfmdata)
    end
    return tfmdata
end

caches.compilemethod = "both"

reset_callback("define_font")

--[[doc--
Finally we register the callbacks
--doc]]--

if luaotfload.font_definer == "old"  then
  add_to_callback("define_font",
                  define_font_wrapper,
                  "luaotfload.define_font",
                  1)
elseif luaotfload.font_definer == "generic"  then
  add_to_callback("define_font",
                  generic_context.callback_define_font,
                  "luaotfload.define_font",
                  1)
elseif luaotfload.font_definer == "patch"  then
  add_to_callback("define_font",
                  patch_defined_font,
                  "luaotfload.define_font",
                  1)
end

loadmodule"features.lua"

-- vim:tw=71:sw=4:ts=4:expandtab