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