1 %%% markup.ily -- generic markup commands
3 %%% Author: Nicolas Sceaux <nicolas.sceaux@free.fr>
8 %%% like \hspace, but for vertical space
10 %%% \smallCaps <string>
11 %%% like built-in \smallCaps, but dealing with accented letters
13 %%% \when-property <symbol> <markup>
14 %%% if symbol is find in properties, interpret the markup
15 %%% otherwise, return an empty stencil
17 %%% \line-width-ratio <ratio> <markup>
18 %%% interpret markup with a line-width set to current line-width * ratio
21 %%% build a copyight line, using the maintainer and copyrightYear
24 %%% \wordwrap-center <markup-list>
25 %%% like wordwrap, but center align the lines
27 %%% Markup lines commands
28 %%% =====================
29 %%% \wordwrap-center-lines <markup-list>
30 %%% make a markup list composed centered lines of text.
32 %%% Redefinition of \column, \justify and \wordwrap
33 %%% to fix spacing around blocks
34 #(define-markup-command (column layout props args) (markup-list?)
35 #:properties ((baseline-skip))
36 (let ((arg-stencils (interpret-markup-list layout props args)))
37 (stack-lines DOWN 0.0 0
38 (space-lines baseline-skip
39 (remove ly:stencil-empty? arg-stencils)))))
41 #(define-markup-command (justify layout props args)
43 #:properties ((baseline-skip)
44 wordwrap-internal-markup-list)
47 (space-lines baseline-skip
48 (wordwrap-internal-markup-list layout props #t args))))
50 #(define-markup-command (wordwrap layout props args)
52 #:properties ((baseline-skip)
53 wordwrap-internal-markup-list)
56 (space-lines baseline-skip
57 (wordwrap-internal-markup-list layout props #f args))))
62 #(define-markup-command (vspace layout props amount) (number?)
63 "This produces a invisible object taking vertical space."
64 (let ((amount (* amount 3.0)))
66 (ly:make-stencil "" (cons -1 1) (cons 0 amount))
67 (ly:make-stencil "" (cons -1 1) (cons amount amount)))))
69 #(define-markup-command (copyright layout props) ()
70 (let* ((maintainer (chain-assoc-get 'header:maintainer props))
71 (this-year (+ 1900 (tm:year (gmtime (current-time)))))
72 (year (string->number (or (chain-assoc-get 'header:copyrightYear props)
73 (number->string this-year)))))
74 (interpret-markup layout props
76 (if (= year this-year)
77 (format #f "~a" this-year)
78 (format #f "~a-~a" year this-year))
81 #(define-markup-command (today layout props) ()
82 (let ((today (gmtime (current-time))))
83 (interpret-markup layout props
85 (+ 1900 (tm:year today))
89 #(define-markup-command (today-french layout props) ()
90 (let* ((date (gmtime (current-time)))
91 (months '#("janvier" "février" "mars" "avril"
92 "mai" "juin" "juillet" "août"
93 "septembre" "octobre" "novembre"
95 (day (if (= (tm:mday date) 1)
96 (markup (#:concat ("1" #:super "er")))
97 (number->string (tm:mday date))))
98 (month (vector-ref months (tm:mon date)))
99 (year (number->string (+ 1900 (tm:year date)))))
101 layout props (markup day month year))))
103 #(define-markup-command (when-property layout props symbol markp) (symbol? markup?)
104 (if (chain-assoc-get symbol props)
105 (interpret-markup layout props markp)
106 (ly:make-stencil '() '(1 . -1) '(1 . -1))))
108 #(define-markup-command (apply-fromproperty layout props fn symbol)
110 (let ((m (chain-assoc-get symbol props)))
112 (interpret-markup layout props (fn m))
115 #(define-markup-command (line-width-ratio layout props width-ratio arg)
117 (interpret-markup layout props
118 (markup #:override (cons 'line-width (* width-ratio
119 (chain-assoc-get 'line-width props)))
122 #(define-markup-list-command (line-width-ratio-lines layout props width-ratio args)
123 (number? markup-list?)
124 (interpret-markup-list layout props
125 (make-override-lines-markup-list
126 (cons 'line-width (* width-ratio
127 (chain-assoc-get 'line-width props)))
130 #(define-markup-list-command (with-line-width-ratio layout props width-ratio args)
131 (number? markup-list?)
132 (let* ((line-width (chain-assoc-get 'line-width props))
133 (new-line-width (* width-ratio line-width))
134 (indent (* 0.5 (- line-width new-line-width)))
135 (stencils (interpret-markup-list layout
136 (cons `((line-width . ,new-line-width)) props)
138 (interpret-markup-list layout props
139 (map (lambda (stencil)
140 (markup #:hspace indent #:stencil stencil))
143 #(define-markup-command (force-line-width-ratio layout props ratio arg)
145 (let* ((new-line-width (* ratio (chain-assoc-get 'line-width props)))
146 (line-stencil (interpret-markup layout props
147 (markup #:override (cons 'line-width new-line-width)
151 (interval-length (ly:stencil-extent line-stencil X))))))
152 (interpret-markup layout props (markup #:concat (#:stencil line-stencil #:hspace gap)))))
154 #(define-markup-list-command (two-column-lines layout props col1 col2)
155 (markup-list? markup-list?)
156 (interpret-markup-list layout props
157 (make-column-lines-markup-list
159 (let map-on-lists ((col1 col1)
161 (if (and (null? col1) (null? col2))
163 (let ((line-col1 (if (null? col1) "" (car col1)))
164 (line-col2 (if (null? col2) "" (car col2)))
165 (rest-col1 (if (null? col1) '() (cdr col1)))
166 (rest-col2 (if (null? col2) '() (cdr col2))))
170 #:force-line-width-ratio 0.45 line-col1
172 #:force-line-width-ratio 0.45 line-col2
175 (map-on-lists rest-col1 rest-col2))))))))
177 #(define-markup-command (tacet-lyrics layout props score text)
178 (markup? markup-list?)
179 #:properties ((column-number 2))
183 \fontsize #-2 \override #`(column-number . ,column-number)
184 \column\page-columns {
185 \fontsize #2 \line { \hspace #10 Tacet $score }
191 #(define-markup-command (tacet layout props num) (number?)
192 (let ((score (ly:make-score (make-music
193 'MultiMeasureRestMusic
194 'duration (ly:make-duration 0 0 num)))))
195 (ly:score-add-output-def! score #{ \layout {
198 \context { \Score skipBars = ##t }
201 \remove "Time_signature_engraver"
202 \remove "Clef_engraver"
203 \remove "Staff_symbol_engraver"
204 \override MultiMeasureRest #'expand-limit = #2
209 #{ \markup\fontsize #2 { \hspace #10 Tacet $(make-score-markup score) } #})))
211 #(define-markup-command (lyrics layout props text)
213 #:properties ((column-number 2))
217 \fontsize #-2 \override #`(column-number . ,column-number)
218 \column\page-columns { $text } } #}))
220 #(define-markup-list-command (indented-lines layout props indent args)
221 (number? markup-list?)
222 (let* ((new-line-width (- (chain-assoc-get 'line-width props) indent))
223 (lines (interpret-markup-list layout
224 (cons `((line-width . ,new-line-width)) props)
226 (interpret-markup-list layout props
228 (markup #:hspace indent #:stencil line))
231 #(define-markup-list-command (wordwrap-center-lines layout props args)
233 (map (lambda (stencil)
234 (interpret-markup layout props (markup #:fill-line (#:stencil stencil))))
235 (interpret-markup-list layout props (make-wordwrap-lines-markup-list args))))
237 #(define-markup-list-command (centered-lines layout props args)
239 (let ((baseline-skip (chain-assoc-get 'baseline-skip props)))
240 (space-lines baseline-skip
241 (interpret-markup-list layout props
242 (map (lambda (arg) (markup #:fill-line (arg)))
245 #(define-markup-list-command (fontsize-lines layout props increment args)
246 (number? markup-list?)
247 #:properties ((font-size 0)
250 (interpret-markup-list layout
251 (cons `((baseline-skip . ,(* baseline-skip (magstep increment)))
252 (word-space . ,(* word-space (magstep increment)))
253 (font-size . ,(+ font-size increment)))
257 #(define-markup-list-command (abs-fontsize-lines layout props size args)
258 (number? markup-list?)
259 (let* ((ref-size (ly:output-def-lookup layout 'text-font-size 12))
260 (text-props (list (ly:output-def-lookup layout 'text-font-defaults)))
261 (ref-word-space (chain-assoc-get 'word-space text-props 0.6))
262 (ref-baseline (chain-assoc-get 'baseline-skip text-props 3))
263 (magnification (/ size ref-size)))
264 (interpret-markup-list layout
265 (cons `((baseline-skip . ,(* magnification ref-baseline))
266 (word-space . ,(* magnification ref-word-space))
267 (font-size . ,(magnification->font-size magnification)))
271 #(define-markup-command (wordwrap-center layout props args) (markup-list?)
272 (interpret-markup layout props
274 (make-wordwrap-center-lines-markup-list args))))
276 #(define (page-ref-aux layout props label gauge next)
277 (let* ((gauge-stencil
278 (interpret-markup layout props
279 (make-concat-markup (list gauge next))))
280 (x-ext (ly:stencil-extent gauge-stencil X))
281 (y-ext (ly:stencil-extent gauge-stencil Y)))
283 `(delay-stencil-evaluation
284 ,(delay (ly:stencil-expr
285 (let* ((table (ly:output-def-lookup layout 'label-page-table))
286 (label-page (and (list? table) (assoc label table)))
287 (page-number (and label-page (cdr label-page)))
288 (page-markup (if page-number
289 (markup #:page-link page-number
290 #:concat ((format "~a" page-number)
292 (markup #:concat ("?" next))))
293 (page-stencil (interpret-markup layout props page-markup))
294 (gap (- (interval-length x-ext)
295 (interval-length (ly:stencil-extent page-stencil X)))))
296 (interpret-markup layout props
297 (markup #:concat (page-markup #:hspace gap)))))))
301 #(define-markup-command (page-refI layout props label next)
303 (page-ref-aux layout props label "0" next))
305 #(define-markup-command (page-refII layout props label next)
307 (page-ref-aux layout props label "00" next))
309 #(define-markup-command (page-refIII layout props label next)
311 (page-ref-aux layout props label "000" next))
313 #(define-markup-command (super layout props arg) (markup?)
314 (ly:stencil-translate-axis
317 (cons `((font-size . ,(- (chain-assoc-get 'font-size props 0) 3))) props)
319 (* 0.25 (chain-assoc-get 'baseline-skip props))
322 #(define-markup-list-command (paragraph layout props text) (markup-list?)
323 (let ((indentation (markup #:pad-to-box (cons 0 3) (cons 0 0) #:null)))
324 (interpret-markup-list layout props
325 (make-justified-lines-markup-list (cons indentation text)))))
327 #(define-markup-list-command (columns paper props text) (markup-list?)
328 (interpret-markup-list paper props
329 (make-column-lines-markup-list text)))
331 #(define-markup-command (separation-line layout props width) (number?)
332 (interpret-markup layout props
333 (markup #:fill-line (#:draw-line (cons (/ (* 20 width) (*staff-size*)) 0)))))
335 #(define-markup-command (sep layout props) ()
336 (interpret-markup layout props
337 (markup #:pad-around 1 #:fill-line (#:draw-line '(50 . 0)))))
339 #(define-markup-command (boxed-justify layout props text) (markup-list?)
340 (interpret-markup layout props
341 (make-override-markup '(box-padding . 1)
344 (make-justified-lines-markup-list text))))))
346 #(define-markup-command (after-system layout props arg) (markup?)
347 (let ((stencil (interpret-markup layout props arg)))
348 (ly:make-stencil (ly:stencil-expr (ly:stencil-aligned-to stencil Y DOWN))
349 (ly:stencil-extent stencil X)
352 %%% Guile does not deal with accented letters
353 #(use-modules (ice-9 regex))
354 %%;; actually defined below, in a closure
355 #(define-public string-upper-case #f)
356 #(define accented-char-upper-case? #f)
357 #(define accented-char-lower-case? #f)
359 %%;; an accented character is seen as two characters by guile
360 #(let ((lower-case-accented-string "éèêëáàâäíìîïóòôöúùûüçœæ")
361 (upper-case-accented-string "ÉÈÊËÁÀÂÄÍÌÎÏÓÒÔÖÚÙÛÜÇŒÆ"))
362 (define (group-by-2 chars result)
363 (if (or (null? chars) (null? (cdr chars)))
365 (group-by-2 (cddr chars)
366 (cons (string (car chars) (cadr chars))
368 (let ((lower-case-accented-chars
369 (group-by-2 (string->list lower-case-accented-string) (list)))
370 (upper-case-accented-chars
371 (group-by-2 (string->list upper-case-accented-string) (list))))
372 (set! string-upper-case
374 (define (replace-chars str froms tos)
377 (replace-chars (regexp-substitute/global #f (car froms) str
378 'pre (car tos) 'post)
381 (string-upcase (replace-chars str
382 lower-case-accented-chars
383 upper-case-accented-chars))))
384 (set! accented-char-upper-case?
385 (lambda (char1 char2)
386 (member (string char1 char2) upper-case-accented-chars string=?)))
387 (set! accented-char-lower-case?
388 (lambda (char1 char2)
389 (member (string char1 char2) lower-case-accented-chars string=?)))))
391 #(define-markup-command (smallCaps layout props text) (markup?)
392 "Turn @code{text}, which should be a string, to small caps.
394 \\markup \\small-caps \"Text between double quotes\"
396 (define (string-list->markup strings lower)
397 (let ((final-string (string-upper-case
398 (apply string-append (reverse strings)))))
400 (markup #:fontsize -2 final-string)
402 (define (make-small-caps rest-chars currents current-is-lower prev-result)
403 (if (null? rest-chars)
404 (make-concat-markup (reverse! (cons (string-list->markup
405 currents current-is-lower)
407 (let* ((ch1 (car rest-chars))
408 (ch2 (and (not (null? (cdr rest-chars))) (cadr rest-chars)))
409 (this-char-string (string ch1))
410 (is-lower (char-lower-case? ch1))
411 (next-rest-chars (cdr rest-chars)))
412 (cond ((and ch2 (accented-char-lower-case? ch1 ch2))
413 (set! this-char-string (string ch1 ch2))
415 (set! next-rest-chars (cddr rest-chars)))
416 ((and ch2 (accented-char-upper-case? ch1 ch2))
417 (set! this-char-string (string ch1 ch2))
419 (set! next-rest-chars (cddr rest-chars))))
420 (if (or (and current-is-lower is-lower)
421 (and (not current-is-lower) (not is-lower)))
422 (make-small-caps next-rest-chars
423 (cons this-char-string currents)
426 (make-small-caps next-rest-chars
427 (list this-char-string)
431 (cons (string-list->markup
432 currents current-is-lower)
434 (interpret-markup layout props
436 (make-small-caps (string->list text) (list) #f (list))
439 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
440 %%% Character lists, inline quoted scores, etc.
443 #(define-music-function (parser location bar-num) (number?)
444 #{ \override Score.BarNumber #'break-visibility = #'#(#f #f #t)
446 \set Score.currentBarNumber = #bar-num #})
449 \override Score.StaffSymbol #'staff-space = #(magstep -2)
450 \set Score . fontSize = #-2
453 quoteLayout = \layout {
456 \context { \Staff \remove "Time_signature_engraver" }
457 \context { \Voice \override Script #'avoid-slur = #'outside }
458 \context { \Score \override BarNumber.break-visibility = #'#(#f #f #t) }
461 tinyLayout = \layout {
464 \override StaffSymbol #'staff-space = #(magstep -2)
467 \context { \FiguredBass \override BassFigure.font-size = #-2 }
470 tinyQuoteLayout = \layout {
474 \override StaffSymbol #'staff-space = #(magstep -2)
477 \context { \FiguredBass \override BassFigure.font-size = #-2 }
480 quoteEmptyLayout = \layout {
483 \context { \Score skipBars = ##t }
486 \remove "Time_signature_engraver"
487 \remove "Clef_engraver"
488 \remove "Staff_symbol_engraver"
489 \override MultiMeasureRest #'expand-limit = #2
493 smallLayout = \layout {
495 \Staff fontSize = #-1
496 \override StaffSymbol.staff-space = #(magstep -1)
498 \context { \Lyrics fontSize = #-1 }
499 \context { \FiguredBass \override BassFigure.font-size = #-1 }
500 \context { \Voice \override Script.avoid-slur = #'outside }
501 \context { \CueVoice \override Script.avoid-slur = #'outside }
504 \override NonMusicalPaperColumn.line-break-permission = #'allow
505 \override NonMusicalPaperColumn.page-break-permission = #'allow
509 onlyNotesLayout = \layout {
513 \remove "Time_signature_engraver"
514 \remove "Clef_engraver"
515 \remove "Staff_symbol_engraver"
517 \context { \Score \remove "Bar_number_engraver" }
520 characterLayout = \layout {
522 line-width = #(if (eqv? #t (ly:get-option 'ancient-style))
528 \override Clef #'full-size-change = ##t
529 \remove "Bar_engraver"
533 \remove "Stem_engraver"
538 #(define-music-function (parser location clef1 clef2 low-note high-note)
539 (string? string? ly:music? ly:music?)
540 (let* ((low-pitch (ly:music-property low-note 'pitch))
541 (high-pitch (ly:music-property high-note 'pitch))
544 'elements (list (make-music
546 'duration (ly:make-duration 2 0 1 1)
550 'duration (ly:make-duration 2 0 1 1)
551 'pitch high-pitch)))))
552 (if (eqv? #t (ly:get-option 'ancient-style))
553 #{ \new Staff { \clef $clef1 $chord } #}
556 \set Staff.forceClef = ##t
557 \clef $clef2 s8. $chord s2
561 #(define-markup-command (character-ambitus layout props name ambitus)
563 #:properties ((character-width-ratio 16/20)
564 (ambitus-width-ratio 3/20))
568 (interpret-markup layout props
570 #:force-line-width-ratio ambitus-width-ratio
571 #:vcenter #:fill-line (#:null #:left-align ambitus)
573 #:force-line-width-ratio character-width-ratio
574 #:vcenter #:smallCaps name)))))
576 #(define-markup-command (character-two-columns layout props col1 col2)
578 #:properties ((word-space 0.6)
579 (character-width-ratio 10/30)
580 (ambitus-width-ratio 4/30))
584 \override #`(character-width-ratio . ,character-width-ratio)
585 \override #`(ambitus-width-ratio . ,ambitus-width-ratio)
588 \override #`(word-space . ,word-space) $col1
590 \override #`(word-space . ,word-space) $col2
594 #(define-markup-command (character-three-columns layout props col1 col2 col3)
595 (markup? markup? markup?)
596 #:properties ((word-space 0.6))
599 (markup (#:concat (#:override `(word-space . ,word-space) col1
601 #:override `(word-space . ,word-space) col2
603 #:override `(word-space . ,word-space) col3)))))
605 #(define-markup-command (sline layout props args) (markup-list?)
606 (interpret-markup layout props
607 (make-line-markup (cons (make-hspace-markup 4) args))))
610 #(define-markup-command (verse layout props syllab-count words)
611 (number? markup-list?)
614 (if (< syllab-count 12)
615 (make-line-markup (cons (make-hspace-markup (* 1.5 (- 12 syllab-count)))
617 (make-line-markup words))))
622 footnote-auto-numbering = ##t
623 footnote-numbering-function =
625 (markup #:small #:box (number->string (+ 1 num))))
626 footnote-separator-markup = \markup\override #'(span-factor . 1/4) \draw-hline
627 footnote-padding = 2\mm
628 footnote-footer-padding = 1\mm
632 #(define-music-function (parser this-location offset note)
633 (number-pair? markup?)
634 (let ((foot-mus (make-music
636 'X-offset (car offset)
637 'Y-offset (cdr offset)
638 'automatically-numbered #t
639 'text (make-null-markup)
640 'footnote-text note)))
642 #{ <>-\tweak footnote-music #foot-mus ^\markup\transparent\box "1" #}))
644 footnoteHereNoSpace =
645 #(define-music-function (parser this-location offset note)
646 (number-pair? markup?)
647 (let ((foot-mus (make-music
649 'X-offset (car offset)
650 'Y-offset (cdr offset)
651 'automatically-numbered #t
652 'text (make-null-markup)
653 'footnote-text note)))
655 #{ <>-\tweak footnote-music #foot-mus ^\markup\null #}))
660 #(define-music-function (parser this-location text)
663 \once\override TextSpanner.bound-details.left.stencil-align-dir-y = #CENTER
664 \once\override TextSpanner.bound-details.left.text =
665 \markup\whiteout { $text \hspace #1 }
666 \once\override TextSpanner.bound-details.left-broken.text = \markup\transparent t %##f