summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/scripts/lyluatex/lyluatex.lua
blob: 599eb7b8bb3b12a933a37aa5b78c11e4ff91d44e (plain)
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
local err, warn, info, log = luatexbase.provides_module({
    name               = "lyluatex",
    version            = '0',
    greinternalversion = internalversion,
    date               = "2017/11/27",
    description        = "Module lyluatex.",
    author             = "The Gregorio Project (see CONTRIBUTORS.md)",
    copyright          = "2008-2017 - The Gregorio Project",
    license            = "MIT",
})

local md5 = require 'md5'


LILYPOND = 'lilypond'
TMP = 'tmp_ly'
N = 0


function ly_definir_programme(lilypond)
    if lilypond then LILYPOND = lilypond end
end


function contenuIntegral(contenu)
    local content =""
    for i, Line in ipairs(contenu:explode('\n')) do
	if Line:find("^%s*[^%%]*\\include") then
	    local i = io.open(Line:gsub('%s*\\include%s*"(.*)"%s*$', "%1"), 'r')
	    if i then
		content = content .. contenuIntegral(i:read('*a'))
	    else
		content = content .. Line .. "\n"
	    end
	else
	    content = content .. Line .. "\n"
	end
    end
    return content
end


function direct_ly(ly, largeur, facteur)
    N = N + 1
    facteur = calcul_facteur(facteur)
    ly = ly:gsub('\\par ', '\n'):gsub('\\([^%s]*) %-([^%s])', '\\%1-%2')
    local sortie = TMP..'/'..string.gsub(md5.sumhexa(contenuIntegral(ly))..'-'..facteur..'-'..largeur, '%.', '-')
    if not lfs.isfile(sortie..'-systems.tex') then
        compiler_ly(entete_lilypond(facteur, largeur - 10)..'\n'..ly, sortie, true)
    end
    retour_tex(sortie)
end


function inclure_ly(entree, currfiledir, largeur, facteur, pleinepage)
    facteur = calcul_facteur(facteur)
    nom = splitext(entree, 'ly')
    entree = currfiledir..nom..'.ly'
    if not lfs.isfile(entree) then entree = kpse.find_file(nom..'.ly') end
    if not lfs.isfile(entree) then err("Le fichier %s.ly n'existe pas.", nom) end
    local i = io.open(entree, 'r')
    ly = i:read('*a')
    i:close()
    local sortie = TMP..'/' ..string.gsub(md5.sumhexa(contenuIntegral(ly))..'-'..facteur..'-'..largeur..'-', '%.', '-')
    if pleinepage then sortie = sortie..'-pleinepage' end
    if not lfs.isfile(sortie..'-systems.tex') then
        if pleinepage then
            compiler_ly(ly, sortie, false, dirname(entree))
            i = io.open(sortie..'-systems.tex', 'w')
            i:write('\\includepdf[pages=-]{'..sortie..'}')
            i:close()
        else
            compiler_ly(entete_lilypond(facteur, largeur - 10)..'\n'..ly, sortie, true, dirname(entree))
        end
    end
    retour_tex(sortie)
end


function compiler_ly(ly, sortie, eps, include)
    mkdirs(dirname(sortie))
    local commande = LILYPOND.." "..
        "-dno-point-and-click "..
        "-djob-count=2 "..
        "-ddelete-intermediate-files "
    if eps then commande = commande.."-dbackend=eps " end
    if include then commande = commande.."-I '"..lfs.currentdir().."/"..include.."' " end
    commande = commande.."-o "..sortie.." -"
    local p = io.popen(commande, 'w')
    p:write(ly)
    p:close()
end


function entete_lilypond(facteur, largeur)
    return string.format(
[[%%En-tête
\version "2.18.2"
#(define default-toplevel-book-handler
  print-book-with-defaults-as-systems )

#(define toplevel-book-handler
  (lambda ( . rest)
  (set! output-empty-score-list #f)
  (apply print-book-with-defaults rest)))

#(define toplevel-music-handler
  (lambda ( . rest)
   (apply collect-music-for-book rest)))

#(define toplevel-score-handler
  (lambda ( . rest)
   (apply collect-scores-for-book rest)))

#(define toplevel-text-handler
  (lambda ( . rest)
   (apply collect-scores-for-book rest)))


#(set-global-staff-size %s)


%%Paramètres de la partition
\paper{
    indent = 0\mm
    line-width = %s\pt
}

%%Partition originale
]],
facteur,
largeur
)
end


function calcul_facteur(facteur)
    if facteur == 0 then facteur = fontinfo(font.current()).size/39321.6 end
    return facteur
end


function retour_tex(sortie)
    local i = io.open(sortie..'-systems.tex', 'r')
    contenu = i:read("*all")
    i:close()
    texoutput, _ = string.gsub(
        contenu,
        [[includegraphics{]], [[includegraphics{]]..dirname(sortie)
    )
    tex.print(([[\noindent]]..texoutput):explode('\n'))
end


function dirname(str)
    if str:match(".-/.-") then
        local name = string.gsub(str, "(.*/)(.*)", "%1")
        return name
    else
        return ''
    end
end


function splitext(str, ext)
    if str:match(".-%..-") then
        local name = string.gsub(str, "(.*)(%." .. ext .. ")", "%1")
        return name
    else
        return str
    end
end


function mkdirs(str)
    path = '.'
    for dir in string.gmatch(str, '([^%/]+)') do
        path = path .. '/' .. dir
        lfs.mkdir(path)
    end
end


local fontdata = fonts.hashes.identifiers
function fontinfo(id)
    local f = fontdata[id]
    if f then
        return f
    end
    return font.fonts[id]
end


mkdirs(TMP)