-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathutil.ly
335 lines (294 loc) · 13.3 KB
/
util.ly
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
\version "2.14.2"
% Michael's utilities
dropLyricsIV = {
\override LyricText #'extra-offset = #'(0 . -0.4)
\override LyricHyphen #'extra-offset = #'(0 . -0.4)
\override LyricExtender #'extra-offset = #'(0 . -0.4)
\override StanzaNumber #'extra-offset = #'(0 . -0.4)
}
dropLyricsV = {
\override LyricText #'extra-offset = #'(0 . -0.5)
\override LyricHyphen #'extra-offset = #'(0 . -0.5)
\override LyricExtender #'extra-offset = #'(0 . -0.5)
\override StanzaNumber #'extra-offset = #'(0 . -0.5)
}
dropLyricsVI = {
\override LyricText #'extra-offset = #'(0 . -0.6)
\override LyricHyphen #'extra-offset = #'(0 . -0.6)
\override LyricExtender #'extra-offset = #'(0 . -0.6)
\override StanzaNumber #'extra-offset = #'(0 . -0.6)
}
dropLyricsVII = {
\override LyricText #'extra-offset = #'(0 . -0.7)
\override LyricHyphen #'extra-offset = #'(0 . -0.7)
\override LyricExtender #'extra-offset = #'(0 . -0.7)
\override StanzaNumber #'extra-offset = #'(0 . -0.7)
}
dropLyricsVIII = {
\override LyricText #'extra-offset = #'(0 . -0.8)
\override LyricHyphen #'extra-offset = #'(0 . -0.8)
\override LyricExtender #'extra-offset = #'(0 . -0.8)
\override StanzaNumber #'extra-offset = #'(0 . -0.8)
}
dropLyricsIX = {
\override LyricText #'extra-offset = #'(0 . -0.9)
\override LyricHyphen #'extra-offset = #'(0 . -0.9)
\override LyricExtender #'extra-offset = #'(0 . -0.9)
\override StanzaNumber #'extra-offset = #'(0 . -0.9)
}
dropLyricsXI = {
\override LyricText #'extra-offset = #'(0 . -1.1)
\override LyricHyphen #'extra-offset = #'(0 . -1.1)
\override LyricExtender #'extra-offset = #'(0 . -1.1)
\override StanzaNumber #'extra-offset = #'(0 . -1.1)
}
dropLyricsXII = {
\override LyricText #'extra-offset = #'(0 . -1.2)
\override LyricHyphen #'extra-offset = #'(0 . -1.2)
\override LyricExtender #'extra-offset = #'(0 . -1.2)
\override StanzaNumber #'extra-offset = #'(0 . -1.2)
}
dropLyricsXV = {
\override LyricText #'extra-offset = #'(0 . -1.5)
\override LyricHyphen #'extra-offset = #'(0 . -1.5)
\override LyricExtender #'extra-offset = #'(0 . -1.5)
\override StanzaNumber #'extra-offset = #'(0 . -1.5)
}
raiseLyrics = {
\revert LyricText #'extra-offset
\revert LyricHyphen #'extra-offset
\revert LyricExtender #'extra-offset
\revert StanzaNumber #'extra-offset
}
#(define (splitUp chars pred)
(define (helper chars pred lists)
(if (null? chars) lists
(let* (
(firstc (car chars))
(newlists (helper (cdr chars) pred lists))
(firstlist (car newlists)))
(if (or (null? firstlist)
(eq? (pred firstc) (pred (car firstlist))))
(cons (cons firstc firstlist) (cdr newlists))
(cons (list firstc) newlists))
)
))
(helper chars pred (list '()) )
)
#(define-markup-command (fakeCaps layout props str) (markup?)
"Fake small-caps by turning (sequences of) lower-case letters into upper-case and changing the fontsize"
(let*
((chars (string->list str))
(lists (splitUp chars
(lambda (x) (and
(< (char->integer x) 128)
(char-lower-case? x) ))))
(strings (map
(lambda (s)
(if (char-lower-case? (car s))
(markup #:fontsize -2 (list->string (map char-upcase s)))
(list->string s) ) )
lists))
(mu (markup #:override '(word-space . 0) (make-line-markup strings))) )
(interpret-markup layout props mu)) )
%% UTF-8 utilities
#(define (list->utf-8 l) (apply string-append (map ly:wide-char->utf-8 l)))
#(define (utf-8->list str)
(define (numBytes leader)
(cond ((< leader #x80) 1)
((< leader #xc0) (begin (stderr "programming-error: bad utf-8:~x\n" leader) 1))
((< leader #xe0) 2)
((< leader #xf0) 3)
((< leader #xf8) 4)
(else (begin (stderr "programming-error: utf-8 too big:~x\n" leader) 1))))
(define (helper start l n)
(if (= n 0) start
(helper (+ (* start #x40) (modulo (car l) #x40)) (cdr l) (- n 1))))
(define (utf-8->int l)
(let* ((leader (car l))
(n (- (numBytes leader) 1))
(fac (/ #x80 (expt 2 n)))
(rest (cdr l))
(result (helper (modulo leader fac) rest n)))
result))
(define (toListHelper lst chars)
(if (null? lst) (reverse chars)
(let* ((c (utf-8->int lst))
(n (numBytes (car lst)))
(t (list-tail lst n))
(newC (cons c chars)))
(toListHelper t newC))))
(toListHelper (map char->integer (string->list str)) '() ))
%These are appropriate for Junicode, and other fonts. Override as necessary
oldStyleZeroCode = ##xF730
smallCapsACode = ##xF761
%For Linux Libertine
oldStyleZeroCodeLL = ##xE01A
smallCapsACodeLL = ##xE051
#(define (change-char-helper aa test? offset)
(if (string? aa)
(let* ((chars (string->list aa))
(tosc (map (lambda (c)
(if (and (<= (char->integer c) 127) (test? c))
(ly:wide-char->utf-8 (+ (char->integer c) offset))
(string c) ) )
chars))
(newStr (apply string-append tosc)))
newStr)
aa)
)
#(define (change-char-helper aa test? offset)
(if (string? aa)
(let* ((chars (utf-8->list aa))
(tosc (map (lambda (c)
(if (and (<= c 127) (test? (integer->char c)))
(ly:wide-char->utf-8 (+ c offset))
(if (and (not (eq? test? char-numeric?)) (and (<= c 255) (>= c 224)))
(ly:wide-char->utf-8 (+ c offset))
(if (= c #x0153)
(ly:wide-char->utf-8 #xF6FA)
(ly:wide-char->utf-8 c) ) ) ) )
chars))
(newStr (apply string-append tosc)))
newStr)
aa)
)
#(define (to-old-style str) (change-char-helper str char-numeric?
(- oldStyleZeroCode (char->integer #\0))))
#(define (to-small-caps str) (change-char-helper str char-lower-case?
(- smallCapsACode (char->integer #\a))))
#(define-markup-command (realCaps layout props str) (markup?)
"Real small capitals"
(interpret-markup layout props (to-small-caps str)))
#(define-markup-command (oldStyleNum layout props str) (markup?)
"Old-style numerals"
(interpret-markup layout props (to-old-style str)))
#(define-markup-command (oldStylePageNum layout props str) (markup?)
"Old-style Page numbers"
(interpret-markup layout props (to-old-style (number->string (chain-assoc-get 'page:page-number props -1)))))
#(define-markup-command (smallCapsOldStyle layout props str) (markup?)
"Real small caps and old-style numerals"
(interpret-markup layout props (to-old-style (to-small-caps str))))
#(define-markup-command (concat layout props markups) (markup-list?)
"Concatenate markups with no spaces"
(interpret-markup layout (cons '((word-space . 0)) props)
(make-line-markup markups)))
%%%
#(define-public (bar-number-print grob)
"Print function for making oldStyle numbers. Useful for BarNumber, for example"
(let*
((text (ly:grob-property grob 'text))
(layout (ly:grob-layout grob))
(defs (ly:output-def-lookup layout 'text-font-defaults))
(props (ly:grob-alist-chain grob defs)))
(ly:text-interface::interpret-markup layout
props
(if (string? text)
(markup #:oldStyleNum text)
text))))
%%%
% Utilities for Baskerville 1757 ligatures
% like string-split, but with a substring rather than a char
#(define (string-split-sub str sub)
(let* ((idx (string-contains str sub)))
(if idx
(let* ((len1 (string-length sub))
(first (substring str 0 idx))
(rest (substring str (+ idx len1)))
(lrest (string-split-sub rest sub)))
(cons first lrest))
(list str)
)))
% takes a string, returns a list
#(define (replace-subs str from to)
(if (string? str)
(list-join (string-split-sub str from) to)
(list str)))
%list -> list
#(define (replace-subr strings subs . rest)
(let* ((fn (if(null? rest) (lambda (x) x) (car rest) )))
(if (null? subs)
strings
(replace-subr (apply append (map
(lambda (str) (replace-subs str (car subs) (fn (cadr subs)) ))
strings ))
(cddr subs) fn))))
#(define (make-lig-list str . rest)
(apply replace-subr (cons (list str) (cons (list "ffi" "ffi" "ffl" "ffl" "fi" "fi" "fl" "fl" "ff" "ff" ) rest))))
#(define (make-blig-markup-fn ligFont)
(lambda (s)
(define (imagstep x) (* (log x) (/ 6 (log 2))))
(let ((step (imagstep (/ 11 12))))
(markup #:fontsize step #:override `(font-name . ,ligFont) s))))
#(define (make-bv-lig-list ligFont str)
(apply replace-subr (cons (list str) (cons (list "ffi" "I" "ffl" "L" "fi" "i" "fl" "l" "ff" "f" ) (list
(make-blig-markup-fn ligFont ))))))
#(define (make-bv-hlig-list ligFont l)
(apply replace-subr (cons l (cons (list "ct" "C" "st" "S" ) (list
(make-blig-markup-fn ligFont) )))))
#(define (add-ligs str)
(apply string-append (make-lig-list str)))
doHlig = ##t
#(define-markup-command (addBLigs layout props str) (markup?)
"Add Baskerville 1757 ligatures, including st and ct, to a string"
(let* ((str1 (add-ligs str))
(shape (chain-assoc-get 'font-shape props #f))
(isItalic (eqv? shape 'italic))
(ligFont (if isItalic "Baskerville1757Ligatures Italic" "Baskerville1757Ligatures"))
(markups1 (make-bv-lig-list ligFont str1))
(markups (if doHlig (make-bv-hlig-list ligFont markups1) markups1))
(result (markup #:override '(word-space . 0) (make-line-markup markups ))))
(interpret-markup layout props result)))
#(define-markup-list-command (addBLigsList layout props strings) (markup-list?)
"Add Baskerville 1757 ligatures, including st and ct, to a list of strings"
(interpret-markup-list layout props
(map (lambda (x) (markup #:addBLigs x)) strings)))
#(define-public (add-bligs-print grob)
"Print function for adding Baskerville ligatures"
(let*
((text (ly:grob-property grob 'text))
(layout (ly:grob-layout grob))
(defs (ly:output-def-lookup layout 'text-font-defaults))
(props (ly:grob-alist-chain grob defs)))
(ly:text-interface::interpret-markup layout
props
(if (string? text)
(markup #:addBLigs text)
text))))
#(define space-set (list->char-set (string->list ".?-;,:“”‘’–— */()[]{}|<>!`~&")))
#(define (width grob text-string)
(let*
((layout (ly:grob-layout grob))
(props (ly:grob-alist-chain grob (ly:output-def-lookup layout
'text-font-defaults))
)
)
(if (and (string? text-string) (eq? 0 (string-length text-string)))
0
(cdr (ly:stencil-extent (ly:text-interface::interpret-markup layout
props (markup text-string)) X)
)
)
)
)
#(define (center-on-word grob)
(let* (
(text (ly:grob-property-data grob 'text))
(syllable (if (string? text) text ""))
(word-position (if (integer? (string-skip syllable space-set)) (string-skip syllable space-set) 0))
(word-end (if (integer? (string-skip-right syllable space-set)) (+ (string-skip-right syllable space-set) 1) (string-length syllable)))
(preword (substring syllable 0 word-position))
(word (substring syllable word-position word-end ))
(preword-width (if (string? text) (width grob preword) 0))
(word-width (if (string? text) (width grob word) (width grob text)))
(notehead (ly:grob-parent grob X))
(refp (ly:grob-common-refpoint notehead grob X))
(note-extent (ly:grob-extent notehead refp X))
(note-width (- (cdr note-extent) (car note-extent)))
)
(if (= -1 (ly:grob-property-data grob 'self-alignment-X))
(- 0 preword-width)
(- (/ (- note-width word-width) 2) preword-width)
)
)
)