Debian Codegraph @

texk/gregorio/gregorio-src/tex/gregoriotex-nabc.lua

raw symbols SCIP
1 --GregorioTeX Nabc Lua file.
2 --
3 --Copyright (C) 2014-2024 The Gregorio Project (see CONTRIBUTORS.md)
4 --
5 --This file is part of Gregorio.
6 --
7 --Gregorio is free software: you can redistribute it and/or modify
8 --it under the terms of the GNU General Public License as published by
9 --the Free Software Foundation, either version 3 of the License, or
10 --(at your option) any later version.
11 --
12 --Gregorio is distributed in the hope that it will be useful,
13 --but WITHOUT ANY WARRANTY; without even the implied warranty of
14 --MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 --GNU General Public License for more details.
16 --
17 --You should have received a copy of the GNU General Public License
18 --along with Gregorio. If not, see <http://www.gnu.org/licenses/>.
19
20 -- this file contains lua functions used by GregorioTeX St. Gall ancient
21 -- neume support when called with LuaTeX.
22
23 -- GREGORIO_VERSION 6.1.0
24
25 local catcode_at_letter = luatexbase.catcodetables['gre@atletter']
26
27 local gregallaliases = {
28 ["ci~"] = "cl>",
29 ["pe~"] = "ta>",
30 ["vs~"] = "ta>",
31 ["pf~"] = "po>",
32 ["sc~"] = "pe>2",
33 ["sa~"] = "pe>2",
34 ["suu1"] = "ta-",
35 ["ppu1"] = "ta-",
36 ["pfG"] = "sfM",
37 ["cl~"] = "vi>",
38 ["pr~"] = "vs>",
39 ["visu2"] = "ci",
40 ["vi-su2"] = "ci-",
41 ["visu3"] = "ci1",
42 ["visu1sux1"] = "ci>",
43 ["clSsut1"] = "ciG",
44 ["clSsu2"] = "ciG1",
45 ["visu1suw1"] = "ciM",
46 ["vi-su1suw1"] = "ciM-",
47 ["visu1sut1"] = "ciS",
48 ["visut2"] = "ciS1",
49 ["visu2lsc3"] = "cilsc3",
50 ["vipp2"] = "sc",
51 ["vi-ppt2"] = "sc-",
52 ["vipp3"] = "sc1",
53 ["vi>pp2"] = "sc>",
54 ["peppt1"] = "scG",
55 ["vippt2"] = "scS",
56 ["ta>ppt1"] = "sc~",
57 ["clpp2"] = "sf",
58 ["cl-ppt2"] = "sf-",
59 ["cl>pp2"] = "sf>",
60 ["toppt1"] = "sfG",
61 ["clMpp2"] = "sfM1",
62 ["clppt2"] = "sfS",
63 ["clpp2lsc2"] = "sflsc2",
64 ["ciSppt1"] = "vippt1su1sut1",
65 ["cippt1"] = "vippt1su2",
66 ["cippt1lsc3"] = "vippt1su2lsc3",
67 ["cippu1"] = "vippu1su2",
68 ["cippu1lsc3"] = "vippu1su2lsc3",
69 ["clppt1"] = "toG",
70 ["pf!cl~"] = "cl!po>",
71 ["pf!cl~ppt1"] = "cl!po>ppt1"
72 }
73
74 local function gregallreadfont(fontname, font_id)
75 local tab = {}
76 local metrics = {}
77 local fontdata = font.getfont(font_id)
78 -- The unicodes table may be lazy-loaded, so iterating it may not
79 -- return everything. Attempting to retrieve the code point of a
80 -- glyph that has not already been loaded will trigger the __index
81 -- method in the metatable (implemented by the fontloader) to load
82 -- the table until the glyph is found. When iterating the
83 -- unicodes, we want the whole table to be filled, so we try to
84 -- access a non-existing glyph in order to force load the entire
85 -- table.
86 local ignored = fontdata.resources.unicodes['_this_is_hopefully_a_nonexistent_glyph_']
87 for key, value in pairs(fontdata.resources.unicodes) do
88 local g = fontdata.characters[value]
89 local name = key:gsub("B", ">"):gsub("N", "-"):gsub("E", "!"):gsub("T", "~")
90 if g and (value >= 0xe400) and (value < 0xf000) then
91 tab[name] = "\\char" .. value
92 metrics[name] = { width = g.width, height = g.height, depth = (g.depth and g.depth or 0) }
93 end
94 end
95 if fontname ~= "grelaon" then
96 for key, value in pairs(gregallaliases) do
97 if tab[value] and not tab[key] then
98 tab[key] = tab[value]
99 metrics[key] = metrics[value]
100 end
101 end
102 end
103 return tab, metrics
104 end
105
106 local gregalltab = {}
107 local gregallmetrics = {}
108
109 local gregallneumekinds = { vi = 1, pu = 1, ta = 1, gr = 1, cl = 1, un = 1, pv = 1, pe = 1, po = 1, to = 1, ci = 1, sc = 1, pf = 1, sf = 1, tr = 1,
110 st = 1, ds = 1, ts = 1, tg = 1, bv = 1, tv = 1, pr = 1, pi = 1, vs = 1, ["or"] = 1, sa = 1, pq = 1, qi = 1, ql = 1, pt = 1,
111 un = 1, oc = 1, ni = 1 }
112 local gregalllskinds = { c = 1, t = 1, s = 1, l = 1, x = 1, ["+"] = 1, a = 1, al = 1, am = 1, b = 1, cm = 1, co = 1, cw = 1, d = 1, e = 1, eq = 1,
113 ew = 1, f = 1, fid = 1, fr = 1, g = 1, h = 1, hp = 1, hn = 1, i = 1, im = 1, iv = 1, k = 1, lb = 1, lc = 1, len = 1,
114 lm = 1, lp = 1, lt = 1, m = 1, md = 1, moll = 1, n = 1, nl = 1, nt = 1, p = 1, par = 1, pfec = 1, pm = 1, q = 1,
115 sb = 1, sc = 1, sc = 1, simil = 1, simul = 1, sj = 1, sjc = 1, sjcm = 1, sm = 1, st = 1, sta = 1, su = 1, tb = 1,
116 th = 1, tm = 1, tw = 1, v = 1, ve = 1, vol = 1,
117 ["eq-"] = 1, equ = 1, simp = 1, simpl = 1, sp = 1 }
118 local grelaonltkinds = { i = 1, ["do"] = 1, dr = 1, dx = 1, ps = 1, qm = 1, sb = 1, se = 1, sj = 1, sl = 1, sn = 1, sp = 1, sr = 1, st = 1, us = 1 }
119
120 -- Parse a single base neume
121 local gregallparse_base = function (str, idx, len)
122 local ret = str:sub(idx, idx + 1)
123 local alt = {}
124 local height = 5
125 local alts = "MSG-><~"
126 if idx >= len or not gregallneumekinds[ret] then return 1 end
127 idx = idx + 2
128 -- The alternation modifiers can be written in arbitrary order,
129 -- canonicalize it and remove duplicates.
130 while idx <= len and alts:find(str:sub(idx, idx)) do
131 alt[str:sub(idx, idx)] = 1
132 idx = idx + 1
133 end
134 for i = 1, 7 do
135 local c = alts:sub(i, i)
136 if alt[c] then ret = ret .. c end
137 end
138 -- This is followed by a single optional variant digit.
139 if idx <= len and string.find("123456789", str:sub(idx, idx)) then
140 ret = ret .. str:sub(idx, idx)
141 idx = idx + 1
142 end
143 -- Ambitus not handled yet, neither during parsing, nor when
144 -- typesetting.
145 -- Optional height, h[a-np].
146 if idx < len and str:sub(idx, idx) == "h" then
147 local p = string.find("abcdefghijklmnp", str:sub(idx + 1, idx + 1))
148 if not p then return 1 end
149 height = p - 1
150 idx = idx + 2
151 end
152 return 0, idx, ret, height
153 end
154
155 -- Parse one neume, which is one base neume or several base neumes
156 -- separated with ! characters, and all this followed by arbitrary
157 -- ls, pp and su modifiers.
158 local gregallparse_neume = function (str, idx, len)
159 local err
160 local bases = {}
161 local heights = {}
162 local pp = ''
163 local su = ''
164 local ls = {}
165 local lsidx = 0
166 local i = 1
167 err, idx, bases[0], heights[0] = gregallparse_base(str, idx, len)
168 if err == 1 then return 1 end
169 while idx <= len and str:sub(idx, idx) == "!" do
170 err, idx, bases[i], heights[i] = gregallparse_base(str, idx + 1, len)
171 if err == 1 then return 1 end
172 i = i + 1
173 end
174 while idx < len do
175 local v = str:sub(idx, idx + 1)
176 if v == "ls" then
177 local idx2 = idx + 2
178 while idx2 <= len and not string.find("123456789", str:sub(idx2, idx2)) do
179 idx2 = idx2 + 1
180 end
181 if idx2 > len or not gregalllskinds[str:sub(idx + 2, idx2 - 1)] then return 1 end
182 ls[lsidx] = str:sub(idx, idx2)
183 lsidx = lsidx + 1
184 idx = idx2 + 1
185 elseif v == "lt" then
186 local idx2 = idx + 2
187 while idx2 <= len and not string.find("123456789", str:sub(idx2, idx2)) do
188 idx2 = idx2 + 1
189 end
190 if idx2 > len or not grelaonltkinds[str:sub(idx + 2, idx2 - 1)] then return 1 end
191 ls[lsidx] = str:sub(idx, idx2)
192 lsidx = lsidx + 1
193 idx = idx2 + 1
194 elseif v == "su" or v == "pp" then
195 local mod = ''
196 idx = idx + 2
197 local c = str:sub(idx, idx)
198 if idx <= len and string.find("tuvwxyqnz", c) then
199 mod = mod .. c
200 idx = idx + 1
201 c = str:sub(idx, idx)
202 end
203 -- Pre/subpuncta with height not supported yet
204 -- the heights would need to be adjusted relatively to heights[0]
205 if idx > len or not string.find("123456789", c) then return 1 end
206 mod = mod .. c
207 if v == "su" then su = su .. "su" .. mod else pp = pp .. "pp" .. mod end
208 idx = idx + 1
209 else break end
210 end
211 return 0, idx, bases, heights, ls, pp, su
212 end
213
214 local add_ls = function(base, pre, post, ls, position, glyphbox, lsbox, baseraise, lwidths, curlwidths, scale)
215 local raise = 0
216 if position == 3 or position == 6 or position == 9 then
217 if position == 3 then
218 raise = glyphbox.height - (lsbox.height-lsbox.depth)/2 + baseraise
219 elseif position == 6 then
220 raise = (glyphbox.height-glyphbox.depth)/2 - (lsbox.height+lsbox.depth)/2 + baseraise
221 else
222 raise = -glyphbox.depth - (lsbox.height+lsbox.depth)/2 + baseraise
223 end
224 local kern1 = curlwidths[position] - lwidths[12]
225 curlwidths[position] = curlwidths[position] + lsbox.width
226 local kern2 = lwidths[12] - curlwidths[position]
227 if curlwidths[12] == 0 then
228 curlwidths[12] = 1
229 kern1 = 0
230 end
231 kern1 = kern1 * scale
232 kern2 = kern2 * scale
233 raise = raise * scale
234 return base, pre, post..'\\kern '..string.format("%.3f",kern1)..'sp\\raise '..string.format("%.3f",raise)..'sp\\hbox{'..ls..'}\\kern '..string.format("%.3f",kern2)..'sp'
235 elseif position == 2 or position == 8 then
236 if position == 2 then
237 raise = glyphbox.height + lsbox.depth + baseraise
238 else
239 raise = -glyphbox.depth - lsbox.height + baseraise
240 end
241 local kern1 = -math.max(glyphbox.width, lwidths[11])/2 - lwidths[position]/2 + curlwidths[position]
242 curlwidths[position] = curlwidths[position] + lsbox.width
243 local kern2 = - kern1 - lsbox.width
244 if curlwidths[11] == 0 and glyphbox.width < lwidths[11] then
245 curlwidths[11] = 1
246 base = '\\kern '..string.format("%.3f",(lwidths[11] - glyphbox.width)/2 * scale)..'sp'..base
247 kern1 = kern1 + (lwidths[11] - glyphbox.width)/2
248 end
249 kern1 = kern1 * scale
250 kern2 = kern2 * scale
251 raise = raise * scale
252 return base..'\\kern '..string.format("%.3f",kern1)..'sp\\raise '..string.format("%.3f",raise)..'sp\\hbox{'..ls..'}\\kern '..string.format("%.3f",kern2)..'sp', pre, post
253 else
254 if position == 1 then
255 raise = glyphbox.height - (lsbox.height-lsbox.depth)/2 + baseraise
256 elseif position == 4 then
257 raise = (glyphbox.height-glyphbox.depth)/2 - (lsbox.height+lsbox.depth)/2 + baseraise
258 else
259 raise = -glyphbox.depth - (lsbox.height+lsbox.depth)/2 + baseraise
260 end
261 local kern1 = curlwidths[position] - lwidths[position]
262 curlwidths[position] = curlwidths[position] + lsbox.width
263 local kern2 = lwidths[position] - curlwidths[position]
264 if curlwidths[10] == 0 then
265 curlwidths[10] = 1
266 kern1 = lwidths[10] - lwidths[position]
267 end
268 kern1 = kern1 * scale
269 kern2 = kern2 * scale
270 raise = raise * scale
271 return base, pre..'\\kern '..string.format("%.3f",kern1)..'sp\\raise '..string.format("%.3f",raise)..'sp\\hbox{'..ls..'}\\kern '..string.format("%.3f",kern2)..'sp', post
272 end
273 end
274
275 local add_spacing = function(str, len, idx, ret)
276 while idx <= len and (str:sub(idx, idx) == "/" or str:sub(idx, idx) == "`") do
277 if idx < len and str:sub(idx, idx + 1) == "//" then
278 ret = ret .. "\\gre@hskip \\gre@space@skip@nabclargerspace"
279 idx = idx + 2
280 elseif idx < len and str:sub(idx, idx + 1) == "``" then
281 ret = ret .. "\\gre@hskip -\\gre@space@skip@nabclargerspace"
282 idx = idx + 2
283 elseif str:sub(idx, idx) == "/" then
284 ret = ret .. "\\gre@hskip \\gre@space@skip@nabcinterelementspace"
285 idx = idx + 1
286 else
287 ret = ret .. "\\gre@hskip -\\gre@space@skip@nabcinterelementspace"
288 idx = idx + 1
289 end
290 end
291 return idx, ret
292 end
293
294 local gregallparse_neumes = function(str, kind, scale)
295 local len = str:len()
296 local idx = 1
297 local ret = ''
298 idx, ret = add_spacing(str, len, idx, ret)
299 while idx <= len do
300 local err, bases, heights, ls, pp, su, lscount
301 err, idx, bases, heights, ls, pp, su = gregallparse_neume (str, idx, len)
302 if err == 1 then return ret .. "ERR" end
303 local base = bases[0]
304 local i = 1
305 while bases[i] do
306 base = base .. "!" .. bases[i]
307 local h = heights[i] - heights[0]
308 if h ~= 0 then
309 h = h + 5
310 if h < 0 or h > 14 then
311 base = "ERR"
312 break
313 end
314 base = base .. string.sub("abcdefghijklmnp", h + 1, h + 1)
315 end
316 i = i + 1
317 end
318 local ls5 = ''
319 lscount = 0
320 while ls[lscount] do
321 if not gregalltab[kind][ls[lscount]:sub(1, -2)] then base = "ERR" end
322 if tonumber(ls[lscount]:sub(-1, -1)) == 5 then
323 ls5 = ls5 .. ls[lscount]
324 ls[lscount] = ''
325 end
326 lscount = lscount + 1
327 end
328 if base ~= "ERR" then
329 local l = {}
330 function l.try (kind, base, parts, pp, su, ls5, ls)
331 if parts == 2 and pp ~= '' and su ~= '' and gregalltab[kind][base .. pp .. su .. ls5 .. ls] then return base .. pp .. su .. ls5 .. ls, '', '' end
332 -- Prefer subpunctis over prepunctis.
333 if parts == 1 and su ~= '' and gregalltab[kind][base .. su .. ls5 .. ls] then return base .. su .. ls5 .. ls, pp, '' end
334 if parts == 1 and pp ~= '' and gregalltab[kind][base .. pp .. ls5 .. ls] then return base .. pp .. ls5 .. ls, '', su end
335 -- Prefer subpunctis over significative letters.
336 if parts == 0 and su ~= '' and ls ~= '' and gregalltab[kind][base .. su .. ls5] then return nil, pp, su end
337 if parts == 0 and gregalltab[kind][base .. ls5 .. ls] then return base .. ls5 .. ls, pp, su end
338 return nil, pp, su
339 end
340 local r = nil
341 local ppsuparts = 0
342 if pp ~= '' then ppsuparts = 1 end
343 if su ~= '' then ppsuparts = ppsuparts + 1 end
344 -- We assume here no character in the font has more than three
345 -- significative letters. Significative letters with position 5
346 -- are always required to be in font and have preference over
347 -- pre/subpunctis and other significative letters.
348 local allparts = ppsuparts + lscount
349 if lscount >= 3 then allparts = ppsuparts + 3 end
350 -- Try to match as many parts (ls sequences, pp string, su string) as possible
351 -- except that for ls accept any of ls sequences only if we have all of them.
352 for parts = allparts, 0, -1 do
353 if lscount == 3 and parts >= 3 and parts <= 3 + ppsuparts then
354 r, pp, su = l.try(kind, base, parts - 3, pp, su, ls5, ls[0] .. ls[1] .. ls[2])
355 if (not r) and ls[0] ~= '' and ls[1] ~= '' and ls[2] ~= '' then
356 local p0 = tonumber(ls[0]:sub(-1, -1))
357 local p1 = tonumber(ls[1]:sub(-1, -1))
358 local p2 = tonumber(ls[2]:sub(-1, -1))
359 if (p0 ~= p1) and (p0 ~= p2) and (p1 ~= p2) then
360 r, pp, su = l.try(kind, base, parts - 3, pp, su, ls5, ls[1] .. ls[0] .. ls[2])
361 if not r then r, pp, su = l.try(kind, base, parts - 3, pp, su, ls5, ls[0] .. ls[2] .. ls[1]) end
362 if not r then r, pp, su = l.try(kind, base, parts - 3, pp, su, ls5, ls[1] .. ls[2] .. ls[0]) end
363 if not r then r, pp, su = l.try(kind, base, parts - 3, pp, su, ls5, ls[2] .. ls[0] .. ls[1]) end
364 if not r then r, pp, su = l.try(kind, base, parts - 3, pp, su, ls5, ls[2] .. ls[1] .. ls[0]) end
365 end
366 end
367 if r then
368 ls[0] = ''
369 ls[1] = ''
370 ls[2] = ''
371 break
372 end
373 if r then break end
374 end
375 if lscount == 2 and parts >= 2 and parts <= 2 + ppsuparts then
376 r, pp, su = l.try(kind, base, parts - 2, pp, su, ls5, ls[0] .. ls[1])
377 if (not r) and ls[0] ~= '' and ls[1] ~= '' and (tonumber(ls[0]:sub(-1, -1)) ~= tonumber(ls[1]:sub(-1, -1))) then
378 r, pp, su = l.try(kind, base, parts - 2, pp, su, ls5, ls[1] .. ls[0])
379 end
380 if r then
381 ls[0] = ''
382 ls[1] = ''
383 break
384 end
385 if r then break end
386 end
387 if lscount == 1 and parts >= 1 and parts <= 1 + ppsuparts then
388 r, pp, su = l.try(kind, base, parts - 1, pp, su, ls5, ls[0])
389 if r then
390 ls[0] = ''
391 break
392 end
393 end
394 r, pp, su = l.try(kind, base, parts, pp, su, ls5, '')
395 if r then break end
396 end
397 if not r or (pp ~= '' and not gregalltab[kind][pp]) or (su ~= '' and not gregalltab[kind][su]) then
398 base = "ERR"
399 else
400 base = gregalltab[kind][r]
401 local above = ''
402 local below = ''
403 local rmetrics = { width = gregallmetrics[kind][r].width,
404 height = gregallmetrics[kind][r].height,
405 depth = gregallmetrics[kind][r].depth }
406 -- Should the pre and subpuncta be somehow specially positioned
407 -- against the base neume?
408 if pp ~= '' then
409 base = gregalltab[kind][pp] .. base
410 rmetrics.width = rmetrics.width + gregallmetrics[kind][pp].width
411 rmetrics.height = math.max (rmetrics.height, gregallmetrics[kind][pp].height)
412 rmetrics.depth = math.max (rmetrics.height, gregallmetrics[kind][pp].depth)
413 end
414 if su ~= '' then
415 base = base .. gregalltab[kind][su]
416 rmetrics.width = rmetrics.width + gregallmetrics[kind][su].width
417 rmetrics.height = math.max (rmetrics.height, gregallmetrics[kind][su].height)
418 rmetrics.depth = math.max (rmetrics.height, gregallmetrics[kind][su].depth)
419 end
420 local baseraise = 0
421 if heights[0] ~= 5 then
422 baseraise = (heights[0] - 5) * gregallmetrics[kind].cl.height / 4
423 base = '\\raise '..string.format("%.3f",baseraise * scale)..'sp\\hbox{'..base..'}'
424 end
425 local lwidths = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
426 local curlwidths = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
427 for i = 0, lscount - 1 do
428 if ls[i] ~= '' then
429 local p = tonumber(ls[i]:sub(-1, -1))
430 local l = ls[i]:sub(1, -2)
431 lwidths[p] = lwidths[p] + gregallmetrics[kind][l].width
432 end
433 end
434 lwidths[10] = math.max (lwidths[1], lwidths[4], lwidths[7])
435 lwidths[11] = math.max (lwidths[2], lwidths[8])
436 lwidths[12] = math.max (lwidths[3], lwidths[6], lwidths[9])
437 local pre = ''
438 local post = ''
439 for i = 0, lscount - 1 do
440 if ls[i] ~= '' then
441 local p = tonumber(ls[i]:sub(-1, -1))
442 local l = ls[i]:sub(1, -2)
443 local lstr = gregalltab[kind][l]
444 base, pre, post = add_ls(base, pre, post, lstr, p, rmetrics, gregallmetrics[kind][l], baseraise, lwidths, curlwidths, scale)
445 end
446 end
447 base = pre..base..post
448 end
449 end
450 ret = ret .. base
451 idx, ret = add_spacing(str, len, idx, ret)
452 end
453 return ret
454 end
455
456 local function init_font(fontname)
457 if not gregalltab[fontname] then
458 gregalltab[fontname], gregallmetrics[fontname] = gregallreadfont(fontname, font.current())
459 end
460 end
461
462 local function print_nabc(nabc)
463 tex.sprint(catcode_at_letter, nabc)
464 end
465
466 gregoriotex.parse_nabc = gregallparse_neumes
467 gregoriotex.print_nabc = print_nabc
468 gregoriotex.init_nabc_font = init_font
469 gregoriotex.nabc_font_tables = gregalltab