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