summaryrefslogtreecommitdiff
path: root/luaotfload-auxiliary.lua
blob: be380e375df68d779f1a3769ea5d6f5e491c996c (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
#!/usr/bin/env texlua
-----------------------------------------------------------------------
--         FILE:  luaotfload-auxiliary.lua
--  DESCRIPTION:  part of luaotfload
-- REQUIREMENTS:  luaotfload 2.2
--       AUTHOR:  Khaled Hosny, Élie Roux, Philipp Gesang
--      VERSION:  2.2
--      CREATED:  2013-05-01 14:40:50+0200
-----------------------------------------------------------------------
--

--- this file addresses issue #24
--- https://github.com/lualatex/luaotfload/issues/24#

luaotfload                  = luaotfload or {}
luaotfload.aux              = luaotfload.aux or { }

config                      = config or { }
config.luaotfload           = config.luaotfload or { }

local aux           = luaotfload.aux
local log           = luaotfload.log
local warning       = luaotfload.log
local identifiers   = fonts.hashes.identifiers

local fontid        = font.id
local texsprint     = tex.sprint

local utf8          = unicode.utf8
local stringlower   = string.lower
local stringformat  = string.format
local stringgsub    = string.gsub
local stringbyte    = string.byte
local stringfind    = string.find

-----------------------------------------------------------------------
---                          font patches
-----------------------------------------------------------------------

--[[doc--

The font object (tfmdata) structure has changed since version 1.x, so
in case other packages haven’t been updated we put fallbacks in place
where they’d expect them. Specifically we have in mind:

  · fontspec
  · unicode-math
  · microtype (most likely fixed till TL2013)

--doc]]--

--- fontobj -> fontobj
local add_fontdata_fallbacks = function (fontdata)
  if type(fontdata) == "table" then
    local fontparameters = fontdata.parameters
    local metadata
    if not fontdata.shared then --- that would be a tfm
      --- we can’t really catch everything that
      --- goes wrong; for some reason, fontspec.lua
      --- just assumes it always gets an otf object,
      --- so its capheight callback, which does not
      --- bother to do any checks, will access
      --- fontdata.shared no matter what ...
      fontdata.units = fontdata.units_per_em

    else --- otf
      metadata = fontdata.shared.rawdata.metadata
      fontdata.name    = fontdata.name
                      or fontdata.fullname
                      or fontdata.psname
      fontdata.units   = fontdata.units_per_em
      fontdata.size = fontdata.size or fontparameters.size
      local resources  = fontdata.resources
      --- for legacy fontspec.lua and unicode-math.lua
      fontdata.shared.otfdata          = {
        pfminfo   = { os2_capheight = metadata.pfminfo.os2_capheight },
        metadata  = { ascent = metadata.ascent },
      }
      --- for microtype and fontspec
      local fake_features = { } -- wrong: table.copy(resources.features)
      setmetatable(fake_features, { __index = function (tab, idx)
        warning("some package (probably fontspec) is outdated")
        warning(
          "attempt to index " ..
          "tfmdata.shared.otfdata.luatex.features (%s)",
          idx)
        --os.exit(1)
        return nil --- empty anyways
      end})
      fontdata.shared.otfdata.luatex = {
        unicodes = resources.unicodes,
        features = fake_features,
      }
    end
  end
  return fontdata
end

--if config.luaotfload.compatibility == true then
if true then
  luatexbase.add_to_callback(
    "luaotfload.patch_font",
    add_fontdata_fallbacks,
    "luaotfload.fontdata_fallbacks")
end

--[[doc--

Additionally, the font registry is expected at fonts.identifiers
(fontspec) or fonts.ids (microtype), but in the meantime it has been
migrated to fonts.hashes.identifiers.  We’ll make luaotfload satisfy
those assumptions. (Maybe it’d be more appropriate to use
font.getfont() since Hans made it a harmless wrapper [1].)

[1] http://www.ntg.nl/pipermail/ntg-context/2013/072166.html

--doc]]--

fonts.identifiers = fonts.hashes.identifiers
fonts.ids         = fonts.hashes.identifiers

--[[doc--
This sets two dimensions apparently relied upon by the unicode-math
package.
--doc]]--

local set_sscale_dimens = function (fontdata)
  local mathconstants = fontdata.MathConstants
  local parameters    = fontdata.parameters
  if mathconstants then
    parameters[10] = mathconstants.ScriptPercentScaleDown or 70
    parameters[11] = mathconstants.ScriptScriptPercentScaleDown or 50
  end
  return fontdata
end

luatexbase.add_to_callback(
  "luaotfload.patch_font",
  set_sscale_dimens,
  "luaotfload.aux.set_sscale_dimens")

--- fontobj -> int
local lookup_units = function (fontdata)
  local metadata = fontdata.shared and fontdata.shared.rawdata.metadata
  if metadata and metadata.units_per_em then
    return metadata.units_per_em
  elseif fontdata.parameters and fontdata.parameters.units then
    return fontdata.parameters.units
  elseif fontdata.units then --- v1.x
    return fontdata.units
  end
  return 1000
end

--[[doc--
This callback corrects some values of the Cambria font.
--doc]]--
--- fontobj -> unit
local patch_cambria_domh = function (fontdata)
  local mathconstants = fontdata.MathConstants
  if mathconstants and fontdata.psname == "CambriaMath" then
    --- my test Cambria has 2048
    local units_per_em = fontdata.units_per_em or lookup_units(fontdata)
    local sz           = fontdata.parameters.size or fontdata.size
    local mh           = 2800 / units_per_em * sz
    if mathconstants.DisplayOperatorMinHeight < mh then
      mathconstants.DisplayOperatorMinHeight = mh
    end
  end
end

luatexbase.add_to_callback(
  "luaotfload.patch_font",
  patch_cambria_domh,
  "luaotfload.aux.patch_cambria_domh")

--[[doc--

Comment from fontspec:

 “Here we patch fonts tfm table to emulate \XeTeX's \cs{fontdimen8},
  which stores the caps-height of the font. (Cf.\ \cs{fontdimen5} which
  stores the x-height.)

  Falls back to measuring the glyph if the font doesn't contain the
  necessary information.
  This needs to be extended for fonts that don't contain an `X'.”

--doc]]--

local set_capheight = function (fontdata)
    local shared     = fontdata.shared
    local parameters = fontdata.parameters
    local capheight
    if shared then
      local units_per_em   = parameters.units
      local size           = parameters.size
      local os2_capheight  = shared.rawdata.metadata.pfminfo.os2_capheight

      if os2_capheight > 0 then
        capheight = os2_capheight / units_per_em * size
      else
        local X8 = stringbyte"X"
        if fontdata.characters[X8] then
          capheight = fontdata.characters[X8].height
        else
          capheight = parameters.ascender / units_per_em * size
        end
      end
    else
      local X8 = stringbyte"X"
      if fontdata.characters[X8] then
        capheight = fontdata.characters[X8].height
      end
    end
    if capheight then
      --- is this legit? afaics there’s nothing else on the
      --- array part of that table
      fontdata.parameters[8] = capheight
    end
end

luatexbase.add_to_callback(
  "luaotfload.patch_font",
  set_capheight,
  "luaotfload.aux.set_capheight")

-----------------------------------------------------------------------
---                             glyphs
-----------------------------------------------------------------------

--- int -> int -> bool
local font_has_glyph = function (font_id, codepoint)
  local fontdata = fonts.hashes.identifiers[font_id]
  if fontdata then
    if fontdata.characters[codepoint] ~= nil then return true end
  end
  return false
end

aux.font_has_glyph = font_has_glyph

--[[doc--

  This one is approximately “name_to_slot” from the microtype package;
  note that it is all about Adobe Glyph names and glyph slots in the
  font. The names and values may diverge from actual Unicode.

  http://www.adobe.com/devnet/opentype/archives/glyph.html

--doc]]--

--- int -> string -> (int | false)
local slot_of_name = function (font_id, glyphname)
  local fontdata = identifiers[font_id]
  if fontdata then
    local unicode = fontdata.resources.unicodes[glyphname]
    if unicode and type(unicode) == "number" then
      return unicode
    else
      return unicode[1] --- for multiple components
    end
  end
  return false
end

aux.slot_of_name = slot_of_name

--[[doc--

  Inverse of above; not authoritative as to my knowledge the official
  inverse of the AGL is the AGLFN. Maybe this whole issue should be
  dealt with in a separate package that loads char-def.lua and thereby
  solves the problem for the next couple decades.

  http://partners.adobe.com/public/developer/en/opentype/aglfn13.txt

--doc]]--

local indices

--- int -> (string | false)
local name_of_slot = function (codepoint)
  if not indices then --- this will load the glyph list
    local unicodes = fonts.encodings.agl.unicodes
    indices = table.swapped(unicodes)
  end
  local glyphname = indices[codepoint]
  if glyphname then
    return glyphname
  end
  return false
end

aux.name_of_slot      = name_of_slot

-----------------------------------------------------------------------
---                 features / scripts / languages
-----------------------------------------------------------------------
--- lots of arrowcode ahead

--[[doc--
This function, modeled after “check_script()” from fontspec, returns
true if in the given font, the script “asked_script” is accounted for in at
least one feature.
--doc]]--

--- int -> string -> bool
local provides_script = function (font_id, asked_script)
  asked_script = stringlower(asked_script)
  if font_id and font_id > 0 then
    local fontdata = identifiers[font_id].shared.rawdata
    if fontdata then
      local fontname = fontdata.metadata.fontname
      local features = fontdata.resources.features
      for method, featuredata in next, features do
        --- where method: "gpos" | "gsub"
        for feature, data in next, featuredata do
          if data[asked_script] then
            log(stringformat(
              "font no %d (%s) defines feature %s for script %s",
              font_id, fontname, feature, asked_script))
            return true
          end
        end
      end
      log(stringformat(
        "font no %d (%s) defines no feature for script %s",
        font_id, fontname, asked_script))
    end
  end
  log(stringformat("no font with id %d", font_id))
  return false
end

aux.provides_script = provides_script

--[[doc--
This function, modeled after “check_language()” from fontspec, returns
true if in the given font, the language with tage “asked_language” is
accounted for in the script with tag “asked_script” in at least one
feature.
--doc]]--

--- int -> string -> string -> bool
local provides_language = function (font_id, asked_script, asked_language)
  asked_script     = stringlower(asked_script)
  asked_language   = stringlower(asked_language)
  if font_id and font_id > 0 then
    local fontdata = identifiers[font_id].shared.rawdata
    if fontdata then
      local fontname = fontdata.metadata.fontname
      local features = fontdata.resources.features
      for method, featuredata in next, features do
        --- where method: "gpos" | "gsub"
        for feature, data in next, featuredata do
          local scriptdata = data[asked_script]
          if scriptdata and scriptdata[asked_language] then
            log(stringformat("font no %d (%s) defines feature %s "
                          .. "for script %s with language %s",
                             font_id, fontname, feature,
                             asked_script, asked_language))
            return true
          end
        end
      end
      log(stringformat(
        "font no %d (%s) defines no feature for script %s with language %s",
        font_id, fontname, asked_script, asked_language))
    end
  end
  log(stringformat("no font with id %d", font_id))
  return false
end

aux.provides_language = provides_language

--[[doc--
We strip the syntax elements from feature definitions (shouldn’t
actually be there in the first place, but who cares ...)
--doc]]--

local lpeg        = require"lpeg"
local C, P, S     = lpeg.C, lpeg.P, lpeg.S
local lpegmatch   = lpeg.match

local sign            = S"+-"
local rhs             = P"=" * P(1)^0 * P(-1)
local strip_garbage   = sign^-1 * C((1 - rhs)^1)

--s   = "+foo"        --> foo
--ss  = "-bar"        --> bar
--sss = "baz"         --> baz
--t   = "foo=bar"     --> foo
--tt  = "+bar=baz"    --> bar
--ttt = "-baz=true"   --> baz
--
--print(lpeg.match(strip_garbage, s))
--print(lpeg.match(strip_garbage, ss))
--print(lpeg.match(strip_garbage, sss))
--print(lpeg.match(strip_garbage, t))
--print(lpeg.match(strip_garbage, tt))
--print(lpeg.match(strip_garbage, ttt))

--[[doc--
This function, modeled after “check_feature()” from fontspec, returns
true if in the given font, the language with tag “asked_language” is
accounted for in the script with tag “asked_script” in feature
“asked_feature”.
--doc]]--

--- int -> string -> string -> string -> bool
local provides_feature = function (font_id,        asked_script,
                                   asked_language, asked_feature)
  asked_script    = stringlower(asked_script)
  asked_language  = stringlower(asked_language)
  asked_feature   = lpegmatch(strip_garbage, asked_feature)

  if font_id and font_id > 0 then
    local fontdata = identifiers[font_id].shared.rawdata
    if fontdata then
      local features = fontdata.resources.features
      local fontname = fontdata.metadata.fontname
      for method, featuredata in next, features do
        --- where method: "gpos" | "gsub"
        local feature = featuredata[asked_feature]
        if feature then
          local scriptdata = feature[asked_script]
          if scriptdata and scriptdata[asked_language] then
            log(stringformat("font no %d (%s) defines feature %s "
                          .. "for script %s with language %s",
                             font_id, fontname, asked_feature,
                             asked_script, asked_language))
            return true
          end
        end
      end
      log(stringformat(
        "font no %d (%s) does not define feature %s for script %s with language %s",
        font_id, fontname, asked_feature, asked_script, asked_language))
    end
  end
  log(stringformat("no font with id %d", font_id))
  return false
end

aux.provides_feature = provides_feature

-----------------------------------------------------------------------
---                         font dimensions
-----------------------------------------------------------------------

--- string -> string -> int
local get_math_dimension = function (csname, dimenname)
  local fontdata  = identifiers[fontid(csname)]
  local mathdata  = fontdata.mathparameters
  if mathdata then return mathdata[dimenname] or 0 end
  return 0
end

aux.get_math_dimension = get_math_dimension

--- string -> string -> unit
local sprint_math_dimension = function (csname, dimenname)
  local dim = get_math_dimension(csname, dimenname)
  texsprint(luatexbase.catcodetables["latex-package"], dim)
  texsprint(luatexbase.catcodetables["latex-package"], "sp")
end

aux.sprint_math_dimension = sprint_math_dimension

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