-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
lyluatex.lua
1434 lines (1297 loc) · 45.1 KB
/
lyluatex.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
-- luacheck: ignore ly log self luatexbase internalversion font fonts tex token kpse status ly_opts
local err, warn, info, log = luatexbase.provides_module({
name = "lyluatex",
version = '1.1.5', --LYLUATEX_VERSION
date = "2023/04/18", --LYLUATEX_DATE
description = "Module lyluatex.",
author = "The Gregorio Project − (see Contributors.md)",
copyright = "2015-2023 - jperon and others",
license = "MIT",
})
local lib = require(kpse.find_file("luaoptions-lib.lua") or "luaoptions-lib.lua")
local ly_opts = lua_options.client('ly')
local md5 = require 'md5'
local lfs = require 'lfs'
local ly = {
err = err,
varwidth_available = kpse.find_file('varwidth.sty')
}
local Score = ly_opts.options
Score.__index = Score
local FILELIST
local DIM_OPTIONS = {
'extra-bottom-margin',
'extra-top-margin',
'gutter',
'hpadding',
'indent',
'leftgutter',
'line-width',
'max-protrusion',
'max-left-protrusion',
'max-right-protrusion',
'rightgutter',
'paperwidth',
'paperheight',
'voffset'
}
local HASHIGNORE = {
'autoindent',
'cleantmp',
'do-not-print',
'force-compilation',
'hpadding',
'max-left-protrusion',
'max-right-protrusion',
'print-only',
'valign',
'voffset'
}
local MXML_OPTIONS = {
'absolute',
'language',
'lxml',
'no-articulation-directions',
'no-beaming',
'no-page-layout',
'no-rest-positions',
'verbose',
}
local TEXINFO_OPTIONS = {'doctitle', 'nogettext', 'texidoc'}
local LY_HEAD = [[
%%File header
\version "<<<version>>>"
<<<language>>>
#(define inside-lyluatex #t)
#(set-global-staff-size <<<staffsize>>>)
<<<preamble>>>
\header {
copyright = ""
tagline = ##f
}
\paper{
<<<paper>>> two-sided = ##<<<twoside>>>
line-width = <<<linewidth>>>\pt
<<<indent>>>
<<<raggedright>>>
<<<fonts>>>
}
\layout{
<<<staffprops>>>
<<<fixbadlycroppedstaffgroupbrackets>>>
}<<<header>>>
%%Follows original score
]]
--[[ ========================== Helper functions ========================== --]]
-- dirty fix as info doesn't work as expected
local oldinfo = info
function info(...)
print('\n(lyluatex)', string.format(...))
oldinfo(...)
end
-- debug acts as info if [debug] is specified
local function debug(...)
if Score.debug then info(...) end
end
local function extract_includepaths(includepaths)
includepaths = includepaths:explode(',')
local cfd
if lib.tex_engine.dist == 'MiKTeX' then
cfd = Score.currfiledir:gsub('^$', '.\\')
else
cfd = Score.currfiledir:gsub('^$', './')
end
table.insert(includepaths, 1, cfd)
for i, path in ipairs(includepaths) do
-- delete initial space (in case someone puts a space after the comma)
includepaths[i] = path:gsub('^ ', ''):gsub('^~', os.getenv("HOME")):gsub('^%.%.', './..')
end
return includepaths
end
local function font_default_staffsize()
return lib.current_font_size()/39321.6
end
local function includes_parse(list)
local includes = ''
if list then
includes = [[
]]
list = list:explode(',')
for _, included_file in ipairs(list) do
includes = includes .. '\\include "'..included_file..'.ly"\n'
end
end
return includes
end
local function locate(file, includepaths, ext)
local result
for _, d in ipairs(extract_includepaths(includepaths)) do
if d:sub(-1) ~= '/' then d = d..'/' end
result = d..file
if lfs.isfile(result) then break end
end
if not (result and lfs.isfile(result)) then
if ext and file:match('%.[^%.]+$') ~= ext then
return locate(file..ext, includepaths)
else
return kpse.find_file(file)
end
end
return result
end
local function range_parse(range, nsystems)
local num = tonumber(range)
if num then return {num} end
-- if nsystems is set, we have insert=systems
if nsystems ~= 0 and range:sub(-1) == '-' then range = range..nsystems end
if not (range == '' or range:match('^%d+%s*-%s*%d*$')) then
warn([[
Invalid value '%s' for item
in list of page ranges. Possible entries:
- Single number
- Range (M-N, N-M or N-)
This item will be skipped!
]],
range
)
return
end
local result = {}
local from, to = tonumber(range:match('^%d+')), tonumber(range:match('%d+$'))
if to then
local dir
if from <= to then dir = 1 else dir = -1 end
for i = from, to, dir do table.insert(result, i) end
return result
else return {range} -- N- with insert=fullpage
end
end
local function set_lyscore(score)
ly.score = score
ly.score.nsystems = ly.score:count_systems()
if score.insert ~= 'fullpage' then -- systems and inline
local hoffset = ly.score.protrusion or 0
if hoffset == '' then hoffset = 0 end
ly.score.hoffset = hoffset..'pt'
for s = 1, ly.score.nsystems do
table.insert(ly.score, ly.score.output..'-'..s)
end
else ly.score[1] = ly.score.output
end
end
--[[ ================ Bounding box calculations =========================== --]]
function bbox_get(filename, line_width)
return bbox_read(filename) or bbox_parse(filename, line_width)
end
function bbox_calc(x_1, x_2, y_1, y_2, line_width)
local bb = {
['protrusion'] = -lib.convert_unit(("%fbp"):format(x_1)),
['r_protrusion'] = lib.convert_unit(("%fbp"):format(x_2)) - line_width,
['width'] = lib.convert_unit(("%fbp"):format(x_2))
}
--FIX #192: height is only calculated if really needed, to prevent errors with huge scores.
function bb.__index(_, k)
if k == 'height' then return lib.convert_unit(("%fbp"):format(y_2)) - lib.convert_unit(("%fbp"):format(y_1)) end
end
setmetatable(bb, bb)
return bb
end
function bbox_parse(filename, line_width)
-- get BoundingBox from EPS file
local bbline = lib.readlinematching('^%%%%BoundingBox', io.open(filename..'.eps', 'r'))
if not bbline then return end
local x_1, y_1, x_2, y_2 = bbline:match('(%--%d+)%s(%--%d+)%s(%--%d+)%s(%--%d+)')
-- try to get HiResBoundingBox from PDF (if 'gs' works)
bbline = lib.readlinematching(
'^%%%%HiResBoundingBox',
io.popen('gs -sDEVICE=bbox -q -dBATCH -dNOPAUSE '..filename..'.pdf 2>&1', 'r')
)
if bbline then
local pbb = bbline:gmatch('(%d+%.%d+)')
-- The HiRes BoundingBox retrieved from the PDF differs from the
-- BoundingBox present in the EPS file. In the PDF (0|0) is the
-- Lower Left corner while in the EPS (0|0) represents the top
-- edge at the start of the staff symbol.
-- Therefore we shift the HiRes results by the (truncated)
-- points of the EPS bounding box.
x_1, y_1, x_2, y_2 = pbb() + x_1, pbb() + y_1, pbb() + x_1, pbb() + y_1
else warn([[gs couldn't be launched; there could be rounding errors.]])
end
local f = io.open(filename .. '.bbox', 'w')
f:write(
string.format("return %f, %f, %f, %f, %f", x_1, y_1, x_2, y_2, line_width)
)
f:close()
return bbox_calc(x_1, x_2, y_1, y_2, line_width)
end
function bbox_read(f)
f = f .. '.bbox'
if lfs.isfile(f) then
local x_1, y_1, x_2, y_2, line_width = dofile(f)
return bbox_calc(x_1, x_2, y_1, y_2, line_width)
end
end
--[[ =============== Functions that output LaTeX code ===================== --]]
function latex_filename(printfilename, insert, input_file)
if printfilename and input_file then
if insert ~= 'systems' then
warn('`printfilename` only works with `insert=systems`')
else
local filename = input_file:gsub("(.*/)(.*)", "\\lyFilename{%2}\\par")
tex.sprint(filename)
end
end
end
function latex_fullpagestyle(style, ppn)
local function texoutput(s) tex.sprint('\\includepdfset{pagecommand='..s..'}%') end
if style == '' then
if ppn then texoutput('\\thispagestyle{empty}')
else texoutput('')
end
else texoutput('\\thispagestyle{'..style..'}')
end
end
function latex_includeinline(pdfname, height, valign, hpadding, voffset)
local v_base
if valign == 'bottom' then v_base = 0
elseif valign == 'top' then v_base = lib.convert_unit('1em') - height
else v_base = (lib.convert_unit('1em') - height) / 2
end
tex.sprint(
string.format(
[[\hspace{%fpt}\raisebox{%fpt}{\includegraphics{%s-1}}\hspace{%fpt}]],
hpadding, v_base + voffset, pdfname, hpadding
)
)
end
function latex_includepdf(pdfname, range, papersize)
tex.sprint(string.format(
[[\includepdf[pages={%s},%s]{%s}]],
table.concat(range, ','), papersize and 'noautoscale' or '', pdfname
))
end
function latex_includesystems(filename, range, protrusion, gutter, staffsize, indent_offset)
local h_offset = protrusion + indent_offset
local texoutput = '\\ifx\\preLilyPondExample\\undefined\\else\\preLilyPondExample\\fi\n'
texoutput = texoutput..'\\par\n'
for index, system in pairs(range) do
if not lfs.isfile(filename..'-'..system..'.eps') then break end
texoutput = texoutput..
string.format([[
\noindent\hspace*{%fpt}\includegraphics{%s}%%
]],
h_offset + gutter, filename..'-'..system
)
if index < #range then
texoutput = texoutput..
string.format([[
\ifx\betweenLilyPondSystem\undefined\par\vspace{%fpt plus %fpt minus %fpt}%%
\else\betweenLilyPondSystem{%s}\fi%%
]],
staffsize / 4, staffsize / 12, staffsize / 16,
index
)
end
end
texoutput = texoutput..'\n\\ifx\\postLilyPondExample\\undefined\\else\\postLilyPondExample\\fi'
tex.sprint(texoutput:explode('\n'))
end
function latex_label(label, labelprefix)
if label then tex.sprint('\\label{'..labelprefix..label..'}%%') end
end
ly.verbenv = {[[\begin{verbatim}]], [[\end{verbatim}]]}
function latex_verbatim(verbatim, ly_code, intertext, version)
if verbatim then
if version then tex.sprint('\\lyVersion{'..version..'}') end
local content = table.concat(ly_code:explode('\n'), '\n'):gsub(
'.*%%%s*begin verbatim', ''):gsub(
'%%%s*end verbatim.*', '')
--[[ We unfortunately need an external file,
as verbatim environments are quite special. --]]
local fname = ly_opts.tmpdir..'/verb.tex'
local f = io.open(fname, 'w')
f:write(
ly.verbenv[1]..'\n'..
content..
'\n'..ly.verbenv[2]:gsub([[\end {]], [[\end{]])..'\n'
)
f:close()
tex.sprint('\\input{'..fname..'}')
if intertext then tex.sprint('\\lyIntertext{'..intertext..'}') end
end
end
--[[ =============================== Classes =============================== --]]
-- Score class
function Score:new(ly_code, options, input_file)
local o = options or {}
setmetatable(o, self)
o.output_names = {}
o.input_file = input_file
o.ly_code = ly_code
return o
end
function Score:bbox(system)
if system then
if not self.bboxes then
self.bboxes = {}
for i = 1, self:count_systems() do
table.insert(self.bboxes, bbox_get(self.output..'-'..i, self['line-width']))
end
end
return self.bboxes[system]
else
if not self.bbox then self.bbox = bbox_get(self.output, self['line-width']) end
return self.bbox
end
end
function Score:calc_properties()
self:calc_staff_properties()
-- add includes to lilypond code
self.ly_code = includes_parse(self.include_before_body)
.. self.ly_code
.. includes_parse(self.include_after_body)
-- fragment and relative
if self.relative and not self.fragment then
-- local option takes precedence over global option
if Score.fragment then self.relative = false end
end
if self.relative then
self.fragment = 'true' -- yes, here we need a string, not a bool
if self.relative == '' then self.relative = 1
else self.relative = tonumber(self.relative)
end
end
if self.fragment == '' then
-- by default, included files shouldn't be fragments
if ly.state == 'file' then self.fragment = false end
end
-- default insertion mode
if self.insert == '' then
if ly.state == 'cmd' then self.insert = 'inline'
else self.insert = 'systems'
end
end
-- staffsize
self.staffsize = tonumber(self.staffsize)
if self.staffsize == 0 then self.staffsize = font_default_staffsize() end
if self.insert == 'inline' or self.insert == 'bare-inline' then
local inline_staffsize = tonumber(self['inline-staffsize'])
if inline_staffsize == 0 then inline_staffsize = self.staffsize / 1.5 end
self.staffsize = inline_staffsize
end
-- dimensions that can be given by LaTeX
for _, dimension in pairs(DIM_OPTIONS) do
self[dimension] = lib.convert_unit(self[dimension])
end
self['max-left-protrusion'] = self['max-left-protrusion'] or self['max-protrusion']
self['max-right-protrusion'] = self['max-right-protrusion'] or self['max-protrusion']
if self.quote then
self.leftgutter = self.leftgutter or self.gutter
self.rightgutter = self.rightgutter or self.gutter
self['line-width'] = self['line-width'] - self.leftgutter - self.rightgutter
else
self.leftgutter = 0
self.rightgutter = 0
end
-- store for comparing protrusion against
self.original_lw = self['line-width']
self.original_indent = self.indent
-- explicit indent disables autoindent
if self.indent then self.autoindent = false end
-- score fonts
if self['current-font-as-main'] then self.rmfamily = self['current-font'] end
-- LilyPond version
if self.addversion then self.addversion = self:lilypond_version(true) end
-- temporary file name
self.output = self:output_filename()
end
function Score:calc_range()
local nsystems = self:count_systems(true)
local printonly, donotprint = self['print-only'], self['do-not-print']
if printonly == '' then printonly = '1-' end
local result = tonumber(printonly) and {tonumber(printonly)} or {}
if not result[1] then
for _, r in pairs(printonly:explode(',')) do
local range = range_parse(r:gsub('^%s', ''):gsub('%s$', ''), nsystems)
if range then
for _, v in pairs(range) do table.insert(result, v) end
end
end
end
local rm_result = tonumber(donotprint) and {tonumber(donotprint)} or {}
if not rm_result[1] then
for _, r in pairs(donotprint:explode(',')) do
local range = range_parse(r:gsub('^%s', ''):gsub('%s$', ''), nsystems)
if range then
for _, v in pairs(range) do table.insert(rm_result, v) end
end
end
end
for _, v in pairs(rm_result) do
local k = lib.contains(result, v)
if k then table.remove(result, k) end
end
return result
end
function Score:calc_staff_properties()
-- preset for bare notation symbols in inline images
if self.insert == 'bare-inline' then self.nostaff = 'true' end
-- handle meta properties
if self.notime then
self.notimesig = 'true'
self.notiming = 'true'
end
if self.nostaff then
self.nostaffsymbol = 'true'
self.notimesig = 'true'
-- do *not* suppress timing
self.noclef = 'true'
end
end
function Score:check_compilation()
local debug_msg, doc_debug_msg
if self.debug then
debug_msg = string.format([[
Please check the log file
and the generated LilyPond code in
%s
%s
]],
self.output..'.log', self.output..'.ly'
)
doc_debug_msg = [[
A log file and a LilyPond file have been written.\\
See log for details.]]
else
debug_msg = [[
If you need more information
than the above message,
please retry with option debug=true.
]]
doc_debug_msg = "Re-run with \\texttt{debug} option to investigate."
end
if self.fragment then
local frag_msg = '\n'..[[
As the input code has been automatically wrapped
with a music expression, you may try repeating
with the `nofragment` option.]]
debug_msg = debug_msg..frag_msg
doc_debug_msg = doc_debug_msg..frag_msg
end
if self:is_compiled() then
if self.lilypond_error then
warn([[
LilyPond reported a failed compilation but
produced a score. %s
]],
debug_msg
)
end
-- we do have *a* score (although labeled as failed by LilyPond)
return true
else
self:clean_failed_compilation()
if self.showfailed then
tex.sprint(string.format([[
\begin{quote}
\minibox[frame]{LilyPond failed to compile a score.\\
%s}
\end{quote}
]],
doc_debug_msg
))
warn([[
LilyPond failed to compile the score.
%s
]],
debug_msg
)
else
err([[
LilyPond failed to compile the score.
%s
]],
debug_msg
)
end
-- We don't have any compiled score
return false
end
end
function Score:check_indent(lp)
local nsystems = self:count_systems()
local function handle_autoindent()
self.indent_offset = 0
if lp.shorten > 0 then
if not self.indent or self.indent == 0 then
self.indent = lp.overflow_left
lp.shorten = lib.max(lp.shorten - lp.overflow_left, 0)
else
self.indent = lib.max(self.indent - lp.overflow_left, 0)
end
lp.changed_indent = true
end
end
local function handle_indent()
if not self.indent_offset then
-- First step: deactivate indent
self.indent_offset = 0
if self:count_systems() > 1 then
-- only recompile if the *original* score has more than 1 system
self.indent = 0
lp.changed_indent = true
end
info('Deactivate indentation because of system selection')
elseif lp.shorten > 0 then
self.indent = 0
self.autoindent = true
-- lp.changed_indent = true
handle_autoindent()
info('Deactivated indent causes protrusion.')
end
end
local function regular_score()
-- score without any indent or with the first system
-- printed regularly, with others following.
return not self.original_indent or
nsystems > 1 and #self.range > 1 and self.range[1] == 1
end
local function simple_noindent()
-- score with indent and only one system
return self.original_indent and nsystems == 1
end
if simple_noindent() then
self.indent_offset = -self.indent
warn('Deactivate indent for single-system score.')
elseif self.autoindent then handle_autoindent()
elseif regular_score() then self.indent_offset = 0
else handle_indent()
end
end
function Score:check_properties()
ly_opts:validate_options(self)
for _, k in pairs(TEXINFO_OPTIONS) do
if self[k] then info([[Option %s is specific to Texinfo: ignoring it.]], k) end
end
if self.fragment then
if (self.input_file or
self.ly_code:find([[\book]]) or
self.ly_code:find([[\header]]) or
self.ly_code:find([[\layout]]) or
self.ly_code:find([[\paper]]) or
self.ly_code:find([[\score]])
) then
warn([[
Found something incompatible with `fragment`
(or `relative`). Setting them to false.
]]
)
self.fragment = false
self.relative = false
end
end
end
function Score:check_protrusion(bbox_func)
self.range = self:calc_range()
if self.insert ~= 'systems' then return self:is_compiled() end
local bb = bbox_func(self.output, self['line-width'])
if not bb then return end
-- line_props lp
local lp = {}
-- Determine offset due to left protrusion
lp.overflow_left = lib.max(bb.protrusion - math.floor(self['max-left-protrusion']), 0)
self.protrusion_left = lp.overflow_left - bb.protrusion
-- Determine further line properties
lp.stave_extent = lp.overflow_left + lib.min(self['line-width'], bb.width)
lp.available = self.original_lw + self['max-right-protrusion']
lp.total_extent = lp.stave_extent + bb.r_protrusion
-- Check if stafflines protrude into the right margin after offsetting
-- Note: we can't *reliably* determine this with ragged one-system scores,
-- possibly resulting in unnecessarily short lines when right protrusion is
-- present
lp.stave_overflow_right = lib.max(lp.stave_extent - self.original_lw, 0)
-- Check if image as a whole protrudes over max-right-protrusion
lp.overflow_right = lib.max(lp.total_extent - lp.available, 0)
lp.shorten = lib.max(lp.stave_overflow_right, lp.overflow_right)
lp.changed_indent = false
self:check_indent(lp, bb)
if lp.shorten > 0 or lp.changed_indent then
self['line-width'] = self['line-width'] - lp.shorten
-- recalculate hash to reflect the reduced line-width
if lp.shorten > 0 then
info('Compiled score exceeds protrusion limit(s)')
end
if lp.changed_indent then info([[Adjusted indent.]]) end
self.output = self:output_filename()
warn('Recompile or reuse cached score')
return
else return true
end
end
function Score:clean_failed_compilation()
for file in lfs.dir(self.tmpdir) do
local filename = self.tmpdir..'/'..file
if filename:find(self.output) then os.remove(filename) end
end
end
function Score:content()
local n = ''
local ly_code = self.ly_code
if self.relative then
self.fragment = 'true' -- in case it would serve later
if self.relative < 0 then
for _ = -1, self.relative, -1 do n = n..',' end
elseif self.relative > 0 then
for _ = 1, self.relative do n = n.."'" end
end
return string.format([[\relative c%s {%s}]], n, ly_code)
elseif self.fragment then return [[{]]..ly_code..[[}]]
else return ly_code
end
end
function Score:count_systems(force)
local count = self.system_count
if force or not count then
count = 0
local systems = self.output:match("[^/]*$").."%-%d+%.eps"
for f in lfs.dir(self.tmpdir) do
if f:match(systems) then
count = count + 1
end
end
self.system_count = count
end
return count
end
function Score:delete_intermediate_files()
for _, filename in pairs(self.output_names) do
if self.insert == 'fullpage' then os.remove(filename..'.ps')
else
os.remove(filename..'-systems.tex')
os.remove(filename..'-systems.texi')
os.remove(filename..'.eps')
end
end
end
function Score:flatten_content(ly_code)
--[[ Produce a flattend string from the original content,
including referenced files (if they can be opened.
Other files (from LilyPond's include path) are considered
irrelevant for the purpose of a hashsum.) --]]
-- Replace percent signs with another character that doesn't
-- meddle with Lua's gsub escape character.
ly_code = ly_code:gsub('%%', '#')
local f
local includepaths = self.includepaths..','..self.tmpdir
if self.input_file then includepaths = self.includepaths..','..lib.dirname(self.input_file) end
for iline in ly_code:gmatch('\\include%s*"[^"]*"') do
f = io.open(locate(iline:match('\\include%s*"([^"]*)"'), includepaths, '.ly') or '')
if f then
ly_code = ly_code:gsub(iline, self:flatten_content(f:read('*a')))
f:close()
end
end
return ly_code
end
function Score:footer()
return includes_parse(self.include_footer)
end
function Score:header()
local header = LY_HEAD
for element in LY_HEAD:gmatch('<<<(%w+)>>>') do
header = header:gsub('<<<'..element..'>>>', self['ly_'..element](self) or '')
end
local wh_dest = self['write-headers']
if wh_dest then
if self.input_file then
local _, ext = lib.splitext(wh_dest)
local header_file = ext and wh_dest
or wh_dest..'/'..lib.splitext(lib.basename(self.input_file), 'ly').."-lyluatex-headers.ily"
lib.mkdirs(lib.dirname(header_file))
local f = io.open(header_file, 'w')
f:write(header
:gsub([[%\include "lilypond%-book%-preamble.ly"]], '')
:gsub([[%#%(define inside%-lyluatex %#t%)]], '')
:gsub('\n+', '\n')
)
f:close()
else
warn([[Ignoring 'write-headers' for non-file score.]])
end
end
return header
end
function Score:is_compiled()
if self['force-compilation'] then return false end
return lfs.isfile(self.output..'.pdf') or lfs.isfile(self.output..'.eps') or self:count_systems(true) ~= 0
end
function Score:is_odd_page() return tex.count['c@page'] % 2 == 1 end
function Score:lilypond_cmd()
local input, mode = '-s -', 'w'
if self.debug or lib.tex_engine.dist == 'MiKTeX' then
local f = io.open(self.output..'.ly', 'w')
f:write(self.complete_ly_code)
f:close()
input = self.output..".ly 2>&1"
mode = 'r'
end
local cmd = '"'..self.program..'" '
.. (self.insert == "fullpage" and "" or "-E ")
.. "-dno-point-and-click -djob-count=2 -dno-delete-intermediate-files "
if self:lilypond_version() >= ly.v{2, 24} then cmd = cmd.."-dtall-page-formats=pdf " end
if self['optimize-pdf'] and self:lilypond_has_TeXGS() then cmd = cmd.."-O TeX-GS -dgs-never-embed-fonts " end
if self.input_file then
cmd = cmd..'-I "'..lib.dirname(self.input_file):gsub('^%./', lfs.currentdir()..'/')..'" '
end
for _, dir in ipairs(extract_includepaths(self.includepaths)) do
cmd = cmd..'-I "'..dir:gsub('^%./', lfs.currentdir()..'/')..'" '
end
cmd = cmd..'-o "'..self.output..'" '..input
debug("Command:\n"..cmd)
return cmd, mode
end
function Score:lilypond_has_TeXGS()
return lib.readlinematching('TeX%-GS', io.popen('"'..self.program..'" --help', 'r'))
end
function Score:lilypond_version()
local version = self._lilypond_version
if not version then
version = lib.readlinematching('GNU LilyPond', io.popen('"'..self.program..'" --version', 'r'))
info(
"Compiling score %s with LilyPond executable '%s'.",
self.output, self.program
)
if not version then return end
version = ly.v{version:match('(%d+)%.(%d+)%.?(%d*)')}
debug("VERSION " .. tostring(version))
self._lilypond_version = version
end
return version
end
function Score:ly_fixbadlycroppedstaffgroupbrackets()
return self.fix_badly_cropped_staffgroup_brackets and [[\context {
\Score
\override SystemStartBracket.after-line-breaking =
#(lambda (grob)
(let ((Y-off (ly:grob-property grob 'Y-extent)))
(ly:grob-set-property! grob 'Y-extent
(cons (- (car Y-off) 1.7) (+ (cdr Y-off) 1.7)))))
}]]
or '%% no fix for badly cropped StaffGroup brackets'
end
function Score:ly_fonts()
if self['pass-fonts'] then
local fonts_def
if self:lilypond_version() >= ly.v{2, 25, 4} then
fonts_def = [[fonts.roman = "%s"
fonts.sans = "%s"
fonts.typewriter = "%s"]]
else
fonts_def = [[
#(define fonts
(make-pango-font-tree "%s"
"%s"
"%s"
(/ staff-height pt 20)))
]]
end
return fonts_def:format(self.rmfamily, self.sffamily, self.ttfamily)
else
return '%% fonts not set'
end
end
function Score:ly_header()
return includes_parse(self.include_header)
end
function Score:ly_indent()
if not (self.indent == false and self.insert == 'fullpage') then
return [[indent = ]]..(self.indent or 0)..[[\pt]]
else
return '%% no indent set'
end
end
function Score:ly_language()
if self.language then return '\\language "'..self.language..'"'..[[
]]
else return '' end
end
function Score:ly_linewidth() return self['line-width'] end
function Score:ly_staffsize() return self.staffsize end
function Score:ly_margins()
local horizontal_margins =
self.twoside and string.format([[
inner-margin = %f\pt]], self:tex_margin_inner())
or string.format([[
left-margin = %f\pt]], self:tex_margin_left())
local tex_top = self['extra-top-margin'] + self:tex_margin_top()
local tex_bottom = self['extra-bottom-margin'] + self:tex_margin_bottom()
if self.fullpagealign == 'crop' then
return string.format([[
top-margin = %f\pt
bottom-margin = %f\pt
%s]],
tex_top, tex_bottom, horizontal_margins
)
elseif self.fullpagealign == 'staffline' then
local top_distance = 4 * tex_top / self.staffsize + 2
local bottom_distance = 4 * tex_bottom / self.staffsize + 2
return string.format([[
top-margin = 0\pt
bottom-margin = 0\pt
%s
top-system-spacing =
#'((basic-distance . %f)
(minimum-distance . %f)
(padding . 0)
(stretchability . 0))
top-markup-spacing =
#'((basic-distance . %f)
(minimum-distance . %f)
(padding . 0)
(stretchability . 0))
last-bottom-spacing =
#'((basic-distance . %f)
(minimum-distance . %f)
(padding . 0)
(stretchability . 0))
]],
horizontal_margins,
top_distance,
top_distance,
top_distance,
top_distance,
bottom_distance,
bottom_distance
)
else
err([[
Invalid argument for option 'fullpagealign'.
Allowed: 'crop', 'staffline'.
Given: %s
]],
self.fullpagealign
)
end
end
function Score:ly_paper()
local system_count =
self['system-count'] == '0' and ''
or 'system-count = '..self['system-count']..'\n '
local papersize = '#(set-paper-size "'..(self.papersize or 'lyluatexfmt')..'")'
if self.insert == 'fullpage' then
local first_page_number = self['first-page-number'] or tex.count['c@page']
local pfpn = self['print-first-page-number'] and 't' or 'f'
local ppn = self['print-page-number'] and 't' or 'f'
return string.format([[
%s%s
print-page-number = ##%s
print-first-page-number = ##%s
first-page-number = %d
%s]],
system_count, papersize, ppn, pfpn,
first_page_number, self:ly_margins()
)
else
return string.format([[%s%s]], papersize..[[
]], system_count)
end
end
function Score:ly_preamble()
local result = string.format(
[[#(set! paper-alist (cons '("lyluatexfmt" . (cons (* %f pt) (* %f pt))) paper-alist))]],
self.paperwidth, self.paperheight
)
if self.insert == 'fullpage' then
return result
else
return result..[[
\include "lilypond-book-preamble.ly"]]
end
end