| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
local hpack, traverse, traverse_id, has_attribute, count, remove, insert_after, copy = node.hpack, node.traverse, node.traverse_id, node.has_attribute, node.count, node.remove, node.insert_after, node.copy |
| 23 |
|
| 24 |
gregoriotex = gregoriotex or {} |
| 25 |
local gregoriotex = gregoriotex |
| 26 |
|
| 27 |
local internalversion = '6.1.0' |
| 28 |
|
| 29 |
local err, warn, info, log = luatexbase.provides_module({ |
| 30 |
name = "gregoriotex", |
| 31 |
version = '6.1.0', |
| 32 |
greinternalversion = internalversion, |
| 33 |
date = "2025/02/28", |
| 34 |
description = "GregorioTeX module.", |
| 35 |
author = "The Gregorio Project (see CONTRIBUTORS.md)", |
| 36 |
copyright = "2008-2025 - The Gregorio Project", |
| 37 |
license = "GPLv3+", |
| 38 |
}) |
| 39 |
|
| 40 |
local real_gregorio_exe = nil |
| 41 |
|
| 42 |
gregoriotex.module = { err = err, warn = warn, info = info, log = log } |
| 43 |
|
| 44 |
local format = string.format |
| 45 |
|
| 46 |
local hlist = node.id('hlist') |
| 47 |
local vlist = node.id('vlist') |
| 48 |
local glyph = node.id('glyph') |
| 49 |
local glue = node.id('glue') |
| 50 |
local whatsit = node.id('whatsit') |
| 51 |
local rule = node.id('rule') |
| 52 |
|
| 53 |
local hyphen = tex.defaulthyphenchar or 45 |
| 54 |
|
| 55 |
local part_attr = luatexbase.attributes['gre@attr@part'] |
| 56 |
local part_commentary = 1 |
| 57 |
local part_stafflines = 2 |
| 58 |
local part_initial = 3 |
| 59 |
|
| 60 |
local dash_attr = luatexbase.attributes['gre@attr@dash'] |
| 61 |
local potentialdashvalue = 1 |
| 62 |
local nopotentialdashvalue = 2 |
| 63 |
|
| 64 |
local center_attr = luatexbase.attributes['gre@attr@center'] |
| 65 |
local startcenter = 1 |
| 66 |
local endcenter = 2 |
| 67 |
|
| 68 |
local glyph_id_attr = luatexbase.attributes['gre@attr@glyph@id'] |
| 69 |
local glyph_top_attr = luatexbase.attributes['gre@attr@glyph@top'] |
| 70 |
local glyph_bottom_attr = luatexbase.attributes['gre@attr@glyph@bottom'] |
| 71 |
local prev_line_id = nil |
| 72 |
|
| 73 |
local alteration_type_attr = luatexbase.attributes['gre@attr@alteration@type'] |
| 74 |
local alteration_pitch_attr = luatexbase.attributes['gre@attr@alteration@pitch'] |
| 75 |
local alteration_id_attr = luatexbase.attributes['gre@attr@alteration@id'] |
| 76 |
|
| 77 |
local syllable_id_attr = luatexbase.attributes['gre@attr@syllable@id'] |
| 78 |
|
| 79 |
local cur_score_id = nil |
| 80 |
local score_inclusion = {} |
| 81 |
local line_heights = nil |
| 82 |
local new_line_heights = nil |
| 83 |
local score_heights = nil |
| 84 |
local new_score_heights = nil |
| 85 |
local saved_positions = nil |
| 86 |
local saved_lengths = nil |
| 87 |
local new_saved_lengths = nil |
| 88 |
local saved_newline_before_euouae = nil |
| 89 |
local new_saved_newline_before_euouae = nil |
| 90 |
local last_syllables = nil |
| 91 |
local new_last_syllables = nil |
| 92 |
local score_last_syllables = nil |
| 93 |
local new_score_last_syllables = nil |
| 94 |
local first_alterations = nil |
| 95 |
local new_first_alterations = nil |
| 96 |
local state_hashes = nil |
| 97 |
local new_state_hashes = nil |
| 98 |
local auxname = nil |
| 99 |
local tmpname = nil |
| 100 |
local test_snippet_filename = nil |
| 101 |
local snippet_filename = nil |
| 102 |
local snippet_logname = nil |
| 103 |
|
| 104 |
local base_output_dir = 'tmp-gre' |
| 105 |
local function set_base_output_dir(new_dirname) |
| 106 |
base_output_dir = lfs.normalize(new_dirname) |
| 107 |
end |
| 108 |
|
| 109 |
local space_below_staff = 5 |
| 110 |
local space_above_staff = 13 |
| 111 |
|
| 112 |
local score_fonts = {} |
| 113 |
local symbol_fonts = {} |
| 114 |
local loaded_font_sizes = {} |
| 115 |
local font_factors = {} |
| 116 |
local font_indexed = {} |
| 117 |
local next_variant = 0 |
| 118 |
local variant_prefix = 'gre@font@variant@' |
| 119 |
local number_to_letter = { |
| 120 |
['0'] = 'A', ['1'] = 'B', ['2'] = 'C', ['3'] = 'D', ['4'] = 'E', |
| 121 |
['5'] = 'F', ['6'] = 'G', ['7'] = 'H', ['8'] = 'I', ['9'] = 'J', |
| 122 |
} |
| 123 |
|
| 124 |
local capture_header_macro = {} |
| 125 |
local hashed_spaces = {} |
| 126 |
local space_hash = '' |
| 127 |
|
| 128 |
local per_line_dims = {} |
| 129 |
local per_line_counts = {} |
| 130 |
local saved_dims = {} |
| 131 |
local saved_counts = {} |
| 132 |
|
| 133 |
local catcode_at_letter = luatexbase.catcodetables['gre@atletter'] |
| 134 |
|
| 135 |
local user_defined_subtype = node.subtype('user_defined') |
| 136 |
local create_marker = luatexbase.new_user_whatsit('marker', 'gregoriotex') |
| 137 |
local marker_whatsit_id = luatexbase.get_user_whatsit_id('marker', 'gregoriotex') |
| 138 |
local translation_mark = 1 |
| 139 |
local abovelinestext_mark = 2 |
| 140 |
log("marker whatsit id is %d", marker_whatsit_id) |
| 141 |
|
| 142 |
local function get_prog_output(cmd, tmpname, fmt) |
| 143 |
local rc = os.spawn(cmd) |
| 144 |
local content = nil |
| 145 |
if rc == 0 then |
| 146 |
local f = io.open(tmpname, 'r'); |
| 147 |
if f then |
| 148 |
content = f:read(fmt) |
| 149 |
f:close() |
| 150 |
end |
| 151 |
end |
| 152 |
os.remove(tmpname) |
| 153 |
return content |
| 154 |
end |
| 155 |
|
| 156 |
local function gregorio_exe() |
| 157 |
if real_gregorio_exe == nil then |
| 158 |
local tmp_gabcfile = io.open(test_snippet_filename, 'w') |
| 159 |
tmp_gabcfile:write("name:test;\n%%\n(c4)(g)\n") |
| 160 |
tmp_gabcfile:close() |
| 161 |
|
| 162 |
local exe_version |
| 163 |
|
| 164 |
|
| 165 |
real_gregorio_exe = 'gregorio-6_1_0' |
| 166 |
local cmd = {real_gregorio_exe, '-o', tmpname, test_snippet_filename} |
| 167 |
exe_version = get_prog_output(cmd, tmpname, '*line') |
| 168 |
if not exe_version then |
| 169 |
|
| 170 |
real_gregorio_exe = 'gregorio' |
| 171 |
cmd = {real_gregorio_exe, '-o', tmpname, test_snippet_filename} |
| 172 |
exe_version = get_prog_output(cmd, tmpname, '*line') |
| 173 |
end |
| 174 |
if not exe_version or string.match(exe_version,"%d+%.%d+%.") |
| 175 |
~= string.match(internalversion,"%d+%.%d+%.") then |
| 176 |
real_gregorio_exe = nil |
| 177 |
err("Unable to find gregorio executable.\n".. |
| 178 |
"shell-escape mode may not be activated. Try\n\n".. |
| 179 |
"%s --shell-escape %s.tex\n\n".. |
| 180 |
"See the documentation of Gregorio or your TeX\n".. |
| 181 |
"distribution to automatize it.", |
| 182 |
tex.formatname, tex.jobname) |
| 183 |
end |
| 184 |
|
| 185 |
os.remove(test_snippet_filename) |
| 186 |
log("will use %s", real_gregorio_exe) |
| 187 |
end |
| 188 |
|
| 189 |
return real_gregorio_exe |
| 190 |
end |
| 191 |
|
| 192 |
local function mark(value) |
| 193 |
local marker = create_marker() |
| 194 |
marker.type = 100 |
| 195 |
marker.value = value |
| 196 |
marker.attr = node.current_attr() |
| 197 |
node.write(marker) |
| 198 |
end |
| 199 |
|
| 200 |
local function mark_translation() |
| 201 |
mark(translation_mark) |
| 202 |
end |
| 203 |
|
| 204 |
local function mark_abovelinestext() |
| 205 |
mark(abovelinestext_mark) |
| 206 |
end |
| 207 |
|
| 208 |
local function is_mark(node, value) |
| 209 |
return node.id == whatsit and node.subtype == user_defined_subtype and |
| 210 |
node.user_id == marker_whatsit_id and node.value == value |
| 211 |
end |
| 212 |
|
| 213 |
local function keys_changed(tab1, tab2) |
| 214 |
if tab2 == nil then return true end |
| 215 |
local id,_ |
| 216 |
for id,_ in pairs(tab1) do |
| 217 |
if tab2[id] == nil then return true end |
| 218 |
end |
| 219 |
for id,_ in pairs(tab2) do |
| 220 |
if tab1[id] == nil then return true end |
| 221 |
end |
| 222 |
return false |
| 223 |
end |
| 224 |
|
| 225 |
local function saved_computations_changed(tab1, tab2) |
| 226 |
local score_ids = {} |
| 227 |
local id,_ |
| 228 |
for id,_ in pairs(tab1) do |
| 229 |
score_ids[id] = true |
| 230 |
end |
| 231 |
for id,_ in pairs(tab2) do |
| 232 |
score_ids[id] = true |
| 233 |
end |
| 234 |
for id,_ in pairs(score_ids) do |
| 235 |
local inner1 = tab1[id] |
| 236 |
local inner2 = tab2[id] |
| 237 |
if inner1 == nil or inner2 == nil then return true end |
| 238 |
local keys = {} |
| 239 |
local index |
| 240 |
for index,_ in pairs(inner1) do |
| 241 |
keys[index] = true |
| 242 |
end |
| 243 |
for index,_ in pairs(inner2) do |
| 244 |
keys[index] = true |
| 245 |
end |
| 246 |
for index,_ in pairs(keys) do |
| 247 |
if inner1[index] ~= inner2[index] then return true end |
| 248 |
end |
| 249 |
end |
| 250 |
return false |
| 251 |
end |
| 252 |
|
| 253 |
local function entries_changed(tab1, tab2) |
| 254 |
local k, v |
| 255 |
for k, v in pairs(tab1) do |
| 256 |
if tab2[k] ~= v then return true end |
| 257 |
end |
| 258 |
return false |
| 259 |
end |
| 260 |
|
| 261 |
local function is_greaux_write_needed() |
| 262 |
local id, tab |
| 263 |
if entries_changed(state_hashes, new_state_hashes) or |
| 264 |
entries_changed(new_state_hashes, state_hashes) then |
| 265 |
return true |
| 266 |
end |
| 267 |
for id, tab in pairs(new_line_heights) do |
| 268 |
if keys_changed(tab, line_heights[id]) then return true end |
| 269 |
end |
| 270 |
for id, tab in pairs(line_heights) do |
| 271 |
if keys_changed(tab, new_line_heights[id]) then return true end |
| 272 |
end |
| 273 |
for id, tab in pairs(new_last_syllables) do |
| 274 |
if keys_changed(tab, last_syllables[id]) then return true end |
| 275 |
end |
| 276 |
for id, tab in pairs(last_syllables) do |
| 277 |
if keys_changed(tab, new_last_syllables[id]) then return true end |
| 278 |
end |
| 279 |
if saved_computations_changed(saved_lengths, new_saved_lengths) then |
| 280 |
return true |
| 281 |
end |
| 282 |
if saved_computations_changed(saved_newline_before_euouae, new_saved_newline_before_euouae) then |
| 283 |
return true |
| 284 |
end |
| 285 |
|
| 286 |
for id, tab in pairs(new_first_alterations) do |
| 287 |
if keys_changed(tab, first_alterations[id]) or entries_changed(tab, first_alterations[id]) then return true end |
| 288 |
end |
| 289 |
for id, tab in pairs(first_alterations) do |
| 290 |
if keys_changed(tab, new_first_alterations[id]) or entries_changed(tab, first_alterations[id]) then return true end |
| 291 |
end |
| 292 |
return false |
| 293 |
end |
| 294 |
|
| 295 |
local function write_greaux() |
| 296 |
if is_greaux_write_needed() then |
| 297 |
|
| 298 |
|
| 299 |
local aux = io.open(auxname, 'w') |
| 300 |
if aux then |
| 301 |
log("Writing %s", auxname) |
| 302 |
local id, tab, id2, line, value |
| 303 |
aux:write('return {\n ["line_heights"]={\n') |
| 304 |
for id, tab in pairs(new_line_heights) do |
| 305 |
aux:write(string.format(' ["%s"]={\n', id)) |
| 306 |
for id2, line in pairs(tab) do |
| 307 |
if id2 == 'last' then |
| 308 |
aux:write(string.format(' ["%s"]=%d,\n', id2, line)) |
| 309 |
else |
| 310 |
aux:write(string.format(' [%d]={%d,%d,%d,%d,%d},\n', id2, line[1], |
| 311 |
line[2], line[3], line[4], line[5])) |
| 312 |
end |
| 313 |
end |
| 314 |
aux:write(' },\n') |
| 315 |
end |
| 316 |
aux:write(' },\n ["last_syllables"]={\n') |
| 317 |
for id, tab in pairs(new_last_syllables) do |
| 318 |
aux:write(string.format(' ["%s"]={\n', id)) |
| 319 |
for id2, value in pairs(tab) do |
| 320 |
if id2 == 'state' then |
| 321 |
aux:write(string.format(' state="%s",\n', value)) |
| 322 |
else |
| 323 |
aux:write(string.format(' [%d]=%d,\n', id2, value)) |
| 324 |
end |
| 325 |
end |
| 326 |
aux:write(' },\n') |
| 327 |
end |
| 328 |
aux:write(' },\n ["saved_lengths"]={\n') |
| 329 |
for id, tab in pairs(new_saved_lengths) do |
| 330 |
aux:write(string.format(' ["%s"]={\n', id)) |
| 331 |
for id2, value in pairs(tab) do |
| 332 |
if value ~= nil then |
| 333 |
aux:write(string.format(' [%d]=%d,\n', id2, value)) |
| 334 |
end |
| 335 |
end |
| 336 |
aux:write(' },\n') |
| 337 |
end |
| 338 |
aux:write(' },\n ["saved_newline_before_euouae"]={\n') |
| 339 |
for id, tab in pairs(new_saved_newline_before_euouae) do |
| 340 |
aux:write(string.format(' ["%s"]={\n', id)) |
| 341 |
for id2, value in pairs(tab) do |
| 342 |
if value ~= nil then |
| 343 |
if value then |
| 344 |
aux:write(string.format(' [%d]=true,\n', id2)) |
| 345 |
else |
| 346 |
aux:write(string.format(' [%d]=false,\n', id2)) |
| 347 |
end |
| 348 |
end |
| 349 |
end |
| 350 |
aux:write(' },\n') |
| 351 |
end |
| 352 |
aux:write(' },\n ["state_hashes"]={\n') |
| 353 |
for id, value in pairs(new_state_hashes) do |
| 354 |
aux:write(string.format(' ["%s"]="%s",\n', id, value)) |
| 355 |
end |
| 356 |
aux:write(' },\n') |
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
aux:write(' ["first_alterations"]={\n') |
| 363 |
for id, tab in pairs(new_first_alterations) do |
| 364 |
aux:write(string.format(' ["%s"]={', id)) |
| 365 |
for i, value in pairs(tab) do |
| 366 |
aux:write(string.format('[%q]=%s,', i, value)) |
| 367 |
end |
| 368 |
aux:write(string.format('},\n')) |
| 369 |
end |
| 370 |
aux:write(' },\n}\n') |
| 371 |
aux:close() |
| 372 |
else |
| 373 |
err("\n Unable to open %s", auxname) |
| 374 |
end |
| 375 |
|
| 376 |
warn("Line heights, variable brace lengths, or soft flats/sharps may have changed. Rerun to fix.") |
| 377 |
end |
| 378 |
end |
| 379 |
|
| 380 |
local function init(arg, enable_height_computation) |
| 381 |
|
| 382 |
local outputdir = nil |
| 383 |
for k,v in pairs(arg) do |
| 384 |
if v:find('%-output%-directory') then |
| 385 |
if v:find('%-output%-directory=') then |
| 386 |
outputdir=string.explode(v, '=')[2] |
| 387 |
else |
| 388 |
outputdir=arg[tonumber(k)+1] |
| 389 |
end |
| 390 |
end |
| 391 |
end |
| 392 |
|
| 393 |
local basepath = tex.jobname |
| 394 |
if outputdir and lfs.isdir(outputdir) then |
| 395 |
basepath = outputdir..'/'..basepath |
| 396 |
end |
| 397 |
basepath = lfs.normalize(basepath) |
| 398 |
|
| 399 |
auxname = basepath..'.gaux' |
| 400 |
tmpname = basepath..'.gtmp' |
| 401 |
test_snippet_filename = basepath..'.test.gsnippet' |
| 402 |
snippet_filename = basepath..'.gsnippet' |
| 403 |
snippet_logname = basepath..'.gsniplog' |
| 404 |
|
| 405 |
|
| 406 |
texio.write_nl('('..auxname..')') |
| 407 |
if lfs.isfile(auxname) then |
| 408 |
log("Reading %s", auxname) |
| 409 |
local score_info = dofile(auxname) |
| 410 |
line_heights = score_info.line_heights or {} |
| 411 |
last_syllables = score_info.last_syllables or {} |
| 412 |
state_hashes = score_info.state_hashes or {} |
| 413 |
saved_lengths = score_info.saved_lengths or {} |
| 414 |
saved_newline_before_euouae = score_info.saved_newline_before_euouae or {} |
| 415 |
first_alterations = score_info.first_alterations or {} |
| 416 |
else |
| 417 |
line_heights = {} |
| 418 |
last_syllables = {} |
| 419 |
state_hashes = {} |
| 420 |
saved_lengths = {} |
| 421 |
saved_newline_before_euouae = {} |
| 422 |
first_alterations = {} |
| 423 |
end |
| 424 |
|
| 425 |
if enable_height_computation then |
| 426 |
new_line_heights = {} |
| 427 |
new_last_syllables = {} |
| 428 |
new_state_hashes = {} |
| 429 |
|
| 430 |
local mcb_version = luatexbase.get_module_version and |
| 431 |
luatexbase.get_module_version('luatexbase-mcb') or 9999 |
| 432 |
if mcb_version and mcb_version > 0.6 then |
| 433 |
luatexbase.add_to_callback('finish_pdffile', write_greaux, |
| 434 |
'gregoriotex.write_greaux') |
| 435 |
else |
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
luatexbase.add_to_callback('stop_run', write_greaux, |
| 442 |
'gregoriotex.write_greaux') |
| 443 |
end |
| 444 |
else |
| 445 |
warn('Height computation has been skipped. Gregorio will use '.. |
| 446 |
'previously computed values if available but will not recompute '.. |
| 447 |
'line heights. Remove or undefine \\greskipheightcomputation to '.. |
| 448 |
'resume height computation.') |
| 449 |
end |
| 450 |
saved_positions = {} |
| 451 |
new_saved_lengths = {} |
| 452 |
new_saved_newline_before_euouae = {} |
| 453 |
new_first_alterations = {} |
| 454 |
end |
| 455 |
|
| 456 |
|
| 457 |
local tmpnode = node.new(glyph, 0) |
| 458 |
tmpnode.font = 0 |
| 459 |
tmpnode.char = hyphen |
| 460 |
local function gethyphennode() |
| 461 |
return copy(tmpnode) |
| 462 |
end |
| 463 |
|
| 464 |
local function getdashnnode() |
| 465 |
local hyphnode = gethyphennode() |
| 466 |
local dashnode = hpack(hyphnode) |
| 467 |
dashnode.shift = 0 |
| 468 |
return dashnode,hyphnode |
| 469 |
end |
| 470 |
|
| 471 |
|
| 472 |
local function dump_nodes_helper(head, indent) |
| 473 |
local dots = string.rep('..', indent) |
| 474 |
for n in traverse(head) do |
| 475 |
local ids = format("%s", has_attribute(n, glyph_id)) |
| 476 |
if node.type(n.id) == 'penalty' then |
| 477 |
log(dots .. "%s %s {%s}", node.type(n.id), n.penalty, ids) |
| 478 |
elseif n.id == whatsit and n.subtype == user_defined_subtype and n.user_id == marker_whatsit_id then |
| 479 |
log(dots .. "marker-whatsit %s", n.value) |
| 480 |
elseif n.id == glyph then |
| 481 |
local f = font.fonts[n.font] |
| 482 |
local charname |
| 483 |
for k, v in pairs(f.resources.unicodes) do |
| 484 |
if v == n.char then charname = k end |
| 485 |
end |
| 486 |
log(dots .. "glyph %s {%s}", charname, ids) |
| 487 |
elseif n.id == rule then |
| 488 |
log(dots .. "rule [%s] width=%.2fpt height=%.2fpt depth=%.2fpt", n.subtype, n.width/2^16, n.height/2^16, n.depth/2^16) |
| 489 |
elseif n.id == hlist or n.id ==vlist then |
| 490 |
log(dots .. "%s [%s] width=%.2fpt height=%.2fpt depth=%.2fpt shift=%.2fpt {%s}", node.type(n.id), n.subtype, n.width/2^16, n.height/2^16, n.depth/2^16, n.shift/2^16, ids) |
| 491 |
elseif n.id == glue then |
| 492 |
log(dots .. "glue [%s] width=%.2fpt", n.subtype, n.width/2^16) |
| 493 |
else |
| 494 |
log(dots .. "node %s [%s] {%s}", node.type(n.id), n.subtype, ids) |
| 495 |
end |
| 496 |
if n.id == hlist or n.id == vlist then |
| 497 |
dump_nodes_helper(n.head, indent+1) |
| 498 |
end |
| 499 |
end |
| 500 |
end |
| 501 |
|
| 502 |
local function dump_nodes(head) |
| 503 |
log('--begin dump--') |
| 504 |
dump_nodes_helper(head, 0) |
| 505 |
log('--end dump--') |
| 506 |
end |
| 507 |
|
| 508 |
|
| 509 |
local function get_first_node_by_id(id, head) |
| 510 |
for n in traverse_id(id, head) do |
| 511 |
return n |
| 512 |
end |
| 513 |
end |
| 514 |
|
| 515 |
local function center_translation(startnode, endnode, ratio, sign, order) |
| 516 |
|
| 517 |
local total_width = node.dimensions(ratio, sign, order, startnode, endnode) |
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
local vlistnode = get_first_node_by_id(vlist, startnode.head) |
| 538 |
local hlistnode = get_first_node_by_id(hlist, vlistnode.head) |
| 539 |
local glyphnode = get_first_node_by_id(glyph, hlistnode.head) |
| 540 |
|
| 541 |
local trans_width = node.dimensions(glyphnode) |
| 542 |
|
| 543 |
local X = (total_width - trans_width) / 2 |
| 544 |
vlistnode.prev.kern = X |
| 545 |
vlistnode.next.kern = -X |
| 546 |
end |
| 547 |
|
| 548 |
local debug_types_activated = {['linesglues'] = false} |
| 549 |
|
| 550 |
local function set_debug_string(debugstring) |
| 551 |
for debugtype in string.gmatch(debugstring, "[^,]+") do |
| 552 |
debug_types_activated[debugtype] = true |
| 553 |
end |
| 554 |
end |
| 555 |
|
| 556 |
local glue_sign_name = {[0] = 'normal', [1] = 'stretching', [2] = 'shrinking'} |
| 557 |
|
| 558 |
local function debugmessage(type, ...) |
| 559 |
if (debug_types_activated[type] or debug_types_activated['all']) then |
| 560 |
texio.write_nl('GregorioTeX debug: ('..type..'): '..format(...)) |
| 561 |
end |
| 562 |
end |
| 563 |
|
| 564 |
gregoriotex.module.debugmessage = debugmessage |
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
local function adjust_fullwidth (line) |
| 570 |
|
| 571 |
local line_width = line.width |
| 572 |
for child in node.traverse(line.head) do |
| 573 |
if child.id == glue and child.subtype == 8 then |
| 574 |
line_width = line_width - node.effective_glue(child, line) |
| 575 |
end |
| 576 |
end |
| 577 |
debugmessage("stafflines", "line width %spt", line_width/2^16) |
| 578 |
|
| 579 |
local function visit(cur) |
| 580 |
for child in node.traverse_list(cur.head) do |
| 581 |
if has_attribute(child, part_attr, part_commentary) then |
| 582 |
debugmessage("adjust_fullwidth", "commentary width %spt -> %spt", child.width/2^16, line_width/2^16) |
| 583 |
child.width = line_width |
| 584 |
local new = node.hpack(child.head, line_width, 'exactly') |
| 585 |
cur.head = node.insert_before(cur.head, child, new) |
| 586 |
cur.head = node.remove(cur.head, child) |
| 587 |
elseif has_attribute(child, part_attr, part_stafflines) then |
| 588 |
debugmessage("adjust_fullwidth", "staff width %spt -> %spt", child.width/2^16, line_width/2^16) |
| 589 |
for r in traverse_id(rule, child.head) do |
| 590 |
r.width = line_width |
| 591 |
end |
| 592 |
child.width = line_width |
| 593 |
else |
| 594 |
visit(child) |
| 595 |
end |
| 596 |
end |
| 597 |
end |
| 598 |
|
| 599 |
visit(line) |
| 600 |
end |
| 601 |
|
| 602 |
|
| 603 |
local function adjust_hlist(cur) |
| 604 |
local new_height = 0 |
| 605 |
local new_depth = 0 |
| 606 |
for child in traverse(cur.head) do |
| 607 |
if child.id == hlist or child.id == vlist then |
| 608 |
new_height = math.max(new_height, child.height - child.shift) |
| 609 |
new_depth = math.max(new_depth, child.depth + child.shift) |
| 610 |
elseif child.id == rule or child.id == glyph then |
| 611 |
new_height = math.max(new_height, child.height) |
| 612 |
new_depth = math.max(new_depth, child.depth) |
| 613 |
end |
| 614 |
end |
| 615 |
debugmessage('adjust_hlist', 'height %spt -> %spt, depth %spt -> %spt', cur.height/2^16, new_height/2^16, cur.depth/2^16, new_depth/2^16) |
| 616 |
cur.height = new_height |
| 617 |
cur.depth = new_depth |
| 618 |
end |
| 619 |
|
| 620 |
local function find_attr(cur, attr, val) |
| 621 |
if has_attribute(cur, attr, val) then |
| 622 |
return cur |
| 623 |
elseif cur.id == hlist or cur.id == vlist then |
| 624 |
for child in traverse(cur.head) do |
| 625 |
local found = find_attr(child, attr, val) |
| 626 |
if found ~= nil then |
| 627 |
return found |
| 628 |
end |
| 629 |
end |
| 630 |
end |
| 631 |
end |
| 632 |
|
| 633 |
local function drop_initial(h) |
| 634 |
|
| 635 |
local indented = tex.count['gre@count@initiallines'] |
| 636 |
debugmessage("initial", "%s indented lines", indented) |
| 637 |
|
| 638 |
local initial, initial_line |
| 639 |
local save_height, save_depth, save_shift |
| 640 |
|
| 641 |
|
| 642 |
for line in traverse_id(hlist, h) do |
| 643 |
initial = find_attr(line, part_attr, part_initial) |
| 644 |
if initial ~= nil then |
| 645 |
initial_line = line |
| 646 |
break |
| 647 |
end |
| 648 |
end |
| 649 |
debugmessage('initial', 'found initial with height=%spt depth=%spt shift=%spt', initial.height/2^16, initial.depth/2^16, initial.shift/2^16) |
| 650 |
save_height, save_depth, save_shift = initial.height, initial.depth, initial.shift |
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
local last_line |
| 655 |
local last_glue |
| 656 |
local last_distance = 0 |
| 657 |
local line_num = 0 |
| 658 |
for line in traverse(h) do |
| 659 |
if line.id == glue then |
| 660 |
debugmessage("initial", "glue %spt", line.width/2^16) |
| 661 |
if line_num == indented then last_glue = line end |
| 662 |
|
| 663 |
if line_num >= 1 and line_num < indented then |
| 664 |
last_distance = last_distance + line.width |
| 665 |
end |
| 666 |
elseif line.id == hlist then |
| 667 |
debugmessage("initial", "line height=%spt depth=%spt", line.height/2^16, line.depth/2^16) |
| 668 |
line_num = line_num + 1 |
| 669 |
if line_num > 1 and line_num <= indented then |
| 670 |
last_distance = last_distance + line.height |
| 671 |
end |
| 672 |
if line_num < indented then |
| 673 |
last_distance = last_distance + line.depth |
| 674 |
end |
| 675 |
if line_num <= indented then last_line = line end |
| 676 |
end |
| 677 |
end |
| 678 |
debugmessage("initial", "distance from first to last indented line is %spt", last_distance/2^16) |
| 679 |
|
| 680 |
|
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
local initial_shift = 0 |
| 685 |
if tex.count['gre@count@initialposition'] == 2 then |
| 686 |
debugmessage('initial', 'align to baseline of last line') |
| 687 |
initial_shift = last_distance |
| 688 |
elseif tex.count['gre@count@initialposition'] == 3 then |
| 689 |
debugmessage('initial', 'align to bottom of last line') |
| 690 |
initial_shift = last_distance + last_line.depth |
| 691 |
end |
| 692 |
debugmessage("initial", "initial shift is %spt", initial_shift/2^16) |
| 693 |
|
| 694 |
|
| 695 |
initial.shift = save_shift + initial_shift |
| 696 |
|
| 697 |
|
| 698 |
initial_line.height = math.max(initial_line.height, save_height - initial_shift) |
| 699 |
|
| 700 |
local save_last_depth = last_line.depth |
| 701 |
last_line.depth = math.max(last_line.depth, save_depth + initial_shift - last_distance) |
| 702 |
|
| 703 |
|
| 704 |
if last_glue and last_glue.subtype == 2 then |
| 705 |
last_glue.width = last_glue.width - last_line.depth + save_last_depth |
| 706 |
if last_glue.width < tex.lineskiplimit then |
| 707 |
last_glue.subtype = 1 |
| 708 |
node.setglue(last_glue, node.getglue(tex.lineskip)) |
| 709 |
end |
| 710 |
debugmessage('initial', 'set last_glue to %spt plus %spt minus %spt', last_glue.width/2^16, last_glue.stretch/2^16, last_glue.shrink/2^16) |
| 711 |
end |
| 712 |
|
| 713 |
end |
| 714 |
|
| 715 |
|
| 716 |
|
| 717 |
local function post_linebreak(h, groupcode, glyphes) |
| 718 |
|
| 719 |
local lastseennode = nil |
| 720 |
local centerstartnode = nil |
| 721 |
local line_id = nil |
| 722 |
local line_top = nil |
| 723 |
local line_bottom = nil |
| 724 |
local line_has_translation = false |
| 725 |
local line_has_abovelinestext = false |
| 726 |
local linenum = 0 |
| 727 |
local syl_id = nil |
| 728 |
|
| 729 |
for line in traverse(h) do |
| 730 |
if line.id == glue then |
| 731 |
if line.next ~= nil and line.next.id == hlist |
| 732 |
and has_attribute(line.next, dash_attr) |
| 733 |
and count(hlist, line.next.head) <= 2 then |
| 734 |
|
| 735 |
h, line = remove(h, line) |
| 736 |
end |
| 737 |
elseif line.id == hlist and has_attribute(line, dash_attr) then |
| 738 |
|
| 739 |
if count(hlist, line.head) <= 2 then |
| 740 |
|
| 741 |
h, line = remove(h, line) |
| 742 |
else |
| 743 |
linenum = linenum + 1 |
| 744 |
debugmessage('linesglues', 'line %d: %s factor %.0f%%', linenum, glue_sign_name[line.glue_sign], line.glue_set*100) |
| 745 |
centerstartnode = nil |
| 746 |
line_id = nil |
| 747 |
line_top = nil |
| 748 |
line_bottom = nil |
| 749 |
line_has_translation = false |
| 750 |
line_has_abovelinestext = false |
| 751 |
|
| 752 |
for n in traverse_id(hlist, line.head) do |
| 753 |
syl_id = has_attribute(n, syllable_id_attr) or syl_id |
| 754 |
if has_attribute(n, center_attr, startcenter) then |
| 755 |
centerstartnode = n |
| 756 |
elseif has_attribute(n, center_attr, endcenter) then |
| 757 |
if not centerstartnode then |
| 758 |
warn("End of a translation centering area encountered on a\nline without translation centering beginning,\nskipping translation...") |
| 759 |
else |
| 760 |
center_translation(centerstartnode, n, line.glue_set, line.glue_sign, line.glue_order) |
| 761 |
end |
| 762 |
end |
| 763 |
|
| 764 |
if new_score_heights then |
| 765 |
local glyph_id = has_attribute(n, glyph_id_attr) |
| 766 |
local glyph_top = has_attribute(n, glyph_top_attr) or 7 |
| 767 |
local glyph_bottom = has_attribute(n, glyph_bottom_attr) or 7 |
| 768 |
if glyph_id and glyph_id > prev_line_id then |
| 769 |
if not line_id or glyph_id > line_id then |
| 770 |
line_id = glyph_id |
| 771 |
end |
| 772 |
if not line_top or glyph_top > line_top then |
| 773 |
line_top = glyph_top |
| 774 |
end |
| 775 |
if not line_bottom or glyph_bottom < line_bottom then |
| 776 |
line_bottom = glyph_bottom |
| 777 |
end |
| 778 |
end |
| 779 |
end |
| 780 |
end |
| 781 |
|
| 782 |
if new_score_heights then |
| 783 |
for n in traverse_id(whatsit, line.head) do |
| 784 |
line_has_translation = line_has_translation or |
| 785 |
is_mark(n, translation_mark) |
| 786 |
line_has_abovelinestext = line_has_abovelinestext or |
| 787 |
is_mark(n, abovelinestext_mark) |
| 788 |
end |
| 789 |
end |
| 790 |
|
| 791 |
if line_id then |
| 792 |
new_score_heights[prev_line_id] = { linenum, line_top, line_bottom, |
| 793 |
line_has_translation and 1 or 0, |
| 794 |
line_has_abovelinestext and 1 or 0 } |
| 795 |
new_score_heights['last'] = prev_line_id |
| 796 |
prev_line_id = line_id |
| 797 |
end |
| 798 |
if new_score_last_syllables and syl_id then |
| 799 |
new_score_last_syllables[syl_id] = syl_id |
| 800 |
end |
| 801 |
end |
| 802 |
end |
| 803 |
end |
| 804 |
|
| 805 |
|
| 806 |
if tex.count['gre@count@initiallines'] > 1 or tex.count['gre@count@initialposition'] == 3 then |
| 807 |
drop_initial(h) |
| 808 |
end |
| 809 |
|
| 810 |
|
| 811 |
for line in traverse_id(hlist, h) do |
| 812 |
adjust_fullwidth(line) |
| 813 |
end |
| 814 |
|
| 815 |
|
| 816 |
|
| 817 |
|
| 818 |
|
| 819 |
for line in traverse_id(hlist, h) do |
| 820 |
local seen = {} |
| 821 |
for n in traverse_id(hlist, line.head) do |
| 822 |
|
| 823 |
|
| 824 |
local t = has_attribute(n, alteration_type_attr) |
| 825 |
if t ~= nil and t > 0 then |
| 826 |
local i = has_attribute(n, alteration_id_attr) |
| 827 |
local h = has_attribute(n, alteration_pitch_attr) |
| 828 |
if seen[h] == nil then |
| 829 |
new_score_first_alterations[i] = 1 |
| 830 |
elseif seen[h] ~= t then |
| 831 |
new_score_first_alterations[i] = 2 |
| 832 |
else |
| 833 |
new_score_first_alterations[i] = 3 |
| 834 |
end |
| 835 |
new_score_first_alterations['last'] = i |
| 836 |
debugmessage("alteration", "id=%s type=%s height=%s seen=%s first=%s", i, t, h, seen[t], new_score_first_alterations[i]) |
| 837 |
seen[h] = t |
| 838 |
end |
| 839 |
end |
| 840 |
end |
| 841 |
|
| 842 |
|
| 843 |
for line in traverse_id(hlist, h) do |
| 844 |
if has_attribute(line, dash_attr) then |
| 845 |
|
| 846 |
local adddash=false |
| 847 |
for n in traverse_id(hlist, line.head) do |
| 848 |
|
| 849 |
|
| 850 |
|
| 851 |
|
| 852 |
if has_attribute(n, dash_attr, potentialdashvalue) then |
| 853 |
adddash=true |
| 854 |
lastseennode=n |
| 855 |
|
| 856 |
elseif has_attribute(n, dash_attr, nopotentialdashvalue) then |
| 857 |
adddash=false |
| 858 |
end |
| 859 |
end |
| 860 |
|
| 861 |
|
| 862 |
if adddash then |
| 863 |
local lastglyph |
| 864 |
|
| 865 |
|
| 866 |
for g in node.traverse_id(glyph, lastseennode.head) do |
| 867 |
lastglyph = g |
| 868 |
end |
| 869 |
if not (lastglyph.char == hyphen or lastglyph.char == 45) then |
| 870 |
local dashnode, hyphnode = getdashnnode() |
| 871 |
hyphnode.font = lastglyph.font |
| 872 |
insert_after(lastseennode.head, lastglyph, dashnode) |
| 873 |
end |
| 874 |
end |
| 875 |
end |
| 876 |
end |
| 877 |
|
| 878 |
|
| 879 |
|
| 880 |
return true |
| 881 |
end |
| 882 |
|
| 883 |
|
| 884 |
|
| 885 |
|
| 886 |
local function disable_hyphenation() |
| 887 |
return false |
| 888 |
end |
| 889 |
|
| 890 |
local get_font_by_id = font.getfont |
| 891 |
local font_id = font.id |
| 892 |
|
| 893 |
|
| 894 |
|
| 895 |
|
| 896 |
local function unsafe_get_font_by_id(id) |
| 897 |
return get_font_by_id(id) or font.fonts[id] |
| 898 |
end |
| 899 |
|
| 900 |
|
| 901 |
|
| 902 |
|
| 903 |
local function get_font_by_name(name) |
| 904 |
local id = font_id(name) |
| 905 |
return id >= 0 and get_font_by_id(id) |
| 906 |
end |
| 907 |
|
| 908 |
|
| 909 |
|
| 910 |
|
| 911 |
local function get_score_font_id(name) |
| 912 |
local sfnt = score_fonts[name] |
| 913 |
if sfnt then |
| 914 |
return font_id(sfnt) |
| 915 |
end |
| 916 |
return -1 |
| 917 |
end |
| 918 |
|
| 919 |
local resource_dummy = { unicodes = { } } |
| 920 |
|
| 921 |
|
| 922 |
|
| 923 |
|
| 924 |
local function get_font_resources(id) |
| 925 |
local fnt = get_font_by_id(id) |
| 926 |
if fnt then |
| 927 |
return fnt.resources or resource_dummy |
| 928 |
end |
| 929 |
return resource_dummy |
| 930 |
end |
| 931 |
|
| 932 |
|
| 933 |
|
| 934 |
|
| 935 |
local function get_score_font_resources(name) |
| 936 |
return get_font_resources(get_score_font_id(name)) |
| 937 |
end |
| 938 |
|
| 939 |
local function get_score_font_unicode_pairs(name) |
| 940 |
local unicodes = get_score_font_resources(name).unicodes |
| 941 |
if not font_indexed[name] then |
| 942 |
|
| 943 |
|
| 944 |
|
| 945 |
|
| 946 |
|
| 947 |
|
| 948 |
|
| 949 |
|
| 950 |
local ignored = unicodes['_this_is_hopefully_a_nonexistent_glyph_'] |
| 951 |
font_indexed[name] = true |
| 952 |
end |
| 953 |
return pairs(unicodes) |
| 954 |
end |
| 955 |
|
| 956 |
local inside_score = false |
| 957 |
|
| 958 |
|
| 959 |
|
| 960 |
|
| 961 |
|
| 962 |
|
| 963 |
|
| 964 |
|
| 965 |
|
| 966 |
|
| 967 |
local function at_score_beginning(score_id, top_height, bottom_height, |
| 968 |
has_translation, has_above_lines_text, top_height_adj, bottom_height_adj, |
| 969 |
score_font_name) |
| 970 |
inside_score = true |
| 971 |
local inclusion = score_inclusion[score_id] or 1 |
| 972 |
score_inclusion[score_id] = inclusion + 1 |
| 973 |
score_id = score_id..'.'..inclusion |
| 974 |
cur_score_id = score_id |
| 975 |
if (top_height > top_height_adj or bottom_height < bottom_height_adj |
| 976 |
or has_translation ~= 0 or has_above_lines_text ~= 0) |
| 977 |
and tex.count['gre@variableheightexpansion'] == 1 then |
| 978 |
score_heights = line_heights[score_id] or {} |
| 979 |
if new_line_heights then |
| 980 |
new_score_heights = {} |
| 981 |
new_line_heights[score_id] = new_score_heights |
| 982 |
end |
| 983 |
prev_line_id = tex.getattribute(glyph_id_attr) |
| 984 |
else |
| 985 |
score_heights = nil |
| 986 |
new_score_heights = nil |
| 987 |
end |
| 988 |
local text_font = unsafe_get_font_by_id(font.current()) |
| 989 |
local score_font = unsafe_get_font_by_id(font.id('gre@font@music')) |
| 990 |
local state = md5.sumhexa(string.format('%s|%d|%s|%d|%s', text_font.name, |
| 991 |
text_font.size, score_font.name, score_font.size, space_hash)) |
| 992 |
score_last_syllables = last_syllables[score_id] |
| 993 |
if score_last_syllables and state_hashes[score_id] ~= state then |
| 994 |
score_last_syllables = nil |
| 995 |
end |
| 996 |
score_first_alterations = first_alterations[score_id] |
| 997 |
if score_first_alterations and state_hashes[score_id] ~= state then |
| 998 |
score_first_alterations = nil |
| 999 |
end |
| 1000 |
if new_state_hashes then |
| 1001 |
new_state_hashes[score_id] = state |
| 1002 |
end |
| 1003 |
if new_last_syllables then |
| 1004 |
new_score_last_syllables = {} |
| 1005 |
new_last_syllables[score_id] = new_score_last_syllables |
| 1006 |
end |
| 1007 |
if new_first_alterations then |
| 1008 |
new_score_first_alterations = {} |
| 1009 |
new_first_alterations[score_id] = new_score_first_alterations |
| 1010 |
end |
| 1011 |
|
| 1012 |
luatexbase.add_to_callback('post_linebreak_filter', post_linebreak, 'gregoriotex.post_linebreak', 1) |
| 1013 |
luatexbase.add_to_callback("hyphenate", disable_hyphenation, "gregoriotex.disable_hyphenation", 1) |
| 1014 |
end |
| 1015 |
|
| 1016 |
|
| 1017 |
|
| 1018 |
local function at_score_end() |
| 1019 |
inside_score = false |
| 1020 |
luatexbase.remove_from_callback('post_linebreak_filter', 'gregoriotex.post_linebreak') |
| 1021 |
luatexbase.remove_from_callback("hyphenate", "gregoriotex.disable_hyphenation") |
| 1022 |
per_line_dims = {} |
| 1023 |
per_line_counts = {} |
| 1024 |
saved_dims = {} |
| 1025 |
saved_counts = {} |
| 1026 |
end |
| 1027 |
|
| 1028 |
|
| 1029 |
|
| 1030 |
|
| 1031 |
|
| 1032 |
local function fancyhdr_toggle_callbacks() |
| 1033 |
if inside_score then |
| 1034 |
if luatexbase.is_active_callback('post_linebreak_filter','gregoriotex.post_linebreak') then |
| 1035 |
luatexbase.remove_from_callback('post_linebreak_filter', 'gregoriotex.post_linebreak') |
| 1036 |
luatexbase.remove_from_callback("hyphenate", "gregoriotex.disable_hyphenation") |
| 1037 |
else |
| 1038 |
luatexbase.add_to_callback('post_linebreak_filter', post_linebreak, 'gregoriotex.post_linebreak', 1) |
| 1039 |
luatexbase.add_to_callback("hyphenate", disable_hyphenation, "gregoriotex.disable_hyphenation", 1) |
| 1040 |
end |
| 1041 |
end |
| 1042 |
end |
| 1043 |
|
| 1044 |
|
| 1045 |
|
| 1046 |
local windows = package.config:sub(1,1) == "\\" |
| 1047 |
|
| 1048 |
|
| 1049 |
|
| 1050 |
|
| 1051 |
|
| 1052 |
function lfs.normalize(path) |
| 1053 |
if windows then |
| 1054 |
return (path:gsub("\\", "/")) |
| 1055 |
else |
| 1056 |
return path |
| 1057 |
end |
| 1058 |
end |
| 1059 |
|
| 1060 |
local _attributes = lfs.attributes |
| 1061 |
function lfs.attributes(path, ...) |
| 1062 |
path = lfs.normalize(path) |
| 1063 |
if windows then |
| 1064 |
|
| 1065 |
|
| 1066 |
|
| 1067 |
|
| 1068 |
|
| 1069 |
path = path:gsub("/$", "/.") |
| 1070 |
end |
| 1071 |
|
| 1072 |
return _attributes(path, ...) |
| 1073 |
end |
| 1074 |
|
| 1075 |
function lfs.exists(path) |
| 1076 |
return lfs.attributes(path, "mode") ~= nil |
| 1077 |
end |
| 1078 |
|
| 1079 |
function lfs.dirname(oldpath) |
| 1080 |
local path = lfs.normalize(oldpath):gsub("[^/]+/*$", "") |
| 1081 |
if path == "" then |
| 1082 |
return oldpath |
| 1083 |
end |
| 1084 |
return path |
| 1085 |
end |
| 1086 |
|
| 1087 |
local function delete_versioned_files(dir, base, ext) |
| 1088 |
|
| 1089 |
filename = "^"..base.."%-%d+_%d+_%d+[-%a%d]*%."..ext.."$" |
| 1090 |
if dir ~= "" then |
| 1091 |
if lfs.exists(dir) then |
| 1092 |
for a in lfs.dir(dir) do |
| 1093 |
a = lfs.normalize(a) |
| 1094 |
if a:match(filename) then |
| 1095 |
info("Deleting old file %s", dir..a) |
| 1096 |
os.remove(dir..a) |
| 1097 |
end |
| 1098 |
end |
| 1099 |
end |
| 1100 |
else |
| 1101 |
for a in lfs.dir(lfs.currentdir()) do |
| 1102 |
a = lfs.normalize(a) |
| 1103 |
if a:match(filename) then |
| 1104 |
info("Deleting old file %s", a) |
| 1105 |
os.remove(a) |
| 1106 |
end |
| 1107 |
end |
| 1108 |
end |
| 1109 |
end |
| 1110 |
|
| 1111 |
function table.extend(x, y) |
| 1112 |
table.move(y, 1, #y, #x+1, x) |
| 1113 |
end |
| 1114 |
|
| 1115 |
local function compile_gabc(gabc_file, gtex_file, glog_file, allow_deprecated) |
| 1116 |
info("compiling the score %s...", gabc_file) |
| 1117 |
local cmd = {gregorio_exe()} |
| 1118 |
if tex.count['gre@generate@pointandclick'] == 1 then |
| 1119 |
table.insert(cmd, '-p') |
| 1120 |
end |
| 1121 |
if not allow_deprecated then |
| 1122 |
table.insert(cmd, '-D') |
| 1123 |
end |
| 1124 |
|
| 1125 |
table.extend(cmd, {'-W', '-o', gtex_file, '-l', glog_file, gabc_file}) |
| 1126 |
info("running: %s", table.concat(cmd, ' ')) |
| 1127 |
res = os.spawn(cmd) |
| 1128 |
|
| 1129 |
if res == nil then |
| 1130 |
err("\nSomething went wrong when executing\n '%s'.\n" |
| 1131 |
.."shell-escape mode may not be activated. Try\n\n" |
| 1132 |
.."%s --shell-escape %s.tex\n\n" |
| 1133 |
.."See the documentation of Gregorio or your TeX\n" |
| 1134 |
.."distribution to automatize it.", |
| 1135 |
table.concat(cmd, ' '), tex.formatname, tex.jobname) |
| 1136 |
elseif res ~= 0 then |
| 1137 |
err("\nSomething went wrong when executing\n '%s'", |
| 1138 |
table.concat(cmd, ' ')) |
| 1139 |
else |
| 1140 |
|
| 1141 |
local glog = io.open(glog_file, 'a') |
| 1142 |
if glog == nil then |
| 1143 |
warn("\n Unable to open %s for writing. If another program depends on %s, latexmk may not recognize the dependency", glog_file, glog_file) |
| 1144 |
else |
| 1145 |
glog:close() |
| 1146 |
end |
| 1147 |
|
| 1148 |
glog = io.open(glog_file, 'r') |
| 1149 |
if glog == nil then |
| 1150 |
err("\n Unable to open %s for reading", glog_file) |
| 1151 |
else |
| 1152 |
for line in glog:lines() do |
| 1153 |
warn(line) |
| 1154 |
end |
| 1155 |
glog:close() |
| 1156 |
end |
| 1157 |
|
| 1158 |
if res ~= 0 then |
| 1159 |
err("\nAn error occured when compiling the score file\n" |
| 1160 |
.."'%s' with %s.\nPlease check your score file.", gabc_file, |
| 1161 |
gregorio_exe()) |
| 1162 |
else |
| 1163 |
|
| 1164 |
|
| 1165 |
|
| 1166 |
local gtex = io.open(gtex_file, 'a') |
| 1167 |
if gtex == nil then |
| 1168 |
warn("\n Unable to open %s for writing. If another program depends on %s, latexmk may not recognize the dependency.", gtex_file, gtex_file) |
| 1169 |
else |
| 1170 |
gtex:close() |
| 1171 |
end |
| 1172 |
end |
| 1173 |
end |
| 1174 |
end |
| 1175 |
|
| 1176 |
local function locate_file(filename) |
| 1177 |
local result |
| 1178 |
if not gre_input_path then |
| 1179 |
gre_input_path = {""} |
| 1180 |
end |
| 1181 |
for i,k in pairs(gre_input_path) do |
| 1182 |
k = lfs.normalize(k) |
| 1183 |
log("Looking in %s", k) |
| 1184 |
if lfs.isfile(k .. filename) then |
| 1185 |
result = k..filename |
| 1186 |
if result == filename then |
| 1187 |
log("Found %s directly", filename) |
| 1188 |
else |
| 1189 |
log("Found %s in %s", filename, k) |
| 1190 |
end |
| 1191 |
break |
| 1192 |
end |
| 1193 |
end |
| 1194 |
if not result then |
| 1195 |
result = kpse.find_file(filename) |
| 1196 |
if result then |
| 1197 |
log("Found %s at\n%s using kpsewhich", filename, result) |
| 1198 |
if string.match(result," ") then |
| 1199 |
warn("%s contains a space in the path\nTeX will likely complain about this", filename) |
| 1200 |
end |
| 1201 |
else |
| 1202 |
log("Cannot find %s", filename) |
| 1203 |
end |
| 1204 |
end |
| 1205 |
return result |
| 1206 |
end |
| 1207 |
|
| 1208 |
local function include_score(gabc_file, force_gabccompile, allow_deprecated) |
| 1209 |
gabc_file = lfs.normalize(gabc_file) |
| 1210 |
|
| 1211 |
if string.match(gabc_file, "[#%%]") then |
| 1212 |
err("GABC filename contains invalid character(s): # %%\n" |
| 1213 |
.."Rename the file and retry: %s", gabc_file) |
| 1214 |
end |
| 1215 |
local gabc_dir, base |
| 1216 |
local extensions = {['gabc']=true, ['gtex']=true, ['tex']=true} |
| 1217 |
if extensions[string.match(gabc_file, "([^%./]*)$")] then |
| 1218 |
gabc_dir, base = string.match(gabc_file, "(.-)([^/]-)%.?[^%./]*$") |
| 1219 |
else |
| 1220 |
gabc_dir, base = string.match(gabc_file, "(.-)([^/]*)$") |
| 1221 |
end |
| 1222 |
local base_cleaned = base:gsub("[%s%+%&%*%?$@:;!\"\'`]", "-") |
| 1223 |
|
| 1224 |
|
| 1225 |
gabc_file = string.format("%s%s.gabc", gabc_dir, base) |
| 1226 |
local gabc_found = locate_file(gabc_file) |
| 1227 |
if gabc_found then |
| 1228 |
gabc_found = lfs.normalize(gabc_found) |
| 1229 |
end |
| 1230 |
|
| 1231 |
|
| 1232 |
local output_dir = base_output_dir..'/'..gabc_dir |
| 1233 |
output_dir = string.explode(output_dir, '/') |
| 1234 |
for i, _ in ipairs(output_dir) do |
| 1235 |
if output_dir[i] == '..' then output_dir[i] = 'dotdot' end |
| 1236 |
end |
| 1237 |
output_dir = table.concat(output_dir, '/') |
| 1238 |
info('Output directory: %s', output_dir) |
| 1239 |
if not lfs.exists(output_dir) then |
| 1240 |
local ok, message = lfs.mkdirp(output_dir) |
| 1241 |
if not ok then |
| 1242 |
info('Could not create directory %s: %s', output_dir, message) |
| 1243 |
end |
| 1244 |
end |
| 1245 |
|
| 1246 |
|
| 1247 |
gtex_file = string.format("%s%s-%s.gtex", output_dir, base_cleaned, |
| 1248 |
internalversion:gsub("%.", "_")) |
| 1249 |
glog_file = string.format("%s%s-%s.glog", output_dir, base_cleaned, |
| 1250 |
internalversion:gsub("%.", "_")) |
| 1251 |
|
| 1252 |
|
| 1253 |
local needs_compile = false |
| 1254 |
if gabc_found then |
| 1255 |
if lfs.exists(gtex_file) then |
| 1256 |
local gtex_timestamp = lfs.attributes(gtex_file).modification |
| 1257 |
local gabc_timestamp = lfs.attributes(gabc_found).modification |
| 1258 |
if gtex_timestamp < gabc_timestamp then |
| 1259 |
log("%s has been modified and %s needs to be updated. Recompiling the gabc file", gabc_found, gtex_file) |
| 1260 |
needs_compile = true |
| 1261 |
end |
| 1262 |
else |
| 1263 |
log("The file %s does not exist. Compiling gabc file", gtex_file) |
| 1264 |
needs_compile = true |
| 1265 |
end |
| 1266 |
else |
| 1267 |
if lfs.exists(gtex_file) then |
| 1268 |
log("The file %s does not exist. Using gtex file", gabc_file) |
| 1269 |
else |
| 1270 |
err("The file %s does not exist", gabc_file) |
| 1271 |
return |
| 1272 |
end |
| 1273 |
end |
| 1274 |
if needs_compile then |
| 1275 |
|
| 1276 |
|
| 1277 |
delete_versioned_files(gabc_dir, base_cleaned, 'gtex') |
| 1278 |
delete_versioned_files(gabc_dir, base_cleaned, 'glog') |
| 1279 |
|
| 1280 |
delete_versioned_files(output_dir, base_cleaned, 'gtex') |
| 1281 |
delete_versioned_files(output_dir, base_cleaned, 'glog') |
| 1282 |
|
| 1283 |
local gabc = io.open(gabc_found, 'r') |
| 1284 |
if gabc == nil then |
| 1285 |
err("\n Unable to open %s", gabc_found) |
| 1286 |
return |
| 1287 |
else |
| 1288 |
gabc:close() |
| 1289 |
end |
| 1290 |
compile_gabc(gabc_found, gtex_file, glog_file, allow_deprecated) |
| 1291 |
end |
| 1292 |
|
| 1293 |
|
| 1294 |
tex.print(string.format([[\input %s\relax]], gtex_file)) |
| 1295 |
end |
| 1296 |
|
| 1297 |
local function direct_gabc(gabc, header, allow_deprecated) |
| 1298 |
info('Processing gabc snippet...') |
| 1299 |
local f = io.open(snippet_filename, 'w') |
| 1300 |
|
| 1301 |
gabc = gabc:match('^()%s*$') and '' or gabc:match('^%s*(.*%S)') |
| 1302 |
f:write('name:direct-gabc;\n'..(header or '')..'\n%%\n'..gabc:gsub('\\par', '\n')) |
| 1303 |
f:close() |
| 1304 |
cmd = {gregorio_exe(), '-W'} |
| 1305 |
if allow_deprecated then table.insert(cmd, '-D') end |
| 1306 |
table.extend(cmd, {'-o', tmpname, '-l', snippet_logname, snippet_filename}) |
| 1307 |
info('Running %s', table.concat(cmd, ' ')) |
| 1308 |
local content = get_prog_output(cmd, tmpname, '*a') |
| 1309 |
if content == nil then |
| 1310 |
err("\nSomething went wrong when executing\n %s\n" |
| 1311 |
.."shell-escape mode may not be activated. Try\n\n" |
| 1312 |
.."%s --shell-escape %s.tex\n\n" |
| 1313 |
.."See the documentation of Gregorio or your TeX\n" |
| 1314 |
.."distribution to automatize it.", cmd, tex.formatname, tex.jobname) |
| 1315 |
else |
| 1316 |
tex.print(content:explode('\n')) |
| 1317 |
end |
| 1318 |
local glog = io.open(snippet_logname, 'a+') |
| 1319 |
if glog == nil then |
| 1320 |
err("\n Unable to open %s", snippet_logname) |
| 1321 |
else |
| 1322 |
local size = glog:seek('end') |
| 1323 |
if size > 0 then |
| 1324 |
glog:seek('set') |
| 1325 |
local line |
| 1326 |
for line in glog:lines() do |
| 1327 |
warn(line) |
| 1328 |
end |
| 1329 |
warn("*** end of warnings/errors processing snippet ***") |
| 1330 |
end |
| 1331 |
glog:close() |
| 1332 |
end |
| 1333 |
if not (debug_types_activated['snippet'] or debug_types_activated['all']) then |
| 1334 |
os.remove(snippet_filename) |
| 1335 |
os.remove(snippet_logname) |
| 1336 |
end |
| 1337 |
end |
| 1338 |
|
| 1339 |
local function get_gregoriotexluaversion() |
| 1340 |
return internalversion |
| 1341 |
end |
| 1342 |
|
| 1343 |
local function check_one_font_version(name) |
| 1344 |
local gregoriofont = get_font_by_name(name) |
| 1345 |
if gregoriofont then |
| 1346 |
local fontversion = gregoriofont.shared.rawdata.metadata.version |
| 1347 |
if fontversion and string.match(fontversion, "%d+%.%d+%.%d+") ~= string.match(internalversion, "%d+%.%d+%.%d+") then |
| 1348 |
local fontname = gregoriofont.shared.rawdata.metadata.fontname |
| 1349 |
err("\nUncoherent file versions!\ngregoriotex.tex is version %s\nwhile %s.ttf is version %s\nplease reinstall one so that the\nversions match", string.match(internalversion, "%d+%.%d+%.%d+"), fontname, string.match(fontversion, "%d+%.%d+%.%d+")) |
| 1350 |
end |
| 1351 |
end |
| 1352 |
end |
| 1353 |
|
| 1354 |
local function check_font_version() |
| 1355 |
check_one_font_version('gre@font@music') |
| 1356 |
check_one_font_version('gre@font@music@hollow') |
| 1357 |
check_one_font_version('gre@font@music@hole') |
| 1358 |
end |
| 1359 |
|
| 1360 |
local function map_font(name, prefix) |
| 1361 |
log("Mapping font %s", name) |
| 1362 |
local glyph, unicode |
| 1363 |
for glyph, unicode in get_score_font_unicode_pairs(name) do |
| 1364 |
if unicode >= 0 and not string.match(glyph, '%.') then |
| 1365 |
debugmessage("mapfont", "Setting \\Gre%s%s to \\char%d", prefix, glyph, unicode) |
| 1366 |
tex.sprint(catcode_at_letter, string.format( |
| 1367 |
[[\xdef\Gre%s%s{\char%d}]], prefix, glyph, unicode)) |
| 1368 |
end |
| 1369 |
end |
| 1370 |
end |
| 1371 |
|
| 1372 |
local function init_variant_font(font_name, for_score, gre_factor) |
| 1373 |
if font_name ~= '*' then |
| 1374 |
local font_table = for_score and score_fonts or symbol_fonts |
| 1375 |
if font_table[font_name] == nil then |
| 1376 |
local font_csname = variant_prefix..string.gsub(tostring(next_variant), '[0-9]', number_to_letter) |
| 1377 |
font_table[font_name] = font_csname |
| 1378 |
log("Registering variant font %s as %s.", font_name, font_csname) |
| 1379 |
if for_score then |
| 1380 |
local _, name, factor |
| 1381 |
_, _, name, factor = string.find(font_name, '^([^@]*)@(%d*)$') |
| 1382 |
name = name or font_name |
| 1383 |
factor = factor or '100000' |
| 1384 |
local size = gre_factor * tonumber(factor) |
| 1385 |
tex.print(catcode_at_letter, string.format( |
| 1386 |
[[\global\font\%s = {name:%s} at %s sp\relax ]], |
| 1387 |
font_csname, name, size)) |
| 1388 |
|
| 1389 |
loaded_font_sizes[font_name] = {size = size, gre_factor = gre_factor} |
| 1390 |
if font_factors[font_name] == nil then |
| 1391 |
font_factors[font_name] = factor |
| 1392 |
end |
| 1393 |
else |
| 1394 |
|
| 1395 |
tex.print(catcode_at_letter, string.format( |
| 1396 |
[[\gdef\%sSymReload#1{{\edef\localsize{#1}\ifx\localsize\%sSymSize\relax\relax\else\global\font\%s = {name:%s} at \localsize pt\relax\xdef\%sSymSize{\localsize}\fi}}\xdef\%sSymSize{0}\%sSymReload{\gre@symbolfontsize}]], |
| 1397 |
font_csname, font_csname, font_csname, font_name, font_csname, |
| 1398 |
font_csname, font_csname)) |
| 1399 |
end |
| 1400 |
next_variant = next_variant + 1 |
| 1401 |
end |
| 1402 |
end |
| 1403 |
end |
| 1404 |
|
| 1405 |
local function set_score_glyph(csname, font_csname, char) |
| 1406 |
debugmessage('changeglyph', [[Setting \%s to \%s\char%d]], csname, font_csname, char) |
| 1407 |
tex.print(catcode_at_letter, string.format( |
| 1408 |
[[\edef\%s{{\noexpand\%s\char%d}}]], csname, font_csname, char)) |
| 1409 |
end |
| 1410 |
|
| 1411 |
local function set_common_score_glyph(csname, font_csname, char) |
| 1412 |
|
| 1413 |
debugmessage('changeglyph', [[Setting \%s to \char%d]], csname, char) |
| 1414 |
tex.print(catcode_at_letter, string.format( |
| 1415 |
[[\edef\%s{{\char%d}}]], csname, char)) |
| 1416 |
end |
| 1417 |
|
| 1418 |
local function set_symbol_glyph(csname, font_csname, char) |
| 1419 |
tex.print(catcode_at_letter, string.format( |
| 1420 |
[[\def\%s{\%sSymReload{\gre@symbolfontsize}{\%s\char%d}\relax}]], |
| 1421 |
csname, font_csname, font_csname, char)) |
| 1422 |
end |
| 1423 |
|
| 1424 |
local function set_sized_symbol_glyph(csname, font_csname, char) |
| 1425 |
tex.print(catcode_at_letter, string.format( |
| 1426 |
[[\gdef\%s#1{\%sSymReload{#1}{\%s\char%d}\relax}]], |
| 1427 |
csname, font_csname, font_csname, char)) |
| 1428 |
end |
| 1429 |
|
| 1430 |
local function def_glyph(csname, font_name, glyph, font_table, setter) |
| 1431 |
local font_csname = font_table[font_name] |
| 1432 |
local char |
| 1433 |
if string.match(glyph, '^%d+$') then |
| 1434 |
char = tonumber(glyph) |
| 1435 |
else |
| 1436 |
local fid = font_id(font_csname) |
| 1437 |
if fid < 0 then |
| 1438 |
err('\nFont %s is not defined.', font_name) |
| 1439 |
end |
| 1440 |
char = get_font_resources(fid).unicodes[glyph] |
| 1441 |
if char == nil then |
| 1442 |
err('\nGlyph %s in font %s was not found.', glyph, font_name) |
| 1443 |
end |
| 1444 |
end |
| 1445 |
setter(csname, font_csname, char) |
| 1446 |
end |
| 1447 |
|
| 1448 |
local function general_font_for(cavum) |
| 1449 |
if cavum == 'Hollow' then |
| 1450 |
return 'greciliae-hollow' |
| 1451 |
elseif cavum == 'Hole' then |
| 1452 |
return 'greciliae-hole' |
| 1453 |
else |
| 1454 |
return 'greciliae' |
| 1455 |
end |
| 1456 |
end |
| 1457 |
|
| 1458 |
local function change_single_score_glyph(glyph_name, cavum, font_name, replacement) |
| 1459 |
if font_name == '*' then |
| 1460 |
def_glyph('Gre'..cavum..'CP'..glyph_name, general_font_for(cavum), |
| 1461 |
replacement, score_fonts, set_common_score_glyph) |
| 1462 |
else |
| 1463 |
def_glyph('Gre'..cavum..'CP'..glyph_name, font_name, replacement, |
| 1464 |
score_fonts, set_score_glyph) |
| 1465 |
end |
| 1466 |
end |
| 1467 |
|
| 1468 |
local function change_score_glyph(glyph_name, font_name, replacement, cavum) |
| 1469 |
cavum = cavum or '' |
| 1470 |
if string.match(glyph_name, '%*') then |
| 1471 |
glyph_name = '^'..glyph_name:gsub('%*', '.*')..'$' |
| 1472 |
if replacement ~= '' and not string.match(replacement, '^%.') then |
| 1473 |
err('If a wildcard is supplied for glyph name, replacement must be blank or start with a dot.') |
| 1474 |
end |
| 1475 |
local general_font = general_font_for(cavum) |
| 1476 |
local other_font |
| 1477 |
if font_name == '*' then |
| 1478 |
other_font = get_score_font_resources(general_font).unicodes |
| 1479 |
else |
| 1480 |
other_font = get_score_font_resources(font_name).unicodes |
| 1481 |
end |
| 1482 |
local name, char |
| 1483 |
for name, char in get_score_font_unicode_pairs(general_font) do |
| 1484 |
if not string.match(name, '%.') and char >= 0 and string.match(name, glyph_name) then |
| 1485 |
local matched_replacement = name..replacement |
| 1486 |
if other_font[matched_replacement] ~= nil and other_font[matched_replacement] >= 0 then |
| 1487 |
change_single_score_glyph(name, cavum, font_name, matched_replacement) |
| 1488 |
end |
| 1489 |
end |
| 1490 |
end |
| 1491 |
else |
| 1492 |
if string.match(replacement, '^%.') then |
| 1493 |
replacement = glyph_name..replacement |
| 1494 |
end |
| 1495 |
change_single_score_glyph(glyph_name, cavum, font_name, replacement) |
| 1496 |
end |
| 1497 |
end |
| 1498 |
|
| 1499 |
local function reset_score_glyph(glyph_name, cavum) |
| 1500 |
cavum = cavum or '' |
| 1501 |
local general_font = general_font_for(cavum) |
| 1502 |
if string.match(glyph_name, '%*') then |
| 1503 |
glyph_name = '^'..glyph_name:gsub('%*', '.*')..'$' |
| 1504 |
local name, char |
| 1505 |
for name, char in get_score_font_unicode_pairs(general_font) do |
| 1506 |
if not string.match(name, '%.') and char >= 0 and string.match(name, glyph_name) then |
| 1507 |
set_common_score_glyph('Gre'..cavum..'CP'..name, nil, char) |
| 1508 |
end |
| 1509 |
end |
| 1510 |
else |
| 1511 |
local char = get_score_font_resources(general_font).unicodes[glyph_name] |
| 1512 |
if char == nil then |
| 1513 |
err('\nGlyph %s was not found.', glyph_name) |
| 1514 |
end |
| 1515 |
set_common_score_glyph('Gre'..cavum..'CP'..glyph_name, nil, char) |
| 1516 |
end |
| 1517 |
end |
| 1518 |
|
| 1519 |
local function set_font_factor(font_name, font_factor) |
| 1520 |
font_factors[font_name] = font_factor |
| 1521 |
end |
| 1522 |
|
| 1523 |
local function scale_score_fonts(gre_factor) |
| 1524 |
for font_name, font_csname in pairs(score_fonts) do |
| 1525 |
if loaded_font_sizes[font_name] and font_factors[font_name] and loaded_font_sizes[font_name].size ~= gre_factor * font_factors[font_name] then |
| 1526 |
local _, name |
| 1527 |
_, _, name, _ = string.find(font_name, '^([^@]*)@(%d*)$') |
| 1528 |
name = name or font_name |
| 1529 |
local size = gre_factor * font_factors[font_name] |
| 1530 |
log("%s : %s : rescaling %s to %s", font_name, font_csname, name, size) |
| 1531 |
tex.print(catcode_at_letter, string.format( |
| 1532 |
[[\global\font\%s = {name:%s} at %s sp\relax ]], |
| 1533 |
font_csname, name, size)) |
| 1534 |
loaded_font_sizes[font_name] = {size = size, gre_factor = gre_factor} |
| 1535 |
end |
| 1536 |
end |
| 1537 |
end |
| 1538 |
|
| 1539 |
local function def_symbol(csname, font_name, glyph, sized) |
| 1540 |
def_glyph(csname, font_name, glyph, symbol_fonts, |
| 1541 |
sized and set_sized_symbol_glyph or set_symbol_glyph) |
| 1542 |
end |
| 1543 |
|
| 1544 |
local function font_size() |
| 1545 |
tex.print(string.format('%.2f', (unsafe_get_font_by_id(font.current()).size / 65536.0))) |
| 1546 |
end |
| 1547 |
|
| 1548 |
local function adjust_line_height_internal(heights, inside_discretionary, for_next_line) |
| 1549 |
local backup_dims = saved_dims |
| 1550 |
local backup_counts = saved_counts |
| 1551 |
|
| 1552 |
local name, value |
| 1553 |
for name, value in pairs(saved_dims) do |
| 1554 |
tex.sprint(catcode_at_letter, string.format( |
| 1555 |
[[\grechangedim{%s}{%s}{%s}]], name, value[1], value[2])) |
| 1556 |
end |
| 1557 |
for name, value in pairs(saved_counts) do |
| 1558 |
tex.sprint(catcode_at_letter, string.format( |
| 1559 |
[[\grechangecount{%s}{%s}]], name, value)) |
| 1560 |
end |
| 1561 |
|
| 1562 |
saved_dims = {} |
| 1563 |
saved_counts = {} |
| 1564 |
|
| 1565 |
local line_dims = per_line_dims[heights[1]] |
| 1566 |
if line_dims ~= nil then |
| 1567 |
for name, value in pairs(line_dims) do |
| 1568 |
tex.sprint(catcode_at_letter, string.format( |
| 1569 |
[[\gre@changedimforline{%s}{%s}{%s}]], name, value[1], value[2])) |
| 1570 |
end |
| 1571 |
end |
| 1572 |
local line_counts = per_line_counts[heights[1]] |
| 1573 |
if line_counts ~= nil then |
| 1574 |
for name, value in pairs(line_counts) do |
| 1575 |
tex.sprint(catcode_at_letter, string.format( |
| 1576 |
[[\gre@changecountforline{%s}{%s}]], name, value)) |
| 1577 |
end |
| 1578 |
end |
| 1579 |
|
| 1580 |
tex.sprint(catcode_at_letter, string.format( |
| 1581 |
[[\gre@calculate@additionalspaces{%d}{%d}{%d}{%d}]], |
| 1582 |
heights[2], heights[3], heights[4], heights[5])) |
| 1583 |
if inside_discretionary == 0 then |
| 1584 |
tex.sprint(catcode_at_letter, [[\gre@updateleftbox ]]) |
| 1585 |
end |
| 1586 |
if for_next_line then |
| 1587 |
|
| 1588 |
|
| 1589 |
local name, value |
| 1590 |
for name, value in pairs(saved_dims) do |
| 1591 |
tex.sprint(catcode_at_letter, string.format( |
| 1592 |
[[\grechangedim{%s}{%s}{%s}]], name, value[1], value[2])) |
| 1593 |
end |
| 1594 |
for name, value in pairs(saved_counts) do |
| 1595 |
tex.sprint(catcode_at_letter, string.format( |
| 1596 |
[[\grechangecount{%s}{%s}]], name, value)) |
| 1597 |
end |
| 1598 |
|
| 1599 |
saved_dims = backup_dims |
| 1600 |
saved_counts = backup_counts |
| 1601 |
end |
| 1602 |
end |
| 1603 |
|
| 1604 |
local function adjust_line_height(inside_discretionary, for_next_line) |
| 1605 |
if score_heights then |
| 1606 |
local heights = nil |
| 1607 |
if for_next_line then |
| 1608 |
local last = score_heights['last'] |
| 1609 |
if last then |
| 1610 |
|
| 1611 |
|
| 1612 |
local target_id = tex.getattribute(glyph_id_attr) |
| 1613 |
while target_id <= last do |
| 1614 |
heights = score_heights[target_id] |
| 1615 |
if heights then break end |
| 1616 |
target_id = target_id + 1 |
| 1617 |
end |
| 1618 |
end |
| 1619 |
else |
| 1620 |
heights = score_heights[tex.getattribute(glyph_id_attr)] |
| 1621 |
end |
| 1622 |
if heights then |
| 1623 |
adjust_line_height_internal(heights, inside_discretionary, for_next_line) |
| 1624 |
end |
| 1625 |
end |
| 1626 |
end |
| 1627 |
|
| 1628 |
local function save_dim(name, value, modifier) |
| 1629 |
saved_dims[name] = { value, modifier } |
| 1630 |
end |
| 1631 |
|
| 1632 |
local function save_count(name, value) |
| 1633 |
saved_counts[name] = value |
| 1634 |
end |
| 1635 |
|
| 1636 |
local function change_next_score_line_dim(line_expr, name, value, modifier) |
| 1637 |
local linenum_str |
| 1638 |
for linenum_str in string.gmatch(line_expr, "%s*([^,]+)%s*") do |
| 1639 |
local linenum = tonumber(linenum_str) |
| 1640 |
local line_dims = per_line_dims[linenum] |
| 1641 |
if line_dims == nil then |
| 1642 |
line_dims = {} |
| 1643 |
per_line_dims[linenum] = line_dims |
| 1644 |
end |
| 1645 |
line_dims[name] = { value, modifier } |
| 1646 |
end |
| 1647 |
end |
| 1648 |
|
| 1649 |
local function change_next_score_line_count(line_expr, name, value) |
| 1650 |
local linenum_str |
| 1651 |
for linenum_str in string.gmatch(line_expr, "([^,]+)") do |
| 1652 |
local linenum = tonumber(linenum_str) |
| 1653 |
local line_counts = per_line_counts[linenum] |
| 1654 |
if line_counts == nil then |
| 1655 |
line_counts = {} |
| 1656 |
per_line_counts[linenum] = line_counts |
| 1657 |
end |
| 1658 |
line_counts[name] = value |
| 1659 |
end |
| 1660 |
end |
| 1661 |
|
| 1662 |
local function prep_save_position(index, fn) |
| 1663 |
if saved_positions[cur_score_id] == nil then |
| 1664 |
saved_positions[cur_score_id] = {} |
| 1665 |
end |
| 1666 |
saved_positions[cur_score_id][index] = { fn = fn } |
| 1667 |
end |
| 1668 |
|
| 1669 |
local function save_position(index, which) |
| 1670 |
tex.print(catcode_at_letter, string.format([[\luatexlatelua{gregoriotex.late_save_position('%s', %d, %d, \number\gre@lastxpos, \number\gre@lastypos)}]], cur_score_id, index, which)) |
| 1671 |
end |
| 1672 |
|
| 1673 |
local function late_save_position(score_id, index, which, xpos, ypos) |
| 1674 |
info('saving %s, %d [%d] (%d,%d)', score_id, index, which, xpos, ypos) |
| 1675 |
local pos = saved_positions[score_id][index] |
| 1676 |
if pos == nil then |
| 1677 |
err('Attempting to use unprepared position save slot %d', index) |
| 1678 |
return |
| 1679 |
end |
| 1680 |
|
| 1681 |
|
| 1682 |
|
| 1683 |
|
| 1684 |
|
| 1685 |
|
| 1686 |
pos['x'..which] = xpos |
| 1687 |
pos['y'..which] = ypos |
| 1688 |
if pos.x1 ~= nil and pos.y1 ~= nil and pos.x2 ~= nil and pos.y2 ~= nil then |
| 1689 |
pos.fn(score_id, index, pos) |
| 1690 |
|
| 1691 |
end |
| 1692 |
end |
| 1693 |
|
| 1694 |
local function compute_saved_length(score_id, index, pos) |
| 1695 |
if new_saved_lengths[score_id] == nil then |
| 1696 |
new_saved_lengths[score_id] = {} |
| 1697 |
end |
| 1698 |
new_saved_lengths[score_id][index] = pos.x2 - pos.x1 |
| 1699 |
info('computed length for %s, %d: %d', score_id, index, |
| 1700 |
new_saved_lengths[score_id][index]) |
| 1701 |
end |
| 1702 |
|
| 1703 |
local function save_length(index, which) |
| 1704 |
if which == 1 then |
| 1705 |
prep_save_position(index, compute_saved_length) |
| 1706 |
end |
| 1707 |
save_position(index, which) |
| 1708 |
end |
| 1709 |
|
| 1710 |
local function compute_saved_newline_before_euouae(score_id, index, pos) |
| 1711 |
if new_saved_newline_before_euouae[score_id] == nil then |
| 1712 |
new_saved_newline_before_euouae[score_id] = {} |
| 1713 |
end |
| 1714 |
new_saved_newline_before_euouae[score_id][index] = pos.y2 ~= pos.y1 |
| 1715 |
info('computed euouae for %s, %d: %s', score_id, index, |
| 1716 |
new_saved_newline_before_euouae[score_id][index]) |
| 1717 |
end |
| 1718 |
|
| 1719 |
local function save_euouae(index, which) |
| 1720 |
if which == 1 then |
| 1721 |
prep_save_position(index, compute_saved_newline_before_euouae) |
| 1722 |
end |
| 1723 |
tex.sprint(catcode_at_letter, [[\gre@savepos]]) |
| 1724 |
save_position(index, which) |
| 1725 |
end |
| 1726 |
|
| 1727 |
local function var_brace_len(brace) |
| 1728 |
if saved_lengths[cur_score_id] ~= nil then |
| 1729 |
local length = saved_lengths[cur_score_id][brace] |
| 1730 |
if saved_lengths[cur_score_id][brace] ~= nil then |
| 1731 |
if length > 0 then |
| 1732 |
tex.print(string.format('%dsp', length)) |
| 1733 |
return |
| 1734 |
else |
| 1735 |
warn('Dynamically sized signs spanning multiple lines unsupported, using length 2mm.') |
| 1736 |
end |
| 1737 |
end |
| 1738 |
end |
| 1739 |
tex.print('2mm') |
| 1740 |
end |
| 1741 |
|
| 1742 |
|
| 1743 |
local function is_ypos_different(index) |
| 1744 |
if saved_newline_before_euouae[cur_score_id] ~= nil then |
| 1745 |
local newline_before_euouae = |
| 1746 |
saved_newline_before_euouae[cur_score_id][index] |
| 1747 |
if newline_before_euouae then |
| 1748 |
tex.sprint([[\number0\relax ]]) |
| 1749 |
return |
| 1750 |
end |
| 1751 |
end |
| 1752 |
tex.sprint([[\number1\relax ]]) |
| 1753 |
end |
| 1754 |
|
| 1755 |
local function width_to_bp(width, value_if_star) |
| 1756 |
if width == '*' then |
| 1757 |
tex.print(value_if_star or '0') |
| 1758 |
else |
| 1759 |
tex.print(tex.sp(width) * 1.00375 / 65536) |
| 1760 |
end |
| 1761 |
end |
| 1762 |
|
| 1763 |
|
| 1764 |
local function hypotenuse(width, height) |
| 1765 |
log("width %s height %s", width, height) |
| 1766 |
local a = tex.sp(width) |
| 1767 |
local b = tex.sp(height) |
| 1768 |
tex.sprint(math.sqrt((a * a) + (b * b)) .. 'sp') |
| 1769 |
end |
| 1770 |
|
| 1771 |
|
| 1772 |
local function rotation(width, height) |
| 1773 |
local a = tex.sp(width) |
| 1774 |
local b = tex.sp(height) |
| 1775 |
tex.sprint(math.deg(math.atan2(b, a))) |
| 1776 |
end |
| 1777 |
|
| 1778 |
local function scale_space(factor) |
| 1779 |
local skip = tex.getskip('gre@skip@temp@four') |
| 1780 |
skip.width = skip.width * factor |
| 1781 |
tex.setskip('gre@skip@temp@four',skip) |
| 1782 |
|
| 1783 |
end |
| 1784 |
|
| 1785 |
local function set_header_capture(header, macro_name, flags) |
| 1786 |
if macro_name == '' then |
| 1787 |
capture_header_macro[header] = nil |
| 1788 |
log("no longer capturing header %s", header) |
| 1789 |
else |
| 1790 |
local macro = {} |
| 1791 |
macro.name = macro_name |
| 1792 |
flags:gsub("([^,]+)", function(flag) |
| 1793 |
if flag == "string" then macro.takes_string = true |
| 1794 |
elseif flag == "name" then macro.takes_header_name = true |
| 1795 |
else err("unknown header capture flag: %s", flag) |
| 1796 |
end |
| 1797 |
end) |
| 1798 |
capture_header_macro[header] = macro |
| 1799 |
log("capturing header %s using %s, string=%s, name=%s", header, macro.name, macro.takes_string, macro.takes_header_name) |
| 1800 |
end |
| 1801 |
end |
| 1802 |
|
| 1803 |
local function capture_header(header, value) |
| 1804 |
local macro = capture_header_macro[header] |
| 1805 |
if macro ~= nil then |
| 1806 |
tex.sprint(string.format([[\%s{]], macro.name)) |
| 1807 |
if macro.takes_header_name then |
| 1808 |
tex.sprint(-2, header) |
| 1809 |
tex.sprint('}{') |
| 1810 |
end |
| 1811 |
if macro.takes_string then |
| 1812 |
tex.sprint(-2, value) |
| 1813 |
else |
| 1814 |
tex.sprint(value) |
| 1815 |
end |
| 1816 |
tex.print('}') |
| 1817 |
end |
| 1818 |
end |
| 1819 |
|
| 1820 |
local function mode_part(part) |
| 1821 |
if part ~= '' then |
| 1822 |
if not unicode.utf8.match(part, '^%p') then |
| 1823 |
tex.sprint([[\thinspace]]) |
| 1824 |
end |
| 1825 |
tex.print(part) |
| 1826 |
end |
| 1827 |
end |
| 1828 |
|
| 1829 |
|
| 1830 |
local function is_last_syllable_on_line() |
| 1831 |
if score_last_syllables then |
| 1832 |
if score_last_syllables[tex.getattribute(syllable_id_attr)] then |
| 1833 |
tex.print(0) |
| 1834 |
else |
| 1835 |
tex.print(1) |
| 1836 |
end |
| 1837 |
else |
| 1838 |
|
| 1839 |
|
| 1840 |
tex.print(0) |
| 1841 |
end |
| 1842 |
end |
| 1843 |
|
| 1844 |
local function hash_spaces(name, value) |
| 1845 |
hashed_spaces[name] = value |
| 1846 |
local k, _ |
| 1847 |
local keys = {} |
| 1848 |
for k,_ in pairs(hashed_spaces) do |
| 1849 |
table.insert(keys, k) |
| 1850 |
end |
| 1851 |
table.sort(keys) |
| 1852 |
local mash = '' |
| 1853 |
for _,k in ipairs(keys) do |
| 1854 |
mash = string.format('%s%s:%s|', mash, k, hashed_spaces[k]) |
| 1855 |
end |
| 1856 |
space_hash = md5.sumhexa(mash) |
| 1857 |
end |
| 1858 |
|
| 1859 |
local function is_first_alteration(next) |
| 1860 |
|
| 1861 |
|
| 1862 |
|
| 1863 |
|
| 1864 |
if score_first_alterations then |
| 1865 |
local i = tex.getattribute(alteration_id_attr) |
| 1866 |
if next == 1 then |
| 1867 |
i = i + 1 |
| 1868 |
while score_first_alterations[i] == nil and i < score_first_alterations['last'] do |
| 1869 |
i = i + 1 |
| 1870 |
end |
| 1871 |
end |
| 1872 |
local v = score_first_alterations[i] |
| 1873 |
if v ~= nil then |
| 1874 |
debugmessage("alteration", "%s", v) |
| 1875 |
tex.print(v) |
| 1876 |
else |
| 1877 |
tex.print(0) |
| 1878 |
end |
| 1879 |
else |
| 1880 |
tex.print(0) |
| 1881 |
end |
| 1882 |
end |
| 1883 |
|
| 1884 |
gregoriotex.number_to_letter = number_to_letter |
| 1885 |
gregoriotex.init = init |
| 1886 |
gregoriotex.include_score = include_score |
| 1887 |
gregoriotex.at_score_end = at_score_end |
| 1888 |
gregoriotex.at_score_beginning = at_score_beginning |
| 1889 |
gregoriotex.check_font_version = check_font_version |
| 1890 |
gregoriotex.get_gregoriotexluaversion = get_gregoriotexluaversion |
| 1891 |
gregoriotex.map_font = map_font |
| 1892 |
gregoriotex.init_variant_font = init_variant_font |
| 1893 |
gregoriotex.change_score_glyph = change_score_glyph |
| 1894 |
gregoriotex.reset_score_glyph = reset_score_glyph |
| 1895 |
gregoriotex.scale_score_fonts = scale_score_fonts |
| 1896 |
gregoriotex.set_font_factor = set_font_factor |
| 1897 |
gregoriotex.def_symbol = def_symbol |
| 1898 |
gregoriotex.font_size = font_size |
| 1899 |
gregoriotex.direct_gabc = direct_gabc |
| 1900 |
gregoriotex.adjust_line_height = adjust_line_height |
| 1901 |
gregoriotex.var_brace_len = var_brace_len |
| 1902 |
gregoriotex.save_length = save_length |
| 1903 |
gregoriotex.mark_translation = mark_translation |
| 1904 |
gregoriotex.mark_abovelinestext = mark_abovelinestext |
| 1905 |
gregoriotex.width_to_bp = width_to_bp |
| 1906 |
gregoriotex.hypotenuse = hypotenuse |
| 1907 |
gregoriotex.rotation = rotation |
| 1908 |
gregoriotex.scale_space = scale_space |
| 1909 |
gregoriotex.set_header_capture = set_header_capture |
| 1910 |
gregoriotex.capture_header = capture_header |
| 1911 |
gregoriotex.is_ypos_different = is_ypos_different |
| 1912 |
gregoriotex.save_euouae = save_euouae |
| 1913 |
gregoriotex.mode_part = mode_part |
| 1914 |
gregoriotex.set_debug_string = set_debug_string |
| 1915 |
gregoriotex.late_save_position = late_save_position |
| 1916 |
gregoriotex.is_last_syllable_on_line = is_last_syllable_on_line |
| 1917 |
gregoriotex.hash_spaces = hash_spaces |
| 1918 |
gregoriotex.save_dim = save_dim |
| 1919 |
gregoriotex.save_count = save_count |
| 1920 |
gregoriotex.change_next_score_line_dim = change_next_score_line_dim |
| 1921 |
gregoriotex.change_next_score_line_count = change_next_score_line_count |
| 1922 |
gregoriotex.set_base_output_dir = set_base_output_dir |
| 1923 |
gregoriotex.is_first_alteration = is_first_alteration |
| 1924 |
gregoriotex.fancyhdr_toggle_callbacks = fancyhdr_toggle_callbacks |
| 1925 |
|
| 1926 |
dofile(kpse.find_file('gregoriotex-nabc.lua', 'lua')) |
| 1927 |
dofile(kpse.find_file('gregoriotex-signs.lua', 'lua')) |
| 1928 |
dofile(kpse.find_file('gregoriotex-symbols.lua', 'lua')) |