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