summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/luatex/luaotfload/luaotfload.lua
blob: 343abc5f8307dab964b32aa406a2f622b35f8438 (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
-- 
--  This is file `luaotfload.lua',
--  generated with the docstrip utility.
-- 
--  The original source files were:
-- 
--  luaotfload.dtx  (with options: `lua')
--  This is a generated file.
--  
--  Copyright (C) 2009-2013
--       by  Elie Roux      <elie.roux@telecom-bretagne.eu>
--       and Khaled Hosny   <khaledhosny@eglug.org>
--       and Philipp Gesang <philipp.gesang@alumni.uni-heidelberg.de>
--  
--       Home:      https://github.com/lualatex/luaotfload
--       Support:   <lualatex-dev@tug.org>.
--  
--  This work is under the GPL v2.0 license.
--  
--  This work consists of the main source file luaotfload.dtx
--  and the derived files
--      luaotfload.sty, luaotfload.lua
--  
luaotfload                  = luaotfload or {}
local luaotfload            = luaotfload

config                            = config or { }
config.luaotfload                 = config.luaotfload or { }
------.luaotfload.resolver        = config.luaotfload.resolver  or "normal"
config.luaotfload.resolver        = config.luaotfload.resolver  or "cached"
config.luaotfload.definer         = config.luaotfload.definer   or "patch"
config.luaotfload.loglevel        = config.luaotfload.loglevel  or 1
config.luaotfload.color_callback  = config.luaotfload.color_callback  or "pre_linebreak_filter"
--luaotfload.prefer_merge     = config.luaotfload.prefer_merge or true

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

local luatexbase = luatexbase

local type, next       = type, next
local setmetatable     = setmetatable
local find_file        = kpse.find_file
local lfsisfile        = lfs.isfile
local stringfind       = string.find
local stringformat     = string.format
local stringmatch      = string.match
local stringsub        = string.sub

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


luaotfload.font_definer = "patch" --- | ^^e2^^80^^9cgeneric^^e2^^80^^9d | ^^e2^^80^^9cold^^e2^^80^^9d

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

luaotfload.error        = error
luaotfload.warning      = warning
luaotfload.info         = info
luaotfload.log          = log


local luatex_version = 76

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


local fl_prefix = "luaotfload" -- ^^e2^^80^^9cluatex^^e2^^80^^9d for luatex-plain
local loadmodule = function (name)
    require(fl_prefix .."-"..name)
end

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


local starttime = os.gettimeofday()

local trapped_register  = callback.register
callback.register       = dummy_function


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

    attributes = attributes or { }

    attributes.private = function (name)
        local attr   = "luaotfload@" .. name --- used to be: ^^e2^^80^^9cotfl@^^e2^^80^^9d
        local number = the_attributes[attr]
        if not number then
            number = new_attribute(attr)
        end
        return number
    end
end


local context_environment = { }

local push_namespaces = function ()
    log("push namespace for font loader")
    local normalglobal = { }
    for k, v in next, _G do
        normalglobal[k] = v
    end
    return normalglobal
end

local pop_namespaces = function (normalglobal, isolate)
    if normalglobal then
        local _G = _G
        local mode = "non-destructive"
        if isolate then mode = "destructive" end
        log("pop namespace from font loader -- " .. mode)
        for k, v in next, _G do
            if not normalglobal[k] then
                context_environment[k] = v
                if isolate then
                    _G[k] = nil
                end
            end
        end
        for k, v in next, normalglobal do
            _G[k] = v
        end
        -- just to be sure:
        setmetatable(context_environment,_G)
    else
        log("irrecoverable error during pop_namespace: no globals to restore")
        os.exit()
    end
end

luaotfload.context_environment  = context_environment
luaotfload.push_namespaces      = push_namespaces
luaotfload.pop_namespaces       = pop_namespaces

local our_environment = push_namespaces()


tex.attribute[0] = 0


loadmodule"merged.lua"
---loadmodule"font-odv.lua" --- <= Devanagari support from Context

if fonts then

    if not fonts._merge_loaded_message_done_ then
        --- a program talking first person -- HH sure believes in strong AI ...
        log[[^^e2^^80^^9cI am using the merged version of 'luaotfload.lua' here. If]]
        log[[ you run into problems or experience unexpected behaviour,]]
        log[[ and if you have ConTeXt installed you can try to delete the]]
        log[[ file 'luaotfload-font-merged.lua' as I might then use the]]
        log[[ possibly updated libraries. The merged version is not]]
        log[[ supported as it is a frozen instance. Problems can be]]
        log[[ reported to the ConTeXt mailing list.^^e2^^80^^9d]]
    end
    fonts._merge_loaded_message_done_ = true

else--- the loading sequence is known to change, so this might have to
    --- be updated with future updates!
    --- do not modify it though unless there is a change to the merged
    --- package!
    loadmodule("l-lua.lua")
    loadmodule("l-lpeg.lua")
    loadmodule("l-function.lua")
    loadmodule("l-string.lua")
    loadmodule("l-table.lua")
    loadmodule("l-io.lua")
    loadmodule("l-file.lua")
    loadmodule("l-boolean.lua")
    loadmodule("l-math.lua")
    loadmodule("util-str.lua")
    loadmodule('luatex-basics-gen.lua')
    loadmodule('data-con.lua')
    loadmodule('luatex-basics-nod.lua')
    loadmodule('font-ini.lua')
    loadmodule('font-con.lua')
    loadmodule('luatex-fonts-enc.lua')
    loadmodule('font-cid.lua')
    loadmodule('font-map.lua')
    loadmodule('luatex-fonts-syn.lua')
    loadmodule('luatex-fonts-tfm.lua')
    loadmodule('font-oti.lua')
    loadmodule('font-otf.lua')
    loadmodule('font-otb.lua')
    loadmodule('node-inj.lua')
    loadmodule('font-ota.lua')
    loadmodule('font-otn.lua')
    loadmodule('font-otp.lua')--- since 2013-04-23
    loadmodule('luatex-fonts-lua.lua')
    loadmodule('font-def.lua')
    loadmodule('luatex-fonts-def.lua')
    loadmodule('luatex-fonts-ext.lua')
    loadmodule('luatex-fonts-cbk.lua')
end --- non-merge fallback scope


pop_namespaces(our_environment, false)-- true)

log("fontloader loaded in %0.3f seconds", os.gettimeofday()-starttime)


callback.register = trapped_register


add_to_callback("pre_linebreak_filter",
                nodes.simple_font_handler,
                "luaotfload.node_processor",
                1)
add_to_callback("hpack_filter",
                nodes.simple_font_handler,
                "luaotfload.node_processor",
                1)
add_to_callback("find_vf_file",
                find_vf_file, "luaotfload.find_vf_file")

loadmodule"lib-dir.lua"   --- required by luaofload-database.lua
loadmodule"override.lua"  --- ^^e2^^80^^9cluat-ovr^^e2^^80^^9d

logs.set_loglevel(config.luaotfload.loglevel)

loadmodule"loaders.lua"    --- ^^e2^^80^^9cfont-pfb^^e2^^80^^9d new in 2.0, added 2011
loadmodule"database.lua"   --- ^^e2^^80^^9cfont-nms^^e2^^80^^9d
loadmodule"colors.lua"     --- ^^e2^^80^^9cfont-clr^^e2^^80^^9d


local request_resolvers   = fonts.definers.resolvers
local formats             = fonts.formats
formats.ofm               = "type1"

request_resolvers.file = function (specification)
    local found = fonts.names.crude_file_lookup(specification.name)
    --local found = fonts.names.crude_file_lookup_verbose(specification.name)
    specification.name = found[1]
    --if format then specification.forced = format end
end


--request_resolvers.anon = request_resolvers.name

local type1_formats = { "tfm", "ofm", }

request_resolvers.anon = function (specification)
    local name = specification.name
    for i=1, #type1_formats do
        local format = type1_formats[i]
        if resolvers.findfile(name, format) then
            specification.name = file.addsuffix(name, format)
            return
        end
    end
    request_resolvers.name(specification)
end

request_resolvers.path = function (specification)
    local exists, _ = lfsisfile(specification.name)
    if not exists then -- resort to file: lookup
        request_resolvers.file(specification)
    end
end


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


local read_font_file = fonts.definers.read

--- spec -> size -> id -> tmfdata
local patch_defined_font = function (specification, size, id)
    local tfmdata = read_font_file(specification, size, id)
    if type(tfmdata) == "table" and tfmdata.shared then
        --- We need to test for the ^^e2^^80^^9cshared^^e2^^80^^9d field here
        --- or else the fontspec capheight callback will
        --- operate on tfm fonts.
        call_callback("luaotfload.patch_font", tfmdata)
    end
    return tfmdata
end

caches.compilemethod = "both"

reset_callback("define_font")


local font_definer = config.luaotfload.definer

if font_definer == "generic"  then
  add_to_callback("define_font",
                  fonts.definers.read,
                  "luaotfload.define_font",
                  1)
elseif font_definer == "patch"  then
  add_to_callback("define_font",
                  patch_defined_font,
                  "luaotfload.define_font",
                  1)
end

loadmodule"features.lua"    --- contains what was ^^e2^^80^^9cfont-ltx^^e2^^80^^9d and ^^e2^^80^^9cfont-otc^^e2^^80^^9d
loadmodule"auxiliary.lua"   --- additionaly high-level functionality (new)

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

-- 
--  End of File `luaotfload.lua'.