summaryrefslogtreecommitdiff
path: root/luaotfload-deferred.lua
blob: efa02987568b6bddbff6098797972f2582f9c857 (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
--- TODO integrate into luaotfload.dtx

local type, next = type, next

local stringfind = string.find
--- this part is loaded after luatexbase
local loadmodule      = luaotfload.loadmodule
local luatexbase      = luatexbase
local generic_context = generic_context --- from fontloader

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

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

Note: \verb|pre_linebreak_filter| and \verb|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)

luaotfload.loadmodule("font-otc.lua")

loadmodule("lib-dir.lua")    -- required by font-nms; will change with lualibs update
loadmodule("font-nms.lua")
loadmodule("font-clr.lua")
--loadmodule("font-ovr.lua")
loadmodule("font-ltx.lua")

local dummy_function = function ( ) end --- upvalue more efficient than lambda
create_callback("luaotfload.patch_font", "simple", dummy_function)

--[[doc--
This is a wrapper for the imported font loader.
As of 2013, everything it does appears to be redundand, so we won’t use
it.
Nevertheless, it has been adapted to work with the current structure of
font data objects and will stay here for reference / until somebody
reports breakage.

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--
We provide a simplified version of the original font definition
callback.
--doc]]--
local patch_defined_font = function (...)
    local tfmdata = fonts.definers.read(...)
    if type(tfmdata) == "table" and tfmdata.shared then
        call_callback("luaotfload.patch_font", tfmdata)
    end
    --inspect(tfmdata.shared.features)
    return tfmdata
end

fonts.mode = "node"

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

reset_callback("define_font")

if luaotfload.font_definer == "old"  then
  add_to_callback("define_font",
                  old_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

--local register_base_sub = fonts.otf.features.register_base_substitution
--local gsubs = {
    --"ss01", "ss02", "ss03", "ss04", "ss05",
    --"ss06", "ss07", "ss08", "ss09", "ss10",
    --"ss11", "ss12", "ss13", "ss14", "ss15",
    --"ss16", "ss17", "ss18", "ss19", "ss20",
--}

--for _,v in next, gsubs do
    --register_base_sub(v)
--end
--add_to_callback("find_vf_file",
                 --fonts.vf.find,
                --"luaotfload.find_vf_file")

local set_sscale_diments = function (tfmdata)
    local mathconstants = tfmdata.MathConstants
    if mathconstants then
        local tfmparameters = tfmdata.parameters
        if mathconstants.ScriptPercentScaleDown then
            tfmparameters[10] = mathconstants.ScriptPercentScaleDown
        else -- resort to plain TeX default
            tfmparameters[10] = 70
        end
        if mathconstants.ScriptScriptPercentScaleDown then
            tfmparameters[11] = mathconstants.ScriptScriptPercentScaleDown
        else -- resort to plain TeX default
            tfmparameters[11] = 50
        end
    end
end

add_to_callback("luaotfload.patch_font",
                set_sscale_diments,
                "unicodemath.set_sscale_diments")

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

--  End of File `luaotfload.lua'.