Debian Codegraph @

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

raw
1 -----------------------------------------------------------------------
2 -- FILE: luaotfload-features.lua
3 -- DESCRIPTION: part of luaotfload / font features
4 -----------------------------------------------------------------------
5
6 assert(luaotfload_module, "This is a part of luaotfload and should not be loaded independently") {
7 name = "luaotfload-features",
8 version = "3.29", --TAGVERSION
9 date = "2024-12-03", --TAGDATE
10 description = "luaotfload submodule / features",
11 license = "GPL v2.0",
12 author = "Hans Hagen, Khaled Hosny, Elie Roux, Philipp Gesang, Marcel Krüger",
13 copyright = "PRAGMA ADE / ConTeXt Development Team",
14 }
15
16 local type = type
17 local next = next
18 local tonumber = tonumber
19
20 local lpeg = require "lpeg"
21 local lpegmatch = lpeg.match
22 local P = lpeg.P
23 local R = lpeg.R
24 local S = lpeg.S
25 local C = lpeg.C
26
27 local lower = string.lower
28
29 local table = table
30 local tabletohash = table.tohash
31 local tablesort = table.sort
32
33 --- this appears to be based in part on luatex-fonts-def.lua
34
35 local fonts = fonts
36 local definers = fonts.definers
37 local handlers = fonts.handlers
38 local fontidentifiers = fonts.hashes and fonts.hashes.identifiers
39 local otf = handlers.otf
40
41 local config = config or { luaotfload = { run = { } } }
42
43 local as_script = not config.luaotfload.run.live
44 local normalize
45
46 if as_script then
47 function normalize(features)
48 return {
49 axis = features and features.axis,
50 instance = features and features.instance,
51 }
52 end
53 else
54 normalize = otf.features.normalize
55 end
56
57 local log = luaotfload.log
58 local report = log.report
59
60 local stringgsub = string.gsub
61 local stringformat = string.format
62 local stringis_empty = string.is_empty
63
64 local function cmp_by_idx (a, b) return a.idx < b.idx end
65
66 local defined_combos = 0
67
68 local function handle_combination (combo, spec, size)
69 defined_combos = defined_combos + 1
70 if not combo [1] then
71 report ("both", 0, "features",
72 "combo %d: Empty font combination requested.",
73 defined_combos)
74 return false
75 end
76
77 if not fontidentifiers then
78 fontidentifiers = fonts.hashes and fonts.hashes.identifiers
79 end
80
81 local chain = { }
82 local fontids = { }
83 local n = #combo
84
85 tablesort (combo, cmp_by_idx)
86
87 --- pass 1: skim combo and resolve fonts
88 report ("both", 2, "features", "combo %d: combining %d fonts.",
89 defined_combos, n)
90 for i = 1, n do
91 local cur = combo [i]
92 local id = cur.id
93 local idx = cur.idx
94 local fnt = fontidentifiers [id]
95 if fnt then
96 local chars = cur.chars
97 if chars == true then
98 report ("both", 2, "features",
99 " *> %.2d: fallback font %d at rank %d.",
100 i, id, idx)
101 else
102 report ("both", 2, "features",
103 " *> %.2d: include font %d at rank %d (%d items).",
104 i, id, idx, (chars and #chars or 0))
105 end
106 chain [#chain + 1] = { fnt, chars, idx = idx }
107 fontids [#fontids + 1] = { id = id }
108 else
109 report ("both", 0, "features",
110 " *> %.2d: font %d at rank %d unknown, skipping.",
111 n, id, idx)
112 --- TODO might instead attempt to define the font at this point
113 --- but that’d require some modifications to the syntax
114 end
115 end
116
117 local nc = #chain
118 if nc == 0 then
119 report ("both", 0, "features",
120 " *> no valid font (of %d) in combination.", n)
121 return false
122 end
123
124 local basefnt = chain [1] [1]
125 if nc == 1 then
126 report ("both", 0, "features",
127 " *> combination boils down to a single font (%s) \z
128 of %d initially specified; not pursuing this any \z
129 further.", basefnt.fullname, n)
130 return basefnt
131 end
132
133 local basechar = basefnt.characters
134 local baseprop = basefnt.properties
135 baseprop.name = spec
136 baseprop.virtualized = true
137 basefnt.fonts = fontids
138
139 for i = 2, nc do
140 local cur = chain [i]
141 local fnt = cur [1]
142 local def = cur [2]
143 local src = fnt.characters
144 local cnt = 0
145
146 local function pickchr (uc, unavailable)
147 local chr = src [uc]
148 if unavailable == true and basechar [uc] then
149 --- fallback mode: already known
150 return
151 end
152 if chr then
153 chr.commands = { { "slot", i, uc } }
154 basechar [uc] = chr
155 cnt = cnt + 1
156 end
157 end
158
159 if def == true then --> fallback; grab all currently unavailable
160 for uc, _chr in next, src do pickchr (uc, true) end
161 else --> grab only defined range
162 for j = 1, #def do
163 local this = def [j]
164 if type (this) == "number" then
165 report ("both", 2, "features",
166 " *> [%d][%d]: import codepoint U+%.4X",
167 i, j, this)
168 pickchr (this)
169 elseif type (this) == "table" then
170 local lo, hi = unpack (this)
171 report ("both", 2, "features",
172 " *> [%d][%d]: import codepoint range U+%.4X--U+%.4X",
173 i, j, lo, hi)
174 for uc = lo, hi do pickchr (uc) end
175 else
176 report ("both", 0, "features",
177 " *> item no. %d of combination definition \z
178 %d not processable.", j, i)
179 end
180 end
181 end
182 report ("both", 2, "features",
183 " *> font %d / %d: imported %d glyphs into combo.",
184 i, nc, cnt)
185 end
186 return {
187 lookup = "combo",
188 file = basefnt.filename,
189 name = stringformat ("luaotfload<%d>", defined_combos),
190 specification = spec,
191 features = { normal = { } },
192 forced = "evl",
193 eval = function () return basefnt end,
194 size = size,
195 }
196 end
197
198 ---[[ begin excerpt from font-ott.lua ]]
199
200 local function swapped (h)
201 local r = { }
202 for k, v in next, h do
203 r[stringgsub(v,"[^a-z0-9]","")] = k -- is already lower
204 end
205 return r
206 end
207
208 local tables = otf.tables
209 local scripts = tables.scripts
210 local languages = tables.languages
211 local verbosescripts = swapped(scripts )
212 local verboselanguages = swapped(languages)
213
214 ---[[ end excerpt from font-ott.lua ]]
215
216 --[[doc--
217
218 As discussed, we will issue a warning because of incomplete support
219 when one of the scripts below is requested.
220
221 Reference: https://github.com/lualatex/luaotfload/issues/31
222
223 --doc]]--
224
225 local support_incomplete = tabletohash({
226 -- "deva",
227 "beng", "guru", "gujr",
228 "orya", "taml", "telu", "knda",
229 "mlym", "sinh",
230 }, true)
231
232 --[[doc--
233
234 Which features are active by default depends on the script
235 requested.
236
237 --doc]]--
238
239 --- (string, string) dict -> (string, string) dict
240 local function apply_default_features (rawlist)
241 local speclist = {}
242 for k, v in pairs(rawlist) do
243 if type(v) == 'string' then
244 v = ({['true'] = true, ['false'] = false})[lower(v)] or v
245 end
246 speclist[k] = v
247 end
248 local default_features = luaotfload.features
249
250 speclist = speclist or { }
251 speclist[""] = nil --- invalid options stub
252
253 --- handle language tag
254 local language = speclist.language
255 if language then
256 language = stringgsub(lower(language), "[^a-z0-9]", "")
257 language = rawget(verboselanguages, language) -- srsly, rawget?
258 or (languages[language] and language)
259 or "dflt"
260 else
261 language = "dflt"
262 end
263 speclist.language = language
264
265 --- handle script tag
266 local script = speclist.script
267 if script then
268 script = stringgsub(lower(script), "[^a-z0-9]","")
269 script = rawget(verbosescripts, script)
270 or (scripts[script] and script)
271 or "dflt"
272 if support_incomplete[script] then
273 report("log", 0, "features",
274 "Support for the requested script: "
275 .. "%q may be incomplete.", script)
276 end
277 else
278 script = "dflt"
279 end
280 speclist.script = script
281
282 report("log", 2, "features",
283 "Auto-selecting default features for script: %s.",
284 script)
285
286 local requested = default_features.defaults[script]
287 if not requested then
288 report("log", 2, "features",
289 "No default features for script %q, falling back to \"dflt\".",
290 script)
291 requested = default_features.defaults.dflt
292 end
293
294 for feat, state in next, requested do
295 if speclist[feat] == nil then speclist[feat] = state end
296 end
297
298 for feat, state in next, default_features.global do
299 --- This is primarily intended for setting node
300 --- mode unless “base” is requested, as stated
301 --- in the manual.
302 if speclist[feat] == nil then speclist[feat] = state end
303 end
304 return speclist
305 end
306
307 local import_values = {
308 --- That’s what the 1.x parser did, not quite as graciously,
309 --- with an array of branch expressions.
310 -- "style", "optsize",--> from slashed notation; handled otherwise
311 { "lookup", false },
312 { "sub", false },
313 }
314
315 local supported = {
316 b = "b",
317 i = "i",
318 bi = "bi",
319 r = "r",
320 aat = false,
321 icu = false,
322 gr = false,
323 }
324
325 --- (string | (string * string) | bool) list -> (string * number)
326 local function handle_slashed (modifiers)
327 local style, optsize
328 for i=1, #modifiers do
329 local mod = modifiers[i]
330 if type(mod) == "table" and mod[1] == "optsize" then --> optical size
331 optsize = tonumber(mod[2])
332 elseif mod == false then
333 --- ignore
334 report("log", 0, "features", "unsupported font option: %s", v)
335 elseif supported[mod] then
336 style = supported[mod]
337 elseif not stringis_empty(mod) then
338 style = stringgsub(mod, "[^%a%d]", "")
339 end
340 end
341 return style, optsize
342 end
343
344 local extract_subfont
345 do
346 local eof = P(-1)
347 --- Theoretically a valid subfont address can be up to ten
348 --- digits long. Additionally we allow names
349 local sub_expr = P"(" * C((1 - S"()")^1) * P")" * eof
350 local full_path = C(P(1 - sub_expr)^1)
351 extract_subfont = full_path * sub_expr
352 end
353
354 local function analyze(spec_string, size)
355 local request = lpegmatch(luaotfload.parsers.font_request,
356 spec_string)
357 ----inspect(request)
358 if not request then
359 --- happens when called with an absolute path
360 --- in an anonymous lookup;
361 --- we try to behave as friendly as possible
362 --- just go with it ...
363 report("log", 1, "features", "invalid request %q of type anon",
364 spec_string)
365 report("log", 1, "features",
366 "use square bracket syntax or consult the documentation.")
367 --- The result of \fontname must be re-feedable into \font
368 --- which is expected by the Latex font mechanism. Now this
369 --- is complicated with TTC fonts that need to pass the
370 --- number of the requested subfont along with the file name.
371 --- Thus we test whether the request is a bare path only or
372 --- ends in a subfont expression (decimal digits inside
373 --- parentheses).
374 --- https://github.com/lualatex/luaotfload/issues/57
375 local fullpath, sub = lpegmatch(extract_subfont,
376 spec_string)
377 if fullpath and sub then
378 return {
379 lookup = 'path',
380 sub = tonumber(sub) or sub,
381 name = fullpath,
382 size = size,
383 }
384 else
385 return {
386 lookup = 'path',
387 name = spec_string,
388 size = size,
389 }
390 end
391 end
392
393 local lookup, name = request.lookup, request.name
394 if lookup == "combo" then
395 return handle_combination (name, spec_string, size)
396 end
397
398 local features = {
399 raw = request.features or {}
400 }
401
402 local specification = {
403 specification = spec_string,
404 size = size,
405 lookup = lookup,
406 name = name,
407 sub = request.sub or false,
408 features = features,
409 }
410
411 if lookup == 'id' then
412 local original_font = fontidentifiers [request.id]
413 if not original_font then return end
414 local original_spec = original_font.specification
415 if not original_spec then return end
416 if size < 0 then
417 specification.size = original_spec.size * size // -1000
418 end
419 specification.lookup = original_spec.lookup
420 specification.name = original_spec.name
421 specification.sub = specification.sub or original_spec.sub
422 specification.style = original_spec.style
423 specification.optsize = original_spec.optsize
424 specification.forced = original_spec.forced
425 if original_spec.features then
426 features.raw = table.merged(original_spec.features.raw, features.raw)
427 end
428 end
429
430 local processed_features = apply_default_features(features.raw)
431
432 if request.modifiers then
433 local style, optsize = handle_slashed(request.modifiers)
434 specification.style, specification.optsize = style, optsize
435 end
436
437 for n=1, #import_values do
438 local feat = import_values[n][1]
439 local keep = import_values[n][2]
440 local newvalue = processed_features[feat]
441 if newvalue then
442 specification[feat] = newvalue
443 if not keep then
444 processed_features[feat] = nil
445 end
446 end
447 end
448
449 --- The next line sets the “rand” feature to “random”; I haven’t
450 --- investigated it any further (luatex-fonts-ext), so it will
451 --- just stay here.
452 features.normal = normalize (processed_features)
453 if features.normal.instance then
454 if features.normal.axis then
455 report("term and log", 0, "features", "instance and axis provided, instance will be ignored")
456 else
457 specification.instance = features.normal.instance
458 end
459 end
460
461 local forced_mode = request.features and request.features.mode
462 if forced_mode then
463 forced_mode = lower(forced_mode)
464 if fonts.readers[forced_mode] then
465 specification.forced = forced_mode
466 end
467 end
468
469 return specification
470 end
471
472 fonts.definers.analyze = analyze
473
474 if as_script == true then --- skip the remainder of the file
475 report ("log", 5, "features",
476 "Exiting early from luaotfload-features.lua.")
477 return
478 end
479
480 do
481 local helpers = otf.readers.helpers
482 local axistofactors = helpers.axistofactors
483 local cleanname = helpers.cleanname
484 local getaxisscale = helpers.getaxisscale
485 local function search(table, term, key_field, value_field)
486 if not table then return end
487 term = cleanname(term)
488 for i=1, #table do
489 local entry = table[i]
490 if cleanname(entry[key_field]) == term then
491 return entry[value_field]
492 end
493 end
494 end
495 function helpers.getfactors(tfmdata, instance) -- `instance` might refer to an `axis` value here
496 assert(instance == true or type(instance) == "string", "Fontloader changed interface of helpers.getfactors. This is a bug, please notify the luaotfload maintainers.")
497 local variabledata = tfmdata.variabledata
498 if not variabledata then return end
499 local instances = variabledata.instances
500 local axis = variabledata.axis
501 local designaxis = variabledata.designaxis
502 local segments = variabledata.segments
503 if not axis then return end
504 local factors = {}
505 if instance == true then
506 for i=1, #axis do
507 local cur = axis[i]
508 local default = cur.default
509 factors[i] = getaxisscale(segments, cur.minimum, default, cur.maximum, default)
510 end
511 return factors
512 end
513 local values = search(instances, instance, "subfamily", "values")
514 if values then
515 for i=1, #axis do
516 local cur = axis[i]
517 factors[i] = getaxisscale(segments, cur.minimum, cur.default, cur.maximum, values[i].value)
518 end
519 return factors
520 end
521 values = axistofactors(instance)
522 for i=1, #axis do
523 local cur = axis[i]
524 local default = cur.default
525 local value = cur.name and values[cur.name] or values[cur.tag]
526 value = tonumber(value) or (value and search(search(designaxis, cur.tag, "tag", "variants"), cleanname(value), "name", "value")) or default
527 factors[i] = getaxisscale(segments, cur.minimum, default, cur.maximum, value)
528 end
529 return factors
530 end
531
532 -- Additionally we patch trytosharefont to ensure that variable fonts work
533 -- with default values whenever no explicit values are passed.
534 local original_trytosharefont = fonts.constructors.trytosharefont
535 function fonts.constructors.trytosharefont(target, tfmdata)
536 original_trytosharefont(target, tfmdata)
537 if not target.streamprovider and tfmdata.resources.variabledata then
538 local format = tfmdata.properties.format
539 target.streamprovider = format == 'opentype' and 1 or format == 'truetype' and 2 or 0
540 end
541 end
542 end
543
544 -- We assume that the other otf stuff is loaded already; though there’s
545 -- another check below during the initialization phase.
546
547
548 local tlig_specification = {
549 {
550 type = "substitution",
551 features = everywhere,
552 data = {
553 --- quotedblright:
554 --- " (QUOTATION MARK) → ” (RIGHT DOUBLE QUOTATION MARK)
555 [0x0022] = 0x201D,
556
557 --- quoteleft:
558 --- ' (APOSTROPHE) → ’ (RIGHT SINGLE QUOTATION MARK)
559 [0x0027] = 0x2019,
560
561 --- quoteright:
562 --- ` (GRAVE ACCENT) → ‘ (LEFT SINGLE QUOTATION MARK)
563 [0x0060] = 0x2018,
564 },
565 flags = noflags,
566 order = { "tlig" },
567 prepend = true,
568 },
569 {
570 type = "ligature",
571 features = everywhere,
572 data = {
573
574 --- endash:
575 --- [--] (HYPHEN-MINUS, HYPHEN-MINUS) → – (EN DASH)
576 [0x2013] = {0x002D, 0x002D},
577
578 --- emdash:
579 --- [---] (HYPHEN-MINUS, HYPHEN-MINUS, HYPHEN-MINUS) → — (EM DASH)
580 [0x2014] = {0x002D, 0x002D, 0x002D},
581
582 --- quotedblleft:
583 --- [''] (GRAVE ACCENT, GRAVE ACCENT) → “ (LEFT DOUBLE QUOTATION MARK)
584 [0x201C] = {0x0060, 0x0060},
585
586 --- quotedblright:
587 --- [``] (APOSTROPHE, APOSTROPHE) → ” (RIGHT DOUBLE QUOTATION MARK)
588 [0x201D] = {0x0027, 0x0027},
589
590 --- exclamdown:
591 --- [!'] (EXCLAMATION MARK, GRAVE ACCENT) → ¡ (INVERTED EXCLAMATION MARK)
592 [0x00A1] = {0x0021, 0x0060},
593
594 --- questiondown:
595 --- [?'] (QUESTION MARK, GRAVE ACCENT) → ¡ (INVERTED EXCLAMATION MARK)
596 [0x00BF] = {0x003F, 0x0060},
597
598 --- next three originate in T1 encoding (Xetex applies them too)
599 --- quotedblbase:
600 --- [,,] (COMMA, COMMA) → ¡ (DOUBLE LOW-9 QUOTATION MARK)
601 [0x201E] = {0x002C, 0x002C},
602
603 --- LEFT-POINTING DOUBLE ANGLE QUOTATION MARK:
604 --- [,,] (LESS-THAN SIGN, LESS-THAN SIGN) → ¡ (LEFT-POINTING ANGLE QUOTATION MARK)
605 [0x00AB] = {0x003C, 0x003C},
606
607 --- RIGHT-POINTING DOUBLE ANGLE QUOTATION MARK:
608 --- [,,] (GREATER-THAN SIGN, GREATER-THAN SIGN) → ¡ (RIGHT-POINTING DOUBLE ANGLE QUOTATION MARK)
609 [0x00BB] = {0x003E, 0x003E},
610 },
611 flags = noflags,
612 order = { "tlig" },
613 prepend = true,
614 },
615 }
616
617 -- "substitution" features allow to replace individual characters with other
618 -- characters. This is often used inside of fonts to e.g. implement stylistic
619 -- sets.
620 -- As an example how such features can be added in the fontloader, we have a
621 -- simple implementation of the ROT13 cipher as a font feature. It can be
622 -- used by enabling the `rot13` font feature in a font, but we are not aware of
623 -- any practical usecase for this.
624 local rot13_specification = {
625 type = "substitution",
626 features = everywhere,
627 data = {
628 [65] = 78, [ 97] = 110, [78] = 65, [110] = 97,
629 [66] = 79, [ 98] = 111, [79] = 66, [111] = 98,
630 [67] = 80, [ 99] = 112, [80] = 67, [112] = 99,
631 [68] = 81, [100] = 113, [81] = 68, [113] = 100,
632 [69] = 82, [101] = 114, [82] = 69, [114] = 101,
633 [70] = 83, [102] = 115, [83] = 70, [115] = 102,
634 [71] = 84, [103] = 116, [84] = 71, [116] = 103,
635 [72] = 85, [104] = 117, [85] = 72, [117] = 104,
636 [73] = 86, [105] = 118, [86] = 73, [118] = 105,
637 [74] = 87, [106] = 119, [87] = 74, [119] = 106,
638 [75] = 88, [107] = 120, [88] = 75, [120] = 107,
639 [76] = 89, [108] = 121, [89] = 76, [121] = 108,
640 [77] = 90, [109] = 122, [90] = 77, [122] = 109,
641 },
642 flags = noflags,
643 order = { "rot13" },
644 prepend = true,
645 }
646
647 local interrolig_specification = {
648 { type = "ligature", data = { [0x203d] = {0x21, 0x3f}, [0x2e18] = {0xa1, 0xbf}, }, },
649 { type = "ligature", data = { [0x203d] = {0x3f, 0x21}, [0x2e18] = {0xbf, 0xa1}, }, },
650 }
651
652 local autofeatures = {
653 --- always present with Luaotfload; anum for Arabic and Persian is
654 --- predefined in font-otc.
655 { "tlig" , tlig_specification , "tex ligatures and substitutions" },
656 { "rot13", rot13_specification, "rot13" },
657 { "!!??", interrolig_specification, "interrobang substitutions" },
658 }
659
660 local function add_auto_features ()
661 local nfeats = #autofeatures
662 report ("both", 5, "features",
663 "auto-installing %d feature definitions", nfeats)
664 for i = 1, nfeats do
665 local name, spec, desc = unpack (autofeatures [i])
666 spec.description = desc
667 otf.addfeature (name, spec)
668 end
669 end
670
671 luaotfload.apply_default_features = apply_default_features
672
673 do
674 local function mathparaminitializer(tfmdata, value, features)
675 if not next(tfmdata.mathparameters) then return end
676 if value == 'auto' then
677 if features.script == 'math' then return end
678 end
679 tfmdata.mathparameters = {}
680 end
681 fonts.constructors.features.otf.register {
682 name = 'nomathparam',
683 description = 'Set Math parameters based on this font',
684 default = 'auto',
685 initializers = {
686 base = mathparaminitializer,
687 node = mathparaminitializer,
688 -- plug = mathparaminitializer,
689 },
690 }
691 end
692
693 do
694 local function mathfontdimen(tfmdata, _, value)
695 if not (tfmdata.mathparameters and next(tfmdata.mathparameters)) then return end
696 local parameters = tfmdata.parameters
697 local mathparameters = tfmdata.mathparameters
698 if value == 'xetex' then
699 parameters[10] = mathparameters.ScriptPercentScaleDown
700 parameters[11] = mathparameters.ScriptScriptPercentScaleDown
701 parameters[12] = mathparameters.DelimitedSubFormulaMinHeight
702 parameters[13] = mathparameters.DisplayOperatorMinHeight
703 parameters[14] = mathparameters.MathLeading
704 parameters[15] = mathparameters.AxisHeight
705 parameters[16] = mathparameters.AccentBaseHeight
706 parameters[17] = mathparameters.FlattenedAccentBaseHeight
707 parameters[18] = mathparameters.SubscriptShiftDown
708 parameters[19] = mathparameters.SubscriptTopMax
709 parameters[20] = mathparameters.SubscriptBaselineDropMin
710 parameters[21] = mathparameters.SuperscriptShiftUp
711 parameters[22] = mathparameters.SuperscriptShiftUpCramped
712 parameters[23] = mathparameters.SuperscriptBottomMin
713 parameters[24] = mathparameters.SuperscriptBaselineDropMax
714 parameters[25] = mathparameters.SubSuperscriptGapMin
715 parameters[26] = mathparameters.SuperscriptBottomMaxWithSubscript
716 parameters[27] = mathparameters.SpaceAfterScript
717 parameters[28] = mathparameters.UpperLimitGapMin
718 parameters[29] = mathparameters.UpperLimitBaselineRiseMin
719 parameters[30] = mathparameters.LowerLimitGapMin
720 parameters[31] = mathparameters.LowerLimitBaselineDropMin
721 parameters[32] = mathparameters.StackTopShiftUp
722 parameters[33] = mathparameters.StackTopDisplayStyleShiftUp
723 parameters[34] = mathparameters.StackBottomShiftDown
724 parameters[35] = mathparameters.StackBottomDisplayStyleShiftDown
725 parameters[36] = mathparameters.StackGapMin
726 parameters[37] = mathparameters.StackDisplayStyleGapMin
727 parameters[38] = mathparameters.StretchStackTopShiftUp
728 parameters[39] = mathparameters.StretchStackBottomShiftDown
729 parameters[40] = mathparameters.StretchStackGapAboveMin
730 parameters[41] = mathparameters.StretchStackGapBelowMin
731 parameters[42] = mathparameters.FractionNumeratorShiftUp
732 parameters[43] = mathparameters.FractionNumeratorDisplayStyleShiftUp
733 parameters[44] = mathparameters.FractionDenominatorShiftDown
734 parameters[45] = mathparameters.FractionDenominatorDisplayStyleShiftDown
735 parameters[46] = mathparameters.FractionNumeratorGapMin
736 parameters[47] = mathparameters.FractionNumeratorDisplayStyleGapMin
737 parameters[48] = mathparameters.FractionRuleThickness
738 parameters[49] = mathparameters.FractionDenominatorGapMin
739 parameters[50] = mathparameters.FractionDenominatorDisplayStyleGapMin
740 parameters[51] = mathparameters.SkewedFractionHorizontalGap
741 parameters[52] = mathparameters.SkewedFractionVerticalGap
742 parameters[53] = mathparameters.OverbarVerticalGap
743 parameters[54] = mathparameters.OverbarRuleThickness
744 parameters[55] = mathparameters.OverbarExtraAscender
745 parameters[56] = mathparameters.UnderbarVerticalGap
746 parameters[57] = mathparameters.UnderbarRuleThickness
747 parameters[58] = mathparameters.UnderbarExtraDescender
748 parameters[59] = mathparameters.RadicalVerticalGap
749 parameters[60] = mathparameters.RadicalDisplayStyleVerticalGap
750 parameters[61] = mathparameters.RadicalRuleThickness
751 parameters[62] = mathparameters.RadicalExtraAscender
752 parameters[63] = mathparameters.RadicalKernBeforeDegree
753 parameters[64] = mathparameters.RadicalKernAfterDegree
754 parameters[65] = mathparameters.RadicalDegreeBottomRaisePercent
755 -- parameters[66] = mathparameters.MinConnectorOverlap
756 -- parameters[67] = mathparameters.SubscriptShiftDownWithSuperscript
757 -- parameters[68] = mathparameters.FractionDelimiterSize
758 -- parameters[69] = mathparameters.FractionDelimiterDisplayStyleSize
759 -- parameters[70] = mathparameters.NoLimitSubFactor
760 -- parameters[71] = mathparameters.NoLimitSupFactor
761 elseif value == 'tex2' then
762 parameters[8] = mathparameters.FractionNumeratorDisplayStyleShiftUp
763 parameters[9] = mathparameters.FractionNumeratorShiftUp
764 parameters[10] = mathparameters.StackTopShiftUp
765 parameters[11] = mathparameters.FractionDenominatorDisplayStyleShiftDown
766 parameters[12] = mathparameters.FractionDenominatorShiftDown
767 parameters[13] = mathparameters.SuperscriptShiftUp
768 parameters[14] = mathparameters.SuperscriptShiftUp
769 parameters[15] = mathparameters.SuperscriptShiftUpCramped
770 parameters[16] = mathparameters.SubscriptShiftDown
771 parameters[17] = mathparameters.SubscriptShiftDown
772 parameters[18] = mathparameters.SuperscriptBaselineDropMax
773 parameters[19] = mathparameters.SubscriptBaselineDropMin
774 parameters[20] = mathparameters.FractionDelimiterDisplayStyleSize
775 parameters[21] = mathparameters.FractionDelimiterSize
776 parameters[22] = mathparameters.AxisHeight
777 elseif value == 'tex3' then
778 parameters[8] = mathparameters.Defa
779 parameters[9] = mathparameters.UpperLimitGapMin
780 parameters[10] = mathparameters.LowerLimitGapMin
781 parameters[11] = mathparameters.UpperLimitBaselineRiseMin
782 parameters[12] = mathparameters.LowerLimitBaselineDropMin
783 parameters[13] = 0
784 end
785 end
786 fonts.constructors.features.otf.register {
787 name = 'mathfontdimen',
788 description = 'Set fontdimen values for compatibility with other engines',
789 manipulators = {
790 base = mathfontdimen,
791 -- node = mathfontdimen,
792 -- plug = mathfontdimen,
793 },
794 }
795 end
796
797 do
798 local function restore(tfmdata, value, features)
799 if not tfmdata.properties.monospaced then return end
800 if features.fixedspace then return end -- In this case, 'auto' is true
801 local parameters = tfmdata.parameters
802 local space = parameters.space
803 parameters.space_stretch, parameters.space_shrink = space/2, space/3
804 end
805 fonts.constructors.features.otf.register {
806 name = 'internal__variablespace',
807 default = true,
808 initializers = {
809 base = restore,
810 node = restore,
811 },
812 }
813 local function node_fixedspace(tfmdata, value, features)
814 if value == 'auto' then return end
815 if tfmdata.properties.monospaced then return end -- handled by internal__variablespace
816 local parameters = tfmdata.parameters
817 parameters.space_stretch, parameters.space_shrink = 0, 0
818 end
819 local hb = luaotfload.harfbuzz
820 local post_tag = hb and hb.Tag.new'post'
821 local function harf_fixedspace(tfmdata, value, features)
822 if value == 'auto' then
823 -- We have to determine if we have a monospace font.
824 -- Let's be honest, it would be boring if that were easy.
825 local post_table = tfmdata.hb.shared.face:get_table(post_tag):get_data()
826 if #post_table < 16 then
827 -- Invalid OpenType font... Let's assume that it's not
828 -- monospaced:
829 return
830 end
831 local monospaced = string.unpack('>I4', post_table, 13) ~= 0
832 if not monospaced then return end -- FIXME: How to determine?
833 end
834 local parameters = tfmdata.parameters
835 parameters.space_stretch, parameters.space_shrink = 0, 0
836 end
837 fonts.constructors.features.otf.register {
838 name = 'fixedspace',
839 description = 'Do not stretch or shrink spaces',
840 default = 'auto',
841 initializers = {
842 base = node_fixedspace,
843 node = node_fixedspace,
844 plug = harf_fixedspace,
845 },
846 }
847 end
848
849 local uni_normalize = require'lua-uni-normalize'.direct
850 local normalize_lookup = setmetatable({}, {__index = function(t, f)
851 local fontdir = assert(font.getfont(f))
852 local normalize_func = t[fontdir]
853 local characters = fontdir.characters
854 local function result(head)
855 return normalize_func(head, f, characters, true)
856 end
857 t[f] = result
858 return result
859 end})
860 -- When this is loaded as part of luaotfload-tool, then we can't access nodes
861 -- and therefore uni_normalize doesn't exists. In that case we don't need it
862 -- anyway, so just skip it then.
863 local normalize_funcs = uni_normalize and {
864 nfc = uni_normalize.NFC,
865 nfd = uni_normalize.NFD,
866 nfkd = uni_normalize.NFKD,
867 }
868 fonts.constructors.features.otf.register {
869 name = 'normalize',
870 default = 'nfc',
871 description = 'Normalize text to NFC before shaping',
872 manipulators = {
873 node = function(fonttable, _, value)
874 if value == true then
875 value = 'nfc'
876 end
877 local func = normalize_funcs[value]
878 if not func then
879 report ("report", 0, "features",
880 "Unsupported normalization method replaced by NFC")
881 func = normalize_funcs.nfc
882 end
883 normalize_lookup[fonttable] = func
884 end,
885 },
886 processors = {
887 position = 1,
888 node = function(head, f, _, _, _)
889 return normalize_lookup[f](head)
890 end,
891 },
892 }
893 require'luaotfload-arabic'
894
895 -- mathsize feature for compatibility with older fontloader versions
896 -- Not all that useful in most cases since it leads to messy font sizes,
897 -- especially reloading a font using this feature based on it's \fontname
898 -- will load a wrong size.
899 -- Only supported for base mode since the other modes don't make sense for
900 -- math fonts.
901 local calculatescale = fonts.constructors.calculatescale
902 function fonts.constructors.calculatescale(tfmdata, size, _, spec)
903 local parameters = tfmdata.parameters
904 local sizepercentage = parameters and parameters.sizepercentage
905 if sizepercentage then
906 size = size * sizepercentage / 100
907 end
908 return calculatescale(tfmdata, size, _, spec)
909 end
910
911 fonts.constructors.features.otf.register {
912 name = 'mathsize',
913 description = 'Scale math fonts based on their scriptpercentage parameters',
914 initializers = {
915 base = function(tfmdata, mathsize)
916 local mathdata = tfmdata.shared.rawdata.metadata.math
917 if not mathdata then return end
918
919 if mathsize == 1 then
920 tfmdata.parameters.sizepercentage = 100
921 elseif mathsize == 2 then
922 tfmdata.parameters.sizepercentage = mathdata.ScriptPercentScaleDown
923 elseif mathsize == 3 then
924 tfmdata.parameters.sizepercentage = mathdata.ScriptScriptPercentScaleDown
925 end
926 end,
927 },
928 }
929
930 local function unset_designsize(tfmdata)
931 tfmdata.designsize = 0
932 end
933 fonts.constructors.features.otf.register {
934 name = 'no_designsize',
935 description = 'Set designsize to zero to ensure round-tripping in \\fontsize',
936 default = true,
937 manipulators = {
938 base = unset_designsize,
939 node = unset_designsize,
940 plug = unset_designsize,
941 },
942 }
943
944 return function ()
945 if not fonts and fonts.handlers then
946 report ("log", 0, "features",
947 "OTF mechanisms missing -- did you forget to \z
948 load a font loader?")
949 return false
950 end
951 add_auto_features ()
952 return true
953 end
954 -- vim:tw=79:sw=4:ts=4:expandtab