Debian Codegraph @

texmf-dist/tex/luatex/luaotfload/luaotfload-auxiliary.lua

raw
1 -----------------------------------------------------------------------
2 -- FILE: luaotfload-auxiliary.lua
3 -- DESCRIPTION: part of luaotfload
4 -- AUTHOR: Khaled Hosny, Élie Roux, Philipp Gesang
5 -----------------------------------------------------------------------
6
7 assert(luaotfload_module, "This is a part of luaotfload and should not be loaded independently") {
8 name = "luaotfload-auxiliary",
9 version = "3.29", --TAGVERSION
10 date = "2024-12-03", --TAGDATE
11 description = "luaotfload submodule / auxiliary functions",
12 license = "GPL v2.0"
13 }
14
15 luaotfload = luaotfload or { }
16 local log = luaotfload.log
17 local logreport = log.report
18 local fonthashes = fonts.hashes
19 local encodings = fonts.encodings
20 local identifiers = fonthashes.identifiers
21 local fontnames = fonts.names
22
23 local fontid = font.id
24 local texsprint = tex.sprint
25
26 local dofile = dofile
27 local getmetatable = getmetatable
28 local setmetatable = setmetatable
29 local utfcodepoint = utf8.codepoint
30 local stringlower = string.lower
31 local stringupper = string.upper
32 local stringformat = string.format
33 local stringgsub = string.gsub
34 local stringbyte = string.byte
35 local stringfind = string.find
36 local tablecopy = table.copy
37
38 local harf = luaotfload.harfbuzz
39 local GSUBtag, GPOStag
40 if harf then
41 GSUBtag = harf.Tag.new("GSUB")
42 GPOStag = harf.Tag.new("GPOS")
43 end
44
45 local aux = { }
46 local luaotfload_callbacks = { }
47
48 -----------------------------------------------------------------------
49 --- font patches
50 -----------------------------------------------------------------------
51
52 --- https://github.com/khaledhosny/luaotfload/issues/54
53
54 local function rewrite_fontname(tfmdata, specification)
55 local format = tfmdata.format or tfmdata.properties.format
56 if stringfind (specification, " ") then
57 tfmdata.name = stringformat ("%q", specification)
58 else
59 --- other specs should parse just fine
60 tfmdata.name = specification
61 end
62 end
63
64 local rewriting = false
65
66 function aux.start_rewrite_fontname()
67 if rewriting == false then
68 luatexbase.add_to_callback (
69 "luaotfload.patch_font",
70 rewrite_fontname,
71 "luaotfload.rewrite_fontname")
72 rewriting = true
73 logreport ("log", 1, "aux",
74 "start rewriting tfmdata.name field")
75 end
76 end
77
78 function aux.stop_rewrite_fontname()
79 if rewriting == true then
80 luatexbase.remove_from_callback
81 ("luaotfload.patch_font", "luaotfload.rewrite_fontname")
82 rewriting = false
83 logreport ("log", 1, "aux",
84 "stop rewriting tfmdata.name field")
85 end
86 end
87
88
89 --[[doc--
90 This sets two dimensions apparently relied upon by the unicode-math
91 package.
92 --doc]]--
93
94 local function set_sscale_dimens(fontdata)
95 local resources = fontdata.resources if not resources then return end
96 local mathconstants = resources.MathConstants if not mathconstants then return end
97 local parameters = fontdata.parameters if not parameters then return end
98 --- the default values below are complete crap
99 parameters [10] = mathconstants.ScriptPercentScaleDown or 70
100 parameters [11] = mathconstants.ScriptScriptPercentScaleDown or 50
101 end
102
103 luaotfload_callbacks [#luaotfload_callbacks + 1] = {
104 "patch_font", set_sscale_dimens, "set_sscale_dimens",
105 }
106
107 -- Starting with LuaTeX 1.11.2, this is a more reliable way of selecting the
108 -- right font than relying on psnames
109 luaotfload_callbacks [#luaotfload_callbacks + 1] = {
110 "patch_font", function(fontdata)
111 fontdata.subfont = fontdata.specification.sub or 1
112 end, "set_font_index",
113 }
114
115 local default_units = 1000
116
117 --- fontobj -> int
118 local function lookup_units(fontdata)
119 local units = fontdata.units
120 if units and units > 0 then return units end
121 local shared = fontdata.shared if not shared then return default_units end
122 local rawdata = shared.rawdata if not rawdata then return default_units end
123 local metadata = rawdata.metadata if not metadata then return default_units end
124 local capheight = metadata.capheight if not capheight then return default_units end
125 local units = metadata.units or fontdata.units
126 if not units or units == 0 then
127 return default_units
128 end
129 return units
130 end
131
132 --[[doc--
133 This callback corrects some values of the Cambria font.
134 --doc]]--
135 --- fontobj -> unit
136 local function patch_cambria_domh(fontdata)
137 local mathconstants = fontdata.MathConstants
138 if mathconstants and fontdata.psname == "CambriaMath" then
139 --- my test Cambria has 2048
140 local units = fontdata.units or lookup_units(fontdata)
141 local sz = fontdata.parameters.size or fontdata.size
142 local mh = 2800 / units * sz
143 if mathconstants.DisplayOperatorMinHeight < mh then
144 mathconstants.DisplayOperatorMinHeight = mh
145 end
146 end
147 end
148
149 luaotfload_callbacks [#luaotfload_callbacks + 1] = {
150 "patch_font", patch_cambria_domh, "patch_cambria_domh",
151 }
152
153
154 --[[doc--
155
156 Add missing field to fonts that lack it. Addresses issue
157 https://github.com/lualatex/luaotfload/issues/253
158
159 This is considered a hack, especially since importing the
160 unicode-math package fixes the problem quite nicely.
161
162 --doc]]--
163
164 --- fontobj -> unit
165 local function fixup_fontdata(data)
166 local t = type (data)
167 --- Some OT fonts like Libertine R lack the resources table, causing
168 --- the fontloader to nil-index.
169 if t == "table" then
170 if data and not data.resources then data.resources = { } end
171 end
172 end
173
174 luaotfload_callbacks [#luaotfload_callbacks + 1] = {
175 "patch_font_unsafe", fixup_fontdata, "fixup_fontdata",
176 }
177
178
179 --[[doc--
180
181 Comment from fontspec:
182
183 “Here we patch fonts tfm table to emulate \XeTeX's \cs{fontdimen8},
184 which stores the caps-height of the font. (Cf.\ \cs{fontdimen5} which
185 stores the x-height.)
186
187 Falls back to measuring the glyph if the font doesn't contain the
188 necessary information.
189 This needs to be extended for fonts that don't contain an `X'.”
190
191 --doc]]--
192
193 local capheight_reference_chars = { "X", "M", "Ж", "ξ", }
194 local capheight_reference_codepoints do
195 capheight_reference_codepoints = { }
196 for i = 1, #capheight_reference_chars do
197 local chr = capheight_reference_chars [i]
198 capheight_reference_codepoints [i] = utfcodepoint (chr)
199 end
200 end
201
202 local function determine_capheight(fontdata)
203 local parameters = fontdata.parameters if not parameters then return false end
204 local characters = fontdata.characters if not characters then return false end
205 --- Pretty simplistic but it does return *some* value for most fonts;
206 --- we could also refine the approach to return some kind of average
207 --- of all capital letters or a user-provided subset.
208 for i = 1, #capheight_reference_codepoints do
209 local refcp = capheight_reference_codepoints [i]
210 local refchar = characters [refcp]
211 if refchar then
212 logreport ("both", 4, "aux",
213 "picked height of character '%s' (U+%04X) as \\fontdimen8 \z
214 candidate",
215 capheight_reference_chars [i], refcp)
216 return refchar.height
217 end
218 end
219 return false
220 end
221
222 local function query_ascender(fontdata)
223 local parameters = fontdata.parameters if not parameters then return false end
224 local ascender = parameters.ascender
225 if ascender then
226 return ascender --- pre-scaled
227 end
228
229 local shared = fontdata.shared if not shared then return false end
230 local rawdata = shared.rawdata if not rawdata then return false end
231 local metadata = rawdata.metadata if not metadata then return false end
232 ascender = metadata.ascender if not ascender then return false end
233 local size = parameters.size if not size then return false end
234 local units = lookup_units (fontdata)
235 if not units or units == 0 then return false end
236 return ascender * size / units --- scaled
237 end
238
239 local function query_capheight(fontdata)
240 local parameters = fontdata.parameters if not parameters then return false end
241 local shared = fontdata.shared if not shared then return false end
242 local rawdata = shared.rawdata if not rawdata then return false end
243 local metadata = rawdata.metadata if not metadata then return false end
244 local capheight = metadata.capheight if not capheight then return false end
245 local size = parameters.size if not size then return false end
246 local units = lookup_units (fontdata)
247 if not units or units == 0 then return false end
248 return capheight * size / units
249 end
250
251 local function query_fontdimen8(fontdata)
252 local parameters = fontdata.parameters if not parameters then return false end
253 local fontdimen8 = parameters [8]
254 if fontdimen8 then return fontdimen8 end
255 return false
256 end
257
258 local function caphtfmt(ref, ht)
259 if not ht then return "<none>" end
260 if not ref then return tostring (ht) end
261 return stringformat ("%s(δ=%s)", ht, ht - ref)
262 end
263
264 local function set_capheight(fontdata)
265 if not fontdata then
266 logreport ("both", 0, "aux",
267 "error: set_capheight() received garbage")
268 return
269 end
270 local capheight_dimen8 = query_fontdimen8 (fontdata)
271 local capheight_alleged = query_capheight (fontdata)
272 local capheight_ascender = query_ascender (fontdata)
273 local capheight_measured = determine_capheight (fontdata)
274 logreport ("term", 4, "aux",
275 "capht: param[8]=%s advertised=%s ascender=%s measured=%s",
276 tostring (capheight_dimen8),
277 caphtfmt (capheight_dimen8, capheight_alleged),
278 caphtfmt (capheight_dimen8, capheight_ascender),
279 caphtfmt (capheight_dimen8, capheight_measured))
280 if capheight_dimen8 then --- nothing to do
281 return
282 end
283
284 local capheight = capheight_alleged or capheight_ascender or capheight_measured
285 if capheight then
286 fontdata.parameters [8] = capheight
287 end
288 end
289
290 luaotfload_callbacks [#luaotfload_callbacks + 1] = {
291 "patch_font", set_capheight, "set_capheight",
292 }
293
294 -- Of course there are also fonts with no sensible x-height, so let's add a
295 -- fallback there:
296 local function set_xheight(tfmdata)
297 local parameters = tfmdata.parameters
298 if not parameters then return end
299 if not ((parameters.x_height or parameters[5] or 0) == 0) then return end
300 if tfmdata.characters and tfmdata.characters[120] then
301 parameters.x_height = tfmdata.characters[120].height
302 else
303 parameters.x_height = (parameters.ascender or 0)/2
304 end
305 end
306 luaotfload_callbacks [#luaotfload_callbacks + 1] = {
307 "patch_font", set_xheight, "set_xheight",
308 }
309 -----------------------------------------------------------------------
310 --- glyphs and characters
311 -----------------------------------------------------------------------
312
313 --- int -> int -> bool
314 function aux.font_has_glyph(font_id, codepoint)
315 local fontdata = fonts.hashes.identifiers[font_id]
316 if fontdata then
317 if fontdata.characters[codepoint] ~= nil then return true end
318 end
319 return false
320 end
321
322 --- undocumented
323
324 local function raw_slot_of_name(font_id, glyphname)
325 local fontdata = font.fonts[font_id]
326 if fontdata.type == "virtual" then --- get base font for glyph idx
327 local codepoint = encodings.agl.unicodes[glyphname]
328 local glyph = fontdata.characters[codepoint]
329 if fontdata.characters[codepoint] then
330 return codepoint
331 end
332 end
333 return false
334 end
335
336 --[[doc--
337
338 This one is approximately “name_to_slot” from the microtype package;
339 note that it is all about Adobe Glyph names and glyph slots in the
340 font. The names and values may diverge from actual Unicode.
341
342 http://www.adobe.com/devnet/opentype/archives/glyph.html
343
344 The “unsafe” switch triggers a fallback lookup in the raw fonts
345 table. As some of the information is stored as references, this may
346 have unpredictable side-effects.
347
348 --doc]]--
349
350 --- int -> string -> bool -> (int | false)
351 function aux.slot_of_name(font_id, glyphname, unsafe)
352 if not font_id or type (font_id) ~= "number"
353 or not glyphname or type (glyphname) ~= "string"
354 then
355 logreport ("both", 0, "aux",
356 "invalid parameters to slot_of_name (%s, %s)",
357 tostring (font_id), tostring (glyphname))
358 return false
359 end
360
361 local tfmdata = identifiers [font_id]
362 if not tfmdata then return raw_slot_of_name (font_id, glyphname) end
363 local hbdata = tfmdata.hb
364 if hbdata then
365 local hbshared = hbdata.shared
366 local nominals = hbshared.nominals
367 local hbfont = hbshared.font
368
369 local gid = hbfont:get_glyph_from_name(glyphname)
370 if gid ~= nil then
371 return nominals[gid] or gid + hbshared.gid_offset
372 end
373 else
374 local resources = tfmdata.resources if not resources then return false end
375 local unicodes = resources.unicodes if not unicodes then return false end
376
377 local unicode = unicodes [glyphname]
378 if unicode then
379 if type (unicode) == "number" then
380 return unicode
381 else
382 return unicode [1] --- for multiple components
383 end
384 end
385 end
386 return false
387 end
388
389 --[[doc--
390
391 Inverse of above; not authoritative as to my knowledge the official
392 inverse of the AGL is the AGLFN. Maybe this whole issue should be
393 dealt with in a separate package that loads char-def.lua and thereby
394 solves the problem for the next couple decades.
395
396 http://partners.adobe.com/public/developer/en/opentype/aglfn13.txt
397
398 --doc]]--
399
400 local indices
401
402 --- int -> (string | false)
403 function aux.name_of_slot(codepoint)
404 if not codepoint or type (codepoint) ~= "number" then
405 logreport ("both", 0, "aux",
406 "invalid parameters to name_of_slot (%s)",
407 tostring (codepoint))
408 return false
409 end
410
411 if not indices then --- this will load the glyph list
412 local unicodes = encodings.agl.unicodes
413 if not unicodes or not next (unicodes)then
414 logreport ("both", 0, "aux",
415 "name_of_slot: failed to load the AGL.")
416 end
417 indices = table.swapped (unicodes)
418 end
419
420 local glyphname = indices [codepoint]
421 if glyphname then
422 return glyphname
423 end
424 return false
425 end
426
427 --[[doc--
428
429 Get the GID of the glyph associated with a given name.
430
431 --doc]]--
432 function aux.gid_of_name(font_id, glyphname)
433 local slot = aux.slot_of_name(font_id, glyphname)
434 if not slot then return end
435 local tfmdata = identifiers[font_id] or font.fonts[font_id]
436 -- assert(tfmdata) -- Otherwise slot_of_name would have failed already
437 return tfmdata.characters[slot].index or slot
438 end
439
440 -----------------------------------------------------------------------
441 --- features / scripts / languages
442 -----------------------------------------------------------------------
443 --- lots of arrowcode ahead
444
445 local function get_features(tfmdata)
446 local resources = tfmdata.resources if not resources then return false end
447 local features = resources.features if not features then return false end
448 return features
449 end
450
451 local function get_hbface(tfmdata)
452 if not tfmdata.hb then return end
453 return tfmdata.hb.shared.face
454 end
455
456 --[[doc--
457 This function, modeled after “check_script()” from fontspec, returns
458 true if in the given font, the script “asked_script” is accounted for in at
459 least one feature.
460 --doc]]--
461
462 --- int -> string -> bool
463 function aux.provides_script(font_id, asked_script)
464 if not font_id or type (font_id) ~= "number"
465 or not asked_script or type (asked_script) ~= "string"
466 then
467 logreport ("both", 0, "aux",
468 "invalid parameters to provides_script(%s, %s)",
469 tostring (font_id), tostring (asked_script))
470 return false
471 end
472 asked_script = stringlower(asked_script)
473 local tfmdata = identifiers[font_id]
474 if not tfmdata then
475 logreport ("log", 0, "aux", "no font with id %d", font_id)
476 return false
477 end
478 local hbface = get_hbface(tfmdata)
479 if hbface then
480 local script = harf.Tag.new(asked_script == "dflt" and "DFLT"
481 or asked_script)
482 for _, tag in next, { GSUBtag, GPOStag } do
483 if hbface:ot_layout_find_script(tag, script) then
484 return true
485 end
486 end
487 return false
488 else
489 local fontname = tfmdata.fontname
490 local features = get_features (tfmdata)
491 if features == false then
492 logreport ("log", 1, "aux", "font no %d lacks a features table", font_id)
493 return false
494 end
495 for method, featuredata in next, features do
496 --- where method: "gpos" | "gsub"
497 for feature, data in next, featuredata do
498 if data[asked_script] then
499 logreport ("log", 2, "aux",
500 "font no %d (%s) defines feature %s for script %s",
501 font_id, fontname, feature, asked_script)
502 return true
503 end
504 end
505 end
506 logreport ("log", 2, "aux",
507 "font no %d (%s) defines no feature for script %s",
508 font_id, fontname, asked_script)
509 end
510 return false
511 end
512
513 --[[doc--
514 This function, modeled after “check_language()” from fontspec, returns
515 true if in the given font, the language with tage “asked_language” is
516 accounted for in the script with tag “asked_script” in at least one
517 feature.
518 --doc]]--
519
520 --- int -> string -> string -> bool
521 function aux.provides_language(font_id, asked_script, asked_language)
522 if not font_id or type (font_id) ~= "number"
523 or not asked_script or type (asked_script) ~= "string"
524 or not asked_language or type (asked_language) ~= "string"
525 then
526 logreport ("both", 0, "aux",
527 "invalid parameters to provides_language(%s, %s, %s)",
528 tostring (font_id),
529 tostring (asked_script),
530 tostring (asked_language))
531 return false
532 end
533 asked_script = stringlower(asked_script)
534 local tfmdata = identifiers[font_id]
535 if not tfmdata then
536 logreport ("log", 0, "aux", "no font with id %d", font_id)
537 return false
538 end
539 local hbface = get_hbface(tfmdata)
540 if hbface then
541 asked_language = stringupper(asked_language)
542 if asked_language == "DFLT" then
543 return aux.provides_script(font_id, asked_script)
544 end
545 local script = harf.Tag.new(asked_script == "dflt" and "DFLT"
546 or asked_script)
547 local language = harf.Tag.new(asked_language == "DFLT" and "dflt"
548 or asked_language)
549 for _, tag in next, { GSUBtag, GPOStag } do
550 local _, script_idx = hbface:ot_layout_find_script(tag, script)
551 if hbface:ot_layout_find_language(tag, script_idx, language) then
552 return true
553 end
554 end
555 return false
556 else
557 asked_language = stringlower(asked_language)
558 local tfmdata = identifiers[font_id]
559 if not tfmdata then return false end
560 local fontname = tfmdata.fontname
561 local features = get_features (tfmdata)
562 if features == false then
563 logreport ("log", 1, "aux", "font no %d lacks a features table", font_id)
564 return false
565 end
566 for method, featuredata in next, features do
567 --- where method: "gpos" | "gsub"
568 for feature, data in next, featuredata do
569 local scriptdata = data[asked_script]
570 if scriptdata and scriptdata[asked_language] then
571 logreport ("log", 2, "aux",
572 "font no %d (%s) defines feature %s "
573 .. "for script %s with language %s",
574 font_id, fontname, feature,
575 asked_script, asked_language)
576 return true
577 end
578 end
579 end
580 logreport ("log", 2, "aux",
581 "font no %d (%s) defines no feature "
582 .. "for script %s with language %s",
583 font_id, fontname, asked_script, asked_language)
584 return false
585 end
586 end
587
588 --[[doc--
589 A function to check if a font is a variable font with a given axis.
590 --doc]]--
591
592 function aux.provides_axis(font_id, asked_axis)
593 if not font_id or type (font_id) ~= "number"
594 or not asked_axis or type (asked_axis) ~= "string"
595 then
596 logreport ("both", 0, "aux",
597 "invalid parameters to provides_axis(%s, %s)",
598 tostring (font_id), tostring (asked_axis))
599 return false
600 end
601 local tfmdata = identifiers[font_id]
602 if not tfmdata then
603 logreport ("log", 0, "aux", "no font with id %d", font_id)
604 return false
605 end
606 local hbface = get_hbface(tfmdata)
607 if hbface then
608 if not hbface:ot_var_has_data() then return false end
609
610 if #asked_axis <= 4 then
611 local tag = harf.Tag.new(asked_axis)
612 local info = hbface:ot_var_find_axis_info(tag)
613 if info then
614 return true -- TODO: More info?
615 end
616 end
617
618 asked_axis = stringlower(asked_axis)
619 local infos = hbface:ot_var_get_axis_infos()
620 for i=1, #infos do
621 local info = infos[i]
622 if hbface:get_name(info.name_id):lower() == asked_axis then
623 return true -- TODO: More info?
624 end
625 end
626 else
627 asked_axis = stringlower(asked_axis)
628 local resources = tfmdata.resources
629 local variabledata = resources and resources.variabledata
630 if not variabledata then return false end
631 local axes = variabledata.axis
632 if not axes then return false end -- Shouldn't happen
633 for i=1, #axes do
634 local axis = axes[i]
635 if axis.tag == asked_axis or axis.name == asked_axis then
636 return true -- TODO: More info?
637 end
638 end
639 end
640 return false
641 end
642
643 --[[doc--
644 We strip the syntax elements from feature definitions (shouldn’t
645 actually be there in the first place, but who cares ...)
646 --doc]]--
647
648 local lpeg = require"lpeg"
649 local C, P, S = lpeg.C, lpeg.P, lpeg.S
650 local lpegmatch = lpeg.match
651
652 local sign = S"+-"
653 local rhs = P"=" * P(1)^0 * P(-1)
654 local strip_garbage = sign^-1 * C((1 - rhs)^1)
655
656 --s = "+foo" --> foo
657 --ss = "-bar" --> bar
658 --sss = "baz" --> baz
659 --t = "foo=bar" --> foo
660 --tt = "+bar=baz" --> bar
661 --ttt = "-baz=true" --> baz
662 --
663 --print(lpeg.match(strip_garbage, s))
664 --print(lpeg.match(strip_garbage, ss))
665 --print(lpeg.match(strip_garbage, sss))
666 --print(lpeg.match(strip_garbage, t))
667 --print(lpeg.match(strip_garbage, tt))
668 --print(lpeg.match(strip_garbage, ttt))
669
670 --[[doc--
671 This function, modeled after “check_feature()” from fontspec, returns
672 true if in the given font, the language with tag “asked_language” is
673 accounted for in the script with tag “asked_script” in feature
674 “asked_feature”.
675 --doc]]--
676
677 --- int -> string -> string -> string -> bool
678 function aux.provides_feature(font_id, asked_script,
679 asked_language, asked_feature)
680 if not font_id or type (font_id) ~= "number"
681 or not asked_script or type (asked_script) ~= "string"
682 or not asked_language or type (asked_language) ~= "string"
683 or not asked_feature or type (asked_feature) ~= "string"
684 then
685 logreport ("both", 0, "aux",
686 "invalid parameters to provides_feature(%s, %s, %s, %s)",
687 tostring (font_id), tostring (asked_script),
688 tostring (asked_language), tostring (asked_feature))
689 return false
690 end
691 asked_script = stringlower(asked_script)
692 asked_feature = lpegmatch(strip_garbage, asked_feature)
693
694 local tfmdata = identifiers[font_id]
695 if not tfmdata then
696 logreport ("log", 0, "aux", "no font with id %d", font_id)
697 return false
698 end
699 local hbface = get_hbface(tfmdata)
700 if hbface then
701 asked_language = stringupper(asked_language)
702 local script = harf.Tag.new(asked_script == "dflt" and "DFLT"
703 or asked_script)
704 local language = harf.Tag.new(asked_language == "DFLT" and "dflt"
705 or asked_language)
706 local feature = harf.Tag.new(asked_feature)
707
708 for _, tag in next, { GSUBtag, GPOStag } do
709 local _, script_idx = hbface:ot_layout_find_script(tag, script)
710 local _, language_idx = hbface:ot_layout_find_language(tag, script_idx, language)
711 if hbface:ot_layout_find_feature(tag, script_idx, language_idx, feature) then
712 return true
713 end
714 end
715 return false
716 else
717 local fontname = tfmdata.fontname
718 asked_language = stringlower(asked_language)
719 local features = get_features (tfmdata)
720 if features == false then
721 logreport ("log", 1, "aux", "font no %d lacks a features table", font_id)
722 return false
723 end
724 for method, featuredata in next, features do
725 --- where method: "gpos" | "gsub"
726 local feature = featuredata[asked_feature]
727 if feature then
728 local scriptdata = feature[asked_script]
729 if scriptdata and scriptdata[asked_language] then
730 logreport ("log", 2, "aux",
731 "font no %d (%s) defines feature %s "
732 .. "for script %s with language %s",
733 font_id, fontname, asked_feature,
734 asked_script, asked_language)
735 return true
736 end
737 end
738 end
739 logreport ("log", 2, "aux",
740 "font no %d (%s) does not define feature %s for script %s with language %s",
741 font_id, fontname, asked_feature, asked_script, asked_language)
742 end
743 end
744
745 -----------------------------------------------------------------------
746 --- font dimensions
747 -----------------------------------------------------------------------
748
749 --- int -> string -> int
750 local function get_math_dimension(font_id, dimenname)
751 if type(font_id) == "string" then
752 font_id = fontid(font_id) --- safeguard
753 end
754 local fontdata = identifiers[font_id]
755 local mathdata = fontdata.mathparameters
756 if mathdata then
757 return mathdata[dimenname] or 0
758 end
759 return 0
760 end
761
762 aux.get_math_dimension = get_math_dimension
763
764 --- int -> string -> unit
765 function aux.sprint_math_dimension(font_id, dimenname)
766 if type(font_id) == "string" then
767 font_id = fontid(font_id)
768 end
769 local dim = get_math_dimension(font_id, dimenname)
770 texsprint(luatexbase.catcodetables["latex-package"], dim, "sp")
771 end
772
773 -----------------------------------------------------------------------
774 --- extra database functions
775 -----------------------------------------------------------------------
776
777 --[====[-- TODO -> port this to new db model
778
779 --- local directories -------------------------------------------------
780
781 --- migrated from luaotfload-database.lua
782 --- https://github.com/lualatex/luaotfload/pull/61#issuecomment-17776975
783
784 --- string -> (int * int)
785 local function scan_external_dir (dir)
786 local old_names, new_names = names.data()
787 if not old_names then
788 old_names = load_names()
789 end
790 new_names = tablecopy(old_names)
791 local n_scanned, n_new = scan_dir(dir, old_names, new_names)
792 --- FIXME
793 --- This doesn’t seem right. If a db update is triggered after this
794 --- point, then the added fonts will be saved along with it --
795 --- which is not as “temporarily” as it should be. (This should be
796 --- addressed during a refactoring of names_resolve().)
797 names.data = new_names
798 return n_scanned, n_new
799 end
800
801 aux.scan_external_dir = scan_external_dir
802
803 --]====]--
804
805 function aux.scan_external_dir()
806 print "ERROR: scan_external_dir() is not implemented"
807 end
808
809 --- db queries --------------------------------------------------------
810
811 --- https://github.com/lualatex/luaotfload/issues/74
812 --- string -> (string * int)
813 local function resolve_fontname(name)
814 local foundname, subfont = luaotfload.resolvers.name {
815 name = name,
816 specification = "name:" .. name,
817 }
818 if foundname then
819 return foundname, subfont
820 end
821 return false, false
822 end
823
824 aux.resolve_fontname = resolve_fontname
825
826 --- string list -> (string * int)
827 function aux.resolve_fontlist(names)
828 for n = 1, #names do
829 local foundname, subfont = resolve_fontname(this)
830 if foundname then
831 return foundname, subfont
832 end
833 end
834 return false, false
835 end
836
837 --- index access ------------------------------------------------------
838
839 --- Based on a discussion on the Luatex mailing list:
840 --- http://tug.org/pipermail/luatex/2014-June/004881.html
841
842 --[[doc--
843
844 aux.read_font_index -- Read the names index from the canonical
845 location and return its contents. This does not affect the behavior
846 of Luaotfload: The returned table is independent of what the font
847 resolvers use internally. Access is raw: each call to the function
848 will result in the entire table being re-read from disk.
849
850 --doc]]--
851
852 local load_names = fontnames.load
853 local access_font_index = fontnames.access_font_index
854
855 function aux.read_font_index()
856 return load_names (true) or { }
857 end
858
859 --[[doc--
860
861 aux.font_index -- Access Luaotfload’s internal database. If the
862 database hasn’t been loaded yet this will cause it to be loaded, with
863 all the possible side-effects like for instance creating the index
864 file if it doesn’t exist, reading all font files, &c.
865
866 --doc]]--
867
868 function aux.font_index() return access_font_index () end
869
870
871 --- loaded fonts ------------------------------------------------------
872
873 --- just a proof of concept
874
875 --- fontobj -> string list -> (string list) list
876 local function get_font_data (tfmdata, keys)
877 local acc = {}
878 for n = 1, #keys do
879 acc[n] = tfmdata[keys[n]] or false
880 end
881 return acc
882 end
883
884 --[[doc--
885
886 The next one operates on the fonts.hashes.identifiers table.
887 It returns a list containing tuples of font ids and the
888 contents of the fields specified in the first argument.
889 Font table entries that were created indirectly -- e.g. by
890 \letterspacefont or during font expansion -- will not be
891 listed.
892
893 --doc]]--
894
895 local default_keys = { "fullname" }
896
897 --- string list -> (int * string list) list
898 function aux.get_loaded_fonts (keys)
899 keys = keys or default_keys
900 local acc = {}
901 for id, tfmdata in pairs(identifiers) do
902 local data = get_font_data(tfmdata, keys)
903 acc[#acc+1] = { id, data }
904 end
905 return acc
906 end
907
908 --- Raw access to the font.* namespace is unsafe so no documentation on
909 --- this one.
910 function aux.get_raw_fonts ()
911 local res = { }
912 for i, v in font.each() do
913 if v.filename then
914 res[#res+1] = { i, v }
915 end
916 end
917 return res
918 end
919
920 -----------------------------------------------------------------------
921 --- font parameters
922 -----------------------------------------------------------------------
923 --- analogy of font-hsh
924
925 fonthashes.parameters = fonthashes.parameters or { }
926 fonthashes.quads = fonthashes.quads or { }
927
928 local parameters = fonthashes.parameters or { }
929 local quads = fonthashes.quads or { }
930
931 setmetatable(parameters, { __index = function (t, font_id)
932 local tfmdata = identifiers[font_id]
933 if not tfmdata then --- unsafe; avoid
934 tfmdata = font.fonts[font_id]
935 end
936 if tfmdata and type(tfmdata) == "table" then
937 local fontparameters = tfmdata.parameters
938 t[font_id] = fontparameters
939 return fontparameters
940 end
941 return nil
942 end})
943
944 --[[doc--
945
946 Note that the reason as to why we prefer functions over table indices
947 is that functions are much safer against unintended manipulation.
948 This justifies the overhead they cost.
949
950 --doc]]--
951
952 --- int -> (number | false)
953 function aux.get_quad(font_id)
954 local quad = quads[font_id]
955 if quad then
956 return quad
957 end
958 local fontparameters = parameters[font_id]
959 if fontparameters then
960 local quad = fontparameters.quad or 0
961 quads[font_id] = quad
962 return quad
963 end
964 return false
965 end
966
967 -----------------------------------------------------------------------
968 --- Script/language fixup
969 -----------------------------------------------------------------------
970 local otftables = fonts.constructors.handlers.otf.tables
971 local function setscript(tfmdata, value)
972 if value then
973 local cleanvalue = string.lower(value)
974 local scripts = otftables and otftables.scripts
975 local properties = tfmdata.properties
976 if not scripts then
977 properties.script = cleanvalue
978 elseif scripts[value] then
979 properties.script = cleanvalue
980 else
981 properties.script = "dflt"
982 end
983 end
984 local resources = tfmdata.resources
985 local features = resources and resources.features
986 if features then
987 local properties = tfmdata.properties
988 local script, language = properties.script, properties.language
989 local script_found, language_found = false, false
990 for _, data in next, features do for _, feature_data in next, data do
991 local scr = feature_data[script]
992 if scr then
993 script_found = true
994 if scr[language] then
995 language_found = true
996 goto double_break
997 end
998 end
999 end end
1000 ::double_break::
1001 if not script_found then properties.script = "dflt" end
1002 if not language_found then properties.language = "dflt" end
1003 end
1004 end
1005 fonts.constructors.features.otf.register {
1006 name = "script",
1007 initializers = {
1008 base = setscript,
1009 node = setscript,
1010 plug = setscript,
1011 },
1012 }
1013
1014 -----------------------------------------------------------------------
1015 --- initialization
1016 -----------------------------------------------------------------------
1017
1018 local function inject_callbacks (lst)
1019 if not lst and next (lst) then return false end
1020
1021 local function inject (def)
1022 local cb, fn, id = unpack (def)
1023 cb = tostring (cb)
1024 id = tostring (id)
1025 if not cb or not fn or not id or not type (fn) == "function" then
1026 logreport ("both", 0, "aux", "Invalid callback requested (%q, %s, %q).",
1027 cb, tostring (fn), id)
1028 return false
1029 end
1030 cb = stringformat ("luaotfload.%s", cb)
1031 id = stringformat ("luaotfload.aux.%s", id)
1032 logreport ("log", 5, "aux", "Installing callback %q->%q.", cb, id)
1033 luatexbase.add_to_callback (cb, fn, id)
1034 return true
1035 end
1036
1037 local ret = true
1038 for i = 1, #lst do ret = inject (lst [i]) end
1039 return ret
1040 end
1041
1042 return function ()
1043 luaotfload.aux = aux
1044 return inject_callbacks (luaotfload_callbacks)
1045 end
1046
1047 -- vim:tw=79:sw=2:ts=8:et
1048