]> git.donarmstrong.com Git - lilypond.git/blob - scm/define-markup-commands.scm
Merge branch 'master' into translation
[lilypond.git] / scm / define-markup-commands.scm
1 ;;;; This file is part of LilyPond, the GNU music typesetter.
2 ;;;;
3 ;;;; Copyright (C) 2000--2012  Han-Wen Nienhuys <hanwen@xs4all.nl>
4 ;;;;                  Jan Nieuwenhuizen <janneke@gnu.org>
5 ;;;;
6 ;;;; LilyPond is free software: you can redistribute it and/or modify
7 ;;;; it under the terms of the GNU General Public License as published by
8 ;;;; the Free Software Foundation, either version 3 of the License, or
9 ;;;; (at your option) any later version.
10 ;;;;
11 ;;;; LilyPond is distributed in the hope that it will be useful,
12 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 ;;;; GNU General Public License for more details.
15 ;;;;
16 ;;;; You should have received a copy of the GNU General Public License
17 ;;;; along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
18
19 ;;;
20 ;;; Markup commands and markup-list commands definitions.
21 ;;;
22 ;;; Markup commands which are part of LilyPond, are defined
23 ;;; in the (lily) module, which is the current module in this file,
24 ;;; using the `define-markup-command' macro.
25 ;;;
26 ;;; Usage:
27 ;;;
28 ;;; (define-markup-command (command-name layout props args...)
29 ;;;   args-signature
30 ;;;   [ #:category category ]
31 ;;;   [ #:properties property-bindings ]
32 ;;;   documentation-string
33 ;;;   ..body..)
34 ;;;
35 ;;; with:
36 ;;;   command-name
37 ;;;     the name of the markup command
38 ;;;
39 ;;;   layout and props
40 ;;;     arguments that are automatically passed to the command when it
41 ;;;     is interpreted.
42 ;;;     `layout' is an output def, which properties can be accessed
43 ;;;     using `ly:output-def-lookup'.
44 ;;;     `props' is a list of property settings which can be accessed
45 ;;;     using `chain-assoc-get' (more on that below)
46 ;;;
47 ;;;   args...
48 ;;;     the command arguments.
49 ;;;     There is no limitation on the order of command arguments.
50 ;;;     However, markup functions taking a markup as their last
51 ;;;     argument are somewhat special as you can apply them to a
52 ;;;     markup list, and the result is a markup list where the
53 ;;;     markup function (with the specified leading arguments) has
54 ;;;     been applied to every element of the original markup list.
55 ;;;
56 ;;;     Since replicating the leading arguments for applying a
57 ;;;     markup function to a markup list is cheap mostly for
58 ;;;     Scheme arguments, you avoid performance pitfalls by just
59 ;;;     using Scheme arguments for the leading arguments of markup
60 ;;;     functions that take a markup as their last argument.
61 ;;;
62 ;;;   args-signature
63 ;;;     the arguments signature, i.e., a list of type predicates which
64 ;;;     are used to type check the arguments, and also to define the general
65 ;;;     argument types (markup, markup-list, scheme) that the command is
66 ;;;     expecting.
67 ;;;     For instance, if a command expects a number, then a markup, the
68 ;;;     signature would be: (number? markup?)
69 ;;;
70 ;;;   category
71 ;;;     for documentation purpose, builtin markup commands are grouped by
72 ;;;     category.  This can be any symbol.  When documentation is generated,
73 ;;;     the symbol is converted to a capitalized string, where hyphens are
74 ;;;     replaced by spaces.
75 ;;;
76 ;;;   property-bindings
77 ;;;     this is used both for documentation generation, and to ease
78 ;;;     programming the command itself.  It is list of
79 ;;;        (property-name default-value)
80 ;;;     or (property-name)
81 ;;;     elements.  Each property is looked-up in the `props' argument, and
82 ;;;     the symbol naming the property is bound to its value.
83 ;;;     When the property is not found in `props', then the symbol is bound
84 ;;;     to the given default value.  When no default value is given, #f is
85 ;;;     used instead.
86 ;;;     Thus, using the following property bindings:
87 ;;;       ((thickness 0.1)
88 ;;;        (font-size 0))
89 ;;;     is equivalent to writing:
90 ;;;       (let ((thickness (chain-assoc-get 'thickness props 0.1))
91 ;;;             (font-size (chain-assoc-get 'font-size props 0)))
92 ;;;         ..body..)
93 ;;;     When a command `B' internally calls an other command `A', it may
94 ;;;     desirable to see in `B' documentation all the properties and
95 ;;;     default values used by `A'.  In that case, add `A-markup' to the
96 ;;;     property-bindings of B.  (This is used when generating
97 ;;;     documentation, but won't create bindings.)
98 ;;;
99 ;;;   documentation-string
100 ;;;     the command documentation string (used to generate manuals)
101 ;;;
102 ;;;   body
103 ;;;     the command body.  The function is supposed to return a stencil.
104 ;;;
105 ;;; Each markup command definition shall have a documentation string
106 ;;; with description, syntax and example.
107
108 (use-modules (ice-9 regex))
109
110 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
111 ;; utility functions
112 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
113
114 (define-public empty-stencil (ly:make-stencil '() '(1 . -1) '(1 . -1)))
115 (define-public point-stencil (ly:make-stencil "" '(0 . 0) '(0 . 0)))
116
117 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
118 ;; geometric shapes
119 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
120
121 (define-markup-command (draw-line layout props dest)
122   (number-pair?)
123   #:category graphic
124   #:properties ((thickness 1))
125   "
126 @cindex drawing lines within text
127
128 A simple line.
129 @lilypond[verbatim,quote]
130 \\markup {
131   \\draw-line #'(4 . 4)
132   \\override #'(thickness . 5)
133   \\draw-line #'(-3 . 0)
134 }
135 @end lilypond"
136   (let ((th (* (ly:output-def-lookup layout 'line-thickness)
137                thickness))
138         (x (car dest))
139         (y (cdr dest)))
140     (make-line-stencil th 0 0 x y)))
141
142 (define-markup-command (draw-dashed-line layout props dest)
143   (number-pair?)
144   #:category graphic
145   #:properties ((thickness 1)
146                 (on 1)
147                 (off 1)
148                 (phase 0)
149                 (full-length #t))
150   "
151 @cindex drawing dashed lines within text
152
153 A dashed line.
154
155 If @code{full-length} is set to #t (default) the dashed-line extends to the
156 whole length given by @var{dest}, without white space at beginning or end.
157 @code{off} will then be altered to fit.
158 To insist on the given (or default) values of @code{on}, @code{off} use
159 @code{\\override #'(full-length . #f)}
160 Manual settings for @code{on},@code{off} and @code{phase} are possible.
161 @lilypond[verbatim,quote]
162 \\markup {
163   \\draw-dashed-line #'(5.1 . 2.3)
164   \\override #'(on . 0.3)
165   \\override #'(off . 0.5)
166   \\draw-dashed-line #'(5.1 . 2.3)
167 }
168 @end lilypond"
169   (let* ((line-thickness (ly:output-def-lookup layout 'line-thickness))
170          ;; Calculate the thickness to be used.
171          (th (* line-thickness thickness))
172          (half-thick (/ th 2))
173          ;; Get the extensions in x- and y-direction.
174          (x (car dest))
175          (y (cdr dest))
176          ;; Calculate the length of the dashed line.
177          (line-length (sqrt (+ (expt x 2) (expt y 2)))))
178
179     (if (and full-length (not (= (+ on off) 0)))
180         (begin
181           ;; Add double-thickness to avoid overlapping.
182           (set! off (+ (* 2 th) off))
183           (let* (;; Make a guess how often the off/on-pair should be printed
184                  ;; after the initial `on´.
185                  ;; Assume a minimum of 1 to avoid division by zero.
186                  (guess (max 1 (round (/ (- line-length on) (+ off on)))))
187                  ;; Not sure about the value or why corr is necessary at all,
188                  ;; but it seems to be necessary.
189                  (corr (if (= on 0)
190                            (/ line-thickness 10)
191                            0))
192                  ;; Calculate a new value for off to fit the
193                  ;; line-length.
194                  (new-off (/ (- line-length corr (* (1+ guess) on)) guess))
195                  )
196               (cond
197
198               ;; Settings for (= on 0). Resulting in a dotted line.
199
200                     ;; If line-length isn't shorter than `th´, change the given
201                     ;; value for `off´ to fit the line-length.
202                     ((and (= on 0) (< th line-length))
203                       (set! off new-off))
204
205                     ;; If the line-length is shorter than `th´, it makes no
206                     ;; sense to adjust `off´. The rounded edges of the lines
207                     ;; would prevent any nice output.
208                     ;; Do nothing.
209                     ;; This will result in a single dot for very short lines.
210                     ((and (= on 0) (>= th line-length))
211                       #f)
212
213               ;; Settings for (not (= on 0)). Resulting in a dashed line.
214
215                     ;; If line-length isn't shorter than one go of on-off-on,
216                     ;; change the given value for `off´ to fit the line-length.
217                     ((< (+ (* 2 on) off) line-length)
218                       (set! off new-off))
219                     ;; If the line-length is too short, but greater than
220                     ;; (* 4 th) set on/off to (/ line-length 3)
221                     ((< (* 4 th) line-length)
222                       (set! on (/ line-length 3))
223                       (set! off (/ line-length 3)))
224                     ;; If the line-length is shorter than (* 4 th), it makes
225                     ;; no sense trying to adjust on/off. The rounded edges of
226                     ;; the lines would prevent any nice output.
227                     ;; Simply set `on´ to line-length.
228                     (else
229                       (set! on line-length))))))
230
231     ;; If `on´ or `off´ is negative, or the sum of `on' and `off' equals zero a
232     ;; ghostscript-error occurs while calling
233     ;; (ly:make-stencil (list 'dashed-line th on off x y phase) x-ext y-ext)
234     ;; Better be paranoid.
235     (if (or (= (+ on off) 0)
236             (negative? on)
237             (negative? off))
238         (begin
239           (ly:warning "Can't print a line - setting on/off to default")
240           (set! on 1)
241           (set! off 1)))
242
243     ;; To give the lines produced by \draw-line and \draw-dashed-line the same
244     ;; length, half-thick has to be added to the stencil-extensions.
245     (ly:make-stencil
246       (list 'dashed-line th on off x y phase)
247         (interval-widen (ordered-cons 0 x) half-thick)
248         (interval-widen (ordered-cons 0 y) half-thick))))
249
250 (define-markup-command (draw-dotted-line layout props dest)
251   (number-pair?)
252   #:category graphic
253   #:properties ((thickness 1)
254                 (off 1)
255                 (phase 0))
256   "
257 @cindex drawing dotted lines within text
258
259 A dotted line.
260
261 The dotted-line always extends to the whole length given by @var{dest}, without
262 white space at beginning or end.
263 Manual settings for @code{off} are possible to get larger or smaller space
264 between the dots.
265 The given (or default) value of @code{off} will be altered to fit the
266 line-length.
267 @lilypond[verbatim,quote]
268 \\markup {
269   \\draw-dotted-line #'(5.1 . 2.3)
270   \\override #'(thickness . 2)
271   \\override #'(off . 0.2)
272   \\draw-dotted-line #'(5.1 . 2.3)
273 }
274 @end lilypond"
275
276   (let ((new-props (prepend-alist-chain 'on 0
277                      (prepend-alist-chain 'full-length #t props))))
278
279   (interpret-markup layout
280                     new-props
281                     (markup #:draw-dashed-line dest))))
282
283 (define-markup-command (draw-hline layout props)
284   ()
285   #:category graphic
286   #:properties ((draw-line-markup)
287                 (line-width)
288                 (span-factor 1))
289   "
290 @cindex drawing a line across a page
291
292 Draws a line across a page, where the property @code{span-factor}
293 controls what fraction of the page is taken up.
294 @lilypond[verbatim,quote]
295 \\markup {
296   \\column {
297     \\draw-hline
298     \\override #'(span-factor . 1/3)
299     \\draw-hline
300   }
301 }
302 @end lilypond"
303   (interpret-markup layout
304                     props
305                     (markup #:draw-line (cons (* line-width
306                                                   span-factor)
307                                                0))))
308
309 (define-markup-command (draw-circle layout props radius thickness filled)
310   (number? number? boolean?)
311   #:category graphic
312   "
313 @cindex drawing circles within text
314
315 A circle of radius @var{radius} and thickness @var{thickness},
316 optionally filled.
317
318 @lilypond[verbatim,quote]
319 \\markup {
320   \\draw-circle #2 #0.5 ##f
321   \\hspace #2
322   \\draw-circle #2 #0 ##t
323 }
324 @end lilypond"
325   (make-circle-stencil radius thickness filled))
326
327 (define-markup-command (triangle layout props filled)
328   (boolean?)
329   #:category graphic
330   #:properties ((thickness 0.1)
331                 (font-size 0)
332                 (baseline-skip 2))
333   "
334 @cindex drawing triangles within text
335
336 A triangle, either filled or empty.
337
338 @lilypond[verbatim,quote]
339 \\markup {
340   \\triangle ##t
341   \\hspace #2
342   \\triangle ##f
343 }
344 @end lilypond"
345   (let ((ex (* (magstep font-size) 0.8 baseline-skip)))
346     (ly:make-stencil
347      `(polygon '(0.0 0.0
348                      ,ex 0.0
349                      ,(* 0.5 ex)
350                      ,(* 0.86 ex))
351            ,thickness
352            ,filled)
353      (cons 0 ex)
354      (cons 0 (* .86 ex)))))
355
356 (define-markup-command (circle layout props arg)
357   (markup?)
358   #:category graphic
359   #:properties ((thickness 1)
360                 (font-size 0)
361                 (circle-padding 0.2))
362   "
363 @cindex circling text
364
365 Draw a circle around @var{arg}.  Use @code{thickness},
366 @code{circle-padding} and @code{font-size} properties to determine line
367 thickness and padding around the markup.
368
369 @lilypond[verbatim,quote]
370 \\markup {
371   \\circle {
372     Hi
373   }
374 }
375 @end lilypond"
376   (let ((th (* (ly:output-def-lookup layout 'line-thickness)
377                thickness))
378          (pad (* (magstep font-size) circle-padding))
379          (m (interpret-markup layout props arg)))
380     (circle-stencil m th pad)))
381
382 (define-markup-command (with-url layout props url arg)
383   (string? markup?)
384   #:category graphic
385   "
386 @cindex inserting URL links into text
387
388 Add a link to URL @var{url} around @var{arg}.  This only works in
389 the PDF backend.
390
391 @lilypond[verbatim,quote]
392 \\markup {
393   \\with-url #\"http://lilypond.org/\" {
394     LilyPond ... \\italic {
395       music notation for everyone
396     }
397   }
398 }
399 @end lilypond"
400   (let* ((stil (interpret-markup layout props arg))
401          (xextent (ly:stencil-extent stil X))
402          (yextent (ly:stencil-extent stil Y))
403          (old-expr (ly:stencil-expr stil))
404          (url-expr (list 'url-link url `(quote ,xextent) `(quote ,yextent))))
405
406     (ly:stencil-add (ly:make-stencil url-expr xextent yextent) stil)))
407
408 (define-markup-command (page-link layout props page-number arg)
409   (number? markup?)
410   #:category other
411   "
412 @cindex referencing page numbers in text
413
414 Add a link to the page @var{page-number} around @var{arg}.  This only works
415 in the PDF backend.
416
417 @lilypond[verbatim,quote]
418 \\markup {
419   \\page-link #2  { \\italic { This links to page 2... } }
420 }
421 @end lilypond"
422   (let* ((stil (interpret-markup layout props arg))
423          (xextent (ly:stencil-extent stil X))
424          (yextent (ly:stencil-extent stil Y))
425          (old-expr (ly:stencil-expr stil))
426          (link-expr (list 'page-link page-number `(quote ,xextent) `(quote ,yextent))))
427
428     (ly:stencil-add (ly:make-stencil link-expr xextent yextent) stil)))
429
430 (define-markup-command (with-link layout props label arg)
431   (symbol? markup?)
432   #:category other
433   "
434 @cindex referencing page labels in text
435
436 Add a link to the page holding label @var{label} around @var{arg}.  This
437 only works in the PDF backend.
438
439 @lilypond[verbatim,quote]
440 \\markup {
441   \\with-link #'label {
442     \\italic { This links to the page containing the label... }
443   }
444 }
445 @end lilypond"
446   (let* ((arg-stencil (interpret-markup layout props arg))
447          (x-ext (ly:stencil-extent arg-stencil X))
448          (y-ext (ly:stencil-extent arg-stencil Y)))
449     (ly:make-stencil
450      `(delay-stencil-evaluation
451        ,(delay (ly:stencil-expr
452                 (let* ((table (ly:output-def-lookup layout 'label-page-table))
453                        (page-number (if (list? table)
454                                         (assoc-get label table)
455                                         #f))
456                        (link-expr (list 'page-link page-number
457                                         `(quote ,x-ext) `(quote ,y-ext))))
458                   (ly:stencil-add (ly:make-stencil link-expr x-ext y-ext)
459 arg-stencil)))))
460      x-ext
461      y-ext)))
462
463
464 (define-markup-command (beam layout props width slope thickness)
465   (number? number? number?)
466   #:category graphic
467   "
468 @cindex drawing beams within text
469
470 Create a beam with the specified parameters.
471 @lilypond[verbatim,quote]
472 \\markup {
473   \\beam #5 #1 #2
474 }
475 @end lilypond"
476   (let* ((y (* slope width))
477          (yext (cons (min 0 y) (max 0 y)))
478          (half (/ thickness 2)))
479
480     (ly:make-stencil
481      `(polygon ',(list
482                   0 (/ thickness -2)
483                     width (+ (* width slope)  (/ thickness -2))
484                     width (+ (* width slope)  (/ thickness 2))
485                     0 (/ thickness 2))
486                ,(ly:output-def-lookup layout 'blot-diameter)
487                #t)
488      (cons 0 width)
489      (cons (+ (- half) (car yext))
490            (+ half (cdr yext))))))
491
492 (define-markup-command (underline layout props arg)
493   (markup?)
494   #:category font
495   #:properties ((thickness 1) (offset 2))
496   "
497 @cindex underlining text
498
499 Underline @var{arg}.  Looks at @code{thickness} to determine line
500 thickness, and @code{offset} to determine line y-offset.
501
502 @lilypond[verbatim,quote]
503 \\markup \\fill-line {
504   \\underline \"underlined\"
505   \\override #'(offset . 5)
506   \\override #'(thickness . 1)
507   \\underline \"underlined\"
508   \\override #'(offset . 1)
509   \\override #'(thickness . 5)
510   \\underline \"underlined\"
511 }
512 @end lilypond"
513   (let* ((thick (ly:output-def-lookup layout 'line-thickness))
514          (underline-thick (* thickness thick))
515          (markup (interpret-markup layout props arg))
516          (x1 (car (ly:stencil-extent markup X)))
517          (x2 (cdr (ly:stencil-extent markup X)))
518          (y (* thick (- offset)))
519          (line (make-line-stencil underline-thick x1 y x2 y)))
520     (ly:stencil-add markup line)))
521
522 (define-markup-command (box layout props arg)
523   (markup?)
524   #:category font
525   #:properties ((thickness 1)
526                 (font-size 0)
527                 (box-padding 0.2))
528   "
529 @cindex enclosing text within a box
530
531 Draw a box round @var{arg}.  Looks at @code{thickness},
532 @code{box-padding} and @code{font-size} properties to determine line
533 thickness and padding around the markup.
534
535 @lilypond[verbatim,quote]
536 \\markup {
537   \\override #'(box-padding . 0.5)
538   \\box
539   \\line { V. S. }
540 }
541 @end lilypond"
542   (let* ((th (* (ly:output-def-lookup layout 'line-thickness)
543                 thickness))
544          (pad (* (magstep font-size) box-padding))
545          (m (interpret-markup layout props arg)))
546     (box-stencil m th pad)))
547
548 (define-markup-command (filled-box layout props xext yext blot)
549   (number-pair? number-pair? number?)
550   #:category graphic
551   "
552 @cindex drawing solid boxes within text
553 @cindex drawing boxes with rounded corners
554
555 Draw a box with rounded corners of dimensions @var{xext} and
556 @var{yext}.  For example,
557 @verbatim
558 \\filled-box #'(-.3 . 1.8) #'(-.3 . 1.8) #0
559 @end verbatim
560 creates a box extending horizontally from -0.3 to 1.8 and
561 vertically from -0.3 up to 1.8, with corners formed from a
562 circle of diameter@tie{}0 (i.e., sharp corners).
563
564 @lilypond[verbatim,quote]
565 \\markup {
566   \\filled-box #'(0 . 4) #'(0 . 4) #0
567   \\filled-box #'(0 . 2) #'(-4 . 2) #0.4
568   \\filled-box #'(1 . 8) #'(0 . 7) #0.2
569   \\with-color #white
570   \\filled-box #'(-4.5 . -2.5) #'(3.5 . 5.5) #0.7
571 }
572 @end lilypond"
573   (ly:round-filled-box
574    xext yext blot))
575
576 (define-markup-command (rounded-box layout props arg)
577   (markup?)
578   #:category graphic
579   #:properties ((thickness 1)
580                 (corner-radius 1)
581                 (font-size 0)
582                 (box-padding 0.5))
583   "@cindex enclosing text in a box with rounded corners
584    @cindex drawing boxes with rounded corners around text
585 Draw a box with rounded corners around @var{arg}.  Looks at @code{thickness},
586 @code{box-padding} and @code{font-size} properties to determine line
587 thickness and padding around the markup; the @code{corner-radius} property
588 makes it possible to define another shape for the corners (default is 1).
589
590 @lilypond[quote,verbatim,relative=2]
591 c4^\\markup {
592   \\rounded-box {
593     Overtura
594   }
595 }
596 c,8. c16 c4 r
597 @end lilypond"
598   (let ((th (* (ly:output-def-lookup layout 'line-thickness)
599                thickness))
600         (pad (* (magstep font-size) box-padding))
601         (m (interpret-markup layout props arg)))
602     (ly:stencil-add (rounded-box-stencil m th pad corner-radius)
603                     m)))
604
605 (define-markup-command (rotate layout props ang arg)
606   (number? markup?)
607   #:category align
608   "
609 @cindex rotating text
610
611 Rotate object with @var{ang} degrees around its center.
612
613 @lilypond[verbatim,quote]
614 \\markup {
615   default
616   \\hspace #2
617   \\rotate #45
618   \\line {
619     rotated 45°
620   }
621 }
622 @end lilypond"
623   (let* ((stil (interpret-markup layout props arg)))
624     (ly:stencil-rotate stil ang 0 0)))
625
626 (define-markup-command (whiteout layout props arg)
627   (markup?)
628   #:category other
629   "
630 @cindex adding a white background to text
631
632 Provide a white background for @var{arg}.
633
634 @lilypond[verbatim,quote]
635 \\markup {
636   \\combine
637     \\filled-box #'(-1 . 10) #'(-3 . 4) #1
638     \\whiteout whiteout
639 }
640 @end lilypond"
641   (stencil-whiteout (interpret-markup layout props arg)))
642
643 (define-markup-command (pad-markup layout props amount arg)
644   (number? markup?)
645   #:category align
646   "
647 @cindex padding text
648 @cindex putting space around text
649
650 Add space around a markup object.
651
652 @lilypond[verbatim,quote]
653 \\markup {
654   \\box {
655     default
656   }
657   \\hspace #2
658   \\box {
659     \\pad-markup #1 {
660       padded
661     }
662   }
663 }
664 @end lilypond"
665   (let*
666       ((stil (interpret-markup layout props arg))
667        (xext (ly:stencil-extent stil X))
668        (yext (ly:stencil-extent stil Y)))
669
670     (ly:make-stencil
671      (ly:stencil-expr stil)
672      (interval-widen xext amount)
673      (interval-widen yext amount))))
674
675 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
676 ;; space
677 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
678
679 (define-markup-command (strut layout props)
680   ()
681   #:category other
682   "
683 @cindex creating vertical spaces in text
684
685 Create a box of the same height as the space in the current font."
686   (let ((m (ly:text-interface::interpret-markup layout props " ")))
687     (ly:make-stencil (ly:stencil-expr m)
688                      '(0 . 0)
689                      (ly:stencil-extent m X)
690                      )))
691
692 (define-markup-command (hspace layout props amount)
693   (number?)
694   #:category align
695   #:properties ((word-space))
696   "
697 @cindex creating horizontal spaces in text
698
699 Create an invisible object taking up horizontal space @var{amount}.
700
701 @lilypond[verbatim,quote]
702 \\markup {
703   one
704   \\hspace #2
705   two
706   \\hspace #8
707   three
708 }
709 @end lilypond"
710   (let ((corrected-space (- amount word-space)))
711     (ly:make-stencil "" (cons 0 corrected-space) '(0 . 0))))
712
713 (define-markup-command (vspace layout props amount)
714  (number?)
715  #:category align
716  "
717 @cindex creating vertical spaces in text
718
719 Create an invisible object taking up vertical space
720 of @var{amount} multiplied by 3.
721
722 @lilypond[verbatim,quote]
723 \\markup {
724     \\center-column {
725     one
726     \\vspace #2
727     two
728     \\vspace #5
729     three
730   }
731 }
732 @end lilypond"
733   (let ((amount (* amount 3.0)))
734     (ly:make-stencil "" (cons 0 0) (cons 0 amount))))
735
736
737 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
738 ;; importing graphics.
739 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
740
741 (define-markup-command (stencil layout props stil)
742   (ly:stencil?)
743   #:category other
744   "
745 @cindex importing stencils into text
746
747 Use a stencil as markup.
748
749 @lilypond[verbatim,quote]
750 \\markup {
751   \\stencil #(make-circle-stencil 2 0 #t)
752 }
753 @end lilypond"
754   stil)
755
756 (define bbox-regexp
757   (make-regexp "%%BoundingBox:[ \t]+([0-9-]+)[ \t]+([0-9-]+)[ \t]+([0-9-]+)[ \t]+([0-9-]+)"))
758
759 (define (get-postscript-bbox string)
760   "Extract the bbox from STRING, or return #f if not present."
761   (let*
762       ((match (regexp-exec bbox-regexp string)))
763
764     (if match
765         (map (lambda (x)
766                (string->number (match:substring match x)))
767              (cdr (iota 5)))
768
769         #f)))
770
771 (define-markup-command (epsfile layout props axis size file-name)
772   (number? number? string?)
773   #:category graphic
774   "
775 @cindex inlining an Encapsulated PostScript image
776
777 Inline an EPS image.  The image is scaled along @var{axis} to
778 @var{size}.
779
780 @lilypond[verbatim,quote]
781 \\markup {
782   \\general-align #Y #DOWN {
783     \\epsfile #X #20 #\"context-example.eps\"
784     \\epsfile #Y #20 #\"context-example.eps\"
785   }
786 }
787 @end lilypond"
788   (if (ly:get-option 'safe)
789       (interpret-markup layout props "not allowed in safe")
790       (eps-file->stencil axis size file-name)
791       ))
792
793 (define-markup-command (postscript layout props str)
794   (string?)
795   #:category graphic
796   "
797 @cindex inserting PostScript directly into text
798 This inserts @var{str} directly into the output as a PostScript
799 command string.
800
801 @lilypond[verbatim,quote]
802 ringsps = #\"
803   0.15 setlinewidth
804   0.9 0.6 moveto
805   0.4 0.6 0.5 0 361 arc
806   stroke
807   1.0 0.6 0.5 0 361 arc
808   stroke
809   \"
810
811 rings = \\markup {
812   \\with-dimensions #'(-0.2 . 1.6) #'(0 . 1.2)
813   \\postscript #ringsps
814 }
815
816 \\relative c'' {
817   c2^\\rings
818   a2_\\rings
819 }
820 @end lilypond"
821   ;; FIXME
822   (ly:make-stencil
823    (list 'embedded-ps
824          (format #f "
825 gsave currentpoint translate
826 0.1 setlinewidth
827  ~a
828 grestore
829 "
830                  str))
831    '(0 . 0) '(0 . 0)))
832
833 (define-markup-command (path layout props thickness commands) (number? list?)
834   #:category graphic
835   #:properties ((line-cap-style 'round)
836                 (line-join-style 'round)
837                 (filled #f))
838   "
839 @cindex paths, drawing
840 @cindex drawing paths
841 Draws a path with line thickness @var{thickness} according to the
842 directions given in @var{commands}.  @var{commands} is a list of
843 lists where the @code{car} of each sublist is a drawing command and
844 the @code{cdr} comprises the associated arguments for each command.
845
846 Line-cap styles and line-join styles may be customized by
847 overriding the @code{line-cap-style} and @code{line-join-style}
848 properties, respectively.  Available line-cap styles are
849 @code{'butt}, @code{'round}, and @code{'square}.  Available
850 line-join styles are @code{'miter}, @code{'round}, and
851 @code{'bevel}.
852
853 The property @code{filled} specifies whether or not the path is
854 filled with color.
855
856 There are seven commands available to use in the list
857 @code{commands}: @code{moveto}, @code{rmoveto}, @code{lineto},
858 @code{rlineto}, @code{curveto}, @code{rcurveto}, and
859 @code{closepath}.  Note that the commands that begin with @emph{r}
860 are the relative variants of the other three commands.
861
862 The commands @code{moveto}, @code{rmoveto}, @code{lineto}, and
863 @code{rlineto} take 2 arguments; they are the X and Y coordinates
864 for the destination point.
865
866 The commands @code{curveto} and @code{rcurveto} create cubic
867 Bézier curves, and take 6 arguments; the first two are the X and Y
868 coordinates for the first control point, the second two are the X
869 and Y coordinates for the second control point, and the last two
870 are the X and Y coordinates for the destination point.
871
872 The @code{closepath} command takes zero arguments and closes the
873 current subpath in the active path.
874
875 Note that a sequence of commands @emph{must} begin with a
876 @code{moveto} or @code{rmoveto} to work with the SVG output.
877
878 @lilypond[verbatim,quote]
879 samplePath =
880   #'((moveto 0 0)
881      (lineto -1 1)
882      (lineto 1 1)
883      (lineto 1 -1)
884      (curveto -5 -5 -5 5 -1 0)
885      (closepath))
886
887 \\markup {
888   \\path #0.25 #samplePath
889 }
890 @end lilypond"
891   (let* ((half-thickness (/ thickness 2))
892          (current-point '(0 . 0))
893          (set-point (lambda (lst) (set! current-point lst)))
894          (relative? (lambda (x)
895                       (string-prefix? "r" (symbol->string (car x)))))
896          ;; For calculating extents, we want to modify the command
897          ;; list so that all coordinates are absolute.
898          (new-commands (map (lambda (x)
899                               (cond
900                                 ;; for rmoveto, rlineto
901                                 ((and (relative? x) (= 3 (length x)))
902                                  (let ((cp (cons
903                                              (+ (car current-point)
904                                                 (second x))
905                                              (+ (cdr current-point)
906                                                 (third x)))))
907                                    (set-point cp)
908                                    (list (car cp)
909                                          (cdr cp))))
910                                 ;; for rcurveto
911                                 ((and (relative? x) (= 7 (length x)))
912                                  (let* ((old-cp current-point)
913                                         (cp (cons
914                                               (+ (car old-cp)
915                                                  (sixth x))
916                                               (+ (cdr old-cp)
917                                                  (seventh x)))))
918                                    (set-point cp)
919                                    (list (+ (car old-cp) (second x))
920                                          (+ (cdr old-cp) (third x))
921                                          (+ (car old-cp) (fourth x))
922                                          (+ (cdr old-cp) (fifth x))
923                                          (car cp)
924                                          (cdr cp))))
925                                 ;; for moveto, lineto
926                                 ((= 3 (length x))
927                                  (set-point (cons (second x)
928                                                   (third x)))
929                                  (drop x 1))
930                                 ;; for curveto
931                                 ((= 7 (length x))
932                                  (set-point (cons (sixth x)
933                                                   (seventh x)))
934                                  (drop x 1))
935                                 ;; keep closepath for filtering;
936                                 ;; see `without-closepath'.
937                                 (else x)))
938                             commands))
939          ;; path-min-max does not accept 0-arg lists,
940          ;; and since closepath does not affect extents, filter
941          ;; out those commands here.
942          (without-closepath (filter (lambda (x)
943                                       (not (equal? 'closepath (car x))))
944                                     new-commands))
945          (extents (path-min-max
946                     ;; set the origin to the first moveto
947                     (list (list-ref (car without-closepath) 0)
948                           (list-ref (car without-closepath) 1))
949                     without-closepath))
950          (X-extent (cons (list-ref extents 0) (list-ref extents 1)))
951          (Y-extent (cons (list-ref extents 2) (list-ref extents 3)))
952          (command-list (fold-right append '() commands)))
953
954     ;; account for line thickness
955     (set! X-extent (interval-widen X-extent half-thickness))
956     (set! Y-extent (interval-widen Y-extent half-thickness))
957
958     (ly:make-stencil
959       `(path ,thickness `(,@',command-list)
960              ',line-cap-style ',line-join-style ,filled)
961       X-extent
962       Y-extent)))
963
964 (define-markup-list-command (score-lines layout props score)
965   (ly:score?)
966   "
967 This is the same as the @code{\\score} markup but delivers its
968 systems as a list of lines.  This is not usually called directly by
969 the user.  Instead, it is called when the parser encounters
970 @code{\\score} in a context where only markup lists are allowed.  When
971 used as the argument of a toplevel @code{\\markuplist}, the result can
972 be split across pages."
973   (let ((output (ly:score-embedded-format score layout)))
974
975     (if (ly:music-output? output)
976         (map
977          (lambda (paper-system)
978            ;; shift such that the refpoint of the bottom staff of
979            ;; the first system is the baseline of the score
980            (ly:stencil-translate-axis
981             (paper-system-stencil paper-system)
982             (- (car (paper-system-staff-extents paper-system)))
983             Y))
984          (vector->list (ly:paper-score-paper-systems output)))
985         (begin
986           (ly:warning (_"no systems found in \\score markup, does it have a \\layout block?"))
987           '()))))
988
989 (define-markup-command (score layout props score)
990   (ly:score?)
991   #:category music
992   #:properties ((baseline-skip))
993   "
994 @cindex inserting music into text
995
996 Inline an image of music.  The reference point (usually the middle
997 staff line) of the lowest staff in the top system is placed on the
998 baseline.
999
1000 @lilypond[verbatim,quote]
1001 \\markup {
1002   \\score {
1003     \\new PianoStaff <<
1004       \\new Staff \\relative c' {
1005         \\key f \\major
1006         \\time 3/4
1007         \\mark \\markup { Allegro }
1008         f2\\p( a4)
1009         c2( a4)
1010         bes2( g'4)
1011         f8( e) e4 r
1012       }
1013       \\new Staff \\relative c {
1014         \\clef bass
1015         \\key f \\major
1016         \\time 3/4
1017         f8( a c a c a
1018         f c' es c es c)
1019         f,( bes d bes d bes)
1020         f( g bes g bes g)
1021       }
1022     >>
1023     \\layout {
1024       indent = 0.0\\cm
1025       \\context {
1026         \\Score
1027         \\override RehearsalMark
1028           #'break-align-symbols = #'(time-signature key-signature)
1029         \\override RehearsalMark
1030           #'self-alignment-X = #LEFT
1031       }
1032       \\context {
1033         \\Staff
1034         \\override TimeSignature
1035           #'break-align-anchor-alignment = #LEFT
1036       }
1037     }
1038   }
1039 }
1040 @end lilypond"
1041   (stack-stencils Y DOWN baseline-skip
1042                   (score-lines-markup-list layout props score)))
1043
1044 (define-markup-command (null layout props)
1045   ()
1046   #:category other
1047   "
1048 @cindex creating empty text objects
1049
1050 An empty markup with extents of a single point.
1051
1052 @lilypond[verbatim,quote]
1053 \\markup {
1054   \\null
1055 }
1056 @end lilypond"
1057   point-stencil)
1058
1059 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1060 ;; basic formatting.
1061 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1062
1063 (define-markup-command (simple layout props str)
1064   (string?)
1065   #:category font
1066   "
1067 @cindex simple text strings
1068
1069 A simple text string; @code{\\markup @{ foo @}} is equivalent with
1070 @code{\\markup @{ \\simple #\"foo\" @}}.
1071
1072 Note: for creating standard text markup or defining new markup commands,
1073 the use of @code{\\simple} is unnecessary.
1074
1075 @lilypond[verbatim,quote]
1076 \\markup {
1077   \\simple #\"simple\"
1078   \\simple #\"text\"
1079   \\simple #\"strings\"
1080 }
1081 @end lilypond"
1082   (interpret-markup layout props str))
1083
1084 (define-markup-command (tied-lyric layout props str)
1085   (string?)
1086   #:category music
1087   #:properties ((word-space))
1088   "
1089 @cindex simple text strings with tie characters
1090
1091 Like simple-markup, but use tie characters for @q{~} tilde symbols.
1092
1093 @lilypond[verbatim,quote]
1094 \\markup \\column {
1095   \\tied-lyric #\"Siam navi~all'onde~algenti Lasciate~in abbandono\"
1096   \\tied-lyric #\"Impetuosi venti I nostri~affetti sono\"
1097   \\tied-lyric #\"Ogni diletto~e scoglio Tutta la vita~e~un mar.\"
1098 }
1099 @end lilypond"
1100   (define (replace-ties tie str)
1101     (if (string-contains str "~")
1102         (let*
1103           ((half-space (/ word-space 2))
1104            (parts (string-split str #\~))
1105            (tie-str (markup #:hspace half-space
1106                             #:musicglyph tie
1107                             #:hspace half-space))
1108            (joined  (list-join parts tie-str)))
1109           (make-concat-markup joined))
1110         str))
1111
1112   (define short-tie-regexp (make-regexp "~[^.]~"))
1113   (define (match-short str) (regexp-exec short-tie-regexp str))
1114
1115   (define (replace-short str mkp)
1116     (let ((match (match-short str)))
1117       (if (not match)
1118           (make-concat-markup (list
1119             mkp
1120             (replace-ties "ties.lyric.default" str)))
1121           (let ((new-str (match:suffix match))
1122                 (new-mkp (make-concat-markup (list
1123                           mkp
1124                           (replace-ties "ties.lyric.default"
1125                                         (match:prefix match))
1126                           (replace-ties "ties.lyric.short"
1127                                         (match:substring match))))))
1128               (replace-short new-str new-mkp)))))
1129
1130   (interpret-markup layout
1131                     props
1132                     (replace-short str (markup))))
1133
1134 (define-public empty-markup
1135   (make-simple-markup ""))
1136
1137 ;; helper for justifying lines.
1138 (define (get-fill-space word-count line-width word-space text-widths)
1139   "Calculate the necessary paddings between each two adjacent texts.
1140   The lengths of all texts are stored in @var{text-widths}.
1141   The normal formula for the padding between texts a and b is:
1142   padding = line-width/(word-count - 1) - (length(a) + length(b))/2
1143   The first and last padding have to be calculated specially using the
1144   whole length of the first or last text.
1145   All paddings are checked to be at least word-space, to ensure that
1146   no texts collide.
1147   Return a list of paddings."
1148   (cond
1149    ((null? text-widths) '())
1150
1151    ;; special case first padding
1152    ((= (length text-widths) word-count)
1153     (cons
1154      (- (- (/ line-width (1- word-count)) (car text-widths))
1155         (/ (car (cdr text-widths)) 2))
1156      (get-fill-space word-count line-width word-space (cdr text-widths))))
1157    ;; special case last padding
1158    ((= (length text-widths) 2)
1159     (list (- (/ line-width (1- word-count))
1160              (+ (/ (car text-widths) 2) (car (cdr text-widths)))) 0))
1161    (else
1162     (let ((default-padding
1163             (- (/ line-width (1- word-count))
1164                (/ (+ (car text-widths) (car (cdr text-widths))) 2))))
1165       (cons
1166        (if (> word-space default-padding)
1167            word-space
1168            default-padding)
1169        (get-fill-space word-count line-width word-space (cdr text-widths)))))))
1170
1171 (define-markup-command (fill-line layout props args)
1172   (markup-list?)
1173   #:category align
1174   #:properties ((text-direction RIGHT)
1175                 (word-space 0.6)
1176                 (line-width #f))
1177   "Put @var{markups} in a horizontal line of width @var{line-width}.
1178 The markups are spaced or flushed to fill the entire line.
1179 If there are no arguments, return an empty stencil.
1180
1181 @lilypond[verbatim,quote]
1182 \\markup {
1183   \\column {
1184     \\fill-line {
1185       Words evenly spaced across the page
1186     }
1187     \\null
1188     \\fill-line {
1189       \\line { Text markups }
1190       \\line {
1191         \\italic { evenly spaced }
1192       }
1193       \\line { across the page }
1194     }
1195   }
1196 }
1197 @end lilypond"
1198   (let* ((orig-stencils (interpret-markup-list layout props args))
1199          (stencils
1200           (map (lambda (stc)
1201                  (if (ly:stencil-empty? stc)
1202                      point-stencil
1203                      stc)) orig-stencils))
1204          (text-widths
1205           (map (lambda (stc)
1206                  (if (ly:stencil-empty? stc)
1207                      0.0
1208                      (interval-length (ly:stencil-extent stc X))))
1209                stencils))
1210          (text-width (apply + text-widths))
1211          (word-count (length stencils))
1212          (line-width (or line-width (ly:output-def-lookup layout 'line-width)))
1213          (fill-space
1214           (cond
1215            ((= word-count 1)
1216             (list
1217              (/ (- line-width text-width) 2)
1218              (/ (- line-width text-width) 2)))
1219            ((= word-count 2)
1220             (list
1221              (- line-width text-width)))
1222            (else
1223             (get-fill-space word-count line-width word-space text-widths))))
1224
1225          (line-contents (if (= word-count 1)
1226                             (list
1227                              point-stencil
1228                              (car stencils)
1229                              point-stencil)
1230                             stencils)))
1231
1232     (if (null? (remove ly:stencil-empty? orig-stencils))
1233         empty-stencil
1234         (begin
1235           (if (= text-direction LEFT)
1236               (set! line-contents (reverse line-contents)))
1237           (set! line-contents
1238                 (stack-stencils-padding-list
1239                  X RIGHT fill-space line-contents))
1240           (if (> word-count 1)
1241               ;; shift s.t. stencils align on the left edge, even if
1242               ;; first stencil had negative X-extent (e.g. center-column)
1243               ;; (if word-count = 1, X-extents are already normalized in
1244               ;; the definition of line-contents)
1245               (set! line-contents
1246                     (ly:stencil-translate-axis
1247                      line-contents
1248                      (- (car (ly:stencil-extent (car stencils) X)))
1249                      X)))
1250           line-contents))))
1251
1252 (define-markup-command (line layout props args)
1253   (markup-list?)
1254   #:category align
1255   #:properties ((word-space)
1256                 (text-direction RIGHT))
1257   "Put @var{args} in a horizontal line.  The property @code{word-space}
1258 determines the space between markups in @var{args}.
1259
1260 @lilypond[verbatim,quote]
1261 \\markup {
1262   \\line {
1263     one two three
1264   }
1265 }
1266 @end lilypond"
1267   (let ((stencils (interpret-markup-list layout props args)))
1268     (if (= text-direction LEFT)
1269         (set! stencils (reverse stencils)))
1270     (stack-stencil-line
1271      word-space
1272      (remove ly:stencil-empty? stencils))))
1273
1274 (define-markup-command (concat layout props args)
1275   (markup-list?)
1276   #:category align
1277   "
1278 @cindex concatenating text
1279 @cindex ligatures in text
1280
1281 Concatenate @var{args} in a horizontal line, without spaces in between.
1282 Strings and simple markups are concatenated on the input level, allowing
1283 ligatures.  For example, @code{\\concat @{ \"f\" \\simple #\"i\" @}} is
1284 equivalent to @code{\"fi\"}.
1285
1286 @lilypond[verbatim,quote]
1287 \\markup {
1288   \\concat {
1289     one
1290     two
1291     three
1292   }
1293 }
1294 @end lilypond"
1295   (define (concat-string-args arg-list)
1296     (fold-right (lambda (arg result-list)
1297                   (let ((result (if (pair? result-list)
1298                                     (car result-list)
1299                                   '())))
1300                     (if (and (pair? arg) (eqv? (car arg) simple-markup))
1301                       (set! arg (cadr arg)))
1302                     (if (and (string? result) (string? arg))
1303                         (cons (string-append arg result) (cdr result-list))
1304                       (cons arg result-list))))
1305                 '()
1306                 arg-list))
1307
1308   (interpret-markup layout
1309                     (prepend-alist-chain 'word-space 0 props)
1310                     (make-line-markup
1311                      (make-override-lines-markup-list
1312                       (cons 'word-space
1313                             (chain-assoc-get 'word-space props))
1314                       (if (markup-command-list? args)
1315                           args
1316                           (concat-string-args args))))))
1317
1318 (define (wordwrap-stencils stencils
1319                            justify base-space line-width text-dir)
1320   "Perform simple wordwrap, return stencil of each line."
1321   (define space (if justify
1322                     ;; justify only stretches lines.
1323                     (* 0.7 base-space)
1324                     base-space))
1325   (define (take-list width space stencils
1326                      accumulator accumulated-width)
1327     "Return (head-list . tail) pair, with head-list fitting into width"
1328     (if (null? stencils)
1329         (cons accumulator stencils)
1330         (let* ((first (car stencils))
1331                (first-wid (cdr (ly:stencil-extent (car stencils) X)))
1332                (newwid (+ space first-wid accumulated-width)))
1333           (if (or (null? accumulator)
1334                   (< newwid width))
1335               (take-list width space
1336                          (cdr stencils)
1337                          (cons first accumulator)
1338                          newwid)
1339               (cons accumulator stencils)))))
1340   (let loop ((lines '())
1341              (todo stencils))
1342     (let* ((line-break (take-list line-width space todo
1343                                   '() 0.0))
1344            (line-stencils (car line-break))
1345            (space-left (- line-width
1346                           (apply + (map (lambda (x) (cdr (ly:stencil-extent x X)))
1347                                         line-stencils))))
1348            (line-word-space (cond ((not justify) space)
1349                                   ;; don't stretch last line of paragraph.
1350                                   ;; hmmm . bug - will overstretch the last line in some case.
1351                                   ((null? (cdr line-break))
1352                                    base-space)
1353                                   ((null? line-stencils) 0.0)
1354                                   ((null? (cdr line-stencils)) 0.0)
1355                                   (else (/ space-left (1- (length line-stencils))))))
1356            (line (stack-stencil-line line-word-space
1357                                      (if (= text-dir RIGHT)
1358                                          (reverse line-stencils)
1359                                          line-stencils))))
1360       (if (pair? (cdr line-break))
1361           (loop (cons line lines)
1362                 (cdr line-break))
1363           (begin
1364             (if (= text-dir LEFT)
1365                 (set! line
1366                       (ly:stencil-translate-axis
1367                        line
1368                        (- line-width (interval-end (ly:stencil-extent line X)))
1369                        X)))
1370             (reverse (cons line lines)))))))
1371
1372 (define-markup-list-command (wordwrap-internal layout props justify args)
1373   (boolean? markup-list?)
1374   #:properties ((line-width #f)
1375                 (word-space)
1376                 (text-direction RIGHT))
1377   "Internal markup list command used to define @code{\\justify} and @code{\\wordwrap}."
1378   (wordwrap-stencils (remove ly:stencil-empty?
1379                              (interpret-markup-list layout props args))
1380                      justify
1381                      word-space
1382                      (or line-width
1383                          (ly:output-def-lookup layout 'line-width))
1384                      text-direction))
1385
1386 (define-markup-command (justify layout props args)
1387   (markup-list?)
1388   #:category align
1389   #:properties ((baseline-skip)
1390                 wordwrap-internal-markup-list)
1391   "
1392 @cindex justifying text
1393
1394 Like @code{\\wordwrap}, but with lines stretched to justify the margins.
1395 Use @code{\\override #'(line-width . @var{X})} to set the line width;
1396 @var{X}@tie{}is the number of staff spaces.
1397
1398 @lilypond[verbatim,quote]
1399 \\markup {
1400   \\justify {
1401     Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed
1402     do eiusmod tempor incididunt ut labore et dolore magna aliqua.
1403     Ut enim ad minim veniam, quis nostrud exercitation ullamco
1404     laboris nisi ut aliquip ex ea commodo consequat.
1405   }
1406 }
1407 @end lilypond"
1408   (stack-lines DOWN 0.0 baseline-skip
1409                (wordwrap-internal-markup-list layout props #t args)))
1410
1411 (define-markup-command (wordwrap layout props args)
1412   (markup-list?)
1413   #:category align
1414   #:properties ((baseline-skip)
1415                 wordwrap-internal-markup-list)
1416   "Simple wordwrap.  Use @code{\\override #'(line-width . @var{X})} to set
1417 the line width, where @var{X} is the number of staff spaces.
1418
1419 @lilypond[verbatim,quote]
1420 \\markup {
1421   \\wordwrap {
1422     Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed
1423     do eiusmod tempor incididunt ut labore et dolore magna aliqua.
1424     Ut enim ad minim veniam, quis nostrud exercitation ullamco
1425     laboris nisi ut aliquip ex ea commodo consequat.
1426   }
1427 }
1428 @end lilypond"
1429   (stack-lines DOWN 0.0 baseline-skip
1430                (wordwrap-internal-markup-list layout props #f args)))
1431
1432 (define-markup-list-command (wordwrap-string-internal layout props justify arg)
1433   (boolean? string?)
1434   #:properties ((line-width)
1435                 (word-space)
1436                 (text-direction RIGHT))
1437   "Internal markup list command used to define @code{\\justify-string} and
1438 @code{\\wordwrap-string}."
1439   (let* ((para-strings (regexp-split
1440                         (string-regexp-substitute
1441                          "\r" "\n"
1442                          (string-regexp-substitute "\r\n" "\n" arg))
1443                         "\n[ \t\n]*\n[ \t\n]*"))
1444          (list-para-words (map (lambda (str)
1445                                  (regexp-split str "[ \t\n]+"))
1446                                para-strings))
1447          (para-lines (map (lambda (words)
1448                             (let* ((stencils
1449                                     (remove ly:stencil-empty?
1450                                             (map (lambda (x)
1451                                                    (interpret-markup layout props x))
1452                                                  words))))
1453                               (wordwrap-stencils stencils
1454                                                  justify word-space
1455                                                  line-width text-direction)))
1456                           list-para-words)))
1457     (apply append para-lines)))
1458
1459 (define-markup-command (wordwrap-string layout props arg)
1460   (string?)
1461   #:category align
1462   #:properties ((baseline-skip)
1463                 wordwrap-string-internal-markup-list)
1464   "Wordwrap a string.  Paragraphs may be separated with double newlines.
1465
1466 @lilypond[verbatim,quote]
1467 \\markup {
1468   \\override #'(line-width . 40)
1469   \\wordwrap-string #\"Lorem ipsum dolor sit amet, consectetur
1470       adipisicing elit, sed do eiusmod tempor incididunt ut labore
1471       et dolore magna aliqua.
1472
1473
1474       Ut enim ad minim veniam, quis nostrud exercitation ullamco
1475       laboris nisi ut aliquip ex ea commodo consequat.
1476
1477
1478       Excepteur sint occaecat cupidatat non proident, sunt in culpa
1479       qui officia deserunt mollit anim id est laborum\"
1480 }
1481 @end lilypond"
1482   (stack-lines DOWN 0.0 baseline-skip
1483                (wordwrap-string-internal-markup-list layout props #f arg)))
1484
1485 (define-markup-command (justify-string layout props arg)
1486   (string?)
1487   #:category align
1488   #:properties ((baseline-skip)
1489                 wordwrap-string-internal-markup-list)
1490   "Justify a string.  Paragraphs may be separated with double newlines
1491
1492 @lilypond[verbatim,quote]
1493 \\markup {
1494   \\override #'(line-width . 40)
1495   \\justify-string #\"Lorem ipsum dolor sit amet, consectetur
1496       adipisicing elit, sed do eiusmod tempor incididunt ut labore
1497       et dolore magna aliqua.
1498
1499
1500       Ut enim ad minim veniam, quis nostrud exercitation ullamco
1501       laboris nisi ut aliquip ex ea commodo consequat.
1502
1503
1504       Excepteur sint occaecat cupidatat non proident, sunt in culpa
1505       qui officia deserunt mollit anim id est laborum\"
1506 }
1507 @end lilypond"
1508   (stack-lines DOWN 0.0 baseline-skip
1509                (wordwrap-string-internal-markup-list layout props #t arg)))
1510
1511 (define-markup-command (wordwrap-field layout props symbol)
1512   (symbol?)
1513   #:category align
1514   "Wordwrap the data which has been assigned to @var{symbol}.
1515
1516 @lilypond[verbatim,quote]
1517 \\header {
1518   title = \"My title\"
1519   myText = \"Lorem ipsum dolor sit amet, consectetur adipisicing
1520     elit, sed do eiusmod tempor incididunt ut labore et dolore
1521     magna aliqua.  Ut enim ad minim veniam, quis nostrud
1522     exercitation ullamco laboris nisi ut aliquip ex ea commodo
1523     consequat.\"
1524 }
1525
1526 \\paper {
1527   bookTitleMarkup = \\markup {
1528     \\column {
1529       \\fill-line { \\fromproperty #'header:title }
1530       \\null
1531       \\wordwrap-field #'header:myText
1532     }
1533   }
1534 }
1535
1536 \\markup {
1537   \\null
1538 }
1539 @end lilypond"
1540   (let* ((m (chain-assoc-get symbol props)))
1541     (if (string? m)
1542         (wordwrap-string-markup layout props m)
1543         empty-stencil)))
1544
1545 (define-markup-command (justify-field layout props symbol)
1546   (symbol?)
1547   #:category align
1548   "Justify the data which has been assigned to @var{symbol}.
1549
1550 @lilypond[verbatim,quote]
1551 \\header {
1552   title = \"My title\"
1553   myText = \"Lorem ipsum dolor sit amet, consectetur adipisicing
1554     elit, sed do eiusmod tempor incididunt ut labore et dolore magna
1555     aliqua.  Ut enim ad minim veniam, quis nostrud exercitation ullamco
1556     laboris nisi ut aliquip ex ea commodo consequat.\"
1557 }
1558
1559 \\paper {
1560   bookTitleMarkup = \\markup {
1561     \\column {
1562       \\fill-line { \\fromproperty #'header:title }
1563       \\null
1564       \\justify-field #'header:myText
1565     }
1566   }
1567 }
1568
1569 \\markup {
1570   \\null
1571 }
1572 @end lilypond"
1573   (let* ((m (chain-assoc-get symbol props)))
1574     (if (string? m)
1575         (justify-string-markup layout props m)
1576         empty-stencil)))
1577
1578 (define-markup-command (combine layout props arg1 arg2)
1579   (markup? markup?)
1580   #:category align
1581   "
1582 @cindex merging text
1583
1584 Print two markups on top of each other.
1585
1586 Note: @code{\\combine} cannot take a list of markups enclosed in
1587 curly braces as an argument; the follow example will not compile:
1588
1589 @example
1590 \\combine @{ a list @}
1591 @end example
1592
1593 @lilypond[verbatim,quote]
1594 \\markup {
1595   \\fontsize #5
1596   \\override #'(thickness . 2)
1597   \\combine
1598     \\draw-line #'(0 . 4)
1599     \\arrow-head #Y #DOWN ##f
1600 }
1601 @end lilypond"
1602   (let* ((s1 (interpret-markup layout props arg1))
1603          (s2 (interpret-markup layout props arg2)))
1604     (ly:stencil-add s1 s2)))
1605
1606 ;;
1607 ;; TODO: should extract baseline-skip from each argument somehow..
1608 ;;
1609 (define-markup-command (column layout props args)
1610   (markup-list?)
1611   #:category align
1612   #:properties ((baseline-skip))
1613   "
1614 @cindex stacking text in a column
1615
1616 Stack the markups in @var{args} vertically.  The property
1617 @code{baseline-skip} determines the space between markups
1618 in @var{args}.
1619
1620 @lilypond[verbatim,quote]
1621 \\markup {
1622   \\column {
1623     one
1624     two
1625     three
1626   }
1627 }
1628 @end lilypond"
1629   (let ((arg-stencils (interpret-markup-list layout props args)))
1630     (stack-lines -1 0.0 baseline-skip
1631                  (remove ly:stencil-empty? arg-stencils))))
1632
1633 (define-markup-command (dir-column layout props args)
1634   (markup-list?)
1635   #:category align
1636   #:properties ((direction)
1637                 (baseline-skip))
1638   "
1639 @cindex changing direction of text columns
1640
1641 Make a column of @var{args}, going up or down, depending on the
1642 setting of the @code{direction} layout property.
1643
1644 @lilypond[verbatim,quote]
1645 \\markup {
1646   \\override #`(direction . ,UP) {
1647     \\dir-column {
1648       going up
1649     }
1650   }
1651   \\hspace #1
1652   \\dir-column {
1653     going down
1654   }
1655   \\hspace #1
1656   \\override #'(direction . 1) {
1657     \\dir-column {
1658       going up
1659     }
1660   }
1661 }
1662 @end lilypond"
1663   (stack-lines (if (number? direction) direction -1)
1664                0.0
1665                baseline-skip
1666                (interpret-markup-list layout props args)))
1667
1668 (define (general-column align-dir baseline mols)
1669   "Stack @var{mols} vertically, aligned to  @var{align-dir} horizontally."
1670
1671   (let* ((aligned-mols (map (lambda (x) (ly:stencil-aligned-to x X align-dir)) mols))
1672          (stacked-stencil (stack-lines -1 0.0 baseline aligned-mols))
1673          (stacked-extent (ly:stencil-extent stacked-stencil X)))
1674     (ly:stencil-translate-axis stacked-stencil (- (car stacked-extent)) X )))
1675
1676 (define-markup-command (center-column layout props args)
1677   (markup-list?)
1678   #:category align
1679   #:properties ((baseline-skip))
1680   "
1681 @cindex centering a column of text
1682
1683 Put @code{args} in a centered column.
1684
1685 @lilypond[verbatim,quote]
1686 \\markup {
1687   \\center-column {
1688     one
1689     two
1690     three
1691   }
1692 }
1693 @end lilypond"
1694   (general-column CENTER baseline-skip (interpret-markup-list layout props args)))
1695
1696 (define-markup-command (left-column layout props args)
1697   (markup-list?)
1698   #:category align
1699   #:properties ((baseline-skip))
1700  "
1701 @cindex text columns, left-aligned
1702
1703 Put @code{args} in a left-aligned column.
1704
1705 @lilypond[verbatim,quote]
1706 \\markup {
1707   \\left-column {
1708     one
1709     two
1710     three
1711   }
1712 }
1713 @end lilypond"
1714   (general-column LEFT baseline-skip (interpret-markup-list layout props args)))
1715
1716 (define-markup-command (right-column layout props args)
1717   (markup-list?)
1718   #:category align
1719   #:properties ((baseline-skip))
1720  "
1721 @cindex text columns, right-aligned
1722
1723 Put @code{args} in a right-aligned column.
1724
1725 @lilypond[verbatim,quote]
1726 \\markup {
1727   \\right-column {
1728     one
1729     two
1730     three
1731   }
1732 }
1733 @end lilypond"
1734   (general-column RIGHT baseline-skip (interpret-markup-list layout props args)))
1735
1736 (define-markup-command (vcenter layout props arg)
1737   (markup?)
1738   #:category align
1739   "
1740 @cindex vertically centering text
1741
1742 Align @code{arg} to its Y@tie{}center.
1743
1744 @lilypond[verbatim,quote]
1745 \\markup {
1746   one
1747   \\vcenter
1748   two
1749   three
1750 }
1751 @end lilypond"
1752   (let* ((mol (interpret-markup layout props arg)))
1753     (ly:stencil-aligned-to mol Y CENTER)))
1754
1755 (define-markup-command (center-align layout props arg)
1756   (markup?)
1757   #:category align
1758   "
1759 @cindex horizontally centering text
1760
1761 Align @code{arg} to its X@tie{}center.
1762
1763 @lilypond[verbatim,quote]
1764 \\markup {
1765   \\column {
1766     one
1767     \\center-align
1768     two
1769     three
1770   }
1771 }
1772 @end lilypond"
1773   (let* ((mol (interpret-markup layout props arg)))
1774     (ly:stencil-aligned-to mol X CENTER)))
1775
1776 (define-markup-command (right-align layout props arg)
1777   (markup?)
1778   #:category align
1779   "
1780 @cindex right aligning text
1781
1782 Align @var{arg} on its right edge.
1783
1784 @lilypond[verbatim,quote]
1785 \\markup {
1786   \\column {
1787     one
1788     \\right-align
1789     two
1790     three
1791   }
1792 }
1793 @end lilypond"
1794   (let* ((m (interpret-markup layout props arg)))
1795     (ly:stencil-aligned-to m X RIGHT)))
1796
1797 (define-markup-command (left-align layout props arg)
1798   (markup?)
1799   #:category align
1800   "
1801 @cindex left aligning text
1802
1803 Align @var{arg} on its left edge.
1804
1805 @lilypond[verbatim,quote]
1806 \\markup {
1807   \\column {
1808     one
1809     \\left-align
1810     two
1811     three
1812   }
1813 }
1814 @end lilypond"
1815   (let* ((m (interpret-markup layout props arg)))
1816     (ly:stencil-aligned-to m X LEFT)))
1817
1818 (define-markup-command (general-align layout props axis dir arg)
1819   (integer? number? markup?)
1820   #:category align
1821   "
1822 @cindex controlling general text alignment
1823
1824 Align @var{arg} in @var{axis} direction to the @var{dir} side.
1825
1826 @lilypond[verbatim,quote]
1827 \\markup {
1828   \\column {
1829     one
1830     \\general-align #X #LEFT
1831     two
1832     three
1833     \\null
1834     one
1835     \\general-align #X #CENTER
1836     two
1837     three
1838     \\null
1839     \\line {
1840       one
1841       \\general-align #Y #UP
1842       two
1843       three
1844     }
1845     \\null
1846     \\line {
1847       one
1848       \\general-align #Y #3.2
1849       two
1850       three
1851     }
1852   }
1853 }
1854 @end lilypond"
1855   (let* ((m (interpret-markup layout props arg)))
1856     (ly:stencil-aligned-to m axis dir)))
1857
1858 (define-markup-command (halign layout props dir arg)
1859   (number? markup?)
1860   #:category align
1861   "
1862 @cindex setting horizontal text alignment
1863
1864 Set horizontal alignment.  If @var{dir} is @w{@code{-1}}, then it is
1865 left-aligned, while @code{+1} is right.  Values in between interpolate
1866 alignment accordingly.
1867
1868 @lilypond[verbatim,quote]
1869 \\markup {
1870   \\column {
1871     one
1872     \\halign #LEFT
1873     two
1874     three
1875     \\null
1876     one
1877     \\halign #CENTER
1878     two
1879     three
1880     \\null
1881     one
1882     \\halign #RIGHT
1883     two
1884     three
1885     \\null
1886     one
1887     \\halign #-5
1888     two
1889     three
1890   }
1891 }
1892 @end lilypond"
1893   (let* ((m (interpret-markup layout props arg)))
1894     (ly:stencil-aligned-to m X dir)))
1895
1896 (define-markup-command (with-dimensions layout props x y arg)
1897   (number-pair? number-pair? markup?)
1898   #:category other
1899   "
1900 @cindex setting extent of text objects
1901
1902 Set the dimensions of @var{arg} to @var{x} and@tie{}@var{y}."
1903   (let* ((m (interpret-markup layout props arg)))
1904     (ly:make-stencil (ly:stencil-expr m) x y)))
1905
1906 (define-markup-command (pad-around layout props amount arg)
1907   (number? markup?)
1908   #:category align
1909   "Add padding @var{amount} all around @var{arg}.
1910
1911 @lilypond[verbatim,quote]
1912 \\markup {
1913   \\box {
1914     default
1915   }
1916   \\hspace #2
1917   \\box {
1918     \\pad-around #0.5 {
1919       padded
1920     }
1921   }
1922 }
1923 @end lilypond"
1924   (let* ((m (interpret-markup layout props arg))
1925          (x (ly:stencil-extent m X))
1926          (y (ly:stencil-extent m Y)))
1927     (ly:make-stencil (ly:stencil-expr m)
1928                      (interval-widen x amount)
1929                      (interval-widen y amount))))
1930
1931 (define-markup-command (pad-x layout props amount arg)
1932   (number? markup?)
1933   #:category align
1934   "
1935 @cindex padding text horizontally
1936
1937 Add padding @var{amount} around @var{arg} in the X@tie{}direction.
1938
1939 @lilypond[verbatim,quote]
1940 \\markup {
1941   \\box {
1942     default
1943   }
1944   \\hspace #4
1945   \\box {
1946     \\pad-x #2 {
1947       padded
1948     }
1949   }
1950 }
1951 @end lilypond"
1952   (let* ((m (interpret-markup layout props arg))
1953          (x (ly:stencil-extent m X))
1954          (y (ly:stencil-extent m Y)))
1955     (ly:make-stencil (ly:stencil-expr m)
1956                      (interval-widen x amount)
1957                      y)))
1958
1959 (define-markup-command (put-adjacent layout props axis dir arg1 arg2)
1960   (integer? ly:dir? markup? markup?)
1961   #:category align
1962   "Put @var{arg2} next to @var{arg1}, without moving @var{arg1}."
1963   (let ((m1 (interpret-markup layout props arg1))
1964         (m2 (interpret-markup layout props arg2)))
1965     (ly:stencil-combine-at-edge m1 axis dir m2 0.0)))
1966
1967 (define-markup-command (transparent layout props arg)
1968   (markup?)
1969   #:category other
1970   "Make @var{arg} transparent.
1971
1972 @lilypond[verbatim,quote]
1973 \\markup {
1974   \\transparent {
1975     invisible text
1976   }
1977 }
1978 @end lilypond"
1979   (let* ((m (interpret-markup layout props arg))
1980          (x (ly:stencil-extent m X))
1981          (y (ly:stencil-extent m Y)))
1982     (ly:make-stencil "" x y)))
1983
1984 (define-markup-command (pad-to-box layout props x-ext y-ext arg)
1985   (number-pair? number-pair? markup?)
1986   #:category align
1987   "Make @var{arg} take at least @var{x-ext}, @var{y-ext} space.
1988
1989 @lilypond[verbatim,quote]
1990 \\markup {
1991   \\box {
1992     default
1993   }
1994   \\hspace #4
1995   \\box {
1996     \\pad-to-box #'(0 . 10) #'(0 . 3) {
1997       padded
1998     }
1999   }
2000 }
2001 @end lilypond"
2002   (let* ((m (interpret-markup layout props arg))
2003          (x (ly:stencil-extent m X))
2004          (y (ly:stencil-extent m Y)))
2005     (ly:make-stencil (ly:stencil-expr m)
2006                      (interval-union x-ext x)
2007                      (interval-union y-ext y))))
2008
2009 (define-markup-command (hcenter-in layout props length arg)
2010   (number? markup?)
2011   #:category align
2012   "Center @var{arg} horizontally within a box of extending
2013 @var{length}/2 to the left and right.
2014
2015 @lilypond[quote,verbatim]
2016 \\new StaffGroup <<
2017   \\new Staff {
2018     \\set Staff.instrumentName = \\markup {
2019       \\hcenter-in #12
2020       Oboe
2021     }
2022     c''1
2023   }
2024   \\new Staff {
2025     \\set Staff.instrumentName = \\markup {
2026       \\hcenter-in #12
2027       Bassoon
2028     }
2029     \\clef tenor
2030     c'1
2031   }
2032 >>
2033 @end lilypond"
2034   (interpret-markup layout props
2035                     (make-pad-to-box-markup
2036                      (cons (/ length -2) (/ length 2))
2037                      '(0 . 0)
2038                      (make-center-align-markup arg))))
2039
2040 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2041 ;; property
2042 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2043
2044 (define-markup-command (property-recursive layout props symbol)
2045   (symbol?)
2046   #:category other
2047   "Print out a warning when a header field markup contains some recursive
2048 markup definition."
2049   (ly:warning "Recursive definition of property ~a detected!" symbol)
2050   empty-stencil)
2051
2052 (define-markup-command (fromproperty layout props symbol)
2053   (symbol?)
2054   #:category other
2055   "Read the @var{symbol} from property settings, and produce a stencil
2056 from the markup contained within.  If @var{symbol} is not defined, it
2057 returns an empty markup.
2058
2059 @lilypond[verbatim,quote]
2060 \\header {
2061   myTitle = \"myTitle\"
2062   title = \\markup {
2063     from
2064     \\italic
2065     \\fromproperty #'header:myTitle
2066   }
2067 }
2068 \\markup {
2069   \\null
2070 }
2071 @end lilypond"
2072   (let ((m (chain-assoc-get symbol props)))
2073     (if (markup? m)
2074         ;; prevent infinite loops by clearing the interpreted property:
2075         (interpret-markup layout (cons (list (cons symbol `(,property-recursive-markup ,symbol))) props) m)
2076         empty-stencil)))
2077
2078 (define-markup-command (on-the-fly layout props procedure arg)
2079   (procedure? markup?)
2080   #:category other
2081   "Apply the @var{procedure} markup command to @var{arg}.
2082 @var{procedure} should take a single argument."
2083   (let ((anonymous-with-signature (lambda (layout props arg) (procedure layout props arg))))
2084     (set-object-property! anonymous-with-signature
2085                           'markup-signature
2086                           (list markup?))
2087     (interpret-markup layout props (list anonymous-with-signature arg))))
2088
2089 (define-markup-command (footnote layout props mkup note)
2090   (markup? markup?)
2091   #:category other
2092   "Have footnote @var{note} act as an annotation to the markup @var{mkup}.
2093
2094 @lilypond[verbatim,quote]
2095 \\markup {
2096   \\auto-footnote a b
2097   \\override #'(padding . 0.2)
2098   \\auto-footnote c d
2099 }
2100 @end lilypond
2101 The footnote will not be annotated automatically."
2102   (ly:stencil-combine-at-edge
2103     (interpret-markup layout props mkup)
2104     X
2105     RIGHT
2106     (ly:make-stencil
2107       `(footnote (gensym "footnote") #f ,(interpret-markup layout props note))
2108       '(0 . 0)
2109       '(0 . 0))
2110     0.0))
2111
2112 (define-markup-command (auto-footnote layout props mkup note)
2113   (markup? markup?)
2114   #:category other
2115   #:properties ((raise 0.5)
2116                 (padding 0.0))
2117   "Have footnote @var{note} act as an annotation to the markup @var{mkup}.
2118
2119 @lilypond[verbatim,quote]
2120 \\markup {
2121   \\auto-footnote a b
2122   \\override #'(padding . 0.2)
2123   \\auto-footnote c d
2124 }
2125 @end lilypond
2126 The footnote will be annotated automatically."
2127   (let* ((markup-stencil (interpret-markup layout props mkup))
2128          (footnote-hash (gensym "footnote"))
2129          (stencil-seed 0)
2130          (gauge-stencil (interpret-markup
2131                            layout
2132                            props
2133                            ((ly:output-def-lookup
2134                                layout
2135                                'footnote-numbering-function)
2136                               stencil-seed)))
2137          (x-ext (ly:stencil-extent gauge-stencil X))
2138          (y-ext (ly:stencil-extent gauge-stencil Y))
2139          (footnote-number
2140            `(delay-stencil-evaluation
2141               ,(delay
2142                 (ly:stencil-expr
2143                   (let* ((table
2144                           (ly:output-def-lookup layout
2145                                                 'number-footnote-table))
2146                          (footnote-stencil (if (list? table)
2147                                                (assoc-get footnote-hash
2148                                                           table)
2149                                                empty-stencil))
2150                          (footnote-stencil (if (ly:stencil? footnote-stencil)
2151                                                footnote-stencil
2152                                                (begin
2153                                                  (ly:programming-error
2154 "Cannot find correct footnote for a markup object.")
2155                                                  empty-stencil)))
2156                          (gap (- (interval-length x-ext)
2157                                  (interval-length
2158                                    (ly:stencil-extent footnote-stencil X))))
2159                          (y-trans (- (+ (cdr y-ext)
2160                                         raise)
2161                                      (cdr (ly:stencil-extent footnote-stencil
2162                                                              Y)))))
2163                     (ly:stencil-translate footnote-stencil
2164                                           (cons gap y-trans)))))))
2165          (main-stencil (ly:stencil-combine-at-edge
2166                          markup-stencil
2167                          X
2168                          RIGHT
2169                          (ly:make-stencil footnote-number x-ext y-ext)
2170                          padding)))
2171   (ly:stencil-add
2172     main-stencil
2173     (ly:make-stencil
2174       `(footnote ,footnote-hash #t ,(interpret-markup layout props note))
2175       '(0 . 0)
2176       '(0 . 0)))))
2177
2178 (define-markup-command (override layout props new-prop arg)
2179   (pair? markup?)
2180   #:category other
2181   "
2182 @cindex overriding properties within text markup
2183
2184 Add the argument @var{new-prop} to the property list.  Properties
2185 may be any property supported by @rinternals{font-interface},
2186 @rinternals{text-interface} and
2187 @rinternals{instrument-specific-markup-interface}.
2188
2189 @lilypond[verbatim,quote]
2190 \\markup {
2191   \\line {
2192     \\column {
2193       default
2194       baseline-skip
2195     }
2196     \\hspace #2
2197     \\override #'(baseline-skip . 4) {
2198       \\column {
2199         increased
2200         baseline-skip
2201       }
2202     }
2203   }
2204 }
2205 @end lilypond"
2206   (interpret-markup layout (cons (list new-prop) props) arg))
2207
2208 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2209 ;; files
2210 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2211
2212 (define-markup-command (verbatim-file layout props name)
2213   (string?)
2214   #:category other
2215   "Read the contents of file @var{name}, and include it verbatim.
2216
2217 @lilypond[verbatim,quote]
2218 \\markup {
2219   \\verbatim-file #\"simple.ly\"
2220 }
2221 @end lilypond"
2222   (interpret-markup layout props
2223                     (if  (ly:get-option 'safe)
2224                          "verbatim-file disabled in safe mode"
2225                          (let* ((str (ly:gulp-file name))
2226                                 (lines (string-split str #\nl)))
2227                            (make-typewriter-markup
2228                             (make-column-markup lines))))))
2229
2230 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2231 ;; fonts.
2232 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2233
2234
2235 (define-markup-command (smaller layout props arg)
2236   (markup?)
2237   #:category font
2238   "Decrease the font size relative to the current setting.
2239
2240 @lilypond[verbatim,quote]
2241 \\markup {
2242   \\fontsize #3.5 {
2243     some large text
2244     \\hspace #2
2245     \\smaller {
2246       a bit smaller
2247     }
2248     \\hspace #2
2249     more large text
2250   }
2251 }
2252 @end lilypond"
2253   (interpret-markup layout props
2254    `(,fontsize-markup -1 ,arg)))
2255
2256 (define-markup-command (larger layout props arg)
2257   (markup?)
2258   #:category font
2259   "Increase the font size relative to the current setting.
2260
2261 @lilypond[verbatim,quote]
2262 \\markup {
2263   default
2264   \\hspace #2
2265   \\larger
2266   larger
2267 }
2268 @end lilypond"
2269   (interpret-markup layout props
2270    `(,fontsize-markup 1 ,arg)))
2271
2272 (define-markup-command (finger layout props arg)
2273   (markup?)
2274   #:category font
2275   "Set @var{arg} as small numbers.
2276
2277 @lilypond[verbatim,quote]
2278 \\markup {
2279   \\finger {
2280     1 2 3 4 5
2281   }
2282 }
2283 @end lilypond"
2284   (interpret-markup layout
2285                     (cons '((font-size . -5) (font-encoding . fetaText)) props)
2286                     arg))
2287
2288 (define-markup-command (abs-fontsize layout props size arg)
2289   (number? markup?)
2290   #:category font
2291   "Use @var{size} as the absolute font size to display @var{arg}.
2292 Adjusts @code{baseline-skip} and @code{word-space} accordingly.
2293
2294 @lilypond[verbatim,quote]
2295 \\markup {
2296   default text font size
2297   \\hspace #2
2298   \\abs-fontsize #16 { text font size 16 }
2299   \\hspace #2
2300   \\abs-fontsize #12 { text font size 12 }
2301 }
2302 @end lilypond"
2303   (let* ((ref-size (ly:output-def-lookup layout 'text-font-size 12))
2304          (text-props (list (ly:output-def-lookup layout 'text-font-defaults)))
2305          (ref-word-space (chain-assoc-get 'word-space text-props 0.6))
2306          (ref-baseline (chain-assoc-get 'baseline-skip text-props 3))
2307          (magnification (/ size ref-size)))
2308      (interpret-markup
2309       layout
2310       (cons
2311        `((baseline-skip . ,(* magnification ref-baseline))
2312          (word-space . ,(* magnification ref-word-space))
2313          (font-size . ,(magnification->font-size magnification)))
2314        props)
2315       arg)))
2316
2317 (define-markup-command (fontsize layout props increment arg)
2318   (number? markup?)
2319   #:category font
2320   #:properties ((font-size 0)
2321                 (word-space 1)
2322                 (baseline-skip 2))
2323   "Add @var{increment} to the font-size.  Adjusts @code{baseline-skip}
2324 accordingly.
2325
2326 @lilypond[verbatim,quote]
2327 \\markup {
2328   default
2329   \\hspace #2
2330   \\fontsize #-1.5
2331   smaller
2332 }
2333 @end lilypond"
2334   (interpret-markup
2335    layout
2336    (cons
2337     `((baseline-skip . ,(* baseline-skip (magstep increment)))
2338       (word-space . ,(* word-space (magstep increment)))
2339       (font-size . ,(+ font-size increment)))
2340     props)
2341    arg))
2342
2343 (define-markup-command (magnify layout props sz arg)
2344   (number? markup?)
2345   #:category font
2346   "
2347 @cindex magnifying text
2348
2349 Set the font magnification for its argument.  In the following
2350 example, the middle@tie{}A is 10% larger:
2351
2352 @example
2353 A \\magnify #1.1 @{ A @} A
2354 @end example
2355
2356 Note: Magnification only works if a font name is explicitly selected.
2357 Use @code{\\fontsize} otherwise.
2358
2359 @lilypond[verbatim,quote]
2360 \\markup {
2361   default
2362   \\hspace #2
2363   \\magnify #1.5 {
2364     50% larger
2365   }
2366 }
2367 @end lilypond"
2368   (interpret-markup
2369    layout
2370    (prepend-alist-chain 'font-size (magnification->font-size sz) props)
2371    arg))
2372
2373 (define-markup-command (bold layout props arg)
2374   (markup?)
2375   #:category font
2376   "Switch to bold font-series.
2377
2378 @lilypond[verbatim,quote]
2379 \\markup {
2380   default
2381   \\hspace #2
2382   \\bold
2383   bold
2384 }
2385 @end lilypond"
2386   (interpret-markup layout (prepend-alist-chain 'font-series 'bold props) arg))
2387
2388 (define-markup-command (sans layout props arg)
2389   (markup?)
2390   #:category font
2391   "Switch to the sans serif font family.
2392
2393 @lilypond[verbatim,quote]
2394 \\markup {
2395   default
2396   \\hspace #2
2397   \\sans {
2398     sans serif
2399   }
2400 }
2401 @end lilypond"
2402   (interpret-markup layout (prepend-alist-chain 'font-family 'sans props) arg))
2403
2404 (define-markup-command (number layout props arg)
2405   (markup?)
2406   #:category font
2407   "Set font family to @code{number}, which yields the font used for
2408 time signatures and fingerings.  This font contains numbers and
2409 some punctuation; it has no letters.
2410
2411 @lilypond[verbatim,quote]
2412 \\markup {
2413   \\number {
2414     0 1 2 3 4 5 6 7 8 9 . ,
2415   }
2416 }
2417 @end lilypond"
2418   (interpret-markup layout (prepend-alist-chain 'font-encoding 'fetaText props) arg))
2419
2420 (define-markup-command (roman layout props arg)
2421   (markup?)
2422   #:category font
2423   "Set font family to @code{roman}.
2424
2425 @lilypond[verbatim,quote]
2426 \\markup {
2427   \\sans \\bold {
2428     sans serif, bold
2429     \\hspace #2
2430     \\roman {
2431       text in roman font family
2432     }
2433     \\hspace #2
2434     return to sans
2435   }
2436 }
2437 @end lilypond"
2438   (interpret-markup layout (prepend-alist-chain 'font-family 'roman props) arg))
2439
2440 (define-markup-command (huge layout props arg)
2441   (markup?)
2442   #:category font
2443   "Set font size to +2.
2444
2445 @lilypond[verbatim,quote]
2446 \\markup {
2447   default
2448   \\hspace #2
2449   \\huge
2450   huge
2451 }
2452 @end lilypond"
2453   (interpret-markup layout (prepend-alist-chain 'font-size 2 props) arg))
2454
2455 (define-markup-command (large layout props arg)
2456   (markup?)
2457   #:category font
2458   "Set font size to +1.
2459
2460 @lilypond[verbatim,quote]
2461 \\markup {
2462   default
2463   \\hspace #2
2464   \\large
2465   large
2466 }
2467 @end lilypond"
2468   (interpret-markup layout (prepend-alist-chain 'font-size 1 props) arg))
2469
2470 (define-markup-command (normalsize layout props arg)
2471   (markup?)
2472   #:category font
2473   "Set font size to default.
2474
2475 @lilypond[verbatim,quote]
2476 \\markup {
2477   \\teeny {
2478     this is very small
2479     \\hspace #2
2480     \\normalsize {
2481       normal size
2482     }
2483     \\hspace #2
2484     teeny again
2485   }
2486 }
2487 @end lilypond"
2488   (interpret-markup layout (prepend-alist-chain 'font-size 0 props) arg))
2489
2490 (define-markup-command (small layout props arg)
2491   (markup?)
2492   #:category font
2493   "Set font size to -1.
2494
2495 @lilypond[verbatim,quote]
2496 \\markup {
2497   default
2498   \\hspace #2
2499   \\small
2500   small
2501 }
2502 @end lilypond"
2503   (interpret-markup layout (prepend-alist-chain 'font-size -1 props) arg))
2504
2505 (define-markup-command (tiny layout props arg)
2506   (markup?)
2507   #:category font
2508   "Set font size to -2.
2509
2510 @lilypond[verbatim,quote]
2511 \\markup {
2512   default
2513   \\hspace #2
2514   \\tiny
2515   tiny
2516 }
2517 @end lilypond"
2518   (interpret-markup layout (prepend-alist-chain 'font-size -2 props) arg))
2519
2520 (define-markup-command (teeny layout props arg)
2521   (markup?)
2522   #:category font
2523   "Set font size to -3.
2524
2525 @lilypond[verbatim,quote]
2526 \\markup {
2527   default
2528   \\hspace #2
2529   \\teeny
2530   teeny
2531 }
2532 @end lilypond"
2533   (interpret-markup layout (prepend-alist-chain 'font-size -3 props) arg))
2534
2535 (define-markup-command (fontCaps layout props arg)
2536   (markup?)
2537   #:category font
2538   "Set @code{font-shape} to @code{caps}
2539
2540 Note: @code{\\fontCaps} requires the installation and selection of
2541 fonts which support the @code{caps} font shape."
2542   (interpret-markup layout (prepend-alist-chain 'font-shape 'caps props) arg))
2543
2544 ;; Poor man's caps
2545 (define-markup-command (smallCaps layout props arg)
2546   (markup?)
2547   #:category font
2548   "Emit @var{arg} as small caps.
2549
2550 Note: @code{\\smallCaps} does not support accented characters.
2551
2552 @lilypond[verbatim,quote]
2553 \\markup {
2554   default
2555   \\hspace #2
2556   \\smallCaps {
2557     Text in small caps
2558   }
2559 }
2560 @end lilypond"
2561   (define (char-list->markup chars lower)
2562     (let ((final-string (string-upcase (reverse-list->string chars))))
2563       (if lower
2564           (markup #:fontsize -2 final-string)
2565           final-string)))
2566   (define (make-small-caps rest-chars currents current-is-lower prev-result)
2567     (if (null? rest-chars)
2568         (make-concat-markup
2569           (reverse! (cons (char-list->markup currents current-is-lower)
2570                           prev-result)))
2571         (let* ((ch (car rest-chars))
2572                (is-lower (char-lower-case? ch)))
2573           (if (or (and current-is-lower is-lower)
2574                   (and (not current-is-lower) (not is-lower)))
2575               (make-small-caps (cdr rest-chars)
2576                                (cons ch currents)
2577                                is-lower
2578                                prev-result)
2579               (make-small-caps (cdr rest-chars)
2580                                (list ch)
2581                                is-lower
2582                                (if (null? currents)
2583                                    prev-result
2584                                    (cons (char-list->markup
2585                                             currents current-is-lower)
2586                                          prev-result)))))))
2587   (interpret-markup layout props
2588     (if (string? arg)
2589         (make-small-caps (string->list arg) (list) #f (list))
2590         arg)))
2591
2592 (define-markup-command (caps layout props arg)
2593   (markup?)
2594   #:category font
2595   "Copy of the @code{\\smallCaps} command.
2596
2597 @lilypond[verbatim,quote]
2598 \\markup {
2599   default
2600   \\hspace #2
2601   \\caps {
2602     Text in small caps
2603   }
2604 }
2605 @end lilypond"
2606   (interpret-markup layout props (make-smallCaps-markup arg)))
2607
2608 (define-markup-command (dynamic layout props arg)
2609   (markup?)
2610   #:category font
2611   "Use the dynamic font.  This font only contains @b{s}, @b{f}, @b{m},
2612 @b{z}, @b{p}, and @b{r}.  When producing phrases, like
2613 @q{pi@`{u}@tie{}@b{f}}, the normal words (like @q{pi@`{u}}) should be
2614 done in a different font.  The recommended font for this is bold and italic.
2615 @lilypond[verbatim,quote]
2616 \\markup {
2617   \\dynamic {
2618     sfzp
2619   }
2620 }
2621 @end lilypond"
2622   (interpret-markup
2623    layout (prepend-alist-chain 'font-encoding 'fetaText props) arg))
2624
2625 (define-markup-command (text layout props arg)
2626   (markup?)
2627   #:category font
2628   "Use a text font instead of music symbol or music alphabet font.
2629
2630 @lilypond[verbatim,quote]
2631 \\markup {
2632   \\number {
2633     1, 2,
2634     \\text {
2635       three, four,
2636     }
2637     5
2638   }
2639 }
2640 @end lilypond"
2641
2642   ;; ugh - latin1
2643   (interpret-markup layout (prepend-alist-chain 'font-encoding 'latin1 props)
2644                     arg))
2645
2646 (define-markup-command (italic layout props arg)
2647   (markup?)
2648   #:category font
2649   "Use italic @code{font-shape} for @var{arg}.
2650
2651 @lilypond[verbatim,quote]
2652 \\markup {
2653   default
2654   \\hspace #2
2655   \\italic
2656   italic
2657 }
2658 @end lilypond"
2659   (interpret-markup layout (prepend-alist-chain 'font-shape 'italic props) arg))
2660
2661 (define-markup-command (typewriter layout props arg)
2662   (markup?)
2663   #:category font
2664   "Use @code{font-family} typewriter for @var{arg}.
2665
2666 @lilypond[verbatim,quote]
2667 \\markup {
2668   default
2669   \\hspace #2
2670   \\typewriter
2671   typewriter
2672 }
2673 @end lilypond"
2674   (interpret-markup
2675    layout (prepend-alist-chain 'font-family 'typewriter props) arg))
2676
2677 (define-markup-command (upright layout props arg)
2678   (markup?)
2679   #:category font
2680   "Set @code{font-shape} to @code{upright}.  This is the opposite
2681 of @code{italic}.
2682
2683 @lilypond[verbatim,quote]
2684 \\markup {
2685   \\italic {
2686     italic text
2687     \\hspace #2
2688     \\upright {
2689       upright text
2690     }
2691     \\hspace #2
2692     italic again
2693   }
2694 }
2695 @end lilypond"
2696   (interpret-markup
2697    layout (prepend-alist-chain 'font-shape 'upright props) arg))
2698
2699 (define-markup-command (medium layout props arg)
2700   (markup?)
2701   #:category font
2702   "Switch to medium font-series (in contrast to bold).
2703
2704 @lilypond[verbatim,quote]
2705 \\markup {
2706   \\bold {
2707     some bold text
2708     \\hspace #2
2709     \\medium {
2710       medium font series
2711     }
2712     \\hspace #2
2713     bold again
2714   }
2715 }
2716 @end lilypond"
2717   (interpret-markup layout (prepend-alist-chain 'font-series 'medium props)
2718                     arg))
2719
2720 (define-markup-command (normal-text layout props arg)
2721   (markup?)
2722   #:category font
2723   "Set all font related properties (except the size) to get the default
2724 normal text font, no matter what font was used earlier.
2725
2726 @lilypond[verbatim,quote]
2727 \\markup {
2728   \\huge \\bold \\sans \\caps {
2729     huge bold sans caps
2730     \\hspace #2
2731     \\normal-text {
2732       huge normal
2733     }
2734     \\hspace #2
2735     as before
2736   }
2737 }
2738 @end lilypond"
2739   ;; ugh - latin1
2740   (interpret-markup layout
2741                     (cons '((font-family . roman) (font-shape . upright)
2742                             (font-series . medium) (font-encoding . latin1))
2743                           props)
2744                     arg))
2745
2746 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2747 ;; symbols.
2748 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2749
2750 (define-markup-command (musicglyph layout props glyph-name)
2751   (string?)
2752   #:category music
2753   "@var{glyph-name} is converted to a musical symbol; for example,
2754 @code{\\musicglyph #\"accidentals.natural\"} selects the natural sign from
2755 the music font.  See @ruser{The Feta font} for a complete listing of
2756 the possible glyphs.
2757
2758 @lilypond[verbatim,quote]
2759 \\markup {
2760   \\musicglyph #\"f\"
2761   \\musicglyph #\"rests.2\"
2762   \\musicglyph #\"clefs.G_change\"
2763 }
2764 @end lilypond"
2765   (let* ((font (ly:paper-get-font layout
2766                                   (cons '((font-encoding . fetaMusic)
2767                                           (font-name . #f))
2768
2769                                                  props)))
2770          (glyph (ly:font-get-glyph font glyph-name)))
2771     (if (null? (ly:stencil-expr glyph))
2772         (ly:warning (_ "Cannot find glyph ~a") glyph-name))
2773
2774     glyph))
2775
2776 (define-markup-command (doublesharp layout props)
2777   ()
2778   #:category music
2779   "Draw a double sharp symbol.
2780
2781 @lilypond[verbatim,quote]
2782 \\markup {
2783   \\doublesharp
2784 }
2785 @end lilypond"
2786   (interpret-markup layout props (markup #:musicglyph (assoc-get 1 standard-alteration-glyph-name-alist ""))))
2787
2788 (define-markup-command (sesquisharp layout props)
2789   ()
2790   #:category music
2791   "Draw a 3/2 sharp symbol.
2792
2793 @lilypond[verbatim,quote]
2794 \\markup {
2795   \\sesquisharp
2796 }
2797 @end lilypond"
2798   (interpret-markup layout props (markup #:musicglyph (assoc-get 3/4 standard-alteration-glyph-name-alist ""))))
2799
2800 (define-markup-command (sharp layout props)
2801   ()
2802   #:category music
2803   "Draw a sharp symbol.
2804
2805 @lilypond[verbatim,quote]
2806 \\markup {
2807   \\sharp
2808 }
2809 @end lilypond"
2810   (interpret-markup layout props (markup #:musicglyph (assoc-get 1/2 standard-alteration-glyph-name-alist ""))))
2811
2812 (define-markup-command (semisharp layout props)
2813   ()
2814   #:category music
2815   "Draw a semisharp symbol.
2816
2817 @lilypond[verbatim,quote]
2818 \\markup {
2819   \\semisharp
2820 }
2821 @end lilypond"
2822   (interpret-markup layout props (markup #:musicglyph (assoc-get 1/4 standard-alteration-glyph-name-alist ""))))
2823
2824 (define-markup-command (natural layout props)
2825   ()
2826   #:category music
2827   "Draw a natural symbol.
2828
2829 @lilypond[verbatim,quote]
2830 \\markup {
2831   \\natural
2832 }
2833 @end lilypond"
2834   (interpret-markup layout props (markup #:musicglyph (assoc-get 0 standard-alteration-glyph-name-alist ""))))
2835
2836 (define-markup-command (semiflat layout props)
2837   ()
2838   #:category music
2839   "Draw a semiflat symbol.
2840
2841 @lilypond[verbatim,quote]
2842 \\markup {
2843   \\semiflat
2844 }
2845 @end lilypond"
2846   (interpret-markup layout props (markup #:musicglyph (assoc-get -1/4 standard-alteration-glyph-name-alist ""))))
2847
2848 (define-markup-command (flat layout props)
2849   ()
2850   #:category music
2851   "Draw a flat symbol.
2852
2853 @lilypond[verbatim,quote]
2854 \\markup {
2855   \\flat
2856 }
2857 @end lilypond"
2858   (interpret-markup layout props (markup #:musicglyph (assoc-get -1/2 standard-alteration-glyph-name-alist ""))))
2859
2860 (define-markup-command (sesquiflat layout props)
2861   ()
2862   #:category music
2863   "Draw a 3/2 flat symbol.
2864
2865 @lilypond[verbatim,quote]
2866 \\markup {
2867   \\sesquiflat
2868 }
2869 @end lilypond"
2870   (interpret-markup layout props (markup #:musicglyph (assoc-get -3/4 standard-alteration-glyph-name-alist ""))))
2871
2872 (define-markup-command (doubleflat layout props)
2873   ()
2874   #:category music
2875   "Draw a double flat symbol.
2876
2877 @lilypond[verbatim,quote]
2878 \\markup {
2879   \\doubleflat
2880 }
2881 @end lilypond"
2882   (interpret-markup layout props (markup #:musicglyph (assoc-get -1 standard-alteration-glyph-name-alist ""))))
2883
2884 (define-markup-command (with-color layout props color arg)
2885   (color? markup?)
2886   #:category other
2887   "
2888 @cindex coloring text
2889
2890 Draw @var{arg} in color specified by @var{color}.
2891
2892 @lilypond[verbatim,quote]
2893 \\markup {
2894   \\with-color #red
2895   red
2896   \\hspace #2
2897   \\with-color #green
2898   green
2899   \\hspace #2
2900   \\with-color #blue
2901   blue
2902 }
2903 @end lilypond"
2904   (let ((stil (interpret-markup layout props arg)))
2905     (ly:make-stencil (list 'color color (ly:stencil-expr stil))
2906                      (ly:stencil-extent stil X)
2907                      (ly:stencil-extent stil Y))))
2908
2909 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2910 ;; glyphs
2911 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2912
2913 (define-markup-command (arrow-head layout props axis dir filled)
2914   (integer? ly:dir? boolean?)
2915   #:category graphic
2916   "Produce an arrow head in specified direction and axis.
2917 Use the filled head if @var{filled} is specified.
2918 @lilypond[verbatim,quote]
2919 \\markup {
2920   \\fontsize #5 {
2921     \\general-align #Y #DOWN {
2922       \\arrow-head #Y #UP ##t
2923       \\arrow-head #Y #DOWN ##f
2924       \\hspace #2
2925       \\arrow-head #X #RIGHT ##f
2926       \\arrow-head #X #LEFT ##f
2927     }
2928   }
2929 }
2930 @end lilypond"
2931   (let*
2932       ((name (format #f "arrowheads.~a.~a~a"
2933                      (if filled
2934                          "close"
2935                          "open")
2936                      axis
2937                      dir)))
2938     (ly:font-get-glyph
2939      (ly:paper-get-font layout (cons '((font-encoding . fetaMusic))
2940                                      props))
2941      name)))
2942
2943 (define-markup-command (lookup layout props glyph-name)
2944   (string?)
2945   #:category other
2946   "Lookup a glyph by name.
2947
2948 @lilypond[verbatim,quote]
2949 \\markup {
2950   \\override #'(font-encoding . fetaBraces) {
2951     \\lookup #\"brace200\"
2952     \\hspace #2
2953     \\rotate #180
2954     \\lookup #\"brace180\"
2955   }
2956 }
2957 @end lilypond"
2958   (ly:font-get-glyph (ly:paper-get-font layout props)
2959                      glyph-name))
2960
2961 (define-markup-command (char layout props num)
2962   (integer?)
2963   #:category other
2964   "Produce a single character.  Characters encoded in hexadecimal
2965 format require the prefix @code{#x}.
2966
2967 @lilypond[verbatim,quote]
2968 \\markup {
2969   \\char #65 \\char ##x00a9
2970 }
2971 @end lilypond"
2972   (ly:text-interface::interpret-markup layout props (ly:wide-char->utf-8 num)))
2973
2974 (define number->mark-letter-vector (make-vector 25 #\A))
2975
2976 (do ((i 0 (1+ i))
2977      (j 0 (1+ j)))
2978     ((>= i 26))
2979   (if (= i (- (char->integer #\I) (char->integer #\A)))
2980       (set! i (1+ i)))
2981   (vector-set! number->mark-letter-vector j
2982                (integer->char (+ i (char->integer #\A)))))
2983
2984 (define number->mark-alphabet-vector (list->vector
2985   (map (lambda (i) (integer->char (+ i (char->integer #\A)))) (iota 26))))
2986
2987 (define (number->markletter-string vec n)
2988   "Double letters for big marks."
2989   (let* ((lst (vector-length vec)))
2990
2991     (if (>= n lst)
2992         (string-append (number->markletter-string vec (1- (quotient n lst)))
2993                        (number->markletter-string vec (remainder n lst)))
2994         (make-string 1 (vector-ref vec n)))))
2995
2996 (define-markup-command (markletter layout props num)
2997   (integer?)
2998   #:category other
2999   "Make a markup letter for @var{num}.  The letters start with A to@tie{}Z
3000 (skipping letter@tie{}I), and continue with double letters.
3001
3002 @lilypond[verbatim,quote]
3003 \\markup {
3004   \\markletter #8
3005   \\hspace #2
3006   \\markletter #26
3007 }
3008 @end lilypond"
3009   (ly:text-interface::interpret-markup layout props
3010     (number->markletter-string number->mark-letter-vector num)))
3011
3012 (define-markup-command (markalphabet layout props num)
3013   (integer?)
3014   #:category other
3015    "Make a markup letter for @var{num}.  The letters start with A to@tie{}Z
3016 and continue with double letters.
3017
3018 @lilypond[verbatim,quote]
3019 \\markup {
3020   \\markalphabet #8
3021   \\hspace #2
3022   \\markalphabet #26
3023 }
3024 @end lilypond"
3025    (ly:text-interface::interpret-markup layout props
3026      (number->markletter-string number->mark-alphabet-vector num)))
3027
3028 (define-public (horizontal-slash-interval num forward number-interval mag)
3029   (if forward
3030     (cond ;((= num 6) (interval-widen number-interval (* mag 0.5)))
3031           ;((= num 5) (interval-widen number-interval (* mag 0.5)))
3032           (else (interval-widen number-interval (* mag 0.25))))
3033     (cond ((= num 6) (interval-widen number-interval (* mag 0.5)))
3034           ;((= num 5) (interval-widen number-interval (* mag 0.5)))
3035           (else (interval-widen number-interval (* mag 0.25))))
3036   ))
3037
3038 (define-public (adjust-slash-stencil num forward stencil mag)
3039   (if forward
3040     (cond ((= num 2)
3041               (ly:stencil-translate stencil (cons (* mag -0.00) (* mag 0.2))))
3042           ((= num 3)
3043               (ly:stencil-translate stencil (cons (* mag -0.00) (* mag 0.2))))
3044           ;((= num 5)
3045               ;(ly:stencil-translate stencil (cons (* mag -0.00) (* mag -0.07))))
3046           ;((= num 7)
3047           ;    (ly:stencil-translate stencil (cons (* mag -0.00) (* mag -0.15))))
3048           (else stencil))
3049     (cond ((= num 6)
3050               (ly:stencil-translate stencil (cons (* mag -0.00) (* mag 0.15))))
3051           ;((= num 8)
3052           ;    (ly:stencil-translate stencil (cons (* mag -0.00) (* mag -0.15))))
3053           (else stencil))
3054   )
3055 )
3056
3057 (define (slashed-digit-internal layout props num forward font-size thickness)
3058   (let* ((mag (magstep font-size))
3059          (thickness (* mag
3060                        (ly:output-def-lookup layout 'line-thickness)
3061                        thickness))
3062          ; backward slashes might use slope and point in the other direction!
3063          (dy (* mag (if forward 0.4 -0.4)))
3064          (number-stencil (interpret-markup layout
3065                                            (prepend-alist-chain 'font-encoding 'fetaText props)
3066                                            (number->string num)))
3067          (num-x (horizontal-slash-interval num forward (ly:stencil-extent number-stencil X) mag))
3068          (center (interval-center (ly:stencil-extent number-stencil Y)))
3069          ; Use the real extents of the slash, not the whole number, because we
3070          ; might translate the slash later on!
3071          (num-y (interval-widen (cons center center) (abs dy)))
3072          (is-sane (and (interval-sane? num-x) (interval-sane? num-y)))
3073          (slash-stencil (if is-sane
3074                             (make-line-stencil thickness
3075                                          (car num-x) (- (interval-center num-y) dy)
3076                                          (cdr num-x) (+ (interval-center num-y) dy))
3077                             #f)))
3078     (if (ly:stencil? slash-stencil)
3079       (begin
3080         ; for some numbers we need to shift the slash/backslash up or down to make
3081         ; the slashed digit look better
3082         (set! slash-stencil (adjust-slash-stencil num forward slash-stencil mag))
3083         (set! number-stencil
3084           (ly:stencil-add number-stencil slash-stencil)))
3085       (ly:warning "Unable to create slashed digit ~a" num))
3086     number-stencil))
3087
3088
3089 (define-markup-command (slashed-digit layout props num)
3090   (integer?)
3091   #:category other
3092   #:properties ((font-size 0)
3093                 (thickness 1.6))
3094   "
3095 @cindex slashed digits
3096
3097 A feta number, with slash.  This is for use in the context of
3098 figured bass notation.
3099 @lilypond[verbatim,quote]
3100 \\markup {
3101   \\slashed-digit #5
3102   \\hspace #2
3103   \\override #'(thickness . 3)
3104   \\slashed-digit #7
3105 }
3106 @end lilypond"
3107   (slashed-digit-internal layout props num #t font-size thickness))
3108
3109 (define-markup-command (backslashed-digit layout props num)
3110   (integer?)
3111   #:category other
3112   #:properties ((font-size 0)
3113                 (thickness 1.6))
3114   "
3115 @cindex backslashed digits
3116
3117 A feta number, with backslash.  This is for use in the context of
3118 figured bass notation.
3119 @lilypond[verbatim,quote]
3120 \\markup {
3121   \\backslashed-digit #5
3122   \\hspace #2
3123   \\override #'(thickness . 3)
3124   \\backslashed-digit #7
3125 }
3126 @end lilypond"
3127   (slashed-digit-internal layout props num #f font-size thickness))
3128
3129 ;; eyeglasses
3130 (define eyeglassespath
3131   '((moveto 0.42 0.77)
3132     (rcurveto 0 0.304 -0.246 0.55 -0.55 0.55)
3133     (rcurveto -0.304 0 -0.55 -0.246 -0.55 -0.55)
3134     (rcurveto 0 -0.304 0.246 -0.55 0.55 -0.55)
3135     (rcurveto 0.304 0 0.55 0.246 0.55 0.55)
3136     (closepath)
3137     (moveto 2.07 0.77)
3138     (rcurveto 0 0.304 -0.246 0.55 -0.55 0.55)
3139     (rcurveto -0.304 0 -0.55 -0.246 -0.55 -0.55)
3140     (rcurveto 0 -0.304 0.246 -0.55 0.55 -0.55)
3141     (rcurveto 0.304 0 0.55 0.246 0.55 0.55)
3142     (closepath)
3143     (moveto 1.025 0.935)
3144     (rcurveto 0 0.182 -0.148 0.33 -0.33 0.33)
3145     (rcurveto -0.182 0 -0.33 -0.148 -0.33 -0.33)
3146     (moveto -0.68 0.77)
3147     (rlineto 0.66 1.43)
3148     (rcurveto 0.132 0.286 0.55 0.44 0.385 -0.33)
3149     (moveto 2.07 0.77)
3150     (rlineto 0.66 1.43)
3151     (rcurveto 0.132 0.286 0.55 0.44 0.385 -0.33)))
3152
3153 (define-markup-command (eyeglasses layout props)
3154   ()
3155   #:category other
3156   "Prints out eyeglasses, indicating strongly to look at the conductor.
3157 @lilypond[verbatim,quote]
3158 \\markup { \\eyeglasses }
3159 @end lilypond"
3160   (interpret-markup layout props
3161     (make-override-markup '(line-cap-style . butt)
3162       (make-path-markup 0.15 eyeglassespath))))
3163
3164 (define-markup-command (left-brace layout props size)
3165   (number?)
3166   #:category other
3167   "
3168 A feta brace in point size @var{size}.
3169
3170 @lilypond[verbatim,quote]
3171 \\markup {
3172   \\left-brace #35
3173   \\hspace #2
3174   \\left-brace #45
3175 }
3176 @end lilypond"
3177   (let* ((font (ly:paper-get-font layout
3178                                   (cons '((font-encoding . fetaBraces)
3179                                           (font-name . #f))
3180                                         props)))
3181          (glyph-count (1- (ly:otf-glyph-count font)))
3182          (scale (ly:output-def-lookup layout 'output-scale))
3183          (scaled-size (/ (ly:pt size) scale))
3184          (glyph (lambda (n)
3185                   (ly:font-get-glyph font (string-append "brace"
3186                                                          (number->string n)))))
3187          (get-y-from-brace (lambda (brace)
3188                              (interval-length
3189                               (ly:stencil-extent (glyph brace) Y))))
3190          (find-brace (binary-search 0 glyph-count get-y-from-brace scaled-size))
3191          (glyph-found (glyph find-brace)))
3192
3193     (if (or (null? (ly:stencil-expr glyph-found))
3194             (< scaled-size (interval-length (ly:stencil-extent (glyph 0) Y)))
3195             (> scaled-size (interval-length
3196                             (ly:stencil-extent (glyph glyph-count) Y))))
3197         (begin
3198           (ly:warning (_ "no brace found for point size ~S ") size)
3199           (ly:warning (_ "defaulting to ~S pt")
3200                       (/ (* scale (interval-length
3201                                    (ly:stencil-extent glyph-found Y)))
3202                          (ly:pt 1)))))
3203     glyph-found))
3204
3205 (define-markup-command (right-brace layout props size)
3206   (number?)
3207   #:category other
3208   "
3209 A feta brace in point size @var{size}, rotated 180 degrees.
3210
3211 @lilypond[verbatim,quote]
3212 \\markup {
3213   \\right-brace #45
3214   \\hspace #2
3215   \\right-brace #35
3216 }
3217 @end lilypond"
3218   (interpret-markup layout props (markup #:rotate 180 #:left-brace size)))
3219
3220 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3221 ;; the note command.
3222 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3223
3224 ;; TODO: better syntax.
3225
3226 (define-markup-command (note-by-number layout props log dot-count dir)
3227   (number? number? number?)
3228   #:category music
3229   #:properties ((font-size 0)
3230                 (flag-style '())
3231                 (style '()))
3232   "
3233 @cindex notes within text by log and dot-count
3234
3235 Construct a note symbol, with stem and flag.  By using fractional values for
3236 @var{dir}, longer or shorter stems can be obtained.
3237 Supports all note-head-styles.
3238 Supported flag-styles are @code{default}, @code{old-straight-flag} and
3239 @code{modern-straight-flag}.
3240
3241 @lilypond[verbatim,quote]
3242 \\markup {
3243   \\note-by-number #3 #0 #DOWN
3244   \\hspace #2
3245   \\note-by-number #1 #2 #0.8
3246 }
3247 @end lilypond"
3248   (define (get-glyph-name-candidates dir log style)
3249     (map (lambda (dir-name)
3250                    (format #f "noteheads.~a~a" dir-name
3251                                    (if (and (symbol? style)
3252                                             (not (equal? 'default style)))
3253                                        (select-head-glyph style (min log 2))
3254                                        (min log 2))))
3255                  (list (if (= dir UP) "u" "d")
3256                        "s")))
3257
3258   (define (get-glyph-name font cands)
3259     (if (null? cands)
3260                 ""
3261                 (if (ly:stencil-empty? (ly:font-get-glyph font (car cands)))
3262                     (get-glyph-name font (cdr cands))
3263                     (car cands))))
3264
3265   (define (buildflags flag-stencil remain curr-stencil spacing)
3266      ;; Function to recursively create a stencil with @code{remain} flags
3267      ;; from the single-flag stencil @code{curr-stencil}, which is already
3268      ;; translated to the position of the previous flag position.
3269      ;;
3270      ;; Copy and paste from /scm/flag-styles.scm
3271     (if (> remain 0)
3272         (let* ((translated-stencil
3273                  (ly:stencil-translate-axis curr-stencil spacing Y))
3274                (new-stencil (ly:stencil-add flag-stencil translated-stencil)))
3275           (buildflags new-stencil (- remain 1) translated-stencil spacing))
3276         flag-stencil))
3277
3278   (define (straight-flag-mrkp flag-thickness flag-spacing
3279                                 upflag-angle upflag-length
3280                                 downflag-angle downflag-length
3281                                 dir)
3282   ;; Create a stencil for a straight flag.  @var{flag-thickness} and
3283   ;; @var{flag-spacing} are given in staff spaces, @var{upflag-angle} and
3284   ;; @var{downflag-angle} are given in degrees, and @var{upflag-length} and
3285   ;; @var{downflag-length} are given in staff spaces.
3286   ;;
3287   ;; All lengths are scaled according to the font size of the note.
3288   ;;
3289   ;; From /scm/flag-styles.scm, modified to fit here.
3290
3291       (let* ((stem-up (> dir 0))
3292              ; scale with the note size
3293              (factor (magstep font-size))
3294              (stem-thickness (* factor 0.1))
3295              (line-thickness (ly:output-def-lookup layout 'line-thickness))
3296              (half-stem-thickness (/ (* stem-thickness line-thickness) 2))
3297              (raw-length (if stem-up upflag-length downflag-length))
3298              (angle (if stem-up upflag-angle downflag-angle))
3299              (flag-length (+ (* raw-length factor) half-stem-thickness))
3300              (flag-end (polar->rectangular flag-length angle))
3301              (thickness (* flag-thickness factor))
3302              (thickness-offset (cons 0 (* -1 thickness dir)))
3303              (spacing (* -1 flag-spacing factor dir))
3304              (start (cons (- half-stem-thickness) (* half-stem-thickness dir)))
3305              ; The points of a round-filled-polygon need to be given in
3306              ; clockwise order, otherwise the polygon will be enlarged by
3307              ; blot-size*2!
3308              (points (if stem-up (list start flag-end
3309                                        (offset-add flag-end thickness-offset)
3310                                        (offset-add start thickness-offset))
3311                                  (list start
3312                                        (offset-add start thickness-offset)
3313                                        (offset-add flag-end thickness-offset)
3314                                        flag-end)))
3315              (stencil (ly:round-filled-polygon points half-stem-thickness))
3316              ; Log for 1/8 is 3, so we need to subtract 3
3317              (flag-stencil (buildflags stencil (- log 3) stencil spacing)))
3318         flag-stencil))
3319
3320   (let* ((font (ly:paper-get-font layout (cons '((font-encoding . fetaMusic))
3321                                                  props)))
3322          (size-factor (magstep font-size))
3323          (blot (ly:output-def-lookup layout 'blot-diameter))
3324          (head-glyph-name
3325            (let ((result (get-glyph-name font
3326                            (get-glyph-name-candidates
3327                              (sign dir) log style))))
3328              (if (string-null? result)
3329                  ;; If no glyph name can be found, select default heads.
3330                  ;; Though this usually means an unsupported style has been
3331                  ;; chosen, it also prevents unrelated 'style settings from
3332                  ;; other grobs (e.g., TextSpanner and TimeSignature) leaking
3333                  ;; into markup.
3334                  (get-glyph-name font
3335                    (get-glyph-name-candidates
3336                      (sign dir) log 'default))
3337                  result)))
3338          (head-glyph (ly:font-get-glyph font head-glyph-name))
3339          (ancient-flags? (or (eq? style 'mensural) (eq? style 'neomensural)))
3340          (attach-indices (ly:note-head::stem-attachment font head-glyph-name))
3341          (stem-length (* size-factor (max 3 (- log 1))))
3342          ;; With ancient-flags we want a tighter stem
3343          (stem-thickness (* size-factor (if ancient-flags? 0.1 0.13)))
3344          (stemy (* dir stem-length))
3345          (attach-off (cons (interval-index
3346                              (ly:stencil-extent head-glyph X)
3347                              (* (sign dir) (car attach-indices)))
3348                            ; fixme, this is inconsistent between X & Y.
3349                            (* (sign dir)
3350                               (interval-index
3351                                (ly:stencil-extent head-glyph Y)
3352                                (cdr attach-indices)))))
3353          ;; For a tighter stem (with ancient-flags) the stem-width has to be
3354          ;; adjusted.
3355          (stem-X-corr (if ancient-flags? (* 0.5 dir stem-thickness) 0))
3356          (stem-glyph (and (> log 0)
3357                           (ly:round-filled-box
3358                             (ordered-cons (+ stem-X-corr (car attach-off))
3359                                   (+ stem-X-corr (car attach-off)
3360                                      (* (- (sign dir)) stem-thickness)))
3361                             (cons (min stemy (cdr attach-off))
3362                                   (max stemy (cdr attach-off)))
3363                             (/ stem-thickness 3))))
3364          (dot (ly:font-get-glyph font "dots.dot"))
3365          (dotwid (interval-length (ly:stencil-extent dot X)))
3366          (dots (and (> dot-count 0)
3367                     (apply ly:stencil-add
3368                            (map (lambda (x)
3369                                   (ly:stencil-translate-axis
3370                                    dot (* 2 x dotwid) X))
3371                                 (iota dot-count)))))
3372          ;; Straight-flags. Values taken from /scm/flag-style.scm
3373          (modern-straight-flag (straight-flag-mrkp 0.55 1 -18 1.1 22 1.2 dir))
3374          (old-straight-flag (straight-flag-mrkp 0.55 1 -45 1.2 45 1.4 dir))
3375          ;; Calculate a corrective to avoid a gap between
3376          ;; straight-flags and the stem.
3377          (flag-style-Y-corr (if (or (eq? flag-style 'modern-straight-flag)
3378                                     (eq? flag-style 'old-straight-flag))
3379                                 (/ blot 10 (* -1 dir))
3380                                 0))
3381          (flaggl (and (> log 2)
3382                       (ly:stencil-translate
3383                         (cond ((eq? flag-style 'modern-straight-flag)
3384                                modern-straight-flag)
3385                               ((eq? flag-style 'old-straight-flag)
3386                                old-straight-flag)
3387                               (else
3388                                (ly:font-get-glyph font
3389                                  (format #f (if ancient-flags?
3390                                                 "flags.mensural~a2~a"
3391                                                 "flags.~a~a")
3392                                             (if (> dir 0) "u" "d")
3393                                             log))))
3394                         (cons (+ (car attach-off)
3395                                  ;; For tighter stems (with ancient-flags) the
3396                                  ;; flag has to be adjusted different.
3397                                  (if (and (not ancient-flags?) (< dir 0))
3398                                      stem-thickness
3399                                      0))
3400                               (+ stemy flag-style-Y-corr))))))
3401
3402     ;; If there is a flag on an upstem and the stem is short, move the dots
3403     ;; to avoid the flag.  16th notes get a special case because their flags
3404     ;; hang lower than any other flags.
3405     ;; Not with ancient flags or straight-flags.
3406     (if (and dots (> dir 0) (> log 2)
3407                      (or (eq? flag-style 'default) (null? flag-style))
3408                      (not ancient-flags?)
3409                      (or (< dir 1.15) (and (= log 4) (< dir 1.3))))
3410                 (set! dots (ly:stencil-translate-axis dots 0.5 X)))
3411     (if flaggl
3412         (set! stem-glyph (ly:stencil-add flaggl stem-glyph)))
3413     (if (ly:stencil? stem-glyph)
3414         (set! stem-glyph (ly:stencil-add stem-glyph head-glyph))
3415         (set! stem-glyph head-glyph))
3416     (if (ly:stencil? dots)
3417         (set! stem-glyph
3418               (ly:stencil-add
3419                 (ly:stencil-translate-axis
3420                   dots
3421                   (+ (cdr (ly:stencil-extent head-glyph X)) dotwid)
3422                   X)
3423                 stem-glyph)))
3424     stem-glyph))
3425
3426 (define-public log2
3427   (let ((divisor (log 2)))
3428     (lambda (z) (inexact->exact (/ (log z) divisor)))))
3429
3430 (define (parse-simple-duration duration-string)
3431   "Parse the `duration-string', e.g. ''4..'' or ''breve.'',
3432 and return a (log dots) list."
3433   (let ((match (regexp-exec (make-regexp "(breve|longa|maxima|[0-9]+)(\\.*)")
3434                             duration-string)))
3435     (if (and match (string=? duration-string (match:substring match 0)))
3436         (let ((len (match:substring match 1))
3437               (dots (match:substring match 2)))
3438           (list (cond ((string=? len "breve") -1)
3439                       ((string=? len "longa") -2)
3440                       ((string=? len "maxima") -3)
3441                       (else (log2 (string->number len))))
3442                 (if dots (string-length dots) 0)))
3443         (ly:error (_ "not a valid duration string: ~a") duration-string))))
3444
3445 (define-markup-command (note layout props duration dir)
3446   (string? number?)
3447   #:category music
3448   #:properties (note-by-number-markup)
3449   "
3450 @cindex notes within text by string
3451
3452 This produces a note with a stem pointing in @var{dir} direction, with
3453 the @var{duration} for the note head type and augmentation dots.  For
3454 example, @code{\\note #\"4.\" #-0.75} creates a dotted quarter note, with
3455 a shortened down stem.
3456
3457 @lilypond[verbatim,quote]
3458 \\markup {
3459   \\override #'(style . cross) {
3460     \\note #\"4..\" #UP
3461   }
3462   \\hspace #2
3463   \\note #\"breve\" #0
3464 }
3465 @end lilypond"
3466   (let ((parsed (parse-simple-duration duration)))
3467     (note-by-number-markup layout props (car parsed) (cadr parsed) dir)))
3468
3469 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3470 ;; the rest command.
3471 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3472
3473 (define-markup-command (rest-by-number layout props log dot-count)
3474   (number? number?)
3475   #:category music
3476   #:properties ((font-size 0)
3477                 (style '())
3478                 (multi-measure-rest #f))
3479   "
3480 @cindex rests or multi-measure-rests within text by log and dot-count
3481
3482 A rest or multi-measure-rest symbol.
3483
3484 @lilypond[verbatim,quote]
3485 \\markup {
3486   \\rest-by-number #3 #2
3487   \\hspace #2
3488   \\rest-by-number #0 #1
3489   \\hspace #2
3490   \\override #'(multi-measure-rest . #t)
3491   \\rest-by-number #0 #0
3492 }
3493 @end lilypond"
3494
3495   (define (get-glyph-name-candidates log style)
3496     (let* (;; Choose the style-string to be added.
3497            ;; If no glyph exists, select others for the specified styles
3498            ;; otherwise defaulting.
3499            (style-strg
3500              (cond (
3501                    ;; 'baroque needs to be special-cased, otherwise
3502                    ;; `select-head-glyph´ would catch neomensural-glyphs for
3503                    ;; this style, if (< log 0).
3504                    (eq? style 'baroque)
3505                     (string-append (number->string log) ""))
3506                    ((eq? style 'petrucci)
3507                     (string-append (number->string log) "mensural"))
3508                     ;; In other cases `select-head-glyph´ from output-lib.scm
3509                     ;; works for rest-glyphs, too.
3510                    ((and (symbol? style) (not (eq? style 'default)))
3511                     (select-head-glyph style log))
3512                    (else log)))
3513            ;; Choose ledgered glyphs for whole and half rest.
3514            ;; Except for the specified styles, logs and MultiMeasureRests.
3515            (ledger-style-rests
3516              (if (and (or (list? style)
3517                           (not (member style
3518                                   '(neomensural mensural petrucci))))
3519                       (not multi-measure-rest)
3520                       (or (= log 0) (= log 1)))
3521                 "o"
3522                 "")))
3523       (format #f "rests.~a~a" style-strg ledger-style-rests)))
3524
3525   (define (get-glyph-name font cands)
3526      (if (ly:stencil-empty? (ly:font-get-glyph font cands))
3527         ""
3528         cands))
3529
3530   (let* ((font
3531            (ly:paper-get-font layout
3532              (cons '((font-encoding . fetaMusic)) props)))
3533          (rest-glyph-name
3534             (let ((result
3535                     (get-glyph-name font
3536                       (get-glyph-name-candidates log style))))
3537               (if (string-null? result)
3538                 ;; If no glyph name can be found, select default rests.  Though
3539                 ;; this usually means an unsupported style has been chosen, it
3540                 ;; also prevents unrelated 'style settings from other grobs
3541                 ;; (e.g., TextSpanner and TimeSignature) leaking into markup.
3542                 (get-glyph-name font (get-glyph-name-candidates log 'default))
3543                 result)))
3544          (rest-glyph (ly:font-get-glyph font rest-glyph-name))
3545          (dot (ly:font-get-glyph font "dots.dot"))
3546          (dot-width (interval-length (ly:stencil-extent dot X)))
3547          (dots (and (> dot-count 0)
3548                     (apply ly:stencil-add
3549                            (map (lambda (x)
3550                                   (ly:stencil-translate-axis
3551                                    dot (* 2 x dot-width) X))
3552                                 (iota dot-count))))))
3553
3554     ;; Apart from mensural-, neomensural- and petrucci-style ledgered
3555     ;; glyphs are taken for whole and half rests.
3556     ;; If they are dotted, move the dots in X-direction to avoid collision.
3557     (if (and dots
3558              (< log 2)
3559              (>= log 0)
3560              (not (member style '(neomensural mensural petrucci))))
3561        (set! dots (ly:stencil-translate-axis dots dot-width X)))
3562
3563     ;; Add dots to the rest-glyph.
3564     ;;
3565     ;; Not sure how to vertical align dots.
3566     ;; For now the dots are centered for half, whole or longer rests.
3567     ;; Otherwise placed near the top of the rest.
3568     ;;
3569     ;; Dots for rests with (< log 0) dots are allowed, but not
3570     ;; if multi-measure-rest is set #t.
3571     (if (and (not multi-measure-rest) dots)
3572         (set! rest-glyph
3573               (ly:stencil-add
3574                (ly:stencil-translate
3575                    dots
3576                         (cons
3577                            (+ (cdr (ly:stencil-extent rest-glyph X)) dot-width)
3578                            (if (< log 2)
3579                              (interval-center (ly:stencil-extent rest-glyph Y))
3580                              (- (interval-end (ly:stencil-extent rest-glyph Y))
3581                                 (/ (* 2 dot-width) 3)))))
3582                rest-glyph)))
3583     rest-glyph))
3584
3585 (define-markup-command (rest layout props duration)
3586   (string?)
3587   #:category music
3588   #:properties ((style '())
3589                 (multi-measure-rest #f)
3590                 (multi-measure-rest-number #t)
3591                 (word-space 0.6))
3592   "
3593 @cindex rests or multi-measure-rests within text by string
3594
3595 This produces a rest, with the @var{duration} for the rest type and
3596 augmentation dots.
3597 @code{\"breve\"}, @code{\"longa\"} and @code{\"maxima\"} are valid
3598 input-strings.
3599
3600 Printing MultiMeasureRests could be enabled with
3601 @code{\\override #'(multi-measure-rest . #t)}
3602 If MultiMeasureRests are taken, the MultiMeasureRestNumber is printed above.
3603 This is enabled for all styles using default-glyphs.
3604 Could be disabled with @code{\\override #'(multi-measure-rest-number . #f)}
3605
3606 @lilypond[verbatim,quote]
3607 \\markup {
3608   \\rest #\"4..\"
3609   \\hspace #2
3610   \\rest #\"breve\"
3611   \\hspace #2
3612   \\override #'(multi-measure-rest . #t)
3613   {
3614   \\rest #\"7\"
3615   \\hspace #2
3616   \\override #'(multi-measure-rest-number . #f)
3617   \\rest #\"7\"
3618   }
3619 }
3620 @end lilypond"
3621   ;; Get the number of mmr-glyphs.
3622   ;; Store them in a list.
3623   ;; example: (mmr-numbers 25) -> '(3 0 0 1)
3624   (define (mmr-numbers nmbr)
3625       (let* ((8-bar-glyph (floor (/ nmbr 8)))
3626              (8-remainder (remainder nmbr 8))
3627              (4-bar-glyph (floor (/ 8-remainder 4)))
3628              (4-remainder (remainder nmbr 4))
3629              (2-bar-glyph (floor (/ 4-remainder 2)))
3630              (2-remainder (remainder 4-remainder 2))
3631              (1-bar-glyph (floor (/ 2-remainder 1))))
3632        (list 8-bar-glyph 4-bar-glyph 2-bar-glyph 1-bar-glyph)))
3633
3634   ;; Get the correct mmr-glyphs.
3635   ;; Store them in a list.
3636   ;; example:
3637   ;; (get-mmr-glyphs '(1 0 1 0) '("rests.M3" "rests.M2" "rests.M1" "rests.0"))
3638   ;; -> ("rests.M3" "rests.M1")
3639   (define (get-mmr-glyphs lst1 lst2)
3640      (define (helper l1 l2 l3)
3641         (if (null? l1)
3642            (reverse l3)
3643            (helper (cdr l1)
3644                    (cdr l2)
3645                    (append (make-list (car l1) (car l2)) l3))))
3646       (helper lst1 lst2 '()))
3647
3648   ;; If duration is not valid, print a warning and return empty-stencil
3649   (if (or (and (not (integer? (car (parse-simple-duration duration))))
3650                (not multi-measure-rest))
3651           (and (= (string-length (car (string-split duration #\. ))) 1)
3652                (= (string->number (car (string-split duration #\. ))) 0)))
3653     (begin
3654       (ly:warning (_ "not a valid duration string: ~a - ignoring") duration)
3655       empty-stencil)
3656     (let* (
3657        ;; For simple rests:
3658            ;; Get a (log dots) list.
3659            (parsed (parse-simple-duration duration))
3660            ;; Create the rest-stencil
3661            (stil
3662               (rest-by-number-markup layout props (car parsed) (cadr parsed)))
3663        ;; For MultiMeasureRests:
3664            ;; Get the duration-part of duration
3665            (dur-part-string (car (string-split duration #\. )))
3666            ;; Get the duration of MMR:
3667            ;; If not a number (eg. "maxima") calculate it.
3668            (mmr-duration
3669              (or (string->number dur-part-string) (expt 2 (abs (car parsed)))))
3670            ;; Get a list of the correct number of each mmr-glyph.
3671            (count-mmr-glyphs-list (mmr-numbers mmr-duration))
3672            ;; Create a list of mmr-stencils,
3673            ;; translating the glyph for a whole rest.
3674            (mmr-stils-list
3675               (map
3676                  (lambda (x)
3677                     (let ((single-mmr-stil
3678                             (rest-by-number-markup layout props (* -1 x) 0)))
3679                        (if (= x 0)
3680                           (ly:stencil-translate-axis
3681                             single-mmr-stil
3682                             ;; Ugh, hard-coded, why 1?
3683                             1
3684                             Y)
3685                            single-mmr-stil)))
3686                  (get-mmr-glyphs count-mmr-glyphs-list (reverse (iota 4)))))
3687             ;; Adjust the space between the mmr-glyphs,
3688             ;; if not default-glyphs are used.
3689             (word-space (if (member style
3690                                     '(neomensural mensural petrucci))
3691                            (/ (* word-space 2) 3)
3692                            word-space))
3693             ;; Create the final mmr-stencil
3694             ;; via `stack-stencil-line´ from /scm/markup.scm
3695             (mmr-stil (stack-stencil-line word-space mmr-stils-list)))
3696
3697       ;; Print the number above a multi-measure-rest
3698       ;; Depends on duration, style and multi-measure-rest-number set #t
3699       (if (and multi-measure-rest
3700                multi-measure-rest-number
3701                (> mmr-duration 1)
3702                (not (member style '(neomensural mensural petrucci))))
3703          (let* ((mmr-stil-x-center
3704                    (interval-center (ly:stencil-extent mmr-stil X)))
3705                 (duration-markup
3706                    (markup
3707                       #:fontsize -2
3708                       #:override '(font-encoding . fetaText)
3709                       (number->string mmr-duration)))
3710                 (mmr-number-stil
3711                    (interpret-markup layout props duration-markup))
3712                 (mmr-number-stil-x-center
3713                    (interval-center (ly:stencil-extent mmr-number-stil X))))
3714
3715          (set! mmr-stil (ly:stencil-combine-at-edge
3716                            mmr-stil
3717                            Y UP
3718                            (ly:stencil-translate-axis
3719                               mmr-number-stil
3720                               (- mmr-stil-x-center mmr-number-stil-x-center)
3721                               X)
3722                            ;; Ugh, hardcoded
3723                            0.8))))
3724     (if multi-measure-rest
3725        mmr-stil
3726        stil))))
3727
3728 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3729 ;; translating.
3730 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3731
3732 (define-markup-command (lower layout props amount arg)
3733   (number? markup?)
3734   #:category align
3735   "
3736 @cindex lowering text
3737
3738 Lower @var{arg} by the distance @var{amount}.
3739 A negative @var{amount} indicates raising; see also @code{\\raise}.
3740
3741 @lilypond[verbatim,quote]
3742 \\markup {
3743   one
3744   \\lower #3
3745   two
3746   three
3747 }
3748 @end lilypond"
3749   (ly:stencil-translate-axis (interpret-markup layout props arg)
3750                              (- amount) Y))
3751
3752 (define-markup-command (translate-scaled layout props offset arg)
3753   (number-pair? markup?)
3754   #:category align
3755   #:properties ((font-size 0))
3756   "
3757 @cindex translating text
3758 @cindex scaling text
3759
3760 Translate @var{arg} by @var{offset}, scaling the offset by the
3761 @code{font-size}.
3762
3763 @lilypond[verbatim,quote]
3764 \\markup {
3765   \\fontsize #5 {
3766     * \\translate #'(2 . 3) translate
3767     \\hspace #2
3768     * \\translate-scaled #'(2 . 3) translate-scaled
3769   }
3770 }
3771 @end lilypond"
3772   (let* ((factor (magstep font-size))
3773          (scaled (cons (* factor (car offset))
3774                        (* factor (cdr offset)))))
3775     (ly:stencil-translate (interpret-markup layout props arg)
3776                           scaled)))
3777
3778 (define-markup-command (raise layout props amount arg)
3779   (number? markup?)
3780   #:category align
3781   "
3782 @cindex raising text
3783
3784 Raise @var{arg} by the distance @var{amount}.
3785 A negative @var{amount} indicates lowering, see also @code{\\lower}.
3786
3787 The argument to @code{\\raise} is the vertical displacement amount,
3788 measured in (global) staff spaces.  @code{\\raise} and @code{\\super}
3789 raise objects in relation to their surrounding markups.
3790
3791 If the text object itself is positioned above or below the staff, then
3792 @code{\\raise} cannot be used to move it, since the mechanism that
3793 positions it next to the staff cancels any shift made with
3794 @code{\\raise}.  For vertical positioning, use the @code{padding}
3795 and/or @code{extra-offset} properties.
3796
3797 @lilypond[verbatim,quote]
3798 \\markup {
3799   C
3800   \\small
3801   \\bold
3802   \\raise #1.0
3803   9/7+
3804 }
3805 @end lilypond"
3806   (ly:stencil-translate-axis (interpret-markup layout props arg) amount Y))
3807
3808 (define-markup-command (fraction layout props arg1 arg2)
3809   (markup? markup?)
3810   #:category other
3811   #:properties ((font-size 0))
3812   "
3813 @cindex creating text fractions
3814
3815 Make a fraction of two markups.
3816 @lilypond[verbatim,quote]
3817 \\markup {
3818   π ≈
3819   \\fraction 355 113
3820 }
3821 @end lilypond"
3822   (let* ((m1 (interpret-markup layout props arg1))
3823          (m2 (interpret-markup layout props arg2))
3824          (factor (magstep font-size))
3825          (boxdimen (cons (* factor -0.05) (* factor 0.05)))
3826          (padding (* factor 0.2))
3827          (baseline (* factor 0.6))
3828          (offset (* factor 0.75)))
3829     (set! m1 (ly:stencil-aligned-to m1 X CENTER))
3830     (set! m2 (ly:stencil-aligned-to m2 X CENTER))
3831     (let* ((x1 (ly:stencil-extent m1 X))
3832            (x2 (ly:stencil-extent m2 X))
3833            (line (ly:round-filled-box (interval-union x1 x2) boxdimen 0.0))
3834            ;; should stack mols separately, to maintain LINE on baseline
3835            (stack (stack-lines DOWN padding baseline (list m1 line m2))))
3836       (set! stack
3837             (ly:stencil-aligned-to stack Y CENTER))
3838       (set! stack
3839             (ly:stencil-aligned-to stack X LEFT))
3840       ;; should have EX dimension
3841       ;; empirical anyway
3842       (ly:stencil-translate-axis stack offset Y))))
3843
3844 (define-markup-command (normal-size-super layout props arg)
3845   (markup?)
3846   #:category font
3847   #:properties ((baseline-skip))
3848   "
3849 @cindex setting superscript in standard font size
3850
3851 Set @var{arg} in superscript with a normal font size.
3852
3853 @lilypond[verbatim,quote]
3854 \\markup {
3855   default
3856   \\normal-size-super {
3857     superscript in standard size
3858   }
3859 }
3860 @end lilypond"
3861   (ly:stencil-translate-axis
3862    (interpret-markup layout props arg)
3863    (* 0.5 baseline-skip) Y))
3864
3865 (define-markup-command (super layout props arg)
3866   (markup?)
3867   #:category font
3868   #:properties ((font-size 0)
3869                 (baseline-skip))
3870   "
3871 @cindex superscript text
3872
3873 Set @var{arg} in superscript.
3874
3875 @lilypond[verbatim,quote]
3876 \\markup {
3877   E =
3878   \\concat {
3879     mc
3880     \\super
3881     2
3882   }
3883 }
3884 @end lilypond"
3885   (ly:stencil-translate-axis
3886    (interpret-markup
3887     layout
3888     (cons `((font-size . ,(- font-size 3))) props)
3889     arg)
3890    (* 0.5 baseline-skip)
3891    Y))
3892
3893 (define-markup-command (translate layout props offset arg)
3894   (number-pair? markup?)
3895   #:category align
3896   "
3897 @cindex translating text
3898
3899 Translate @var{arg} relative to its surroundings.  @var{offset}
3900 is a pair of numbers representing the displacement in the X and Y axis.
3901
3902 @lilypond[verbatim,quote]
3903 \\markup {
3904   *
3905   \\translate #'(2 . 3)
3906   \\line { translated two spaces right, three up }
3907 }
3908 @end lilypond"
3909   (ly:stencil-translate (interpret-markup layout props arg)
3910                         offset))
3911
3912 (define-markup-command (sub layout props arg)
3913   (markup?)
3914   #:category font
3915   #:properties ((font-size 0)
3916                 (baseline-skip))
3917   "
3918 @cindex subscript text
3919
3920 Set @var{arg} in subscript.
3921
3922 @lilypond[verbatim,quote]
3923 \\markup {
3924   \\concat {
3925     H
3926     \\sub {
3927       2
3928     }
3929     O
3930   }
3931 }
3932 @end lilypond"
3933   (ly:stencil-translate-axis
3934    (interpret-markup
3935     layout
3936     (cons `((font-size . ,(- font-size 3))) props)
3937     arg)
3938    (* -0.5 baseline-skip)
3939    Y))
3940
3941 (define-markup-command (normal-size-sub layout props arg)
3942   (markup?)
3943   #:category font
3944   #:properties ((baseline-skip))
3945   "
3946 @cindex setting subscript in standard font size
3947
3948 Set @var{arg} in subscript with a normal font size.
3949
3950 @lilypond[verbatim,quote]
3951 \\markup {
3952   default
3953   \\normal-size-sub {
3954     subscript in standard size
3955   }
3956 }
3957 @end lilypond"
3958   (ly:stencil-translate-axis
3959    (interpret-markup layout props arg)
3960    (* -0.5 baseline-skip)
3961    Y))
3962
3963 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3964 ;; brackets.
3965 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3966
3967 (define-markup-command (hbracket layout props arg)
3968   (markup?)
3969   #:category graphic
3970   "
3971 @cindex placing horizontal brackets around text
3972
3973 Draw horizontal brackets around @var{arg}.
3974
3975 @lilypond[verbatim,quote]
3976 \\markup {
3977   \\hbracket {
3978     \\line {
3979       one two three
3980     }
3981   }
3982 }
3983 @end lilypond"
3984   (let ((th 0.1) ;; todo: take from GROB.
3985         (m (interpret-markup layout props arg)))
3986     (bracketify-stencil m X th (* 2.5 th) th)))
3987
3988 (define-markup-command (bracket layout props arg)
3989   (markup?)
3990   #:category graphic
3991   "
3992 @cindex placing vertical brackets around text
3993
3994 Draw vertical brackets around @var{arg}.
3995
3996 @lilypond[verbatim,quote]
3997 \\markup {
3998   \\bracket {
3999     \\note #\"2.\" #UP
4000   }
4001 }
4002 @end lilypond"
4003   (let ((th 0.1) ;; todo: take from GROB.
4004         (m (interpret-markup layout props arg)))
4005     (bracketify-stencil m Y th (* 2.5 th) th)))
4006
4007 (define-markup-command (parenthesize layout props arg)
4008   (markup?)
4009   #:category graphic
4010   #:properties ((angularity 0)
4011                 (padding)
4012                 (size 1)
4013                 (thickness 1)
4014                 (width 0.25))
4015   "
4016 @cindex placing parentheses around text
4017
4018 Draw parentheses around @var{arg}.  This is useful for parenthesizing
4019 a column containing several lines of text.
4020
4021 @lilypond[verbatim,quote]
4022 \\markup {
4023   \\line {
4024     \\parenthesize {
4025       \\column {
4026         foo
4027         bar
4028       }
4029     }
4030     \\override #'(angularity . 2) {
4031       \\parenthesize {
4032         \\column {
4033           bah
4034           baz
4035         }
4036       }
4037     }
4038   }
4039 }
4040 @end lilypond"
4041   (let* ((markup (interpret-markup layout props arg))
4042          (scaled-width (* size width))
4043          (scaled-thickness
4044           (* (chain-assoc-get 'line-thickness props 0.1)
4045              thickness))
4046          (half-thickness
4047           (min (* size 0.5 scaled-thickness)
4048                (* (/ 4 3.0) scaled-width)))
4049          (padding (chain-assoc-get 'padding props half-thickness)))
4050     (parenthesize-stencil
4051      markup half-thickness scaled-width angularity padding)))
4052
4053
4054 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
4055 ;; Delayed markup evaluation
4056 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
4057
4058 (define-markup-command (page-ref layout props label gauge default)
4059   (symbol? markup? markup?)
4060   #:category other
4061   "
4062 @cindex referencing page numbers in text
4063
4064 Reference to a page number.  @var{label} is the label set on the referenced
4065 page (using the @code{\\label} command), @var{gauge} a markup used to estimate
4066 the maximum width of the page number, and @var{default} the value to display
4067 when @var{label} is not found."
4068   (let* ((gauge-stencil (interpret-markup layout props gauge))
4069          (x-ext (ly:stencil-extent gauge-stencil X))
4070          (y-ext (ly:stencil-extent gauge-stencil Y)))
4071     (ly:make-stencil
4072      `(delay-stencil-evaluation
4073        ,(delay (ly:stencil-expr
4074                 (let* ((table (ly:output-def-lookup layout 'label-page-table))
4075                        (page-number (if (list? table)
4076                                         (assoc-get label table)
4077                                         #f))
4078                        (page-markup (if page-number (format #f "~a" page-number) default))
4079                        (page-stencil (interpret-markup layout props page-markup))
4080                        (gap (- (interval-length x-ext)
4081                                (interval-length (ly:stencil-extent page-stencil X)))))
4082                   (interpret-markup layout props
4083                                     (markup #:concat (#:hspace gap page-markup)))))))
4084      x-ext
4085      y-ext)))
4086
4087 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
4088 ;; scaling
4089 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
4090
4091 (define-markup-command (scale layout props factor-pair arg)
4092   (number-pair? markup?)
4093   #:category graphic
4094   "
4095 @cindex scaling markup
4096 @cindex mirroring markup
4097
4098 Scale @var{arg}.  @var{factor-pair} is a pair of numbers
4099 representing the scaling-factor in the X and Y axes.
4100 Negative values may be used to produce mirror images.
4101
4102 @lilypond[verbatim,quote]
4103 \\markup {
4104   \\line {
4105     \\scale #'(2 . 1)
4106     stretched
4107     \\scale #'(1 . -1)
4108     mirrored
4109   }
4110 }
4111 @end lilypond"
4112   (let ((stil (interpret-markup layout props arg))
4113         (sx (car factor-pair))
4114         (sy (cdr factor-pair)))
4115     (ly:stencil-scale stil sx sy)))
4116
4117 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
4118 ;; Repeating
4119 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
4120
4121 (define-markup-command (pattern layout props count axis space pattern)
4122   (integer? integer? number? markup?)
4123   #:category other
4124   "
4125 Prints @var{count} times a @var{pattern} markup.
4126 Patterns are spaced apart by @var{space}.
4127 Patterns are distributed on @var{axis}.
4128
4129 @lilypond[verbatim, quote]
4130 \\markup \\column {
4131   \"Horizontally repeated :\"
4132   \\pattern #7 #X #2 \\flat
4133   \\null
4134   \"Vertically repeated :\"
4135   \\pattern #3 #Y #0.5 \\flat
4136 }
4137 @end lilypond"
4138   (let ((pattern-width (interval-length
4139                          (ly:stencil-extent (interpret-markup layout props pattern) X)))
4140         (new-props (prepend-alist-chain 'word-space 0 (prepend-alist-chain 'baseline-skip 0 props))))
4141     (let loop ((i (1- count)) (patterns (markup)))
4142       (if (zero? i)
4143           (interpret-markup
4144             layout
4145             new-props
4146             (if (= axis X)
4147                 (markup patterns pattern)
4148                 (markup #:column (patterns pattern))))
4149           (loop (1- i)
4150             (if (= axis X)
4151                 (markup patterns pattern #:hspace space)
4152                 (markup #:column (patterns pattern #:vspace space))))))))
4153
4154 (define-markup-command (fill-with-pattern layout props space dir pattern left right)
4155   (number? ly:dir? markup? markup? markup?)
4156   #:category align
4157   #:properties ((word-space)
4158                 (line-width))
4159   "
4160 Put @var{left} and @var{right} in a horizontal line of width @code{line-width}
4161 with a line of markups @var{pattern} in between.
4162 Patterns are spaced apart by @var{space}.
4163 Patterns are aligned to the @var{dir} markup.
4164
4165 @lilypond[verbatim, quote]
4166 \\markup \\column {
4167   \"right-aligned :\"
4168   \\fill-with-pattern #1 #RIGHT . first right
4169   \\fill-with-pattern #1 #RIGHT . second right
4170   \\null
4171   \"center-aligned :\"
4172   \\fill-with-pattern #1.5 #CENTER - left right
4173   \\null
4174   \"left-aligned :\"
4175   \\override #'(line-width . 50)
4176   \\fill-with-pattern #2 #LEFT : left first
4177   \\override #'(line-width . 50)
4178   \\fill-with-pattern #2 #LEFT : left second
4179 }
4180 @end lilypond"
4181   (let* ((pattern-x-extent (ly:stencil-extent (interpret-markup layout props pattern) X))
4182          (pattern-width (interval-length pattern-x-extent))
4183          (left-width (interval-length (ly:stencil-extent (interpret-markup layout props left) X)))
4184          (right-width (interval-length (ly:stencil-extent (interpret-markup layout props right) X)))
4185          (middle-width (max 0 (- line-width (+ (+ left-width right-width) (* word-space 2)))))
4186          (period (+ space pattern-width))
4187          (count (truncate (/ (- middle-width pattern-width) period)))
4188          (x-offset (+ (* (- (- middle-width (* count period)) pattern-width) (/ (1+ dir) 2)) (abs (car pattern-x-extent)))))
4189     (interpret-markup layout props
4190                       (markup left
4191                               #:with-dimensions (cons 0 middle-width) '(0 . 0)
4192                               #:translate (cons x-offset 0)
4193                               #:pattern (1+ count) X space pattern
4194                               right))))
4195
4196 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
4197 ;; Replacements
4198 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
4199
4200 (define-markup-command (replace layout props replacements arg)
4201   (list? markup?)
4202   #:category font
4203   "
4204 Used to automatically replace a string by another in the markup @var{arg}.
4205 Each pair of the alist @var{replacements} specifies what should be replaced.
4206 The @code{key} is the string to be replaced by the @code{value} string.
4207
4208 @lilypond[verbatim, quote]
4209 \\markup \\replace #'((\"thx\" . \"Thanks!\")) thx
4210 @end lilypond"
4211   (interpret-markup
4212    layout
4213    (internal-add-text-replacements
4214     props
4215     replacements)
4216    (markup arg)))
4217
4218 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
4219 ;; Markup list commands
4220 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
4221
4222 (define-public (space-lines baseline stils)
4223   (let space-stil ((stils stils)
4224                    (result (list)))
4225     (if (null? stils)
4226         (reverse! result)
4227         (let* ((stil (car stils))
4228                (dy-top (max (- (/ baseline 1.5)
4229                                (interval-bound (ly:stencil-extent stil Y) UP))
4230                             0.0))
4231                (dy-bottom (max (+ (/ baseline 3.0)
4232                                   (interval-bound (ly:stencil-extent stil Y) DOWN))
4233                                0.0))
4234                (new-stil (ly:make-stencil
4235                           (ly:stencil-expr stil)
4236                           (ly:stencil-extent stil X)
4237                           (cons (- (interval-bound (ly:stencil-extent stil Y) DOWN)
4238                                    dy-bottom)
4239                                 (+ (interval-bound (ly:stencil-extent stil Y) UP)
4240                                    dy-top)))))
4241           (space-stil (cdr stils) (cons new-stil result))))))
4242
4243 (define-markup-list-command (justified-lines layout props args)
4244   (markup-list?)
4245   #:properties ((baseline-skip)
4246                 wordwrap-internal-markup-list)
4247   "
4248 @cindex justifying lines of text
4249
4250 Like @code{\\justify}, but return a list of lines instead of a single markup.
4251 Use @code{\\override-lines #'(line-width . @var{X})} to set the line width;
4252 @var{X}@tie{}is the number of staff spaces."
4253   (space-lines baseline-skip
4254                (interpret-markup-list layout props
4255                                       (make-wordwrap-internal-markup-list #t args))))
4256
4257 (define-markup-list-command (wordwrap-lines layout props args)
4258   (markup-list?)
4259   #:properties ((baseline-skip)
4260                 wordwrap-internal-markup-list)
4261   "Like @code{\\wordwrap}, but return a list of lines instead of a single markup.
4262 Use @code{\\override-lines #'(line-width . @var{X})} to set the line width,
4263 where @var{X} is the number of staff spaces."
4264   (space-lines baseline-skip
4265                (interpret-markup-list layout props
4266                                       (make-wordwrap-internal-markup-list #f args))))
4267
4268 (define-markup-list-command (column-lines layout props args)
4269   (markup-list?)
4270   #:properties ((baseline-skip))
4271   "Like @code{\\column}, but return a list of lines instead of a single markup.
4272 @code{baseline-skip} determines the space between each markup in @var{args}."
4273   (space-lines baseline-skip
4274                (interpret-markup-list layout props args)))
4275
4276 (define-markup-list-command (override-lines layout props new-prop args)
4277   (pair? markup-list?)
4278   "Like @code{\\override}, for markup lists."
4279   (interpret-markup-list layout (cons (list new-prop) props) args))
4280
4281 (define-markup-list-command (map-markup-commands layout props compose args)
4282   (procedure? markup-list?)
4283   "This applies the function @var{compose} to every markup in
4284 @var{args} (including elements of markup list command calls) in order
4285 to produce a new markup list.  Since the return value from a markup
4286 list command call is not a markup list but rather a list of stencils,
4287 this requires passing those stencils off as the results of individual
4288 markup calls.  That way, the results should work out as long as no
4289 markups rely on side effects."
4290   (let ((key (make-symbol "key")))
4291     (catch
4292      key
4293      (lambda ()
4294        ;; if `compose' does not actually interpret its markup
4295        ;; argument, we still need to return a list of stencils,
4296        ;; created from the single returned stencil
4297        (list
4298         (interpret-markup layout props
4299                           (compose
4300                            (make-on-the-fly-markup
4301                             (lambda (layout props m)
4302                               ;; here all effects of `compose' on the
4303                               ;; properties should be visible, so we
4304                               ;; call interpret-markup-list at this
4305                               ;; point of time and harvest its
4306                               ;; stencils
4307                               (throw key
4308                                      (interpret-markup-list
4309                                       layout props args)))
4310                             (make-null-markup))))))
4311      (lambda (key stencils)
4312        (map
4313         (lambda (sten)
4314           (interpret-markup layout props
4315                             (compose (make-stencil-markup sten))))
4316         stencils)))))