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