]> git.donarmstrong.com Git - lilypond.git/blob - scm/define-markup-commands.scm
Run grand replace for 2015.
[lilypond.git] / scm / define-markup-commands.scm
1 ;;;; This file is part of LilyPond, the GNU music typesetter.
2 ;;;;
3 ;;;; Copyright (C) 2000--2015  Han-Wen Nienhuys <hanwen@xs4all.nl>
4 ;;;;                  Jan Nieuwenhuizen <janneke@gnu.org>
5 ;;;;
6 ;;;; LilyPond is free software: you can redistribute it and/or modify
7 ;;;; it under the terms of the GNU General Public License as published by
8 ;;;; the Free Software Foundation, either version 3 of the License, or
9 ;;;; (at your option) any later version.
10 ;;;;
11 ;;;; LilyPond is distributed in the hope that it will be useful,
12 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 ;;;; GNU General Public License for more details.
15 ;;;;
16 ;;;; You should have received a copy of the GNU General Public License
17 ;;;; along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
18
19 ;;;
20 ;;; Markup commands and markup-list commands definitions.
21 ;;;
22 ;;; Markup commands which are part of LilyPond, are defined
23 ;;; in the (lily) module, which is the current module in this file,
24 ;;; using the `define-markup-command' macro.
25 ;;;
26 ;;; Usage:
27 ;;;
28 ;;; (define-markup-command (command-name layout props args...)
29 ;;;   args-signature
30 ;;;   [ #:category category ]
31 ;;;   [ #:properties property-bindings ]
32 ;;;   documentation-string
33 ;;;   ..body..)
34 ;;;
35 ;;; with:
36 ;;;   command-name
37 ;;;     the name of the markup command
38 ;;;
39 ;;;   layout and props
40 ;;;     arguments that are automatically passed to the command when it
41 ;;;     is interpreted.
42 ;;;     `layout' is an output def, which properties can be accessed
43 ;;;     using `ly:output-def-lookup'.
44 ;;;     `props' is a list of property settings which can be accessed
45 ;;;     using `chain-assoc-get' (more on that below)
46 ;;;
47 ;;;   args...
48 ;;;     the command arguments.
49 ;;;     There is no limitation on the order of command arguments.
50 ;;;     However, markup functions taking a markup as their last
51 ;;;     argument are somewhat special as you can apply them to a
52 ;;;     markup list, and the result is a markup list where the
53 ;;;     markup function (with the specified leading arguments) has
54 ;;;     been applied to every element of the original markup list.
55 ;;;
56 ;;;     Since replicating the leading arguments for applying a
57 ;;;     markup function to a markup list is cheap mostly for
58 ;;;     Scheme arguments, you avoid performance pitfalls by just
59 ;;;     using Scheme arguments for the leading arguments of markup
60 ;;;     functions that take a markup as their last argument.
61 ;;;
62 ;;;   args-signature
63 ;;;     the arguments signature, i.e., a list of type predicates which
64 ;;;     are used to type check the arguments, and also to define the general
65 ;;;     argument types (markup, markup-list, scheme) that the command is
66 ;;;     expecting.
67 ;;;     For instance, if a command expects a number, then a markup, the
68 ;;;     signature would be: (number? markup?)
69 ;;;
70 ;;;   category
71 ;;;     for documentation purpose, builtin markup commands are grouped by
72 ;;;     category.  This can be any symbol.  When documentation is generated,
73 ;;;     the symbol is converted to a capitalized string, where hyphens are
74 ;;;     replaced by spaces.
75 ;;;
76 ;;;   property-bindings
77 ;;;     this is used both for documentation generation, and to ease
78 ;;;     programming the command itself.  It is list of
79 ;;;        (property-name default-value)
80 ;;;     or (property-name)
81 ;;;     elements.  Each property is looked-up in the `props' argument, and
82 ;;;     the symbol naming the property is bound to its value.
83 ;;;     When the property is not found in `props', then the symbol is bound
84 ;;;     to the given default value.  When no default value is given, #f is
85 ;;;     used instead.
86 ;;;     Thus, using the following property bindings:
87 ;;;       ((thickness 0.1)
88 ;;;        (font-size 0))
89 ;;;     is equivalent to writing:
90 ;;;       (let ((thickness (chain-assoc-get 'thickness props 0.1))
91 ;;;             (font-size (chain-assoc-get 'font-size props 0)))
92 ;;;         ..body..)
93 ;;;     When a command `B' internally calls an other command `A', it may
94 ;;;     desirable to see in `B' documentation all the properties and
95 ;;;     default values used by `A'.  In that case, add `A-markup' to the
96 ;;;     property-bindings of B.  (This is used when generating
97 ;;;     documentation, but won't create bindings.)
98 ;;;
99 ;;;   documentation-string
100 ;;;     the command documentation string (used to generate manuals)
101 ;;;
102 ;;;   body
103 ;;;     the command body.  The function is supposed to return a stencil.
104 ;;;
105 ;;; Each markup command definition shall have a documentation string
106 ;;; with description, syntax and example.
107
108 (use-modules (ice-9 regex))
109
110 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
111 ;; utility functions
112 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
113
114 (define-public empty-stencil (ly:make-stencil '()
115                                               empty-interval empty-interval))
116 (define-public point-stencil (ly:make-stencil "" '(0 . 0) '(0 . 0)))
117
118 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
119 ;; line has to come early since it is often used implicitly from the
120 ;; markup macro since \markup { a b c } -> \markup \line { a b c }
121 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
122
123 (define-markup-command (line layout props args)
124   (markup-list?)
125   #:category align
126   #:properties ((word-space)
127                 (text-direction RIGHT))
128   "Put @var{args} in a horizontal line.  The property @code{word-space}
129 determines the space between markups in @var{args}.
130
131 @lilypond[verbatim,quote]
132 \\markup {
133   \\line {
134     one two three
135   }
136 }
137 @end lilypond"
138   (let ((stencils (interpret-markup-list layout props args)))
139     (if (= text-direction LEFT)
140         (set! stencils (reverse stencils)))
141     (stack-stencil-line word-space stencils)))
142
143 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
144 ;; geometric shapes
145 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
146
147 (define-markup-command (draw-line layout props dest)
148   (number-pair?)
149   #:category graphic
150   #:properties ((thickness 1))
151   "
152 @cindex drawing lines within text
153
154 A simple line.
155 @lilypond[verbatim,quote]
156 \\markup {
157   \\draw-line #'(4 . 4)
158   \\override #'(thickness . 5)
159   \\draw-line #'(-3 . 0)
160 }
161 @end lilypond"
162   (let ((th (* (ly:output-def-lookup layout 'line-thickness)
163                thickness))
164         (x (car dest))
165         (y (cdr dest)))
166     (make-line-stencil th 0 0 x y)))
167
168 (define-markup-command (draw-dashed-line layout props dest)
169   (number-pair?)
170   #:category graphic
171   #:properties ((thickness 1)
172                 (on 1)
173                 (off 1)
174                 (phase 0)
175                 (full-length #t))
176   "
177 @cindex drawing dashed lines within text
178
179 A dashed line.
180
181 If @code{full-length} is set to #t (default) the dashed-line extends to the
182 whole length given by @var{dest}, without white space at beginning or end.
183 @code{off} will then be altered to fit.
184 To insist on the given (or default) values of @code{on}, @code{off} use
185 @code{\\override #'(full-length . #f)}
186 Manual settings for @code{on},@code{off} and @code{phase} are possible.
187 @lilypond[verbatim,quote]
188 \\markup {
189   \\draw-dashed-line #'(5.1 . 2.3)
190   \\override #'(on . 0.3)
191   \\override #'(off . 0.5)
192   \\draw-dashed-line #'(5.1 . 2.3)
193 }
194 @end lilypond"
195   (let* ((line-thickness (ly:output-def-lookup layout 'line-thickness))
196          ;; Calculate the thickness to be used.
197          (th (* line-thickness thickness))
198          (half-thick (/ th 2))
199          ;; Get the extensions in x- and y-direction.
200          (x (car dest))
201          (y (cdr dest))
202          ;; Calculate the length of the dashed line.
203          (line-length (sqrt (+ (expt x 2) (expt y 2)))))
204
205     (if (and full-length (not (= (+ on off) 0)))
206         (begin
207           ;; Add double-thickness to avoid overlapping.
208           (set! off (+ (* 2 th) off))
209           (let* (;; Make a guess how often the off/on-pair should be printed
210                  ;; after the initial `onĀ“.
211                  ;; Assume a minimum of 1 to avoid division by zero.
212                  (guess (max 1 (round (/ (- line-length on) (+ off on)))))
213                  ;; Not sure about the value or why corr is necessary at all,
214                  ;; but it seems to be necessary.
215                  (corr (if (= on 0)
216                            (/ line-thickness 10)
217                            0))
218                  ;; Calculate a new value for off to fit the
219                  ;; line-length.
220                  (new-off (/ (- line-length corr (* (1+ guess) on)) guess))
221                  )
222             (cond
223
224              ;; Settings for (= on 0). Resulting in a dotted line.
225
226              ;; If line-length isn't shorter than `thĀ“, change the given
227              ;; value for `offĀ“ to fit the line-length.
228              ((and (= on 0) (< th line-length))
229               (set! off new-off))
230
231              ;; If the line-length is shorter than `thĀ“, it makes no
232              ;; sense to adjust `offĀ“. The rounded edges of the lines
233              ;; would prevent any nice output.
234              ;; Do nothing.
235              ;; This will result in a single dot for very short lines.
236              ((and (= on 0) (>= th line-length))
237               #f)
238
239              ;; Settings for (not (= on 0)). Resulting in a dashed line.
240
241              ;; If line-length isn't shorter than one go of on-off-on,
242              ;; change the given value for `offĀ“ to fit the line-length.
243              ((< (+ (* 2 on) off) line-length)
244               (set! off new-off))
245              ;; If the line-length is too short, but greater than
246              ;; (* 4 th) set on/off to (/ line-length 3)
247              ((< (* 4 th) line-length)
248               (set! on (/ line-length 3))
249               (set! off (/ line-length 3)))
250              ;; If the line-length is shorter than (* 4 th), it makes
251              ;; no sense trying to adjust on/off. The rounded edges of
252              ;; the lines would prevent any nice output.
253              ;; Simply set `onĀ“ to line-length.
254              (else
255               (set! on line-length))))))
256
257     ;; If `onĀ“ or `offĀ“ is negative, or the sum of `on' and `off' equals zero a
258     ;; ghostscript-error occurs while calling
259     ;; (ly:make-stencil (list 'dashed-line th on off x y phase) x-ext y-ext)
260     ;; Better be paranoid.
261     (if (or (= (+ on off) 0)
262             (negative? on)
263             (negative? off))
264         (begin
265           (ly:warning "Can't print a line - setting on/off to default")
266           (set! on 1)
267           (set! off 1)))
268
269     ;; To give the lines produced by \draw-line and \draw-dashed-line the same
270     ;; length, half-thick has to be added to the stencil-extensions.
271     (ly:make-stencil
272      (list 'dashed-line th on off x y phase)
273      (interval-widen (ordered-cons 0 x) half-thick)
274      (interval-widen (ordered-cons 0 y) half-thick))))
275
276 (define-markup-command (draw-dotted-line layout props dest)
277   (number-pair?)
278   #:category graphic
279   #:properties ((thickness 1)
280                 (off 1)
281                 (phase 0))
282   "
283 @cindex drawing dotted lines within text
284
285 A dotted line.
286
287 The dotted-line always extends to the whole length given by @var{dest}, without
288 white space at beginning or end.
289 Manual settings for @code{off} are possible to get larger or smaller space
290 between the dots.
291 The given (or default) value of @code{off} will be altered to fit the
292 line-length.
293 @lilypond[verbatim,quote]
294 \\markup {
295   \\draw-dotted-line #'(5.1 . 2.3)
296   \\override #'(thickness . 2)
297   \\override #'(off . 0.2)
298   \\draw-dotted-line #'(5.1 . 2.3)
299 }
300 @end lilypond"
301
302   (let ((new-props (prepend-alist-chain 'on 0
303                                         (prepend-alist-chain 'full-length #t props))))
304
305     (interpret-markup layout
306                       new-props
307                       (markup #:draw-dashed-line dest))))
308
309 (define-markup-command (draw-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-markup-command (first-visible layout props args)
1161   (markup-list?)
1162   #:category other
1163   "Use the first markup in @var{args} that yields a non-empty stencil
1164 and ignore the rest.
1165
1166 @lilypond[verbatim,quote]
1167 \\markup {
1168   \\first-visible {
1169     \\fromproperty #'header:composer
1170     \\italic Unknown
1171   }
1172 }
1173 @end lilypond"
1174   (define (false-if-empty stencil)
1175     (if (ly:stencil-empty? stencil) #f stencil))
1176   (or
1177    (any
1178     (lambda (m)
1179       (if (markup? m)
1180           (false-if-empty (interpret-markup layout props m))
1181           (any false-if-empty (interpret-markup-list layout props (list m)))))
1182     args)
1183    empty-stencil))
1184
1185 (define-public empty-markup
1186   (make-simple-markup ""))
1187
1188 ;; helper for justifying lines.
1189 (define (get-fill-space
1190           word-count line-width word-space text-widths constant-space?)
1191   "Calculate the necessary paddings between adjacent texts in a
1192 single justified line.  The lengths of all texts are stored in
1193 @var{text-widths}.
1194 When @var{constant-space?} is @code{#t}, the formula for the padding
1195 between texts is:
1196 padding = (line-width - total-text-width)/(word-count - 1)
1197 When @var{constant-space?} is @code{#f}, the formula for the
1198 padding between interior texts a and b is:
1199 padding = line-width/(word-count - 1) - (length(a) + length(b))/2
1200 In this case, the first and last padding have to be calculated
1201 specially using the whole length of the first or last text.
1202 All paddings are checked to be at least word-space, to ensure that
1203 no texts collide.
1204 Return a list of paddings."
1205   (cond
1206     ((null? text-widths) '())
1207     (constant-space?
1208      (make-list
1209        (1- word-count)
1210        ;; Ensure that space between words cannot be
1211        ;; less than word-space.
1212        (max
1213          word-space
1214          (/ (- line-width (apply + text-widths))
1215             (1- word-count)))))
1216
1217     ;; special case first padding
1218     ((= (length text-widths) word-count)
1219      (cons
1220        (- (- (/ line-width (1- word-count)) (car text-widths))
1221           (/ (cadr text-widths) 2))
1222        (get-fill-space
1223          word-count line-width word-space (cdr text-widths)
1224                                           constant-space?)))
1225     ;; special case last padding
1226     ((= (length text-widths) 2)
1227      (list (- (/ line-width (1- word-count))
1228               (+ (/ (car text-widths) 2) (cadr text-widths)))
1229            0))
1230     (else
1231       (let ((default-padding
1232               (- (/ line-width (1- word-count))
1233                  (/ (+ (car text-widths) (cadr text-widths)) 2))))
1234         (cons
1235           (if (> word-space default-padding)
1236               word-space
1237               default-padding)
1238           (get-fill-space
1239             word-count line-width word-space (cdr text-widths)
1240                                              constant-space?))))))
1241
1242 (define (justify-line-helper
1243           layout props args text-direction word-space line-width constant-space?)
1244   "Return a stencil which spreads @var{args} along a line of width
1245 @var{line-width}.  If @var{constant-space?} is set to @code{#t}, the
1246 space between words is constant.  If @code{#f}, the distance between
1247 words varies according to their relative lengths."
1248   (let* ((orig-stencils (interpret-markup-list layout props args))
1249          (stencils
1250            (map (lambda (stc)
1251                   (if (ly:stencil-empty? stc X)
1252                       (ly:make-stencil (ly:stencil-expr stc)
1253                                        '(0 . 0) (ly:stencil-extent stc Y))
1254                       stc))
1255                 orig-stencils))
1256          (text-widths
1257            (map (lambda (stc)
1258                   (interval-length (ly:stencil-extent stc X)))
1259                 stencils))
1260          (text-width (apply + text-widths))
1261          (word-count (length stencils))
1262          (line-width (or line-width (ly:output-def-lookup layout 'line-width)))
1263          (fill-space
1264            (cond
1265              ((= word-count 1)
1266               (list
1267                 (/ (- line-width text-width) 2)
1268                 (/ (- line-width text-width) 2)))
1269              ((= word-count 2)
1270               (list
1271                 (- line-width text-width)))
1272              (else
1273                (get-fill-space
1274                  word-count line-width word-space text-widths
1275                                                   constant-space?))))
1276          (line-contents (if (= word-count 1)
1277                             (list
1278                               point-stencil
1279                               (car stencils)
1280                               point-stencil)
1281                             stencils)))
1282
1283     (if (null? (remove ly:stencil-empty? orig-stencils))
1284         empty-stencil
1285         (begin
1286           (if (= text-direction LEFT)
1287               (set! line-contents (reverse line-contents)))
1288           (set! line-contents
1289                 (stack-stencils-padding-list
1290                   X RIGHT fill-space line-contents))
1291           (if (> word-count 1)
1292               ;; shift s.t. stencils align on the left edge, even if
1293               ;; first stencil had negative X-extent (e.g. center-column)
1294               ;; (if word-count = 1, X-extents are already normalized in
1295               ;; the definition of line-contents)
1296               (set! line-contents
1297                     (ly:stencil-translate-axis
1298                       line-contents
1299                       (- (car (ly:stencil-extent (car stencils) X)))
1300                       X)))
1301           line-contents))))
1302
1303 (define-markup-command (fill-line layout props args)
1304   (markup-list?)
1305   #:category align
1306   #:properties ((text-direction RIGHT)
1307                 (word-space 0.6)
1308                 (line-width #f))
1309   "Put @var{markups} in a horizontal line of width @var{line-width}.
1310 The markups are spaced or flushed to fill the entire line.
1311 If there are no arguments, return an empty stencil.
1312
1313 @lilypond[verbatim,quote]
1314 \\markup {
1315   \\column {
1316     \\fill-line {
1317       Words evenly spaced across the page
1318     }
1319     \\null
1320     \\fill-line {
1321       \\line { Text markups }
1322       \\line {
1323         \\italic { evenly spaced }
1324       }
1325       \\line { across the page }
1326     }
1327   }
1328 }
1329 @end lilypond"
1330   (justify-line-helper
1331     layout props args text-direction word-space line-width #f))
1332
1333 (define-markup-command (justify-line layout props args)
1334   (markup-list?)
1335   #:category align
1336   #:properties ((text-direction RIGHT)
1337                 (word-space 0.6)
1338                 (line-width #f))
1339   "Put @var{markups} in a horizontal line of width @var{line-width}.
1340 The markups are spread to fill the entire line and separated by equal
1341 space.  If there are no arguments, return an empty stencil.
1342
1343 @lilypond[verbatim,quote]
1344 \\markup {
1345   \\justify-line {
1346     Space between neighboring words is constant
1347   }
1348 }
1349 @end lilypond"
1350   (justify-line-helper
1351     layout props args text-direction word-space line-width #t))
1352
1353 (define-markup-command (concat layout props args)
1354   (markup-list?)
1355   #:category align
1356   "
1357 @cindex concatenating text
1358 @cindex ligatures in text
1359
1360 Concatenate @var{args} in a horizontal line, without spaces in between.
1361 Strings and simple markups are concatenated on the input level, allowing
1362 ligatures.  For example, @code{\\concat @{ \"f\" \\simple #\"i\" @}} is
1363 equivalent to @code{\"fi\"}.
1364
1365 @lilypond[verbatim,quote]
1366 \\markup {
1367   \\concat {
1368     one
1369     two
1370     three
1371   }
1372 }
1373 @end lilypond"
1374   (define (concat-string-args arg-list)
1375     (fold-right (lambda (arg result-list)
1376                   (let ((result (if (pair? result-list)
1377                                     (car result-list)
1378                                     '())))
1379                     (if (and (pair? arg) (eqv? (car arg) simple-markup))
1380                         (set! arg (cadr arg)))
1381                     (if (and (string? result) (string? arg))
1382                         (cons (string-append arg result) (cdr result-list))
1383                         (cons arg result-list))))
1384                 '()
1385                 arg-list))
1386
1387   (interpret-markup layout
1388                     (prepend-alist-chain 'word-space 0 props)
1389                     (make-line-markup
1390                      (make-override-lines-markup-list
1391                       (cons 'word-space
1392                             (chain-assoc-get 'word-space props))
1393                       (if (markup-command-list? args)
1394                           args
1395                           (concat-string-args args))))))
1396
1397 (define (wordwrap-stencils stencils
1398                            justify base-space line-width text-dir)
1399   "Perform simple wordwrap, return stencil of each line."
1400   (define space (if justify
1401                     ;; justify only stretches lines.
1402                     (* 0.7 base-space)
1403                     base-space))
1404   (define (stencil-len s)
1405     (interval-end (ly:stencil-extent s X)))
1406   (define (maybe-shift line)
1407     (if (= text-dir LEFT)
1408         (ly:stencil-translate-axis
1409          line
1410          (- line-width (stencil-len line))
1411          X)
1412         line))
1413   (if (null? stencils)
1414       '()
1415       (let loop ((lines '())
1416                  (todo stencils))
1417         (let word-loop
1418             ((line (first todo))
1419              (todo (cdr todo))
1420              (word-list (list (first todo))))
1421           (cond
1422            ((pair? todo)
1423             (let ((new (if (= text-dir LEFT)
1424                            (ly:stencil-stack (car todo) X RIGHT line space)
1425                            (ly:stencil-stack line X RIGHT (car todo) space))))
1426               (cond
1427                ((<= (stencil-len new) line-width)
1428                 (word-loop new (cdr todo)
1429                            (cons (car todo) word-list)))
1430                (justify
1431                 (let* ((word-list
1432                         ;; This depends on stencil stacking being
1433                         ;; associative so that stacking
1434                         ;; left-to-right and right-to-left leads to
1435                         ;; the same result
1436                         (if (= text-dir LEFT)
1437                             word-list
1438                             (reverse! word-list)))
1439                        (len (stencil-len line))
1440                        (stretch (- line-width len))
1441                        (spaces
1442                         (- (stencil-len
1443                             (stack-stencils X RIGHT (1+ space) word-list))
1444                            len)))
1445                   (if (zero? spaces)
1446                       ;; Uh oh, nothing to fill.
1447                       (loop (cons (maybe-shift line) lines) todo)
1448                       (loop (cons
1449                              (stack-stencils X RIGHT
1450                                              (+ space (/ stretch spaces))
1451                                              word-list)
1452                              lines)
1453                             todo))))
1454                (else ;; not justify
1455                 (loop (cons (maybe-shift line) lines) todo)))))
1456            ;; todo is null
1457            (justify
1458             ;; Now we have the last line assembled with space
1459             ;; which is compressed.  We want to use the
1460             ;; uncompressed version instead if it fits, and the
1461             ;; justified version if it doesn't.
1462             (let* ((word-list
1463                     ;; This depends on stencil stacking being
1464                     ;; associative so that stacking
1465                     ;; left-to-right and right-to-left leads to
1466                     ;; the same result
1467                     (if (= text-dir LEFT)
1468                         word-list
1469                         (reverse! word-list)))
1470                    (big-line (stack-stencils X RIGHT base-space word-list))
1471                    (big-len (stencil-len big-line))
1472                    (len (stencil-len line)))
1473               (reverse! lines
1474                         (list
1475                          (if (> big-len line-width)
1476                              (stack-stencils X RIGHT
1477                                              (/
1478                                               (+
1479                                                (* (- big-len line-width)
1480                                                   space)
1481                                                (* (- line-width len)
1482                                                   base-space))
1483                                               (- big-len len))
1484                                              word-list)
1485                              (maybe-shift big-line))))))
1486            (else ;; not justify
1487             (reverse! lines (list (maybe-shift line)))))))))
1488
1489
1490 (define-markup-list-command (wordwrap-internal layout props justify args)
1491   (boolean? markup-list?)
1492   #:properties ((line-width #f)
1493                 (word-space)
1494                 (text-direction RIGHT))
1495   "Internal markup list command used to define @code{\\justify} and @code{\\wordwrap}."
1496   (wordwrap-stencils (interpret-markup-list layout props args)
1497                      justify
1498                      word-space
1499                      (or line-width
1500                          (ly:output-def-lookup layout 'line-width))
1501                      text-direction))
1502
1503 (define-markup-command (justify layout props args)
1504   (markup-list?)
1505   #:category align
1506   #:properties ((baseline-skip)
1507                 wordwrap-internal-markup-list)
1508   "
1509 @cindex justifying text
1510
1511 Like @code{\\wordwrap}, but with lines stretched to justify the margins.
1512 Use @code{\\override #'(line-width . @var{X})} to set the line width;
1513 @var{X}@tie{}is the number of staff spaces.
1514
1515 @lilypond[verbatim,quote]
1516 \\markup {
1517   \\justify {
1518     Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed
1519     do eiusmod tempor incididunt ut labore et dolore magna aliqua.
1520     Ut enim ad minim veniam, quis nostrud exercitation ullamco
1521     laboris nisi ut aliquip ex ea commodo consequat.
1522   }
1523 }
1524 @end lilypond"
1525   (stack-lines DOWN 0.0 baseline-skip
1526                (wordwrap-internal-markup-list layout props #t args)))
1527
1528 (define-markup-command (wordwrap layout props args)
1529   (markup-list?)
1530   #:category align
1531   #:properties ((baseline-skip)
1532                 wordwrap-internal-markup-list)
1533   "Simple wordwrap.  Use @code{\\override #'(line-width . @var{X})} to set
1534 the line width, where @var{X} is the number of staff spaces.
1535
1536 @lilypond[verbatim,quote]
1537 \\markup {
1538   \\wordwrap {
1539     Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed
1540     do eiusmod tempor incididunt ut labore et dolore magna aliqua.
1541     Ut enim ad minim veniam, quis nostrud exercitation ullamco
1542     laboris nisi ut aliquip ex ea commodo consequat.
1543   }
1544 }
1545 @end lilypond"
1546   (stack-lines DOWN 0.0 baseline-skip
1547                (wordwrap-internal-markup-list layout props #f args)))
1548
1549 (define-markup-list-command (wordwrap-string-internal layout props justify arg)
1550   (boolean? string?)
1551   #:properties ((line-width)
1552                 (word-space)
1553                 (text-direction RIGHT))
1554   "Internal markup list command used to define @code{\\justify-string} and
1555 @code{\\wordwrap-string}."
1556   (let* ((para-strings (regexp-split
1557                         (string-regexp-substitute
1558                          "\r" "\n"
1559                          (string-regexp-substitute "\r\n" "\n" arg))
1560                         "\n[ \t\n]*\n[ \t\n]*"))
1561          (list-para-words (map (lambda (str)
1562                                  (regexp-split str "[ \t\n]+"))
1563                                para-strings))
1564          (para-lines (map (lambda (words)
1565                             (let* ((stencils
1566                                     (map (lambda (x)
1567                                            (interpret-markup layout props x))
1568                                          words)))
1569                               (wordwrap-stencils stencils
1570                                                  justify word-space
1571                                                  line-width text-direction)))
1572                           list-para-words)))
1573     (concatenate para-lines)))
1574
1575 (define-markup-command (wordwrap-string layout props arg)
1576   (string?)
1577   #:category align
1578   #:properties ((baseline-skip)
1579                 wordwrap-string-internal-markup-list)
1580   "Wordwrap a string.  Paragraphs may be separated with double newlines.
1581
1582 @lilypond[verbatim,quote]
1583 \\markup {
1584   \\override #'(line-width . 40)
1585   \\wordwrap-string #\"Lorem ipsum dolor sit amet, consectetur
1586       adipisicing elit, sed do eiusmod tempor incididunt ut labore
1587       et dolore magna aliqua.
1588
1589
1590       Ut enim ad minim veniam, quis nostrud exercitation ullamco
1591       laboris nisi ut aliquip ex ea commodo consequat.
1592
1593
1594       Excepteur sint occaecat cupidatat non proident, sunt in culpa
1595       qui officia deserunt mollit anim id est laborum\"
1596 }
1597 @end lilypond"
1598   (stack-lines DOWN 0.0 baseline-skip
1599                (wordwrap-string-internal-markup-list layout props #f arg)))
1600
1601 (define-markup-command (justify-string layout props arg)
1602   (string?)
1603   #:category align
1604   #:properties ((baseline-skip)
1605                 wordwrap-string-internal-markup-list)
1606   "Justify a string.  Paragraphs may be separated with double newlines
1607
1608 @lilypond[verbatim,quote]
1609 \\markup {
1610   \\override #'(line-width . 40)
1611   \\justify-string #\"Lorem ipsum dolor sit amet, consectetur
1612       adipisicing elit, sed do eiusmod tempor incididunt ut labore
1613       et dolore magna aliqua.
1614
1615
1616       Ut enim ad minim veniam, quis nostrud exercitation ullamco
1617       laboris nisi ut aliquip ex ea commodo consequat.
1618
1619
1620       Excepteur sint occaecat cupidatat non proident, sunt in culpa
1621       qui officia deserunt mollit anim id est laborum\"
1622 }
1623 @end lilypond"
1624   (stack-lines DOWN 0.0 baseline-skip
1625                (wordwrap-string-internal-markup-list layout props #t arg)))
1626
1627 (define-markup-command (wordwrap-field layout props symbol)
1628   (symbol?)
1629   #:category align
1630   "Wordwrap the data which has been assigned to @var{symbol}.
1631
1632 @lilypond[verbatim,quote]
1633 \\header {
1634   title = \"My title\"
1635   myText = \"Lorem ipsum dolor sit amet, consectetur adipisicing
1636     elit, sed do eiusmod tempor incididunt ut labore et dolore
1637     magna aliqua.  Ut enim ad minim veniam, quis nostrud
1638     exercitation ullamco laboris nisi ut aliquip ex ea commodo
1639     consequat.\"
1640 }
1641
1642 \\paper {
1643   bookTitleMarkup = \\markup {
1644     \\column {
1645       \\fill-line { \\fromproperty #'header:title }
1646       \\null
1647       \\wordwrap-field #'header:myText
1648     }
1649   }
1650 }
1651
1652 \\markup {
1653   \\null
1654 }
1655 @end lilypond"
1656   (let* ((m (chain-assoc-get symbol props)))
1657     (if (string? m)
1658         (wordwrap-string-markup layout props m)
1659         empty-stencil)))
1660
1661 (define-markup-command (justify-field layout props symbol)
1662   (symbol?)
1663   #:category align
1664   "Justify the data which has been assigned to @var{symbol}.
1665
1666 @lilypond[verbatim,quote]
1667 \\header {
1668   title = \"My title\"
1669   myText = \"Lorem ipsum dolor sit amet, consectetur adipisicing
1670     elit, sed do eiusmod tempor incididunt ut labore et dolore magna
1671     aliqua.  Ut enim ad minim veniam, quis nostrud exercitation ullamco
1672     laboris nisi ut aliquip ex ea commodo consequat.\"
1673 }
1674
1675 \\paper {
1676   bookTitleMarkup = \\markup {
1677     \\column {
1678       \\fill-line { \\fromproperty #'header:title }
1679       \\null
1680       \\justify-field #'header:myText
1681     }
1682   }
1683 }
1684
1685 \\markup {
1686   \\null
1687 }
1688 @end lilypond"
1689   (let* ((m (chain-assoc-get symbol props)))
1690     (if (string? m)
1691         (justify-string-markup layout props m)
1692         empty-stencil)))
1693
1694 (define-markup-command (combine layout props arg1 arg2)
1695   (markup? markup?)
1696   #:category align
1697   "
1698 @cindex merging text
1699
1700 Print two markups on top of each other.
1701
1702 Note: @code{\\combine} cannot take a list of markups enclosed in
1703 curly braces as an argument; the follow example will not compile:
1704
1705 @example
1706 \\combine @{ a list @}
1707 @end example
1708
1709 @lilypond[verbatim,quote]
1710 \\markup {
1711   \\fontsize #5
1712   \\override #'(thickness . 2)
1713   \\combine
1714     \\draw-line #'(0 . 4)
1715     \\arrow-head #Y #DOWN ##f
1716 }
1717 @end lilypond"
1718   (let* ((s1 (interpret-markup layout props arg1))
1719          (s2 (interpret-markup layout props arg2)))
1720     (ly:stencil-add s1 s2)))
1721
1722 ;;
1723 ;; TODO: should extract baseline-skip from each argument somehow..
1724 ;;
1725 (define-markup-command (column layout props args)
1726   (markup-list?)
1727   #:category align
1728   #:properties ((baseline-skip))
1729   "
1730 @cindex stacking text in a column
1731
1732 Stack the markups in @var{args} vertically.  The property
1733 @code{baseline-skip} determines the space between markups
1734 in @var{args}.
1735
1736 @lilypond[verbatim,quote]
1737 \\markup {
1738   \\column {
1739     one
1740     two
1741     three
1742   }
1743 }
1744 @end lilypond"
1745   (let ((arg-stencils (interpret-markup-list layout props args)))
1746     (stack-lines -1 0.0 baseline-skip arg-stencils)))
1747
1748 (define-markup-command (dir-column layout props args)
1749   (markup-list?)
1750   #:category align
1751   #:properties ((direction)
1752                 (baseline-skip))
1753   "
1754 @cindex changing direction of text columns
1755
1756 Make a column of @var{args}, going up or down, depending on the
1757 setting of the @code{direction} layout property.
1758
1759 @lilypond[verbatim,quote]
1760 \\markup {
1761   \\override #`(direction . ,UP) {
1762     \\dir-column {
1763       going up
1764     }
1765   }
1766   \\hspace #1
1767   \\dir-column {
1768     going down
1769   }
1770   \\hspace #1
1771   \\override #'(direction . 1) {
1772     \\dir-column {
1773       going up
1774     }
1775   }
1776 }
1777 @end lilypond"
1778   (stack-lines (if (number? direction) direction -1)
1779                0.0
1780                baseline-skip
1781                (interpret-markup-list layout props args)))
1782
1783 (define (general-column align-dir baseline mols)
1784   "Stack @var{mols} vertically, aligned to  @var{align-dir} horizontally."
1785
1786   (let* ((aligned-mols (map (lambda (x) (ly:stencil-aligned-to x X align-dir)) mols))
1787          (stacked-stencil (stack-lines -1 0.0 baseline aligned-mols))
1788          (stacked-extent (ly:stencil-extent stacked-stencil X)))
1789     (ly:stencil-translate-axis stacked-stencil (- (car stacked-extent)) X )))
1790
1791 (define-markup-command (center-column layout props args)
1792   (markup-list?)
1793   #:category align
1794   #:properties ((baseline-skip))
1795   "
1796 @cindex centering a column of text
1797
1798 Put @code{args} in a centered column.
1799
1800 @lilypond[verbatim,quote]
1801 \\markup {
1802   \\center-column {
1803     one
1804     two
1805     three
1806   }
1807 }
1808 @end lilypond"
1809   (general-column CENTER baseline-skip (interpret-markup-list layout props args)))
1810
1811 (define-markup-command (left-column layout props args)
1812   (markup-list?)
1813   #:category align
1814   #:properties ((baseline-skip))
1815   "
1816 @cindex text columns, left-aligned
1817
1818 Put @code{args} in a left-aligned column.
1819
1820 @lilypond[verbatim,quote]
1821 \\markup {
1822   \\left-column {
1823     one
1824     two
1825     three
1826   }
1827 }
1828 @end lilypond"
1829   (general-column LEFT baseline-skip (interpret-markup-list layout props args)))
1830
1831 (define-markup-command (right-column layout props args)
1832   (markup-list?)
1833   #:category align
1834   #:properties ((baseline-skip))
1835   "
1836 @cindex text columns, right-aligned
1837
1838 Put @code{args} in a right-aligned column.
1839
1840 @lilypond[verbatim,quote]
1841 \\markup {
1842   \\right-column {
1843     one
1844     two
1845     three
1846   }
1847 }
1848 @end lilypond"
1849   (general-column RIGHT baseline-skip (interpret-markup-list layout props args)))
1850
1851 (define-markup-command (vcenter layout props arg)
1852   (markup?)
1853   #:category align
1854   "
1855 @cindex vertically centering text
1856
1857 Align @code{arg} to its Y@tie{}center.
1858
1859 @lilypond[verbatim,quote]
1860 \\markup {
1861   one
1862   \\vcenter
1863   two
1864   three
1865 }
1866 @end lilypond"
1867   (let* ((mol (interpret-markup layout props arg)))
1868     (ly:stencil-aligned-to mol Y CENTER)))
1869
1870 (define-markup-command (center-align layout props arg)
1871   (markup?)
1872   #:category align
1873   "
1874 @cindex horizontally centering text
1875
1876 Align @code{arg} to its X@tie{}center.
1877
1878 @lilypond[verbatim,quote]
1879 \\markup {
1880   \\column {
1881     one
1882     \\center-align
1883     two
1884     three
1885   }
1886 }
1887 @end lilypond"
1888   (let* ((mol (interpret-markup layout props arg)))
1889     (ly:stencil-aligned-to mol X CENTER)))
1890
1891 (define-markup-command (right-align layout props arg)
1892   (markup?)
1893   #:category align
1894   "
1895 @cindex right aligning text
1896
1897 Align @var{arg} on its right edge.
1898
1899 @lilypond[verbatim,quote]
1900 \\markup {
1901   \\column {
1902     one
1903     \\right-align
1904     two
1905     three
1906   }
1907 }
1908 @end lilypond"
1909   (let* ((m (interpret-markup layout props arg)))
1910     (ly:stencil-aligned-to m X RIGHT)))
1911
1912 (define-markup-command (left-align layout props arg)
1913   (markup?)
1914   #:category align
1915   "
1916 @cindex left aligning text
1917
1918 Align @var{arg} on its left edge.
1919
1920 @lilypond[verbatim,quote]
1921 \\markup {
1922   \\column {
1923     one
1924     \\left-align
1925     two
1926     three
1927   }
1928 }
1929 @end lilypond"
1930   (let* ((m (interpret-markup layout props arg)))
1931     (ly:stencil-aligned-to m X LEFT)))
1932
1933 (define-markup-command (general-align layout props axis dir arg)
1934   (integer? number? markup?)
1935   #:category align
1936   "
1937 @cindex controlling general text alignment
1938
1939 Align @var{arg} in @var{axis} direction to the @var{dir} side.
1940
1941 @lilypond[verbatim,quote]
1942 \\markup {
1943   \\column {
1944     one
1945     \\general-align #X #LEFT
1946     two
1947     three
1948     \\null
1949     one
1950     \\general-align #X #CENTER
1951     two
1952     three
1953     \\null
1954     \\line {
1955       one
1956       \\general-align #Y #UP
1957       two
1958       three
1959     }
1960     \\null
1961     \\line {
1962       one
1963       \\general-align #Y #3.2
1964       two
1965       three
1966     }
1967   }
1968 }
1969 @end lilypond"
1970   (let* ((m (interpret-markup layout props arg)))
1971     (ly:stencil-aligned-to m axis dir)))
1972
1973 (define-markup-command (halign layout props dir arg)
1974   (number? markup?)
1975   #:category align
1976   "
1977 @cindex setting horizontal text alignment
1978
1979 Set horizontal alignment.  If @var{dir} is @w{@code{-1}}, then it is
1980 left-aligned, while @code{+1} is right.  Values in between interpolate
1981 alignment accordingly.
1982
1983 @lilypond[verbatim,quote]
1984 \\markup {
1985   \\column {
1986     one
1987     \\halign #LEFT
1988     two
1989     three
1990     \\null
1991     one
1992     \\halign #CENTER
1993     two
1994     three
1995     \\null
1996     one
1997     \\halign #RIGHT
1998     two
1999     three
2000     \\null
2001     one
2002     \\halign #-5
2003     two
2004     three
2005   }
2006 }
2007 @end lilypond"
2008   (let* ((m (interpret-markup layout props arg)))
2009     (ly:stencil-aligned-to m X dir)))
2010
2011 (define-markup-command (with-dimensions layout props x y arg)
2012   (number-pair? number-pair? markup?)
2013   #:category other
2014   "
2015 @cindex setting extent of text objects
2016
2017 Set the dimensions of @var{arg} to @var{x} and@tie{}@var{y}."
2018   (let* ((expr (ly:stencil-expr (interpret-markup layout props arg))))
2019     (ly:stencil-add
2020      (make-transparent-box-stencil x y)
2021      (ly:make-stencil
2022       `(delay-stencil-evaluation ,(delay expr))
2023       x y))))
2024
2025 (define-markup-command (pad-around layout props amount arg)
2026   (number? markup?)
2027   #:category align
2028   "Add padding @var{amount} all around @var{arg}.
2029
2030 @lilypond[verbatim,quote]
2031 \\markup {
2032   \\box {
2033     default
2034   }
2035   \\hspace #2
2036   \\box {
2037     \\pad-around #0.5 {
2038       padded
2039     }
2040   }
2041 }
2042 @end lilypond"
2043   (let* ((m (interpret-markup layout props arg))
2044          (x (interval-widen (ly:stencil-extent m X) amount))
2045          (y (interval-widen (ly:stencil-extent m Y) amount)))
2046     (ly:stencil-add (make-transparent-box-stencil x y)
2047                     m)))
2048
2049 (define-markup-command (pad-x layout props amount arg)
2050   (number? markup?)
2051   #:category align
2052   "
2053 @cindex padding text horizontally
2054
2055 Add padding @var{amount} around @var{arg} in the X@tie{}direction.
2056
2057 @lilypond[verbatim,quote]
2058 \\markup {
2059   \\box {
2060     default
2061   }
2062   \\hspace #4
2063   \\box {
2064     \\pad-x #2 {
2065       padded
2066     }
2067   }
2068 }
2069 @end lilypond"
2070   (let* ((m (interpret-markup layout props arg))
2071          (x (ly:stencil-extent m X))
2072          (y (ly:stencil-extent m Y)))
2073     (ly:make-stencil (ly:stencil-expr m)
2074                      (interval-widen x amount)
2075                      y)))
2076
2077 (define-markup-command (put-adjacent layout props axis dir arg1 arg2)
2078   (integer? ly:dir? markup? markup?)
2079   #:category align
2080   "Put @var{arg2} next to @var{arg1}, without moving @var{arg1}."
2081   (let ((m1 (interpret-markup layout props arg1))
2082         (m2 (interpret-markup layout props arg2)))
2083     (ly:stencil-combine-at-edge m1 axis dir m2 0.0)))
2084
2085 (define-markup-command (transparent layout props arg)
2086   (markup?)
2087   #:category other
2088   "Make @var{arg} transparent.
2089
2090 @lilypond[verbatim,quote]
2091 \\markup {
2092   \\transparent {
2093     invisible text
2094   }
2095 }
2096 @end lilypond"
2097   (let* ((m (interpret-markup layout props arg))
2098          (x (ly:stencil-extent m X))
2099          (y (ly:stencil-extent m Y)))
2100     (ly:make-stencil (list 'transparent-stencil (ly:stencil-expr m)) x y)))
2101
2102 (define-markup-command (pad-to-box layout props x-ext y-ext arg)
2103   (number-pair? number-pair? markup?)
2104   #:category align
2105   "Make @var{arg} take at least @var{x-ext}, @var{y-ext} space.
2106
2107 @lilypond[verbatim,quote]
2108 \\markup {
2109   \\box {
2110     default
2111   }
2112   \\hspace #4
2113   \\box {
2114     \\pad-to-box #'(0 . 10) #'(0 . 3) {
2115       padded
2116     }
2117   }
2118 }
2119 @end lilypond"
2120   (ly:stencil-add (make-transparent-box-stencil x-ext y-ext)
2121                   (interpret-markup layout props arg)))
2122
2123 (define-markup-command (hcenter-in layout props length arg)
2124   (number? markup?)
2125   #:category align
2126   "Center @var{arg} horizontally within a box of extending
2127 @var{length}/2 to the left and right.
2128
2129 @lilypond[quote,verbatim]
2130 \\new StaffGroup <<
2131   \\new Staff {
2132     \\set Staff.instrumentName = \\markup {
2133       \\hcenter-in #12
2134       Oboe
2135     }
2136     c''1
2137   }
2138   \\new Staff {
2139     \\set Staff.instrumentName = \\markup {
2140       \\hcenter-in #12
2141       Bassoon
2142     }
2143     \\clef tenor
2144     c'1
2145   }
2146 >>
2147 @end lilypond"
2148   (interpret-markup layout props
2149                     (make-pad-to-box-markup
2150                      (cons (/ length -2) (/ length 2))
2151                      '(0 . 0)
2152                      (make-center-align-markup arg))))
2153
2154 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2155 ;; property
2156 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2157
2158 (define-markup-command (property-recursive layout props symbol)
2159   (symbol?)
2160   #:category other
2161   "Print out a warning when a header field markup contains some recursive
2162 markup definition."
2163   (ly:warning "Recursive definition of property ~a detected!" symbol)
2164   empty-stencil)
2165
2166 (define-markup-command (fromproperty layout props symbol)
2167   (symbol?)
2168   #:category other
2169   "Read the @var{symbol} from property settings, and produce a stencil
2170 from the markup contained within.  If @var{symbol} is not defined, it
2171 returns an empty markup.
2172
2173 @lilypond[verbatim,quote]
2174 \\header {
2175   myTitle = \"myTitle\"
2176   title = \\markup {
2177     from
2178     \\italic
2179     \\fromproperty #'header:myTitle
2180   }
2181 }
2182 \\markup {
2183   \\null
2184 }
2185 @end lilypond"
2186   (let ((m (chain-assoc-get symbol props)))
2187     (if (markup? m)
2188         ;; prevent infinite loops by clearing the interpreted property:
2189         (interpret-markup layout (cons (list (cons symbol `(,property-recursive-markup ,symbol))) props) m)
2190         empty-stencil)))
2191
2192 (define-markup-command (on-the-fly layout props procedure arg)
2193   (procedure? markup?)
2194   #:category other
2195   "Apply the @var{procedure} markup command to @var{arg}.
2196 @var{procedure} should take a single argument."
2197   (let ((anonymous-with-signature (lambda (layout props arg) (procedure layout props arg))))
2198     (set-object-property! anonymous-with-signature
2199                           'markup-signature
2200                           (list markup?))
2201     (interpret-markup layout props (list anonymous-with-signature arg))))
2202
2203 (define-markup-command (footnote layout props mkup note)
2204   (markup? markup?)
2205   #:category other
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 not be annotated automatically."
2216   (ly:stencil-combine-at-edge
2217    (interpret-markup layout props mkup)
2218    X
2219    RIGHT
2220    (ly:make-stencil
2221     `(footnote (gensym "footnote") #f ,(interpret-markup layout props note))
2222     '(0 . 0)
2223     '(0 . 0))
2224    0.0))
2225
2226 (define-markup-command (auto-footnote layout props mkup note)
2227   (markup? markup?)
2228   #:category other
2229   #:properties ((raise 0.5)
2230                 (padding 0.0))
2231   "Have footnote @var{note} act as an annotation to the markup @var{mkup}.
2232
2233 @lilypond[verbatim,quote]
2234 \\markup {
2235   \\auto-footnote a b
2236   \\override #'(padding . 0.2)
2237   \\auto-footnote c d
2238 }
2239 @end lilypond
2240 The footnote will be annotated automatically."
2241   (let* ((markup-stencil (interpret-markup layout props mkup))
2242          (footnote-hash (gensym "footnote"))
2243          (stencil-seed 0)
2244          (gauge-stencil (interpret-markup
2245                          layout
2246                          props
2247                          ((ly:output-def-lookup
2248                            layout
2249                            'footnote-numbering-function)
2250                           stencil-seed)))
2251          (x-ext (ly:stencil-extent gauge-stencil X))
2252          (y-ext (ly:stencil-extent gauge-stencil Y))
2253          (footnote-number
2254           `(delay-stencil-evaluation
2255             ,(delay
2256                (ly:stencil-expr
2257                 (let* ((table
2258                         (ly:output-def-lookup layout
2259                                               'number-footnote-table))
2260                        (footnote-stencil (if (list? table)
2261                                              (assoc-get footnote-hash
2262                                                         table)
2263                                              empty-stencil))
2264                        (footnote-stencil (if (ly:stencil? footnote-stencil)
2265                                              footnote-stencil
2266                                              (begin
2267                                                (ly:programming-error
2268                                                 "Cannot find correct footnote for a markup object.")
2269                                                empty-stencil)))
2270                        (gap (- (interval-length x-ext)
2271                                (interval-length
2272                                 (ly:stencil-extent footnote-stencil X))))
2273                        (y-trans (- (+ (cdr y-ext)
2274                                       raise)
2275                                    (cdr (ly:stencil-extent footnote-stencil
2276                                                            Y)))))
2277                   (ly:stencil-translate footnote-stencil
2278                                         (cons gap y-trans)))))))
2279          (main-stencil (ly:stencil-combine-at-edge
2280                         markup-stencil
2281                         X
2282                         RIGHT
2283                         (ly:make-stencil footnote-number x-ext y-ext)
2284                         padding)))
2285     (ly:stencil-add
2286      main-stencil
2287      (ly:make-stencil
2288       `(footnote ,footnote-hash #t ,(interpret-markup layout props note))
2289       '(0 . 0)
2290       '(0 . 0)))))
2291
2292 (define-markup-command (override layout props new-prop arg)
2293   (pair? markup?)
2294   #:category other
2295   "
2296 @cindex overriding properties within text markup
2297
2298 Add the argument @var{new-prop} to the property list.  Properties
2299 may be any property supported by @rinternals{font-interface},
2300 @rinternals{text-interface} and
2301 @rinternals{instrument-specific-markup-interface}.
2302
2303 @lilypond[verbatim,quote]
2304 \\markup {
2305   \\line {
2306     \\column {
2307       default
2308       baseline-skip
2309     }
2310     \\hspace #2
2311     \\override #'(baseline-skip . 4) {
2312       \\column {
2313         increased
2314         baseline-skip
2315       }
2316     }
2317   }
2318 }
2319 @end lilypond"
2320   (interpret-markup layout (cons (list new-prop) props) arg))
2321
2322 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2323 ;; files
2324 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2325
2326 (define-markup-command (verbatim-file layout props name)
2327   (string?)
2328   #:category other
2329   "Read the contents of file @var{name}, and include it verbatim.
2330
2331 @lilypond[verbatim,quote]
2332 \\markup {
2333   \\verbatim-file #\"simple.ly\"
2334 }
2335 @end lilypond"
2336   (interpret-markup layout props
2337                     (if  (ly:get-option 'safe)
2338                          "verbatim-file disabled in safe mode"
2339                          (let* ((str (ly:gulp-file name))
2340                                 (lines (string-split str #\nl)))
2341                            (make-typewriter-markup
2342                             (make-column-markup lines))))))
2343
2344 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2345 ;; fonts.
2346 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2347
2348
2349 (define-markup-command (smaller layout props arg)
2350   (markup?)
2351   #:category font
2352   "Decrease the font size relative to the current setting.
2353
2354 @lilypond[verbatim,quote]
2355 \\markup {
2356   \\fontsize #3.5 {
2357     some large text
2358     \\hspace #2
2359     \\smaller {
2360       a bit smaller
2361     }
2362     \\hspace #2
2363     more large text
2364   }
2365 }
2366 @end lilypond"
2367   (interpret-markup layout props
2368                     `(,fontsize-markup -1 ,arg)))
2369
2370 (define-markup-command (larger layout props arg)
2371   (markup?)
2372   #:category font
2373   "Increase the font size relative to the current setting.
2374
2375 @lilypond[verbatim,quote]
2376 \\markup {
2377   default
2378   \\hspace #2
2379   \\larger
2380   larger
2381 }
2382 @end lilypond"
2383   (interpret-markup layout props
2384                     `(,fontsize-markup 1 ,arg)))
2385
2386 (define-markup-command (finger layout props arg)
2387   (markup?)
2388   #:category font
2389   "Set @var{arg} as small numbers.
2390
2391 @lilypond[verbatim,quote]
2392 \\markup {
2393   \\finger {
2394     1 2 3 4 5
2395   }
2396 }
2397 @end lilypond"
2398   (interpret-markup layout
2399                     (cons '((font-size . -5) (font-encoding . fetaText)) props)
2400                     arg))
2401
2402 (define-markup-command (abs-fontsize layout props size arg)
2403   (number? markup?)
2404   #:category font
2405   "Use @var{size} as the absolute font size (in points) to display @var{arg}.
2406 Adjusts @code{baseline-skip} and @code{word-space} accordingly.
2407
2408 @lilypond[verbatim,quote]
2409 \\markup {
2410   default text font size
2411   \\hspace #2
2412   \\abs-fontsize #16 { text font size 16 }
2413   \\hspace #2
2414   \\abs-fontsize #12 { text font size 12 }
2415 }
2416 @end lilypond"
2417   (let* ((ref-size (ly:output-def-lookup layout 'text-font-size 12))
2418          (text-props (list (ly:output-def-lookup layout 'text-font-defaults)))
2419          (ref-word-space (chain-assoc-get 'word-space text-props 0.6))
2420          (ref-baseline (chain-assoc-get 'baseline-skip text-props 3))
2421          (magnification (/ size ref-size)))
2422     (interpret-markup
2423      layout
2424      (cons
2425       `((baseline-skip . ,(* magnification ref-baseline))
2426         (word-space . ,(* magnification ref-word-space))
2427         (font-size . ,(magnification->font-size magnification)))
2428       props)
2429      arg)))
2430
2431 (define-markup-command (fontsize layout props increment arg)
2432   (number? markup?)
2433   #:category font
2434   #:properties ((font-size 0)
2435                 (word-space 1)
2436                 (baseline-skip 2))
2437   "Add @var{increment} to the font-size.  Adjusts @code{baseline-skip}
2438 accordingly.
2439
2440 @lilypond[verbatim,quote]
2441 \\markup {
2442   default
2443   \\hspace #2
2444   \\fontsize #-1.5
2445   smaller
2446 }
2447 @end lilypond"
2448   (interpret-markup
2449    layout
2450    (cons
2451     `((baseline-skip . ,(* baseline-skip (magstep increment)))
2452       (word-space . ,(* word-space (magstep increment)))
2453       (font-size . ,(+ font-size increment)))
2454     props)
2455    arg))
2456
2457 (define-markup-command (magnify layout props sz arg)
2458   (number? markup?)
2459   #:category font
2460   "
2461 @cindex magnifying text
2462
2463 Set the font magnification for its argument.  In the following
2464 example, the middle@tie{}A is 10% larger:
2465
2466 @example
2467 A \\magnify #1.1 @{ A @} A
2468 @end example
2469
2470 Note: Magnification only works if a font name is explicitly selected.
2471 Use @code{\\fontsize} otherwise.
2472
2473 @lilypond[verbatim,quote]
2474 \\markup {
2475   default
2476   \\hspace #2
2477   \\magnify #1.5 {
2478     50% larger
2479   }
2480 }
2481 @end lilypond"
2482   (interpret-markup
2483    layout
2484    (prepend-alist-chain 'font-size (magnification->font-size sz) props)
2485    arg))
2486
2487 (define-markup-command (bold layout props arg)
2488   (markup?)
2489   #:category font
2490   "Switch to bold font-series.
2491
2492 @lilypond[verbatim,quote]
2493 \\markup {
2494   default
2495   \\hspace #2
2496   \\bold
2497   bold
2498 }
2499 @end lilypond"
2500   (interpret-markup layout (prepend-alist-chain 'font-series 'bold props) arg))
2501
2502 (define-markup-command (sans layout props arg)
2503   (markup?)
2504   #:category font
2505   "Switch to the sans serif font family.
2506
2507 @lilypond[verbatim,quote]
2508 \\markup {
2509   default
2510   \\hspace #2
2511   \\sans {
2512     sans serif
2513   }
2514 }
2515 @end lilypond"
2516   (interpret-markup layout (prepend-alist-chain 'font-family 'sans props) arg))
2517
2518 (define-markup-command (number layout props arg)
2519   (markup?)
2520   #:category font
2521   "Set font family to @code{number}, which yields the font used for
2522 time signatures and fingerings.  This font contains numbers and
2523 some punctuation; it has no letters.
2524
2525 @lilypond[verbatim,quote]
2526 \\markup {
2527   \\number {
2528     0 1 2 3 4 5 6 7 8 9 . ,
2529   }
2530 }
2531 @end lilypond"
2532   (interpret-markup layout (prepend-alist-chain 'font-encoding 'fetaText props) arg))
2533
2534 (define-markup-command (roman layout props arg)
2535   (markup?)
2536   #:category font
2537   "Set font family to @code{roman}.
2538
2539 @lilypond[verbatim,quote]
2540 \\markup {
2541   \\sans \\bold {
2542     sans serif, bold
2543     \\hspace #2
2544     \\roman {
2545       text in roman font family
2546     }
2547     \\hspace #2
2548     return to sans
2549   }
2550 }
2551 @end lilypond"
2552   (interpret-markup layout (prepend-alist-chain 'font-family 'roman props) arg))
2553
2554 (define-markup-command (huge layout props arg)
2555   (markup?)
2556   #:category font
2557   "Set font size to +2.
2558
2559 @lilypond[verbatim,quote]
2560 \\markup {
2561   default
2562   \\hspace #2
2563   \\huge
2564   huge
2565 }
2566 @end lilypond"
2567   (interpret-markup layout (prepend-alist-chain 'font-size 2 props) arg))
2568
2569 (define-markup-command (large layout props arg)
2570   (markup?)
2571   #:category font
2572   "Set font size to +1.
2573
2574 @lilypond[verbatim,quote]
2575 \\markup {
2576   default
2577   \\hspace #2
2578   \\large
2579   large
2580 }
2581 @end lilypond"
2582   (interpret-markup layout (prepend-alist-chain 'font-size 1 props) arg))
2583
2584 (define-markup-command (normalsize layout props arg)
2585   (markup?)
2586   #:category font
2587   "Set font size to default.
2588
2589 @lilypond[verbatim,quote]
2590 \\markup {
2591   \\teeny {
2592     this is very small
2593     \\hspace #2
2594     \\normalsize {
2595       normal size
2596     }
2597     \\hspace #2
2598     teeny again
2599   }
2600 }
2601 @end lilypond"
2602   (interpret-markup layout (prepend-alist-chain 'font-size 0 props) arg))
2603
2604 (define-markup-command (small layout props arg)
2605   (markup?)
2606   #:category font
2607   "Set font size to -1.
2608
2609 @lilypond[verbatim,quote]
2610 \\markup {
2611   default
2612   \\hspace #2
2613   \\small
2614   small
2615 }
2616 @end lilypond"
2617   (interpret-markup layout (prepend-alist-chain 'font-size -1 props) arg))
2618
2619 (define-markup-command (tiny layout props arg)
2620   (markup?)
2621   #:category font
2622   "Set font size to -2.
2623
2624 @lilypond[verbatim,quote]
2625 \\markup {
2626   default
2627   \\hspace #2
2628   \\tiny
2629   tiny
2630 }
2631 @end lilypond"
2632   (interpret-markup layout (prepend-alist-chain 'font-size -2 props) arg))
2633
2634 (define-markup-command (teeny layout props arg)
2635   (markup?)
2636   #:category font
2637   "Set font size to -3.
2638
2639 @lilypond[verbatim,quote]
2640 \\markup {
2641   default
2642   \\hspace #2
2643   \\teeny
2644   teeny
2645 }
2646 @end lilypond"
2647   (interpret-markup layout (prepend-alist-chain 'font-size -3 props) arg))
2648
2649 (define-markup-command (fontCaps layout props arg)
2650   (markup?)
2651   #:category font
2652   "Set @code{font-shape} to @code{caps}
2653
2654 Note: @code{\\fontCaps} requires the installation and selection of
2655 fonts which support the @code{caps} font shape."
2656   (interpret-markup layout (prepend-alist-chain 'font-shape 'caps props) arg))
2657
2658 ;; Poor man's caps
2659 (define-markup-command (smallCaps layout props arg)
2660   (markup?)
2661   #:category font
2662   "Emit @var{arg} as small caps.
2663
2664 Note: @code{\\smallCaps} does not support accented characters.
2665
2666 @lilypond[verbatim,quote]
2667 \\markup {
2668   default
2669   \\hspace #2
2670   \\smallCaps {
2671     Text in small caps
2672   }
2673 }
2674 @end lilypond"
2675   (define (char-list->markup chars lower)
2676     (let ((final-string (string-upcase (reverse-list->string chars))))
2677       (if lower
2678           (markup #:fontsize -2 final-string)
2679           final-string)))
2680   (define (make-small-caps rest-chars currents current-is-lower prev-result)
2681     (if (null? rest-chars)
2682         (make-concat-markup
2683          (reverse! (cons (char-list->markup currents current-is-lower)
2684                          prev-result)))
2685         (let* ((ch (car rest-chars))
2686                (is-lower (char-lower-case? ch)))
2687           (if (or (and current-is-lower is-lower)
2688                   (and (not current-is-lower) (not is-lower)))
2689               (make-small-caps (cdr rest-chars)
2690                                (cons ch currents)
2691                                is-lower
2692                                prev-result)
2693               (make-small-caps (cdr rest-chars)
2694                                (list ch)
2695                                is-lower
2696                                (if (null? currents)
2697                                    prev-result
2698                                    (cons (char-list->markup
2699                                           currents current-is-lower)
2700                                          prev-result)))))))
2701   (interpret-markup layout props
2702                     (if (string? arg)
2703                         (make-small-caps (string->list arg) (list) #f (list))
2704                         arg)))
2705
2706 (define-markup-command (caps layout props arg)
2707   (markup?)
2708   #:category font
2709   "Copy of the @code{\\smallCaps} command.
2710
2711 @lilypond[verbatim,quote]
2712 \\markup {
2713   default
2714   \\hspace #2
2715   \\caps {
2716     Text in small caps
2717   }
2718 }
2719 @end lilypond"
2720   (interpret-markup layout props (make-smallCaps-markup arg)))
2721
2722 (define-markup-command (dynamic layout props arg)
2723   (markup?)
2724   #:category font
2725   "Use the dynamic font.  This font only contains @b{s}, @b{f}, @b{m},
2726 @b{z}, @b{p}, and @b{r}.  When producing phrases, like
2727 @q{pi@`{u}@tie{}@b{f}}, the normal words (like @q{pi@`{u}}) should be
2728 done in a different font.  The recommended font for this is bold and italic.
2729 @lilypond[verbatim,quote]
2730 \\markup {
2731   \\dynamic {
2732     sfzp
2733   }
2734 }
2735 @end lilypond"
2736   (interpret-markup
2737    layout (prepend-alist-chain 'font-encoding 'fetaText props) arg))
2738
2739 (define-markup-command (text layout props arg)
2740   (markup?)
2741   #:category font
2742   "Use a text font instead of music symbol or music alphabet font.
2743
2744 @lilypond[verbatim,quote]
2745 \\markup {
2746   \\number {
2747     1, 2,
2748     \\text {
2749       three, four,
2750     }
2751     5
2752   }
2753 }
2754 @end lilypond"
2755
2756   ;; ugh - latin1
2757   (interpret-markup layout (prepend-alist-chain 'font-encoding 'latin1 props)
2758                     arg))
2759
2760 (define-markup-command (italic layout props arg)
2761   (markup?)
2762   #:category font
2763   "Use italic @code{font-shape} for @var{arg}.
2764
2765 @lilypond[verbatim,quote]
2766 \\markup {
2767   default
2768   \\hspace #2
2769   \\italic
2770   italic
2771 }
2772 @end lilypond"
2773   (interpret-markup layout (prepend-alist-chain 'font-shape 'italic props) arg))
2774
2775 (define-markup-command (typewriter layout props arg)
2776   (markup?)
2777   #:category font
2778   "Use @code{font-family} typewriter for @var{arg}.
2779
2780 @lilypond[verbatim,quote]
2781 \\markup {
2782   default
2783   \\hspace #2
2784   \\typewriter
2785   typewriter
2786 }
2787 @end lilypond"
2788   (interpret-markup
2789    layout (prepend-alist-chain 'font-family 'typewriter props) arg))
2790
2791 (define-markup-command (upright layout props arg)
2792   (markup?)
2793   #:category font
2794   "Set @code{font-shape} to @code{upright}.  This is the opposite
2795 of @code{italic}.
2796
2797 @lilypond[verbatim,quote]
2798 \\markup {
2799   \\italic {
2800     italic text
2801     \\hspace #2
2802     \\upright {
2803       upright text
2804     }
2805     \\hspace #2
2806     italic again
2807   }
2808 }
2809 @end lilypond"
2810   (interpret-markup
2811    layout (prepend-alist-chain 'font-shape 'upright props) arg))
2812
2813 (define-markup-command (medium layout props arg)
2814   (markup?)
2815   #:category font
2816   "Switch to medium font-series (in contrast to bold).
2817
2818 @lilypond[verbatim,quote]
2819 \\markup {
2820   \\bold {
2821     some bold text
2822     \\hspace #2
2823     \\medium {
2824       medium font series
2825     }
2826     \\hspace #2
2827     bold again
2828   }
2829 }
2830 @end lilypond"
2831   (interpret-markup layout (prepend-alist-chain 'font-series 'medium props)
2832                     arg))
2833
2834 (define-markup-command (normal-text layout props arg)
2835   (markup?)
2836   #:category font
2837   "Set all font related properties (except the size) to get the default
2838 normal text font, no matter what font was used earlier.
2839
2840 @lilypond[verbatim,quote]
2841 \\markup {
2842   \\huge \\bold \\sans \\caps {
2843     huge bold sans caps
2844     \\hspace #2
2845     \\normal-text {
2846       huge normal
2847     }
2848     \\hspace #2
2849     as before
2850   }
2851 }
2852 @end lilypond"
2853   ;; ugh - latin1
2854   (interpret-markup layout
2855                     (cons '((font-family . roman) (font-shape . upright)
2856                             (font-series . medium) (font-encoding . latin1))
2857                           props)
2858                     arg))
2859
2860 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2861 ;; symbols.
2862 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2863
2864 (define-markup-command (musicglyph layout props glyph-name)
2865   (string?)
2866   #:category music
2867   "@var{glyph-name} is converted to a musical symbol; for example,
2868 @code{\\musicglyph #\"accidentals.natural\"} selects the natural sign from
2869 the music font.  See @ruser{The Feta font} for a complete listing of
2870 the possible glyphs.
2871
2872 @lilypond[verbatim,quote]
2873 \\markup {
2874   \\musicglyph #\"f\"
2875   \\musicglyph #\"rests.2\"
2876   \\musicglyph #\"clefs.G_change\"
2877 }
2878 @end lilypond"
2879   (let* ((font (ly:paper-get-font layout
2880                                   (cons '((font-encoding . fetaMusic)
2881                                           (font-name . #f))
2882
2883                                         props)))
2884          (glyph (ly:font-get-glyph font glyph-name)))
2885     (if (null? (ly:stencil-expr glyph))
2886         (ly:warning (_ "Cannot find glyph ~a") glyph-name))
2887
2888     glyph))
2889
2890 (define-markup-command (doublesharp layout props)
2891   ()
2892   #:category music
2893   "Draw a double sharp symbol.
2894
2895 @lilypond[verbatim,quote]
2896 \\markup {
2897   \\doublesharp
2898 }
2899 @end lilypond"
2900   (interpret-markup layout props (markup #:musicglyph (assoc-get 1 standard-alteration-glyph-name-alist ""))))
2901
2902 (define-markup-command (sesquisharp layout props)
2903   ()
2904   #:category music
2905   "Draw a 3/2 sharp symbol.
2906
2907 @lilypond[verbatim,quote]
2908 \\markup {
2909   \\sesquisharp
2910 }
2911 @end lilypond"
2912   (interpret-markup layout props (markup #:musicglyph (assoc-get 3/4 standard-alteration-glyph-name-alist ""))))
2913
2914 (define-markup-command (sharp layout props)
2915   ()
2916   #:category music
2917   "Draw a sharp symbol.
2918
2919 @lilypond[verbatim,quote]
2920 \\markup {
2921   \\sharp
2922 }
2923 @end lilypond"
2924   (interpret-markup layout props (markup #:musicglyph (assoc-get 1/2 standard-alteration-glyph-name-alist ""))))
2925
2926 (define-markup-command (semisharp layout props)
2927   ()
2928   #:category music
2929   "Draw a semisharp symbol.
2930
2931 @lilypond[verbatim,quote]
2932 \\markup {
2933   \\semisharp
2934 }
2935 @end lilypond"
2936   (interpret-markup layout props (markup #:musicglyph (assoc-get 1/4 standard-alteration-glyph-name-alist ""))))
2937
2938 (define-markup-command (natural layout props)
2939   ()
2940   #:category music
2941   "Draw a natural symbol.
2942
2943 @lilypond[verbatim,quote]
2944 \\markup {
2945   \\natural
2946 }
2947 @end lilypond"
2948   (interpret-markup layout props (markup #:musicglyph (assoc-get 0 standard-alteration-glyph-name-alist ""))))
2949
2950 (define-markup-command (semiflat layout props)
2951   ()
2952   #:category music
2953   "Draw a semiflat symbol.
2954
2955 @lilypond[verbatim,quote]
2956 \\markup {
2957   \\semiflat
2958 }
2959 @end lilypond"
2960   (interpret-markup layout props (markup #:musicglyph (assoc-get -1/4 standard-alteration-glyph-name-alist ""))))
2961
2962 (define-markup-command (flat layout props)
2963   ()
2964   #:category music
2965   "Draw a flat symbol.
2966
2967 @lilypond[verbatim,quote]
2968 \\markup {
2969   \\flat
2970 }
2971 @end lilypond"
2972   (interpret-markup layout props (markup #:musicglyph (assoc-get -1/2 standard-alteration-glyph-name-alist ""))))
2973
2974 (define-markup-command (sesquiflat layout props)
2975   ()
2976   #:category music
2977   "Draw a 3/2 flat symbol.
2978
2979 @lilypond[verbatim,quote]
2980 \\markup {
2981   \\sesquiflat
2982 }
2983 @end lilypond"
2984   (interpret-markup layout props (markup #:musicglyph (assoc-get -3/4 standard-alteration-glyph-name-alist ""))))
2985
2986 (define-markup-command (doubleflat layout props)
2987   ()
2988   #:category music
2989   "Draw a double flat symbol.
2990
2991 @lilypond[verbatim,quote]
2992 \\markup {
2993   \\doubleflat
2994 }
2995 @end lilypond"
2996   (interpret-markup layout props (markup #:musicglyph (assoc-get -1 standard-alteration-glyph-name-alist ""))))
2997
2998 (define-markup-command (with-color layout props color arg)
2999   (color? markup?)
3000   #:category other
3001   "
3002 @cindex coloring text
3003
3004 Draw @var{arg} in color specified by @var{color}.
3005
3006 @lilypond[verbatim,quote]
3007 \\markup {
3008   \\with-color #red
3009   red
3010   \\hspace #2
3011   \\with-color #green
3012   green
3013   \\hspace #2
3014   \\with-color #blue
3015   blue
3016 }
3017 @end lilypond"
3018   (let ((stil (interpret-markup layout props arg)))
3019     (ly:make-stencil (list 'color color (ly:stencil-expr stil))
3020                      (ly:stencil-extent stil X)
3021                      (ly:stencil-extent stil Y))))
3022
3023 (define-markup-command (tied-lyric layout props str)
3024   (string?)
3025   #:category music
3026   #:properties ((word-space))
3027   "
3028 @cindex simple text strings with tie characters
3029
3030 Like simple-markup, but use tie characters for @q{~} tilde symbols.
3031
3032 @lilypond[verbatim,quote]
3033 \\markup \\column {
3034   \\tied-lyric #\"Siam navi~all'onde~algenti Lasciate~in abbandono\"
3035   \\tied-lyric #\"Impetuosi venti I nostri~affetti sono\"
3036   \\tied-lyric #\"Ogni diletto~e scoglio Tutta la vita~e~un mar.\"
3037 }
3038 @end lilypond"
3039   (define (replace-ties tie str)
3040     (if (string-contains str "~")
3041         (let*
3042             ((half-space (/ word-space 2))
3043              (parts (string-split str #\~))
3044              (tie-str (markup #:hspace half-space
3045                               #:musicglyph tie
3046                               #:hspace half-space))
3047              (joined  (list-join parts tie-str)))
3048           (make-concat-markup joined))
3049         str))
3050
3051   (define short-tie-regexp (make-regexp "~[^.]~"))
3052   (define (match-short str) (regexp-exec short-tie-regexp str))
3053
3054   (define (replace-short str mkp)
3055     (let ((match (match-short str)))
3056       (if (not match)
3057           (make-concat-markup (list
3058                                mkp
3059                                (replace-ties "ties.lyric.default" str)))
3060           (let ((new-str (match:suffix match))
3061                 (new-mkp (make-concat-markup (list
3062                                               mkp
3063                                               (replace-ties "ties.lyric.default"
3064                                                             (match:prefix match))
3065                                               (replace-ties "ties.lyric.short"
3066                                                             (match:substring match))))))
3067             (replace-short new-str new-mkp)))))
3068
3069   (interpret-markup layout
3070                     props
3071                     (replace-short str (markup))))
3072
3073 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3074 ;; glyphs
3075 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3076
3077 (define-markup-command (arrow-head layout props axis dir filled)
3078   (integer? ly:dir? boolean?)
3079   #:category graphic
3080   "Produce an arrow head in specified direction and axis.
3081 Use the filled head if @var{filled} is specified.
3082 @lilypond[verbatim,quote]
3083 \\markup {
3084   \\fontsize #5 {
3085     \\general-align #Y #DOWN {
3086       \\arrow-head #Y #UP ##t
3087       \\arrow-head #Y #DOWN ##f
3088       \\hspace #2
3089       \\arrow-head #X #RIGHT ##f
3090       \\arrow-head #X #LEFT ##f
3091     }
3092   }
3093 }
3094 @end lilypond"
3095   (let*
3096       ((name (format #f "arrowheads.~a.~a~a"
3097                      (if filled
3098                          "close"
3099                          "open")
3100                      axis
3101                      dir)))
3102     (ly:font-get-glyph
3103      (ly:paper-get-font layout (cons '((font-encoding . fetaMusic))
3104                                      props))
3105      name)))
3106
3107 (define-markup-command (lookup layout props glyph-name)
3108   (string?)
3109   #:category other
3110   "Lookup a glyph by name.
3111
3112 @lilypond[verbatim,quote]
3113 \\markup {
3114   \\override #'(font-encoding . fetaBraces) {
3115     \\lookup #\"brace200\"
3116     \\hspace #2
3117     \\rotate #180
3118     \\lookup #\"brace180\"
3119   }
3120 }
3121 @end lilypond"
3122   (ly:font-get-glyph (ly:paper-get-font layout props)
3123                      glyph-name))
3124
3125 (define-markup-command (char layout props num)
3126   (integer?)
3127   #:category other
3128   "Produce a single character.  Characters encoded in hexadecimal
3129 format require the prefix @code{#x}.
3130
3131 @lilypond[verbatim,quote]
3132 \\markup {
3133   \\char #65 \\char ##x00a9
3134 }
3135 @end lilypond"
3136   (ly:text-interface::interpret-markup layout props (ly:wide-char->utf-8 num)))
3137
3138 (define number->mark-letter-vector (make-vector 25 #\A))
3139
3140 (do ((i 0 (1+ i))
3141      (j 0 (1+ j)))
3142     ((>= i 26))
3143   (if (= i (- (char->integer #\I) (char->integer #\A)))
3144       (set! i (1+ i)))
3145   (vector-set! number->mark-letter-vector j
3146                (integer->char (+ i (char->integer #\A)))))
3147
3148 (define number->mark-alphabet-vector (list->vector
3149                                       (map (lambda (i) (integer->char (+ i (char->integer #\A)))) (iota 26))))
3150
3151 (define (number->markletter-string vec n)
3152   "Double letters for big marks."
3153   (let* ((lst (vector-length vec)))
3154
3155     (if (>= n lst)
3156         (string-append (number->markletter-string vec (1- (quotient n lst)))
3157                        (number->markletter-string vec (remainder n lst)))
3158         (make-string 1 (vector-ref vec n)))))
3159
3160 (define-markup-command (markletter layout props num)
3161   (integer?)
3162   #:category other
3163   "Make a markup letter for @var{num}.  The letters start with A
3164 to@tie{}Z (skipping letter@tie{}I), and continue with double letters.
3165
3166 @lilypond[verbatim,quote]
3167 \\markup {
3168   \\markletter #8
3169   \\hspace #2
3170   \\markletter #26
3171 }
3172 @end lilypond"
3173   (ly:text-interface::interpret-markup layout props
3174                                        (number->markletter-string number->mark-letter-vector num)))
3175
3176 (define-markup-command (markalphabet layout props num)
3177   (integer?)
3178   #:category other
3179   "Make a markup letter for @var{num}.  The letters start with A to@tie{}Z
3180 and continue with double letters.
3181
3182 @lilypond[verbatim,quote]
3183 \\markup {
3184   \\markalphabet #8
3185   \\hspace #2
3186   \\markalphabet #26
3187 }
3188 @end lilypond"
3189   (ly:text-interface::interpret-markup layout props
3190                                        (number->markletter-string number->mark-alphabet-vector num)))
3191
3192 (define-public (horizontal-slash-interval num forward number-interval mag)
3193   (if forward
3194       (cond ;; ((= num 6) (interval-widen number-interval (* mag 0.5)))
3195        ;; ((= num 5) (interval-widen number-interval (* mag 0.5)))
3196        (else (interval-widen number-interval (* mag 0.25))))
3197       (cond ((= num 6) (interval-widen number-interval (* mag 0.5)))
3198             ;; ((= num 5) (interval-widen number-interval (* mag 0.5)))
3199             (else (interval-widen number-interval (* mag 0.25))))
3200       ))
3201
3202 (define-public (adjust-slash-stencil num forward stencil mag)
3203   (if forward
3204       (cond ((= num 2)
3205              (ly:stencil-translate stencil (cons (* mag -0.00) (* mag 0.2))))
3206             ((= num 3)
3207              (ly:stencil-translate stencil (cons (* mag -0.00) (* mag 0.2))))
3208             ;; ((= num 5)
3209             ;;     (ly:stencil-translate stencil (cons (* mag -0.00) (* mag -0.07))))
3210             ;; ((= num 7)
3211             ;;     (ly:stencil-translate stencil (cons (* mag -0.00) (* mag -0.15))))
3212             (else stencil))
3213       (cond ((= num 6)
3214              (ly:stencil-translate stencil (cons (* mag -0.00) (* mag 0.15))))
3215             ;; ((= num 8)
3216             ;;     (ly:stencil-translate stencil (cons (* mag -0.00) (* mag -0.15))))
3217             (else stencil))
3218       )
3219   )
3220
3221 (define (slashed-digit-internal layout props num forward font-size thickness)
3222   (let* ((mag (magstep font-size))
3223          (thickness (* mag
3224                        (ly:output-def-lookup layout 'line-thickness)
3225                        thickness))
3226          ;; backward slashes might use slope and point in the other direction!
3227          (dy (* mag (if forward 0.4 -0.4)))
3228          (number-stencil (interpret-markup layout
3229                                            (prepend-alist-chain 'font-encoding 'fetaText props)
3230                                            (number->string num)))
3231          (num-x (horizontal-slash-interval num forward (ly:stencil-extent number-stencil X) mag))
3232          (center (interval-center (ly:stencil-extent number-stencil Y)))
3233          ;; Use the real extents of the slash, not the whole number,
3234          ;; because we might translate the slash later on!
3235          (num-y (interval-widen (cons center center) (abs dy)))
3236          (is-sane (and (interval-sane? num-x) (interval-sane? num-y)))
3237          (slash-stencil (if is-sane
3238                             (make-line-stencil thickness
3239                                                (car num-x) (- (interval-center num-y) dy)
3240                                                (cdr num-x) (+ (interval-center num-y) dy))
3241                             #f)))
3242     (if (ly:stencil? slash-stencil)
3243         (begin
3244           ;; for some numbers we need to shift the slash/backslash up or
3245           ;; down to make the slashed digit look better
3246           (set! slash-stencil (adjust-slash-stencil num forward slash-stencil mag))
3247           (set! number-stencil
3248                 (ly:stencil-add number-stencil slash-stencil)))
3249         (ly:warning "Unable to create slashed digit ~a" num))
3250     number-stencil))
3251
3252
3253 (define-markup-command (slashed-digit layout props num)
3254   (integer?)
3255   #:category other
3256   #:properties ((font-size 0)
3257                 (thickness 1.6))
3258   "
3259 @cindex slashed digits
3260
3261 A feta number, with slash.  This is for use in the context of
3262 figured bass notation.
3263 @lilypond[verbatim,quote]
3264 \\markup {
3265   \\slashed-digit #5
3266   \\hspace #2
3267   \\override #'(thickness . 3)
3268   \\slashed-digit #7
3269 }
3270 @end lilypond"
3271   (slashed-digit-internal layout props num #t font-size thickness))
3272
3273 (define-markup-command (backslashed-digit layout props num)
3274   (integer?)
3275   #:category other
3276   #:properties ((font-size 0)
3277                 (thickness 1.6))
3278   "
3279 @cindex backslashed digits
3280
3281 A feta number, with backslash.  This is for use in the context of
3282 figured bass notation.
3283 @lilypond[verbatim,quote]
3284 \\markup {
3285   \\backslashed-digit #5
3286   \\hspace #2
3287   \\override #'(thickness . 3)
3288   \\backslashed-digit #7
3289 }
3290 @end lilypond"
3291   (slashed-digit-internal layout props num #f font-size thickness))
3292
3293 ;; eyeglasses
3294 (define eyeglassespath
3295   '((moveto 0.42 0.77)
3296     (rcurveto 0 0.304 -0.246 0.55 -0.55 0.55)
3297     (rcurveto -0.304 0 -0.55 -0.246 -0.55 -0.55)
3298     (rcurveto 0 -0.304 0.246 -0.55 0.55 -0.55)
3299     (rcurveto 0.304 0 0.55 0.246 0.55 0.55)
3300     (closepath)
3301     (moveto 2.07 0.77)
3302     (rcurveto 0 0.304 -0.246 0.55 -0.55 0.55)
3303     (rcurveto -0.304 0 -0.55 -0.246 -0.55 -0.55)
3304     (rcurveto 0 -0.304 0.246 -0.55 0.55 -0.55)
3305     (rcurveto 0.304 0 0.55 0.246 0.55 0.55)
3306     (closepath)
3307     (moveto 1.025 0.935)
3308     (rcurveto 0 0.182 -0.148 0.33 -0.33 0.33)
3309     (rcurveto -0.182 0 -0.33 -0.148 -0.33 -0.33)
3310     (moveto -0.68 0.77)
3311     (rlineto 0.66 1.43)
3312     (rcurveto 0.132 0.286 0.55 0.44 0.385 -0.33)
3313     (moveto 2.07 0.77)
3314     (rlineto 0.66 1.43)
3315     (rcurveto 0.132 0.286 0.55 0.44 0.385 -0.33)))
3316
3317 (define-markup-command (eyeglasses layout props)
3318   ()
3319   #:category other
3320   "Prints out eyeglasses, indicating strongly to look at the conductor.
3321 @lilypond[verbatim,quote]
3322 \\markup { \\eyeglasses }
3323 @end lilypond"
3324   (interpret-markup layout props
3325                     (make-override-markup '(line-cap-style . butt)
3326                                           (make-path-markup 0.15 eyeglassespath))))
3327
3328 (define-markup-command (left-brace layout props size)
3329   (number?)
3330   #:category other
3331   "
3332 A feta brace in point size @var{size}.
3333
3334 @lilypond[verbatim,quote]
3335 \\markup {
3336   \\left-brace #35
3337   \\hspace #2
3338   \\left-brace #45
3339 }
3340 @end lilypond"
3341   (let* ((font (ly:paper-get-font layout
3342                                   (cons '((font-encoding . fetaBraces)
3343                                           (font-name . #f))
3344                                         props)))
3345          (glyph-count (1- (ly:otf-glyph-count font)))
3346          (scale (ly:output-def-lookup layout 'output-scale))
3347          (scaled-size (/ (ly:pt size) scale))
3348          (glyph (lambda (n)
3349                   (ly:font-get-glyph font (string-append "brace"
3350                                                          (number->string n)))))
3351          (get-y-from-brace (lambda (brace)
3352                              (interval-length
3353                               (ly:stencil-extent (glyph brace) Y))))
3354          (find-brace (binary-search 0 glyph-count get-y-from-brace scaled-size))
3355          (glyph-found (glyph find-brace)))
3356
3357     (if (or (null? (ly:stencil-expr glyph-found))
3358             (< scaled-size (interval-length (ly:stencil-extent (glyph 0) Y)))
3359             (> scaled-size (interval-length
3360                             (ly:stencil-extent (glyph glyph-count) Y))))
3361         (begin
3362           (ly:warning (_ "no brace found for point size ~S ") size)
3363           (ly:warning (_ "defaulting to ~S pt")
3364                       (/ (* scale (interval-length
3365                                    (ly:stencil-extent glyph-found Y)))
3366                          (ly:pt 1)))))
3367     glyph-found))
3368
3369 (define-markup-command (right-brace layout props size)
3370   (number?)
3371   #:category other
3372   "
3373 A feta brace in point size @var{size}, rotated 180 degrees.
3374
3375 @lilypond[verbatim,quote]
3376 \\markup {
3377   \\right-brace #45
3378   \\hspace #2
3379   \\right-brace #35
3380 }
3381 @end lilypond"
3382   (interpret-markup layout props (markup #:rotate 180 #:left-brace size)))
3383
3384 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3385 ;; the note command.
3386 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3387
3388 ;; TODO: better syntax.
3389
3390 (define-markup-command (note-by-number layout props log dot-count dir)
3391   (number? number? number?)
3392   #:category music
3393   #:properties ((font-size 0)
3394                 (flag-style '())
3395                 (style '()))
3396   "
3397 @cindex notes within text by log and dot-count
3398
3399 Construct a note symbol, with stem and flag.  By using fractional values for
3400 @var{dir}, longer or shorter stems can be obtained.
3401 Supports all note-head-styles.
3402 Supported flag-styles are @code{default}, @code{old-straight-flag},
3403 @code{modern-straight-flag} and @code{flat-flag}.
3404
3405 @lilypond[verbatim,quote]
3406 \\markup {
3407   \\note-by-number #3 #0 #DOWN
3408   \\hspace #2
3409   \\note-by-number #1 #2 #0.8
3410 }
3411 @end lilypond"
3412   (define (get-glyph-name-candidates dir log style)
3413     (map (lambda (dir-name)
3414            (format #f "noteheads.~a~a" dir-name
3415                    (if (and (symbol? style)
3416                             (not (equal? 'default style)))
3417                        (select-head-glyph style (min log 2))
3418                        (min log 2))))
3419          (list (if (= dir UP) "u" "d")
3420                "s")))
3421
3422   (define (get-glyph-name font cands)
3423     (if (null? cands)
3424         ""
3425         (if (ly:stencil-empty? (ly:font-get-glyph font (car cands)))
3426             (get-glyph-name font (cdr cands))
3427             (car cands))))
3428
3429   (define (buildflags flag-stencil remain curr-stencil spacing)
3430     ;; Function to recursively create a stencil with @code{remain} flags
3431     ;; from the single-flag stencil @code{curr-stencil}, which is already
3432     ;; translated to the position of the previous flag position.
3433     ;;
3434     ;; Copy and paste from /scm/flag-styles.scm
3435     (if (> remain 0)
3436         (let* ((translated-stencil
3437                 (ly:stencil-translate-axis curr-stencil spacing Y))
3438                (new-stencil (ly:stencil-add flag-stencil translated-stencil)))
3439           (buildflags new-stencil (- remain 1) translated-stencil spacing))
3440         flag-stencil))
3441
3442   (define (straight-flag-mrkp flag-thickness flag-spacing
3443                               upflag-angle upflag-length
3444                               downflag-angle downflag-length
3445                               dir)
3446     ;; Create a stencil for a straight flag.  @var{flag-thickness} and
3447     ;; @var{flag-spacing} are given in staff spaces, @var{upflag-angle} and
3448     ;; @var{downflag-angle} are given in degrees, and @var{upflag-length} and
3449     ;; @var{downflag-length} are given in staff spaces.
3450     ;;
3451     ;; All lengths are scaled according to the font size of the note.
3452     ;;
3453     ;; From /scm/flag-styles.scm, modified to fit here.
3454
3455     (let* ((stem-up (> dir 0))
3456            ;; scale with the note size
3457            (factor (magstep font-size))
3458            (stem-thickness (* factor 0.1))
3459            (line-thickness (ly:output-def-lookup layout 'line-thickness))
3460            (half-stem-thickness (/ (* stem-thickness line-thickness) 2))
3461            (raw-length (if stem-up upflag-length downflag-length))
3462            (angle (if stem-up upflag-angle downflag-angle))
3463            (flag-length (+ (* raw-length factor) half-stem-thickness))
3464            (flag-end (if (= angle 0)
3465                          (cons flag-length (* half-stem-thickness dir))
3466                          (polar->rectangular flag-length angle)))
3467            (thickness (* flag-thickness factor))
3468            (thickness-offset (cons 0 (* -1 thickness dir)))
3469            (spacing (* -1 flag-spacing factor dir))
3470            (start (cons (- half-stem-thickness) (* half-stem-thickness dir)))
3471            ;; The points of a round-filled-polygon need to be given in
3472            ;; clockwise order, otherwise the polygon will be enlarged by
3473            ;; blot-size*2!
3474            (points (if stem-up
3475                        (list start
3476                              flag-end
3477                              (offset-add flag-end thickness-offset)
3478                              (offset-add start thickness-offset))
3479                        (list start
3480                              (offset-add start thickness-offset)
3481                              (offset-add flag-end thickness-offset)
3482                              flag-end)))
3483            (stencil (ly:round-filled-polygon points half-stem-thickness))
3484            ;; Log for 1/8 is 3, so we need to subtract 3
3485            (flag-stencil (buildflags stencil (- log 3) stencil spacing)))
3486       flag-stencil))
3487
3488   (let* ((font (ly:paper-get-font layout (cons '((font-encoding . fetaMusic))
3489                                                props)))
3490          (size-factor (magstep font-size))
3491          (blot (ly:output-def-lookup layout 'blot-diameter))
3492          (head-glyph-name
3493           (let ((result (get-glyph-name font
3494                                         (get-glyph-name-candidates
3495                                          (sign dir) log style))))
3496             (if (string-null? result)
3497                 ;; If no glyph name can be found, select default heads.
3498                 ;; Though this usually means an unsupported style has been
3499                 ;; chosen, it also prevents unrelated 'style settings from
3500                 ;; other grobs (e.g., TextSpanner and TimeSignature) leaking
3501                 ;; into markup.
3502                 (get-glyph-name font
3503                                 (get-glyph-name-candidates
3504                                  (sign dir) log 'default))
3505                 result)))
3506          (head-glyph (ly:font-get-glyph font head-glyph-name))
3507          (ancient-flags? (or (eq? style 'mensural) (eq? style 'neomensural)))
3508          (attach-indices (ly:note-head::stem-attachment font head-glyph-name))
3509          (stem-length (* size-factor (max 3 (- log 1))))
3510          ;; With ancient-flags we want a tighter stem
3511          (stem-thickness (* size-factor (if ancient-flags? 0.1 0.13)))
3512          (stemy (* dir stem-length))
3513          (attach-off (cons (interval-index
3514                             (ly:stencil-extent head-glyph X)
3515                             (* (sign dir) (car attach-indices)))
3516                            ;; fixme, this is inconsistent between X & Y.
3517                            (* (sign dir)
3518                               (interval-index
3519                                (ly:stencil-extent head-glyph Y)
3520                                (cdr attach-indices)))))
3521          ;; For a tighter stem (with ancient-flags) the stem-width has to be
3522          ;; adjusted.
3523          (stem-X-corr (if ancient-flags? (* 0.5 dir stem-thickness) 0))
3524          (stem-glyph (and (> log 0)
3525                           (ly:round-filled-box
3526                            (ordered-cons (+ stem-X-corr (car attach-off))
3527                                          (+ stem-X-corr (car attach-off)
3528                                             (* (- (sign dir)) stem-thickness)))
3529                            (cons (min stemy (cdr attach-off))
3530                                  (max stemy (cdr attach-off)))
3531                            (/ stem-thickness 3))))
3532          (dot (ly:font-get-glyph font "dots.dot"))
3533          (dotwid (interval-length (ly:stencil-extent dot X)))
3534          (dots (and (> dot-count 0)
3535                     (apply ly:stencil-add
3536                            (map (lambda (x)
3537                                   (ly:stencil-translate-axis
3538                                    dot (* 2 x dotwid) X))
3539                                 (iota dot-count)))))
3540          ;; Straight-flags. Values taken from /scm/flag-style.scm
3541          (modern-straight-flag (straight-flag-mrkp 0.55 1 -18 1.1 22 1.2 dir))
3542          (old-straight-flag (straight-flag-mrkp 0.55 1 -45 1.2 45 1.4 dir))
3543          (flat-flag (straight-flag-mrkp 0.55 1.0 0 1.0 0 1.0 dir))
3544          ;; Calculate a corrective to avoid a gap between
3545          ;; straight-flags and the stem.
3546          (flag-style-Y-corr (if (or (eq? flag-style 'modern-straight-flag)
3547                                     (eq? flag-style 'old-straight-flag)
3548                                     (eq? flag-style 'flat-flag))
3549                                 (/ blot 10 (* -1 dir))
3550                                 0))
3551          (flaggl (and (> log 2)
3552                       (ly:stencil-translate
3553                        (cond ((eq? flag-style 'modern-straight-flag)
3554                               modern-straight-flag)
3555                              ((eq? flag-style 'old-straight-flag)
3556                               old-straight-flag)
3557                              ((eq? flag-style 'flat-flag)
3558                               flat-flag)
3559                              (else
3560                               (ly:font-get-glyph font
3561                                                  (format #f (if ancient-flags?
3562                                                                 "flags.mensural~a2~a"
3563                                                                 "flags.~a~a")
3564                                                          (if (> dir 0) "u" "d")
3565                                                          log))))
3566                        (cons (+ (car attach-off)
3567                                 ;; For tighter stems (with ancient-flags) the
3568                                 ;; flag has to be adjusted different.
3569                                 (if (and (not ancient-flags?) (< dir 0))
3570                                     stem-thickness
3571                                     0))
3572                              (+ stemy flag-style-Y-corr))))))
3573
3574     ;; If there is a flag on an upstem and the stem is short, move the dots
3575     ;; to avoid the flag.  16th notes get a special case because their flags
3576     ;; hang lower than any other flags.
3577     ;; Not with ancient flags or straight-flags.
3578     (if (and dots (> dir 0) (> log 2)
3579              (or (eq? flag-style 'default) (null? flag-style))
3580              (not ancient-flags?)
3581              (or (< dir 1.15) (and (= log 4) (< dir 1.3))))
3582         (set! dots (ly:stencil-translate-axis dots 0.5 X)))
3583     (if flaggl
3584         (set! stem-glyph (ly:stencil-add flaggl stem-glyph)))
3585     (if (ly:stencil? stem-glyph)
3586         (set! stem-glyph (ly:stencil-add stem-glyph head-glyph))
3587         (set! stem-glyph head-glyph))
3588     (if (ly:stencil? dots)
3589         (set! stem-glyph
3590               (ly:stencil-add
3591                (ly:stencil-translate-axis
3592                 dots
3593                 (+ (cdr (ly:stencil-extent head-glyph X)) dotwid)
3594                 X)
3595                stem-glyph)))
3596     stem-glyph))
3597
3598 (define-public log2
3599   (let ((divisor (log 2)))
3600     (lambda (z) (inexact->exact (/ (log z) divisor)))))
3601
3602 (define (parse-simple-duration duration-string)
3603   "Parse the `duration-string', e.g. ''4..'' or ''breve.'',
3604 and return a (log dots) list."
3605   (let ((match (regexp-exec (make-regexp "(breve|longa|maxima|[0-9]+)(\\.*)")
3606                             duration-string)))
3607     (if (and match (string=? duration-string (match:substring match 0)))
3608         (let ((len (match:substring match 1))
3609               (dots (match:substring match 2)))
3610           (list (cond ((string=? len "breve") -1)
3611                       ((string=? len "longa") -2)
3612                       ((string=? len "maxima") -3)
3613                       (else (log2 (string->number len))))
3614                 (if dots (string-length dots) 0)))
3615         (ly:error (_ "not a valid duration string: ~a") duration-string))))
3616
3617 (define-markup-command (note layout props duration dir)
3618   (string? number?)
3619   #:category music
3620   #:properties (note-by-number-markup)
3621   "
3622 @cindex notes within text by string
3623
3624 This produces a note with a stem pointing in @var{dir} direction, with
3625 the @var{duration} for the note head type and augmentation dots.  For
3626 example, @code{\\note #\"4.\" #-0.75} creates a dotted quarter note, with
3627 a shortened down stem.
3628
3629 @lilypond[verbatim,quote]
3630 \\markup {
3631   \\override #'(style . cross) {
3632     \\note #\"4..\" #UP
3633   }
3634   \\hspace #2
3635   \\note #\"breve\" #0
3636 }
3637 @end lilypond"
3638   (let ((parsed (parse-simple-duration duration)))
3639     (note-by-number-markup layout props (car parsed) (cadr parsed) dir)))
3640
3641 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3642 ;; the rest command.
3643 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3644
3645 (define-markup-command (rest-by-number layout props log dot-count)
3646   (number? number?)
3647   #:category music
3648   #:properties ((font-size 0)
3649                 (style '())
3650                 (multi-measure-rest #f))
3651   "
3652 @cindex rests or multi-measure-rests within text by log and dot-count
3653
3654 A rest or multi-measure-rest symbol.
3655
3656 @lilypond[verbatim,quote]
3657 \\markup {
3658   \\rest-by-number #3 #2
3659   \\hspace #2
3660   \\rest-by-number #0 #1
3661   \\hspace #2
3662   \\override #'(multi-measure-rest . #t)
3663   \\rest-by-number #0 #0
3664 }
3665 @end lilypond"
3666
3667   (define (get-glyph-name-candidates log style)
3668     (let* (;; Choose the style-string to be added.
3669            ;; If no glyph exists, select others for the specified styles
3670            ;; otherwise defaulting.
3671            (style-strg
3672             (cond (
3673                    ;; 'baroque needs to be special-cased, otherwise
3674                    ;; `select-head-glyphĀ“ would catch neomensural-glyphs for
3675                    ;; this style, if (< log 0).
3676                    (eq? style 'baroque)
3677                    (string-append (number->string log) ""))
3678                   ((eq? style 'petrucci)
3679                    (string-append (number->string log) "mensural"))
3680                   ;; In other cases `select-head-glyphĀ“ from output-lib.scm
3681                   ;; works for rest-glyphs, too.
3682                   ((and (symbol? style) (not (eq? style 'default)))
3683                    (select-head-glyph style log))
3684                   (else log)))
3685            ;; Choose ledgered glyphs for whole and half rest.
3686            ;; Except for the specified styles, logs and MultiMeasureRests.
3687            (ledger-style-rests
3688             (if (and (or (list? style)
3689                          (not (member style
3690                                       '(neomensural mensural petrucci))))
3691                      (not multi-measure-rest)
3692                      (or (= log 0) (= log 1)))
3693                 "o"
3694                 "")))
3695       (format #f "rests.~a~a" style-strg ledger-style-rests)))
3696
3697   (define (get-glyph-name font cands)
3698     (if (ly:stencil-empty? (ly:font-get-glyph font cands))
3699         ""
3700         cands))
3701
3702   (let* ((font
3703           (ly:paper-get-font layout
3704                              (cons '((font-encoding . fetaMusic)) props)))
3705          (rest-glyph-name
3706           (let ((result
3707                  (get-glyph-name font
3708                                  (get-glyph-name-candidates log style))))
3709             (if (string-null? result)
3710                 ;; If no glyph name can be found, select default rests.  Though
3711                 ;; this usually means an unsupported style has been chosen, it
3712                 ;; also prevents unrelated 'style settings from other grobs
3713                 ;; (e.g., TextSpanner and TimeSignature) leaking into markup.
3714                 (get-glyph-name font (get-glyph-name-candidates log 'default))
3715                 result)))
3716          (rest-glyph (ly:font-get-glyph font rest-glyph-name))
3717          (dot (ly:font-get-glyph font "dots.dot"))
3718          (dot-width (interval-length (ly:stencil-extent dot X)))
3719          (dots (and (> dot-count 0)
3720                     (apply ly:stencil-add
3721                            (map (lambda (x)
3722                                   (ly:stencil-translate-axis
3723                                    dot (* 2 x dot-width) X))
3724                                 (iota dot-count))))))
3725
3726     ;; Apart from mensural-, neomensural- and petrucci-style ledgered
3727     ;; glyphs are taken for whole and half rests.
3728     ;; If they are dotted, move the dots in X-direction to avoid collision.
3729     (if (and dots
3730              (< log 2)
3731              (>= log 0)
3732              (not (member style '(neomensural mensural petrucci))))
3733         (set! dots (ly:stencil-translate-axis dots dot-width X)))
3734
3735     ;; Add dots to the rest-glyph.
3736     ;;
3737     ;; Not sure how to vertical align dots.
3738     ;; For now the dots are centered for half, whole or longer rests.
3739     ;; Otherwise placed near the top of the rest.
3740     ;;
3741     ;; Dots for rests with (< log 0) dots are allowed, but not
3742     ;; if multi-measure-rest is set #t.
3743     (if (and (not multi-measure-rest) dots)
3744         (set! rest-glyph
3745               (ly:stencil-add
3746                (ly:stencil-translate
3747                 dots
3748                 (cons
3749                  (+ (cdr (ly:stencil-extent rest-glyph X)) dot-width)
3750                  (if (< log 2)
3751                      (interval-center (ly:stencil-extent rest-glyph Y))
3752                      (- (interval-end (ly:stencil-extent rest-glyph Y))
3753                         (/ (* 2 dot-width) 3)))))
3754                rest-glyph)))
3755     rest-glyph))
3756
3757 (define-markup-command (rest layout props duration)
3758   (string?)
3759   #:category music
3760   #:properties ((style '())
3761                 (multi-measure-rest #f)
3762                 (multi-measure-rest-number #t)
3763                 (word-space 0.6))
3764   "
3765 @cindex rests or multi-measure-rests within text by string
3766
3767 This produces a rest, with the @var{duration} for the rest type and
3768 augmentation dots.
3769 @code{\"breve\"}, @code{\"longa\"} and @code{\"maxima\"} are valid
3770 input-strings.
3771
3772 Printing MultiMeasureRests could be enabled with
3773 @code{\\override #'(multi-measure-rest . #t)}
3774 If MultiMeasureRests are taken, the MultiMeasureRestNumber is printed above.
3775 This is enabled for all styles using default-glyphs.
3776 Could be disabled with @code{\\override #'(multi-measure-rest-number . #f)}
3777
3778 @lilypond[verbatim,quote]
3779 \\markup {
3780   \\rest #\"4..\"
3781   \\hspace #2
3782   \\rest #\"breve\"
3783   \\hspace #2
3784   \\override #'(multi-measure-rest . #t)
3785   {
3786   \\rest #\"7\"
3787   \\hspace #2
3788   \\override #'(multi-measure-rest-number . #f)
3789   \\rest #\"7\"
3790   }
3791 }
3792 @end lilypond"
3793   ;; Get the number of mmr-glyphs.
3794   ;; Store them in a list.
3795   ;; example: (mmr-numbers 25) -> '(3 0 0 1)
3796   (define (mmr-numbers nmbr)
3797     (let* ((8-bar-glyph (floor (/ nmbr 8)))
3798            (8-remainder (remainder nmbr 8))
3799            (4-bar-glyph (floor (/ 8-remainder 4)))
3800            (4-remainder (remainder nmbr 4))
3801            (2-bar-glyph (floor (/ 4-remainder 2)))
3802            (2-remainder (remainder 4-remainder 2))
3803            (1-bar-glyph (floor (/ 2-remainder 1))))
3804       (list 8-bar-glyph 4-bar-glyph 2-bar-glyph 1-bar-glyph)))
3805
3806   ;; Get the correct mmr-glyphs.
3807   ;; Store them in a list.
3808   ;; example:
3809   ;; (get-mmr-glyphs '(1 0 1 0) '("rests.M3" "rests.M2" "rests.M1" "rests.0"))
3810   ;; -> ("rests.M3" "rests.M1")
3811   (define (get-mmr-glyphs lst1 lst2)
3812     (define (helper l1 l2 l3)
3813       (if (null? l1)
3814           (reverse l3)
3815           (helper (cdr l1)
3816                   (cdr l2)
3817                   (append (make-list (car l1) (car l2)) l3))))
3818     (helper lst1 lst2 '()))
3819
3820   ;; If duration is not valid, print a warning and return empty-stencil
3821   (if (or (and (not (integer? (car (parse-simple-duration duration))))
3822                (not multi-measure-rest))
3823           (and (= (string-length (car (string-split duration #\. ))) 1)
3824                (= (string->number (car (string-split duration #\. ))) 0)))
3825       (begin
3826         (ly:warning (_ "not a valid duration string: ~a - ignoring") duration)
3827         empty-stencil)
3828       (let* (
3829              ;; For simple rests:
3830              ;; Get a (log dots) list.
3831              (parsed (parse-simple-duration duration))
3832              ;; Create the rest-stencil
3833              (stil
3834               (rest-by-number-markup layout props (car parsed) (cadr parsed)))
3835              ;; For MultiMeasureRests:
3836              ;; Get the duration-part of duration
3837              (dur-part-string (car (string-split duration #\. )))
3838              ;; Get the duration of MMR:
3839              ;; If not a number (eg. "maxima") calculate it.
3840              (mmr-duration
3841               (or (string->number dur-part-string) (expt 2 (abs (car parsed)))))
3842              ;; Get a list of the correct number of each mmr-glyph.
3843              (count-mmr-glyphs-list (mmr-numbers mmr-duration))
3844              ;; Create a list of mmr-stencils,
3845              ;; translating the glyph for a whole rest.
3846              (mmr-stils-list
3847               (map
3848                (lambda (x)
3849                  (let ((single-mmr-stil
3850                         (rest-by-number-markup layout props (* -1 x) 0)))
3851                    (if (= x 0)
3852                        (ly:stencil-translate-axis
3853                         single-mmr-stil
3854                         ;; Ugh, hard-coded, why 1?
3855                         1
3856                         Y)
3857                        single-mmr-stil)))
3858                (get-mmr-glyphs count-mmr-glyphs-list (reverse (iota 4)))))
3859              ;; Adjust the space between the mmr-glyphs,
3860              ;; if not default-glyphs are used.
3861              (word-space (if (member style
3862                                      '(neomensural mensural petrucci))
3863                              (/ (* word-space 2) 3)
3864                              word-space))
3865              ;; Create the final mmr-stencil
3866              ;; via `stack-stencil-lineĀ“ from /scm/markup.scm
3867              (mmr-stil (stack-stencil-line word-space mmr-stils-list)))
3868
3869         ;; Print the number above a multi-measure-rest
3870         ;; Depends on duration, style and multi-measure-rest-number set #t
3871         (if (and multi-measure-rest
3872                  multi-measure-rest-number
3873                  (> mmr-duration 1)
3874                  (not (member style '(neomensural mensural petrucci))))
3875             (let* ((mmr-stil-x-center
3876                     (interval-center (ly:stencil-extent mmr-stil X)))
3877                    (duration-markup
3878                     (markup
3879                      #:fontsize -2
3880                      #:override '(font-encoding . fetaText)
3881                      (number->string mmr-duration)))
3882                    (mmr-number-stil
3883                     (interpret-markup layout props duration-markup))
3884                    (mmr-number-stil-x-center
3885                     (interval-center (ly:stencil-extent mmr-number-stil X))))
3886
3887               (set! mmr-stil (ly:stencil-combine-at-edge
3888                               mmr-stil
3889                               Y UP
3890                               (ly:stencil-translate-axis
3891                                mmr-number-stil
3892                                (- mmr-stil-x-center mmr-number-stil-x-center)
3893                                X)
3894                               ;; Ugh, hardcoded
3895                               0.8))))
3896         (if multi-measure-rest
3897             mmr-stil
3898             stil))))
3899
3900 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3901 ;; fermata markup
3902 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3903
3904 (define-markup-command (fermata layout props) ()
3905   #:category music
3906   #:properties ((direction UP))
3907   "Create a fermata glyph.  When @var{direction} is @code{DOWN}, use
3908 an inverted glyph.  Note that within music, one would usually use the
3909 @code{\\fermata} articulation instead of a markup.
3910
3911 @lilypond[verbatim,quote]
3912  { c''1^\\markup \\fermata d''1_\\markup \\fermata }
3913
3914 \\markup { \\fermata \\override #`(direction . ,DOWN) \\fermata }
3915 @end lilypond
3916 "
3917   (interpret-markup layout props
3918                     (if (eqv? direction DOWN)
3919                         (markup #:musicglyph "scripts.dfermata")
3920                         (markup #:musicglyph "scripts.ufermata"))))
3921
3922 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3923 ;; translating.
3924 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3925
3926 (define-markup-command (lower layout props amount arg)
3927   (number? markup?)
3928   #:category align
3929   "
3930 @cindex lowering text
3931
3932 Lower @var{arg} by the distance @var{amount}.
3933 A negative @var{amount} indicates raising; see also @code{\\raise}.
3934
3935 @lilypond[verbatim,quote]
3936 \\markup {
3937   one
3938   \\lower #3
3939   two
3940   three
3941 }
3942 @end lilypond"
3943   (ly:stencil-translate-axis (interpret-markup layout props arg)
3944                              (- amount) Y))
3945
3946 (define-markup-command (translate-scaled layout props offset arg)
3947   (number-pair? markup?)
3948   #:category align
3949   #:properties ((font-size 0))
3950   "
3951 @cindex translating text
3952 @cindex scaling text
3953
3954 Translate @var{arg} by @var{offset}, scaling the offset by the
3955 @code{font-size}.
3956
3957 @lilypond[verbatim,quote]
3958 \\markup {
3959   \\fontsize #5 {
3960     * \\translate #'(2 . 3) translate
3961     \\hspace #2
3962     * \\translate-scaled #'(2 . 3) translate-scaled
3963   }
3964 }
3965 @end lilypond"
3966   (let* ((factor (magstep font-size))
3967          (scaled (cons (* factor (car offset))
3968                        (* factor (cdr offset)))))
3969     (ly:stencil-translate (interpret-markup layout props arg)
3970                           scaled)))
3971
3972 (define-markup-command (raise layout props amount arg)
3973   (number? markup?)
3974   #:category align
3975   "
3976 @cindex raising text
3977
3978 Raise @var{arg} by the distance @var{amount}.
3979 A negative @var{amount} indicates lowering, see also @code{\\lower}.
3980
3981 The argument to @code{\\raise} is the vertical displacement amount,
3982 measured in (global) staff spaces.  @code{\\raise} and @code{\\super}
3983 raise objects in relation to their surrounding markups.
3984
3985 If the text object itself is positioned above or below the staff, then
3986 @code{\\raise} cannot be used to move it, since the mechanism that
3987 positions it next to the staff cancels any shift made with
3988 @code{\\raise}.  For vertical positioning, use the @code{padding}
3989 and/or @code{extra-offset} properties.
3990
3991 @lilypond[verbatim,quote]
3992 \\markup {
3993   C
3994   \\small
3995   \\bold
3996   \\raise #1.0
3997   9/7+
3998 }
3999 @end lilypond"
4000   (ly:stencil-translate-axis (interpret-markup layout props arg) amount Y))
4001
4002 (define-markup-command (fraction layout props arg1 arg2)
4003   (markup? markup?)
4004   #:category other
4005   #:properties ((font-size 0))
4006   "
4007 @cindex creating text fractions
4008
4009 Make a fraction of two markups.
4010 @lilypond[verbatim,quote]
4011 \\markup {
4012   Ļ€ ā‰ˆ
4013   \\fraction 355 113
4014 }
4015 @end lilypond"
4016   (let* ((m1 (interpret-markup layout props arg1))
4017          (m2 (interpret-markup layout props arg2))
4018          (factor (magstep font-size))
4019          (boxdimen (cons (* factor -0.05) (* factor 0.05)))
4020          (padding (* factor 0.2))
4021          (baseline (* factor 0.6))
4022          (offset (* factor 0.75)))
4023     (set! m1 (ly:stencil-aligned-to m1 X CENTER))
4024     (set! m2 (ly:stencil-aligned-to m2 X CENTER))
4025     (let* ((x1 (ly:stencil-extent m1 X))
4026            (x2 (ly:stencil-extent m2 X))
4027            (line (ly:round-filled-box (interval-union x1 x2) boxdimen 0.0))
4028            ;; should stack mols separately, to maintain LINE on baseline
4029            (stack (stack-lines DOWN padding baseline (list m1 line m2))))
4030       (set! stack
4031             (ly:stencil-aligned-to stack Y CENTER))
4032       (set! stack
4033             (ly:stencil-aligned-to stack X LEFT))
4034       ;; should have EX dimension
4035       ;; empirical anyway
4036       (ly:stencil-translate-axis stack offset Y))))
4037
4038 (define-markup-command (normal-size-super layout props arg)
4039   (markup?)
4040   #:category font
4041   #:properties ((font-size 0))
4042   "
4043 @cindex setting superscript in standard font size
4044
4045 Set @var{arg} in superscript with a normal font size.
4046
4047 @lilypond[verbatim,quote]
4048 \\markup {
4049   default
4050   \\normal-size-super {
4051     superscript in standard size
4052   }
4053 }
4054 @end lilypond"
4055   (ly:stencil-translate-axis
4056    (interpret-markup layout props arg)
4057    (* 1.0 (magstep font-size)) Y))
4058
4059 (define-markup-command (super layout props arg)
4060   (markup?)
4061   #:category font
4062   #:properties ((font-size 0))
4063   "
4064 @cindex superscript text
4065
4066 Set @var{arg} in superscript.
4067
4068 @lilypond[verbatim,quote]
4069 \\markup {
4070   E =
4071   \\concat {
4072     mc
4073     \\super
4074     2
4075   }
4076 }
4077 @end lilypond"
4078   (ly:stencil-translate-axis
4079    (interpret-markup
4080     layout
4081     (cons `((font-size . ,(- font-size 3))) props)
4082     arg)
4083    (* 1.0 (magstep font-size)) ; original font-size
4084    Y))
4085
4086 (define-markup-command (translate layout props offset arg)
4087   (number-pair? markup?)
4088   #:category align
4089   "
4090 @cindex translating text
4091
4092 Translate @var{arg} relative to its surroundings.  @var{offset}
4093 is a pair of numbers representing the displacement in the X and Y axis.
4094
4095 @lilypond[verbatim,quote]
4096 \\markup {
4097   *
4098   \\translate #'(2 . 3)
4099   \\line { translated two spaces right, three up }
4100 }
4101 @end lilypond"
4102   (ly:stencil-translate (interpret-markup layout props arg)
4103                         offset))
4104
4105 (define-markup-command (sub layout props arg)
4106   (markup?)
4107   #:category font
4108   #:properties ((font-size 0))
4109   "
4110 @cindex subscript text
4111
4112 Set @var{arg} in subscript.
4113
4114 @lilypond[verbatim,quote]
4115 \\markup {
4116   \\concat {
4117     H
4118     \\sub {
4119       2
4120     }
4121     O
4122   }
4123 }
4124 @end lilypond"
4125   (ly:stencil-translate-axis
4126    (interpret-markup
4127     layout
4128     (cons `((font-size . ,(- font-size 3))) props)
4129     arg)
4130    (* -0.75 (magstep font-size)) ; original font-size
4131    Y))
4132
4133 (define-markup-command (normal-size-sub layout props arg)
4134   (markup?)
4135   #:category font
4136   #:properties ((font-size 0))
4137   "
4138 @cindex setting subscript in standard font size
4139
4140 Set @var{arg} in subscript with a normal font size.
4141
4142 @lilypond[verbatim,quote]
4143 \\markup {
4144   default
4145   \\normal-size-sub {
4146     subscript in standard size
4147   }
4148 }
4149 @end lilypond"
4150   (ly:stencil-translate-axis
4151    (interpret-markup layout props arg)
4152    (* -0.75 (magstep font-size))
4153    Y))
4154
4155 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
4156 ;; brackets.
4157 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
4158
4159 (define-markup-command (hbracket layout props arg)
4160   (markup?)
4161   #:category graphic
4162   "
4163 @cindex placing horizontal brackets around text
4164
4165 Draw horizontal brackets around @var{arg}.
4166
4167 @lilypond[verbatim,quote]
4168 \\markup {
4169   \\hbracket {
4170     \\line {
4171       one two three
4172     }
4173   }
4174 }
4175 @end lilypond"
4176   (let ((th 0.1) ;; todo: take from GROB.
4177         (m (interpret-markup layout props arg)))
4178     (bracketify-stencil m X th (* 2.5 th) th)))
4179
4180 (define-markup-command (bracket layout props arg)
4181   (markup?)
4182   #:category graphic
4183   "
4184 @cindex placing vertical brackets around text
4185
4186 Draw vertical brackets around @var{arg}.
4187
4188 @lilypond[verbatim,quote]
4189 \\markup {
4190   \\bracket {
4191     \\note #\"2.\" #UP
4192   }
4193 }
4194 @end lilypond"
4195   (let ((th 0.1) ;; todo: take from GROB.
4196         (m (interpret-markup layout props arg)))
4197     (bracketify-stencil m Y th (* 2.5 th) th)))
4198
4199 (define-markup-command (parenthesize layout props arg)
4200   (markup?)
4201   #:category graphic
4202   #:properties ((angularity 0)
4203                 (padding)
4204                 (size 1)
4205                 (thickness 1)
4206                 (width 0.25))
4207   "
4208 @cindex placing parentheses around text
4209
4210 Draw parentheses around @var{arg}.  This is useful for parenthesizing
4211 a column containing several lines of text.
4212
4213 @lilypond[verbatim,quote]
4214 \\markup {
4215   \\line {
4216     \\parenthesize {
4217       \\column {
4218         foo
4219         bar
4220       }
4221     }
4222     \\override #'(angularity . 2) {
4223       \\parenthesize {
4224         \\column {
4225           bah
4226           baz
4227         }
4228       }
4229     }
4230   }
4231 }
4232 @end lilypond"
4233   (let* ((m (interpret-markup layout props arg))
4234          (scaled-width (* size width))
4235          (scaled-thickness
4236           (* (chain-assoc-get 'line-thickness props 0.1)
4237              thickness))
4238          (half-thickness
4239           (min (* size 0.5 scaled-thickness)
4240                (* (/ 4 3.0) scaled-width)))
4241          (padding (chain-assoc-get 'padding props half-thickness)))
4242     (parenthesize-stencil
4243      m half-thickness scaled-width angularity padding)))
4244
4245
4246 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
4247 ;; Delayed markup evaluation
4248 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
4249
4250 (define-markup-command (page-ref layout props label gauge default)
4251   (symbol? markup? markup?)
4252   #:category other
4253   "
4254 @cindex referencing page numbers in text
4255
4256 Reference to a page number.  @var{label} is the label set on the referenced
4257 page (using the @code{\\label} command), @var{gauge} a markup used to estimate
4258 the maximum width of the page number, and @var{default} the value to display
4259 when @var{label} is not found.
4260
4261 (If the current book or bookpart is set to use roman numerals for page numbers,
4262 the reference will be formatted accordingly -- in which case the @var{gauge}'s
4263 width may require additional tweaking.)"
4264   (let* ((gauge-stencil (interpret-markup layout props gauge))
4265          (x-ext (ly:stencil-extent gauge-stencil X))
4266          (y-ext (ly:stencil-extent gauge-stencil Y)))
4267    (ly:stencil-add
4268     (make-transparent-box-stencil x-ext y-ext))
4269     (ly:make-stencil
4270      `(delay-stencil-evaluation
4271        ,(delay (ly:stencil-expr
4272                 (let* ((table (ly:output-def-lookup layout 'label-page-table))
4273                        (page-number (if (list? table)
4274                                         (assoc-get label table)
4275                                         #f))
4276                        (number-type (ly:output-def-lookup layout 'page-number-type))
4277                        (page-markup (if page-number
4278                                         (number-format number-type page-number)
4279                                         default))
4280                        (page-stencil (interpret-markup layout props page-markup))
4281                        (gap (- (interval-length x-ext)
4282                                (interval-length (ly:stencil-extent page-stencil X)))))
4283                   (interpret-markup layout props
4284                                     (markup #:hspace gap page-markup))))))
4285      x-ext
4286      y-ext)))
4287
4288 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
4289 ;; scaling
4290 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
4291
4292 (define-markup-command (scale layout props factor-pair arg)
4293   (number-pair? markup?)
4294   #:category graphic
4295   "
4296 @cindex scaling markup
4297 @cindex mirroring markup
4298
4299 Scale @var{arg}.  @var{factor-pair} is a pair of numbers
4300 representing the scaling-factor in the X and Y axes.
4301 Negative values may be used to produce mirror images.
4302
4303 @lilypond[verbatim,quote]
4304 \\markup {
4305   \\line {
4306     \\scale #'(2 . 1)
4307     stretched
4308     \\scale #'(1 . -1)
4309     mirrored
4310   }
4311 }
4312 @end lilypond"
4313   (let ((stil (interpret-markup layout props arg))
4314         (sx (car factor-pair))
4315         (sy (cdr factor-pair)))
4316     (ly:stencil-scale stil sx sy)))
4317
4318 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
4319 ;; Repeating
4320 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
4321
4322 (define-markup-command (pattern layout props count axis space pattern)
4323   (integer? integer? number? markup?)
4324   #:category other
4325   "
4326 Prints @var{count} times a @var{pattern} markup.
4327 Patterns are spaced apart by @var{space}.
4328 Patterns are distributed on @var{axis}.
4329
4330 @lilypond[verbatim, quote]
4331 \\markup \\column {
4332   \"Horizontally repeated :\"
4333   \\pattern #7 #X #2 \\flat
4334   \\null
4335   \"Vertically repeated :\"
4336   \\pattern #3 #Y #0.5 \\flat
4337 }
4338 @end lilypond"
4339   (let ((pattern-width (interval-length
4340                         (ly:stencil-extent (interpret-markup layout props pattern) X)))
4341         (new-props (prepend-alist-chain 'word-space 0 (prepend-alist-chain 'baseline-skip 0 props))))
4342     (let loop ((i (1- count)) (patterns (markup)))
4343       (if (zero? i)
4344           (interpret-markup
4345            layout
4346            new-props
4347            (if (= axis X)
4348                (markup patterns pattern)
4349                (markup #:column (patterns pattern))))
4350           (loop (1- i)
4351                 (if (= axis X)
4352                     (markup patterns pattern #:hspace space)
4353                     (markup #:column (patterns pattern #:vspace space))))))))
4354
4355 (define-markup-command (fill-with-pattern layout props space dir pattern left right)
4356   (number? ly:dir? markup? markup? markup?)
4357   #:category align
4358   #:properties ((word-space)
4359                 (line-width))
4360   "
4361 Put @var{left} and @var{right} in a horizontal line of width @code{line-width}
4362 with a line of markups @var{pattern} in between.
4363 Patterns are spaced apart by @var{space}.
4364 Patterns are aligned to the @var{dir} markup.
4365
4366 @lilypond[verbatim, quote]
4367 \\markup \\column {
4368   \"right-aligned :\"
4369   \\fill-with-pattern #1 #RIGHT . first right
4370   \\fill-with-pattern #1 #RIGHT . second right
4371   \\null
4372   \"center-aligned :\"
4373   \\fill-with-pattern #1.5 #CENTER - left right
4374   \\null
4375   \"left-aligned :\"
4376   \\override #'(line-width . 50)
4377   \\fill-with-pattern #2 #LEFT : left first
4378   \\override #'(line-width . 50)
4379   \\fill-with-pattern #2 #LEFT : left second
4380 }
4381 @end lilypond"
4382   (let* ((pattern-x-extent (ly:stencil-extent (interpret-markup layout props pattern) X))
4383          (pattern-width (interval-length pattern-x-extent))
4384          (left-width (interval-length (ly:stencil-extent (interpret-markup layout props left) X)))
4385          (right-width (interval-length (ly:stencil-extent (interpret-markup layout props right) X)))
4386          (middle-width (max 0 (- line-width (+ (+ left-width right-width) (* word-space 2)))))
4387          (period (+ space pattern-width))
4388          (count (truncate (/ (- middle-width pattern-width) period)))
4389          (x-offset (+ (* (- (- middle-width (* count period)) pattern-width) (/ (1+ dir) 2)) (abs (car pattern-x-extent)))))
4390     (interpret-markup layout props
4391                       (markup left
4392                               #:with-dimensions (cons 0 middle-width) '(0 . 0)
4393                               #:translate (cons x-offset 0)
4394                               #:pattern (1+ count) X space pattern
4395                               right))))
4396
4397 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
4398 ;; Replacements
4399 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
4400
4401 (define-markup-command (replace layout props replacements arg)
4402   (list? markup?)
4403   #:category font
4404   "
4405 Used to automatically replace a string by another in the markup @var{arg}.
4406 Each pair of the alist @var{replacements} specifies what should be replaced.
4407 The @code{key} is the string to be replaced by the @code{value} string.
4408
4409 @lilypond[verbatim, quote]
4410 \\markup \\replace #'((\"thx\" . \"Thanks!\")) thx
4411 @end lilypond"
4412   (interpret-markup
4413    layout
4414    (internal-add-text-replacements
4415     props
4416     replacements)
4417    (markup arg)))
4418
4419 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
4420 ;; Markup list commands
4421 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
4422
4423 (define-public (space-lines baseline stils)
4424   (let space-stil ((stils stils)
4425                    (result (list)))
4426     (if (null? stils)
4427         (reverse! result)
4428         (let* ((stil (car stils))
4429                (dy-top (max (- (/ baseline 1.5)
4430                                (interval-bound (ly:stencil-extent stil Y) UP))
4431                             0.0))
4432                (dy-bottom (max (+ (/ baseline 3.0)
4433                                   (interval-bound (ly:stencil-extent stil Y) DOWN))
4434                                0.0))
4435                (new-stil (ly:make-stencil
4436                           (ly:stencil-expr stil)
4437                           (ly:stencil-extent stil X)
4438                           (cons (- (interval-bound (ly:stencil-extent stil Y) DOWN)
4439                                    dy-bottom)
4440                                 (+ (interval-bound (ly:stencil-extent stil Y) UP)
4441                                    dy-top)))))
4442           (space-stil (cdr stils) (cons new-stil result))))))
4443
4444 (define-markup-list-command (justified-lines layout props args)
4445   (markup-list?)
4446   #:properties ((baseline-skip)
4447                 wordwrap-internal-markup-list)
4448   "
4449 @cindex justifying lines of text
4450
4451 Like @code{\\justify}, but return a list of lines instead of a single markup.
4452 Use @code{\\override-lines #'(line-width . @var{X})} to set the line width;
4453 @var{X}@tie{}is the number of staff spaces."
4454   (space-lines baseline-skip
4455                (interpret-markup-list layout props
4456                                       (make-wordwrap-internal-markup-list #t args))))
4457
4458 (define-markup-list-command (wordwrap-lines layout props args)
4459   (markup-list?)
4460   #:properties ((baseline-skip)
4461                 wordwrap-internal-markup-list)
4462   "Like @code{\\wordwrap}, but return a list of lines instead of a single markup.
4463 Use @code{\\override-lines #'(line-width . @var{X})} to set the line width,
4464 where @var{X} is the number of staff spaces."
4465   (space-lines baseline-skip
4466                (interpret-markup-list layout props
4467                                       (make-wordwrap-internal-markup-list #f args))))
4468
4469 (define-markup-list-command (column-lines layout props args)
4470   (markup-list?)
4471   #:properties ((baseline-skip))
4472   "Like @code{\\column}, but return a list of lines instead of a single markup.
4473 @code{baseline-skip} determines the space between each markup in @var{args}."
4474   (space-lines baseline-skip
4475                (interpret-markup-list layout props args)))
4476
4477 (define-markup-list-command (override-lines layout props new-prop args)
4478   (pair? markup-list?)
4479   "Like @code{\\override}, for markup lists."
4480   (interpret-markup-list layout (cons (list new-prop) props) args))
4481
4482 (define-markup-list-command (map-markup-commands layout props compose args)
4483   (procedure? markup-list?)
4484   "This applies the function @var{compose} to every markup in
4485 @var{args} (including elements of markup list command calls) in order
4486 to produce a new markup list.  Since the return value from a markup
4487 list command call is not a markup list but rather a list of stencils,
4488 this requires passing those stencils off as the results of individual
4489 markup calls.  That way, the results should work out as long as no
4490 markups rely on side effects."
4491   (let ((key (make-symbol "key")))
4492     (catch
4493      key
4494      (lambda ()
4495        ;; if `compose' does not actually interpret its markup
4496        ;; argument, we still need to return a list of stencils,
4497        ;; created from the single returned stencil
4498        (list
4499         (interpret-markup layout props
4500                           (compose
4501                            (make-on-the-fly-markup
4502                             (lambda (layout props m)
4503                               ;; here all effects of `compose' on the
4504                               ;; properties should be visible, so we
4505                               ;; call interpret-markup-list at this
4506                               ;; point of time and harvest its
4507                               ;; stencils
4508                               (throw key
4509                                      (interpret-markup-list
4510                                       layout props args)))
4511                             (make-null-markup))))))
4512      (lambda (key stencils)
4513        (map
4514         (lambda (sten)
4515           (interpret-markup layout props
4516                             (compose (make-stencil-markup sten))))
4517         stencils)))))