summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/latex/fontspec/fontspec.lua
blob: 9bbb1c78260f270da8adad553f1911a93b201692 (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
-- 
--  This is file `fontspec.lua',
--  generated with the docstrip utility.
-- 
--  The original source files were:
-- 
--  fontspec.dtx  (with options: `lua')
--  
--    _________________________________________
--    The fontspec package for XeLaTeX/LuaLaTeX
--    (C) 2004--2013    Will Robertson and Khaled Hosny
--  
--    License information appended.
--  
--  


fontspec          = fontspec or {}
local fontspec    = fontspec
fontspec.module   = {
    name          = "fontspec",
    version       = 2.4,
    date          = "2013/05/12",
    description   = "Advanced font selection for LuaLaTeX.",
    author        = "Khaled Hosny, Philipp Gesang",
    copyright     = "Khaled Hosny, Philipp Gesang",
    license       = "LPPL"
}

local err, warn, info, log = luatexbase.provides_module(fontspec.module)

fontspec.log     = log
fontspec.warning = warn
fontspec.error   = err

function fontspec.sprint (...)
    tex.sprint(luatexbase.catcodetables['latex-package'], ...)
end
local luaotfload_aux
if luaotfload and luaotfload.aux and luaotfload.aux.slot_of_name then
  luaotfload_aux = true
else
  luaotfload_aux = false
end

local check_script, check_language, check_feature, ids

if luaotfload_aux then
  check_script   = luaotfload.aux.provides_script
  check_language = luaotfload.aux.provides_language
  check_feature  = luaotfload.aux.provides_feature
else
    ids = fonts.identifiers or fonts.ids or fonts.hashes.identifiers -- first two are for backward compatibility
    function check_script(id, script)
        local s = string.lower(script)
        if id and id > 0 then
            local otfdata = ids[id].shared.rawdata
            if otfdata then
                local features = otfdata.resources.features
                for i,_ in pairs(features) do
                    for j,_ in pairs(features[i]) do
                        if features[i][j][s] then
                            fontspec.log("script '%s' exists in font '%s'",
                                          script, ids[id].fullname)
                            return true
                        end
                    end
                end
            end
        end
    end
    function check_language(id, script, language)
        local s = string.lower(script)
        local l = string.lower(language)
        if id and id > 0 then
            local otfdata = ids[id].shared.rawdata
            if otfdata then
                local features = otfdata.resources.features
                for i,_ in pairs(features) do
                    for j,_ in pairs(features[i]) do
                        if features[i][j][s] and features[i][j][s][l] then
                            fontspec.log("language '%s' for script '%s' exists in font '%s'",
                                          language, script, ids[id].fullname)
                            return true
                        end
                    end
                end
            end
        end
    end
    function check_feature(id, script, language, feature)
        local s = string.lower(script)
        local l = string.lower(language)
        local f = string.lower(feature:gsub("^[+-]", ""):gsub("=.*$", ""))
        if id and id > 0 then
            local otfdata = ids[id].shared.rawdata
            if otfdata then
                local features = otfdata.resources.features
                for i,_ in pairs(features) do
                    if features[i][f] and features[i][f][s] then
                        if features[i][f][s][l] == true then
                            fontspec.log("feature '%s' for language '%s' and script '%s' exists in font '%s'",
                                          feature, language, script, ids[id].fullname)
                            return true
                        end
                    end
                end
            end
        end
    end
end
local function tempswatrue()  fontspec.sprint([[\@tempswatrue]])  end
local function tempswafalse() fontspec.sprint([[\@tempswafalse]]) end
function fontspec.check_ot_script(fnt, script)
    if check_script(font.id(fnt), script) then
        tempswatrue()
    else
        tempswafalse()
    end
end
function fontspec.check_ot_lang(fnt, lang, script)
    if check_language(font.id(fnt), script, lang) then
        tempswatrue()
    else
        tempswafalse()
    end
end
function fontspec.check_ot_feat(fnt, feat, lang, script)
    for _, f in ipairs { "+trep", "+tlig", "+anum" } do
        if feat == f then
            tempswatrue()
            return
        end
    end
    if check_feature(font.id(fnt), script, lang, feat) then
        tempswatrue()
    else
        tempswafalse()
    end
end
local get_math_dimension
if luaotfload_aux then
    get_math_dimension = luaotfload.aux.get_math_dimension
else
    get_math_dimension = function(fnt, str)
        local mathdimens = ids[font.id(fnt)].MathConstants
        if mathdimens then
            return mathdimens[str]
        else
            return nil
        end
    end
end
function fontspec.mathfontdimen(fnt, str)
    local mathdimens = get_math_dimension(fnt, str)
    if mathdimens then
        fontspec.sprint(mathdimens)
        fontspec.sprint("sp")
    else
        fontspec.sprint("0pt")
    end
end
if not luaotfload_aux then
    local function set_capheight(fontdata)
        local capheight
        local shared = fontdata.shared
        if shared then
          local metadata
          local units_per_em
          local os2_capheight
          local size = fontdata.size or fontdata.parameters.size

          if shared.rawdata then --- new otfl
              metadata      = shared.rawdata.metadata
              units_per_em  = shared.rawdata.metadata.units_per_em
              os2_capheight = metadata.pfminfo.os2_capheight
          else --- legacy
              metadata      = shared.otfdata.metadata
              units_per_em  = fontdata.units_per_em or fontdata.units
                           or fontdata.parameters.units
              os2_capheight = shared.otfdata.pfminfo.os2_capheight
          end

          if os2_capheight > 0 then
              capheight = os2_capheight / units_per_em * size
          else
              local X8 = string.byte"X"
              if fontdata.characters[X8] then
                  capheight = fontdata.characters[X8].height
              else
                  capheight = metadata.ascent / units_per_em * size
              end
          end
        else
            local X8 = string.byte"X"
            if fontdata.characters[X8] then
                capheight = fontdata.characters[X8].height
            end
        end
        if capheight then
            fontdata.parameters[8] = capheight
        end
    end
    luatexbase.add_to_callback("luaotfload.patch_font", set_capheight, "fontspec.set_capheight")
end
--  
--  Copyright 2004--2013 Will Robertson <wspr81@gmail.com>
--  Copyright 2009--2013   Khaled Hosny <khaledhosny@eglug.org>
--  
--  Distributable under the LaTeX Project Public License,
--  version 1.3c or higher (your choice). The latest version of
--  this license is at: http://www.latex-project.org/lppl.txt
--  
--  This work is "author-maintained" by Will Robertson.
--  
--  This work consists of this file fontspec.dtx
--            and the derived files fontspec.sty,
--                                  fontspec.lua,
--                                  fontspec.cfg,
--                                  fontspec-xetex.tex,
--                                  fontspec-luatex.tex,
--                              and fontspec.pdf.
--  
-- 
--  End of file `fontspec.lua'.