]> git.donarmstrong.com Git - lilypond.git/blob - scm/define-markup-commands.scm
Merge remote branch 'origin' into release/unstable
[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
1343     magna aliqua.  Ut enim ad minim veniam, quis nostrud
1344     exercitation ullamco laboris nisi ut aliquip ex ea commodo
1345     consequat.\"
1346 }
1347
1348 \\paper {
1349   bookTitleMarkup = \\markup {
1350     \\column {
1351       \\fill-line { \\fromproperty #'header:title }
1352       \\null
1353       \\wordwrap-field #'header:myText
1354     }
1355   }
1356 }
1357
1358 \\markup {
1359   \\null
1360 }
1361 @end lilypond"
1362   (let* ((m (chain-assoc-get symbol props)))
1363     (if (string? m)
1364         (wordwrap-string-markup layout props m)
1365         empty-stencil)))
1366
1367 (define-markup-command (justify-field layout props symbol)
1368   (symbol?)
1369   #:category align
1370   "Justify the data which has been assigned to @var{symbol}.
1371
1372 @lilypond[verbatim,quote]
1373 \\header {
1374   title = \"My title\"
1375   myText = \"Lorem ipsum dolor sit amet, consectetur adipisicing
1376     elit, sed do eiusmod tempor incididunt ut labore et dolore magna
1377     aliqua.  Ut enim ad minim veniam, quis nostrud exercitation ullamco
1378     laboris nisi ut aliquip ex ea commodo consequat.\"
1379 }
1380
1381 \\paper {
1382   bookTitleMarkup = \\markup {
1383     \\column {
1384       \\fill-line { \\fromproperty #'header:title }
1385       \\null
1386       \\justify-field #'header:myText
1387     }
1388   }
1389 }
1390
1391 \\markup {
1392   \\null
1393 }
1394 @end lilypond"
1395   (let* ((m (chain-assoc-get symbol props)))
1396     (if (string? m)
1397         (justify-string-markup layout props m)
1398         empty-stencil)))
1399
1400 (define-markup-command (combine layout props arg1 arg2)
1401   (markup? markup?)
1402   #:category align
1403   "
1404 @cindex merging text
1405
1406 Print two markups on top of each other.
1407
1408 Note: @code{\\combine} cannot take a list of markups enclosed in
1409 curly braces as an argument; the follow example will not compile:
1410
1411 @example
1412 \\combine @{ a list @}
1413 @end example
1414
1415 @lilypond[verbatim,quote]
1416 \\markup {
1417   \\fontsize #5
1418   \\override #'(thickness . 2)
1419   \\combine
1420     \\draw-line #'(0 . 4)
1421     \\arrow-head #Y #DOWN ##f
1422 }
1423 @end lilypond"
1424   (let* ((s1 (interpret-markup layout props arg1))
1425          (s2 (interpret-markup layout props arg2)))
1426     (ly:stencil-add s1 s2)))
1427
1428 ;;
1429 ;; TODO: should extract baseline-skip from each argument somehow..
1430 ;;
1431 (define-markup-command (column layout props args)
1432   (markup-list?)
1433   #:category align
1434   #:properties ((baseline-skip))
1435   "
1436 @cindex stacking text in a column
1437
1438 Stack the markups in @var{args} vertically.  The property
1439 @code{baseline-skip} determines the space between markups
1440 in @var{args}.
1441
1442 @lilypond[verbatim,quote]
1443 \\markup {
1444   \\column {
1445     one
1446     two
1447     three
1448   }
1449 }
1450 @end lilypond"
1451   (let ((arg-stencils (interpret-markup-list layout props args)))
1452     (stack-lines -1 0.0 baseline-skip
1453                  (remove ly:stencil-empty? arg-stencils))))
1454
1455 (define-markup-command (dir-column layout props args)
1456   (markup-list?)
1457   #:category align
1458   #:properties ((direction)
1459                 (baseline-skip))
1460   "
1461 @cindex changing direction of text columns
1462
1463 Make a column of @var{args}, going up or down, depending on the
1464 setting of the @code{direction} layout property.
1465
1466 @lilypond[verbatim,quote]
1467 \\markup {
1468   \\override #`(direction . ,UP) {
1469     \\dir-column {
1470       going up
1471     }
1472   }
1473   \\hspace #1
1474   \\dir-column {
1475     going down
1476   }
1477   \\hspace #1
1478   \\override #'(direction . 1) {
1479     \\dir-column {
1480       going up
1481     }
1482   }
1483 }
1484 @end lilypond"
1485   (stack-lines (if (number? direction) direction -1)
1486                0.0
1487                baseline-skip
1488                (interpret-markup-list layout props args)))
1489
1490 (define (general-column align-dir baseline mols)
1491   "Stack @var{mols} vertically, aligned to  @var{align-dir} horizontally."
1492
1493   (let* ((aligned-mols (map (lambda (x) (ly:stencil-aligned-to x X align-dir)) mols))
1494          (stacked-stencil (stack-lines -1 0.0 baseline aligned-mols))
1495          (stacked-extent (ly:stencil-extent stacked-stencil X)))
1496     (ly:stencil-translate-axis stacked-stencil (- (car stacked-extent)) X )))
1497
1498 (define-markup-command (center-column layout props args)
1499   (markup-list?)
1500   #:category align
1501   #:properties ((baseline-skip))
1502   "
1503 @cindex centering a column of text
1504
1505 Put @code{args} in a centered column.
1506
1507 @lilypond[verbatim,quote]
1508 \\markup {
1509   \\center-column {
1510     one
1511     two
1512     three
1513   }
1514 }
1515 @end lilypond"
1516   (general-column CENTER baseline-skip (interpret-markup-list layout props args)))
1517
1518 (define-markup-command (left-column layout props args)
1519   (markup-list?)
1520   #:category align
1521   #:properties ((baseline-skip))
1522  "
1523 @cindex text columns, left-aligned
1524
1525 Put @code{args} in a left-aligned column.
1526
1527 @lilypond[verbatim,quote]
1528 \\markup {
1529   \\left-column {
1530     one
1531     two
1532     three
1533   }
1534 }
1535 @end lilypond"
1536   (general-column LEFT baseline-skip (interpret-markup-list layout props args)))
1537
1538 (define-markup-command (right-column layout props args)
1539   (markup-list?)
1540   #:category align
1541   #:properties ((baseline-skip))
1542  "
1543 @cindex text columns, right-aligned
1544
1545 Put @code{args} in a right-aligned column.
1546
1547 @lilypond[verbatim,quote]
1548 \\markup {
1549   \\right-column {
1550     one
1551     two
1552     three
1553   }
1554 }
1555 @end lilypond"
1556   (general-column RIGHT baseline-skip (interpret-markup-list layout props args)))
1557
1558 (define-markup-command (vcenter layout props arg)
1559   (markup?)
1560   #:category align
1561   "
1562 @cindex vertically centering text
1563
1564 Align @code{arg} to its Y@tie{}center.
1565
1566 @lilypond[verbatim,quote]
1567 \\markup {
1568   one
1569   \\vcenter
1570   two
1571   three
1572 }
1573 @end lilypond"
1574   (let* ((mol (interpret-markup layout props arg)))
1575     (ly:stencil-aligned-to mol Y CENTER)))
1576
1577 (define-markup-command (center-align layout props arg)
1578   (markup?)
1579   #:category align
1580   "
1581 @cindex horizontally centering text
1582
1583 Align @code{arg} to its X@tie{}center.
1584
1585 @lilypond[verbatim,quote]
1586 \\markup {
1587   \\column {
1588     one
1589     \\center-align
1590     two
1591     three
1592   }
1593 }
1594 @end lilypond"
1595   (let* ((mol (interpret-markup layout props arg)))
1596     (ly:stencil-aligned-to mol X CENTER)))
1597
1598 (define-markup-command (right-align layout props arg)
1599   (markup?)
1600   #:category align
1601   "
1602 @cindex right aligning text
1603
1604 Align @var{arg} on its right edge.
1605
1606 @lilypond[verbatim,quote]
1607 \\markup {
1608   \\column {
1609     one
1610     \\right-align
1611     two
1612     three
1613   }
1614 }
1615 @end lilypond"
1616   (let* ((m (interpret-markup layout props arg)))
1617     (ly:stencil-aligned-to m X RIGHT)))
1618
1619 (define-markup-command (left-align layout props arg)
1620   (markup?)
1621   #:category align
1622   "
1623 @cindex left aligning text
1624
1625 Align @var{arg} on its left edge.
1626
1627 @lilypond[verbatim,quote]
1628 \\markup {
1629   \\column {
1630     one
1631     \\left-align
1632     two
1633     three
1634   }
1635 }
1636 @end lilypond"
1637   (let* ((m (interpret-markup layout props arg)))
1638     (ly:stencil-aligned-to m X LEFT)))
1639
1640 (define-markup-command (general-align layout props axis dir arg)
1641   (integer? number? markup?)
1642   #:category align
1643   "
1644 @cindex controlling general text alignment
1645
1646 Align @var{arg} in @var{axis} direction to the @var{dir} side.
1647
1648 @lilypond[verbatim,quote]
1649 \\markup {
1650   \\column {
1651     one
1652     \\general-align #X #LEFT
1653     two
1654     three
1655     \\null
1656     one
1657     \\general-align #X #CENTER
1658     two
1659     three
1660     \\null
1661     \\line {
1662       one
1663       \\general-align #Y #UP
1664       two
1665       three
1666     }
1667     \\null
1668     \\line {
1669       one
1670       \\general-align #Y #3.2
1671       two
1672       three
1673     }
1674   }
1675 }
1676 @end lilypond"
1677   (let* ((m (interpret-markup layout props arg)))
1678     (ly:stencil-aligned-to m axis dir)))
1679
1680 (define-markup-command (halign layout props dir arg)
1681   (number? markup?)
1682   #:category align
1683   "
1684 @cindex setting horizontal text alignment
1685
1686 Set horizontal alignment.  If @var{dir} is @w{@code{-1}}, then it is
1687 left-aligned, while @code{+1} is right.  Values in between interpolate
1688 alignment accordingly.
1689
1690 @lilypond[verbatim,quote]
1691 \\markup {
1692   \\column {
1693     one
1694     \\halign #LEFT
1695     two
1696     three
1697     \\null
1698     one
1699     \\halign #CENTER
1700     two
1701     three
1702     \\null
1703     one
1704     \\halign #RIGHT
1705     two
1706     three
1707     \\null
1708     one
1709     \\halign #-5
1710     two
1711     three
1712   }
1713 }
1714 @end lilypond"
1715   (let* ((m (interpret-markup layout props arg)))
1716     (ly:stencil-aligned-to m X dir)))
1717
1718 (define-markup-command (with-dimensions layout props x y arg)
1719   (number-pair? number-pair? markup?)
1720   #:category other
1721   "
1722 @cindex setting extent of text objects
1723
1724 Set the dimensions of @var{arg} to @var{x} and@tie{}@var{y}."
1725   (let* ((m (interpret-markup layout props arg)))
1726     (ly:make-stencil (ly:stencil-expr m) x y)))
1727
1728 (define-markup-command (pad-around layout props amount arg)
1729   (number? markup?)
1730   #:category align
1731   "Add padding @var{amount} all around @var{arg}.
1732
1733 @lilypond[verbatim,quote]
1734 \\markup {
1735   \\box {
1736     default
1737   }
1738   \\hspace #2
1739   \\box {
1740     \\pad-around #0.5 {
1741       padded
1742     }
1743   }
1744 }
1745 @end lilypond"
1746   (let* ((m (interpret-markup layout props arg))
1747          (x (ly:stencil-extent m X))
1748          (y (ly:stencil-extent m Y)))
1749     (ly:make-stencil (ly:stencil-expr m)
1750                      (interval-widen x amount)
1751                      (interval-widen y amount))))
1752
1753 (define-markup-command (pad-x layout props amount arg)
1754   (number? markup?)
1755   #:category align
1756   "
1757 @cindex padding text horizontally
1758
1759 Add padding @var{amount} around @var{arg} in the X@tie{}direction.
1760
1761 @lilypond[verbatim,quote]
1762 \\markup {
1763   \\box {
1764     default
1765   }
1766   \\hspace #4
1767   \\box {
1768     \\pad-x #2 {
1769       padded
1770     }
1771   }
1772 }
1773 @end lilypond"
1774   (let* ((m (interpret-markup layout props arg))
1775          (x (ly:stencil-extent m X))
1776          (y (ly:stencil-extent m Y)))
1777     (ly:make-stencil (ly:stencil-expr m)
1778                      (interval-widen x amount)
1779                      y)))
1780
1781 (define-markup-command (put-adjacent layout props axis dir arg1 arg2)
1782   (integer? ly:dir? markup? markup?)
1783   #:category align
1784   "Put @var{arg2} next to @var{arg1}, without moving @var{arg1}."
1785   (let ((m1 (interpret-markup layout props arg1))
1786         (m2 (interpret-markup layout props arg2)))
1787     (ly:stencil-combine-at-edge m1 axis dir m2 0.0)))
1788
1789 (define-markup-command (transparent layout props arg)
1790   (markup?)
1791   #:category other
1792   "Make @var{arg} transparent.
1793
1794 @lilypond[verbatim,quote]
1795 \\markup {
1796   \\transparent {
1797     invisible text
1798   }
1799 }
1800 @end lilypond"
1801   (let* ((m (interpret-markup layout props arg))
1802          (x (ly:stencil-extent m X))
1803          (y (ly:stencil-extent m Y)))
1804     (ly:make-stencil "" x y)))
1805
1806 (define-markup-command (pad-to-box layout props x-ext y-ext arg)
1807   (number-pair? number-pair? markup?)
1808   #:category align
1809   "Make @var{arg} take at least @var{x-ext}, @var{y-ext} space.
1810
1811 @lilypond[verbatim,quote]
1812 \\markup {
1813   \\box {
1814     default
1815   }
1816   \\hspace #4
1817   \\box {
1818     \\pad-to-box #'(0 . 10) #'(0 . 3) {
1819       padded
1820     }
1821   }
1822 }
1823 @end lilypond"
1824   (let* ((m (interpret-markup layout props arg))
1825          (x (ly:stencil-extent m X))
1826          (y (ly:stencil-extent m Y)))
1827     (ly:make-stencil (ly:stencil-expr m)
1828                      (interval-union x-ext x)
1829                      (interval-union y-ext y))))
1830
1831 (define-markup-command (hcenter-in layout props length arg)
1832   (number? markup?)
1833   #:category align
1834   "Center @var{arg} horizontally within a box of extending
1835 @var{length}/2 to the left and right.
1836
1837 @lilypond[quote,verbatim]
1838 \\new StaffGroup <<
1839   \\new Staff {
1840     \\set Staff.instrumentName = \\markup {
1841       \\hcenter-in #12
1842       Oboe
1843     }
1844     c''1
1845   }
1846   \\new Staff {
1847     \\set Staff.instrumentName = \\markup {
1848       \\hcenter-in #12
1849       Bassoon
1850     }
1851     \\clef tenor
1852     c'1
1853   }
1854 >>
1855 @end lilypond"
1856   (interpret-markup layout props
1857                     (make-pad-to-box-markup
1858                      (cons (/ length -2) (/ length 2))
1859                      '(0 . 0)
1860                      (make-center-align-markup arg))))
1861
1862 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1863 ;; property
1864 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1865
1866 (define-markup-command (fromproperty layout props symbol)
1867   (symbol?)
1868   #:category other
1869   "Read the @var{symbol} from property settings, and produce a stencil
1870 from the markup contained within.  If @var{symbol} is not defined, it
1871 returns an empty markup.
1872
1873 @lilypond[verbatim,quote]
1874 \\header {
1875   myTitle = \"myTitle\"
1876   title = \\markup {
1877     from
1878     \\italic
1879     \\fromproperty #'header:myTitle
1880   }
1881 }
1882 \\markup {
1883   \\null
1884 }
1885 @end lilypond"
1886   (let ((m (chain-assoc-get symbol props)))
1887     (if (markup? m)
1888         (interpret-markup layout props m)
1889         empty-stencil)))
1890
1891 (define-markup-command (on-the-fly layout props procedure arg)
1892   (symbol? markup?)
1893   #:category other
1894   "Apply the @var{procedure} markup command to @var{arg}.
1895 @var{procedure} should take a single argument."
1896   (let ((anonymous-with-signature (lambda (layout props arg) (procedure layout props arg))))
1897     (set-object-property! anonymous-with-signature
1898                           'markup-signature
1899                           (list markup?))
1900     (interpret-markup layout props (list anonymous-with-signature arg))))
1901
1902 (define-markup-command (footnote layout props mkup note)
1903   (markup? markup?)
1904   #:category other
1905   "Have footnote @var{note} act as an annotation to the markup @var{mkup}.
1906
1907 @lilypond[verbatim,quote]
1908 \\markup {
1909   \\auto-footnote a b
1910   \\override #'(padding . 0.2)
1911   \\auto-footnote c d
1912 }
1913 @end lilypond
1914 The footnote will not be annotated automatically."
1915   (ly:stencil-combine-at-edge
1916     (interpret-markup layout props mkup)
1917     X
1918     RIGHT
1919     (ly:make-stencil
1920       `(footnote (gensym "footnote") #f ,(interpret-markup layout props note))
1921       '(0 . 0)
1922       '(0 . 0))
1923     0.0))
1924
1925 (define-markup-command (auto-footnote layout props mkup note)
1926   (markup? markup?)
1927   #:category other
1928   #:properties ((raise 0.5)
1929                 (padding 0.0))
1930   "Have footnote @var{note} act as an annotation to the markup @var{mkup}.
1931
1932 @lilypond[verbatim,quote]
1933 \\markup {
1934   \\auto-footnote a b
1935   \\override #'(padding . 0.2)
1936   \\auto-footnote c d
1937 }
1938 @end lilypond
1939 The footnote will be annotated automatically."
1940   (let* ((markup-stencil (interpret-markup layout props mkup))
1941          (auto-numbering (ly:output-def-lookup layout
1942                                                'footnote-auto-numbering))
1943          (footnote-hash (gensym "footnote"))
1944          (stencil-seed 0)
1945          (gauge-stencil (if auto-numbering
1946                             (interpret-markup
1947                               layout
1948                               props
1949                               ((ly:output-def-lookup
1950                                  layout
1951                                  'footnote-numbering-function)
1952                                 stencil-seed))
1953                             empty-stencil))
1954          (x-ext (if auto-numbering
1955                     (ly:stencil-extent gauge-stencil X)
1956                     '(0 . 0)))
1957          (y-ext (if auto-numbering
1958                     (ly:stencil-extent gauge-stencil Y)
1959                     '(0 . 0)))
1960          (footnote-number
1961            (if auto-numbering
1962              `(delay-stencil-evaluation
1963                 ,(delay
1964                   (ly:stencil-expr
1965                     (let* ((table
1966                             (ly:output-def-lookup layout
1967                                                   'number-footnote-table))
1968                            (footnote-stencil (if (list? table)
1969                                                  (assoc-get footnote-hash
1970                                                             table)
1971                                                  empty-stencil))
1972                            (footnote-stencil (if (ly:stencil? footnote-stencil)
1973                                                  footnote-stencil
1974                                                  (begin
1975                                                    (ly:programming-error
1976 "Cannot find correct footnote for a markup object.")
1977                                                    empty-stencil)))
1978                            (gap (- (interval-length x-ext)
1979                                    (interval-length
1980                                      (ly:stencil-extent footnote-stencil X))))
1981                            (y-trans (- (+ (cdr y-ext)
1982                                           raise)
1983                                        (cdr (ly:stencil-extent footnote-stencil
1984                                                                Y)))))
1985                       (ly:stencil-translate footnote-stencil
1986                                             (cons gap y-trans))))))
1987              '()))
1988          (main-stencil (ly:stencil-combine-at-edge
1989                          markup-stencil
1990                          X
1991                          RIGHT
1992                          (ly:make-stencil footnote-number x-ext y-ext)
1993                          padding)))
1994   (ly:stencil-add
1995     main-stencil
1996     (ly:make-stencil
1997       `(footnote ,footnote-hash #t ,(interpret-markup layout props note))
1998       '(0 . 0)
1999       '(0 . 0)))))
2000
2001 (define-markup-command (override layout props new-prop arg)
2002   (pair? markup?)
2003   #:category other
2004   "
2005 @cindex overriding properties within text markup
2006
2007 Add the argument @var{new-prop} to the property list.  Properties
2008 may be any property supported by @rinternals{font-interface},
2009 @rinternals{text-interface} and
2010 @rinternals{instrument-specific-markup-interface}.
2011
2012 @lilypond[verbatim,quote]
2013 \\markup {
2014   \\line {
2015     \\column {
2016       default
2017       baseline-skip
2018     }
2019     \\hspace #2
2020     \\override #'(baseline-skip . 4) {
2021       \\column {
2022         increased
2023         baseline-skip
2024       }
2025     }
2026   }
2027 }
2028 @end lilypond"
2029   (interpret-markup layout (cons (list new-prop) props) arg))
2030
2031 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2032 ;; files
2033 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2034
2035 (define-markup-command (verbatim-file layout props name)
2036   (string?)
2037   #:category other
2038   "Read the contents of file @var{name}, and include it verbatim.
2039
2040 @lilypond[verbatim,quote]
2041 \\markup {
2042   \\verbatim-file #\"simple.ly\"
2043 }
2044 @end lilypond"
2045   (interpret-markup layout props
2046                     (if  (ly:get-option 'safe)
2047                          "verbatim-file disabled in safe mode"
2048                          (let* ((str (ly:gulp-file name))
2049                                 (lines (string-split str #\nl)))
2050                            (make-typewriter-markup
2051                             (make-column-markup lines))))))
2052
2053 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2054 ;; fonts.
2055 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2056
2057
2058 (define-markup-command (smaller layout props arg)
2059   (markup?)
2060   #:category font
2061   "Decrease the font size relative to the current setting.
2062
2063 @lilypond[verbatim,quote]
2064 \\markup {
2065   \\fontsize #3.5 {
2066     some large text
2067     \\hspace #2
2068     \\smaller {
2069       a bit smaller
2070     }
2071     \\hspace #2
2072     more large text
2073   }
2074 }
2075 @end lilypond"
2076   (interpret-markup layout props
2077    `(,fontsize-markup -1 ,arg)))
2078
2079 (define-markup-command (larger layout props arg)
2080   (markup?)
2081   #:category font
2082   "Increase the font size relative to the current setting.
2083
2084 @lilypond[verbatim,quote]
2085 \\markup {
2086   default
2087   \\hspace #2
2088   \\larger
2089   larger
2090 }
2091 @end lilypond"
2092   (interpret-markup layout props
2093    `(,fontsize-markup 1 ,arg)))
2094
2095 (define-markup-command (finger layout props arg)
2096   (markup?)
2097   #:category font
2098   "Set @var{arg} as small numbers.
2099
2100 @lilypond[verbatim,quote]
2101 \\markup {
2102   \\finger {
2103     1 2 3 4 5
2104   }
2105 }
2106 @end lilypond"
2107   (interpret-markup layout
2108                     (cons '((font-size . -5) (font-encoding . fetaText)) props)
2109                     arg))
2110
2111 (define-markup-command (abs-fontsize layout props size arg)
2112   (number? markup?)
2113   #:category font
2114   "Use @var{size} as the absolute font size to display @var{arg}.
2115 Adjusts @code{baseline-skip} and @code{word-space} accordingly.
2116
2117 @lilypond[verbatim,quote]
2118 \\markup {
2119   default text font size
2120   \\hspace #2
2121   \\abs-fontsize #16 { text font size 16 }
2122   \\hspace #2
2123   \\abs-fontsize #12 { text font size 12 }
2124 }
2125 @end lilypond"
2126   (let* ((ref-size (ly:output-def-lookup layout 'text-font-size 12))
2127          (text-props (list (ly:output-def-lookup layout 'text-font-defaults)))
2128          (ref-word-space (chain-assoc-get 'word-space text-props 0.6))
2129          (ref-baseline (chain-assoc-get 'baseline-skip text-props 3))
2130          (magnification (/ size ref-size)))
2131     (interpret-markup layout
2132                       (cons `((baseline-skip . ,(* magnification ref-baseline))
2133                               (word-space . ,(* magnification ref-word-space))
2134                               (font-size . ,(magnification->font-size magnification)))
2135                             props)
2136                       arg)))
2137
2138 (define-markup-command (fontsize layout props increment arg)
2139   (number? markup?)
2140   #:category font
2141   #:properties ((font-size 0)
2142                 (word-space 1)
2143                 (baseline-skip 2))
2144   "Add @var{increment} to the font-size.  Adjusts @code{baseline-skip}
2145 accordingly.
2146
2147 @lilypond[verbatim,quote]
2148 \\markup {
2149   default
2150   \\hspace #2
2151   \\fontsize #-1.5
2152   smaller
2153 }
2154 @end lilypond"
2155   (let ((entries (list
2156                   (cons 'baseline-skip (* baseline-skip (magstep increment)))
2157                   (cons 'word-space (* word-space (magstep increment)))
2158                   (cons 'font-size (+ font-size increment)))))
2159     (interpret-markup layout (cons entries props) arg)))
2160
2161 (define-markup-command (magnify layout props sz arg)
2162   (number? markup?)
2163   #:category font
2164   "
2165 @cindex magnifying text
2166
2167 Set the font magnification for its argument.  In the following
2168 example, the middle@tie{}A is 10% larger:
2169
2170 @example
2171 A \\magnify #1.1 @{ A @} A
2172 @end example
2173
2174 Note: Magnification only works if a font name is explicitly selected.
2175 Use @code{\\fontsize} otherwise.
2176
2177 @lilypond[verbatim,quote]
2178 \\markup {
2179   default
2180   \\hspace #2
2181   \\magnify #1.5 {
2182     50% larger
2183   }
2184 }
2185 @end lilypond"
2186   (interpret-markup
2187    layout
2188    (prepend-alist-chain 'font-size (magnification->font-size sz) props)
2189    arg))
2190
2191 (define-markup-command (bold layout props arg)
2192   (markup?)
2193   #:category font
2194   "Switch to bold font-series.
2195
2196 @lilypond[verbatim,quote]
2197 \\markup {
2198   default
2199   \\hspace #2
2200   \\bold
2201   bold
2202 }
2203 @end lilypond"
2204   (interpret-markup layout (prepend-alist-chain 'font-series 'bold props) arg))
2205
2206 (define-markup-command (sans layout props arg)
2207   (markup?)
2208   #:category font
2209   "Switch to the sans serif font family.
2210
2211 @lilypond[verbatim,quote]
2212 \\markup {
2213   default
2214   \\hspace #2
2215   \\sans {
2216     sans serif
2217   }
2218 }
2219 @end lilypond"
2220   (interpret-markup layout (prepend-alist-chain 'font-family 'sans props) arg))
2221
2222 (define-markup-command (number layout props arg)
2223   (markup?)
2224   #:category font
2225   "Set font family to @code{number}, which yields the font used for
2226 time signatures and fingerings.  This font contains numbers and
2227 some punctuation; it has no letters.
2228
2229 @lilypond[verbatim,quote]
2230 \\markup {
2231   \\number {
2232     0 1 2 3 4 5 6 7 8 9 . ,
2233   }
2234 }
2235 @end lilypond"
2236   (interpret-markup layout (prepend-alist-chain 'font-encoding 'fetaText props) arg))
2237
2238 (define-markup-command (roman layout props arg)
2239   (markup?)
2240   #:category font
2241   "Set font family to @code{roman}.
2242
2243 @lilypond[verbatim,quote]
2244 \\markup {
2245   \\sans \\bold {
2246     sans serif, bold
2247     \\hspace #2
2248     \\roman {
2249       text in roman font family
2250     }
2251     \\hspace #2
2252     return to sans
2253   }
2254 }
2255 @end lilypond"
2256   (interpret-markup layout (prepend-alist-chain 'font-family 'roman props) arg))
2257
2258 (define-markup-command (huge layout props arg)
2259   (markup?)
2260   #:category font
2261   "Set font size to +2.
2262
2263 @lilypond[verbatim,quote]
2264 \\markup {
2265   default
2266   \\hspace #2
2267   \\huge
2268   huge
2269 }
2270 @end lilypond"
2271   (interpret-markup layout (prepend-alist-chain 'font-size 2 props) arg))
2272
2273 (define-markup-command (large layout props arg)
2274   (markup?)
2275   #:category font
2276   "Set font size to +1.
2277
2278 @lilypond[verbatim,quote]
2279 \\markup {
2280   default
2281   \\hspace #2
2282   \\large
2283   large
2284 }
2285 @end lilypond"
2286   (interpret-markup layout (prepend-alist-chain 'font-size 1 props) arg))
2287
2288 (define-markup-command (normalsize layout props arg)
2289   (markup?)
2290   #:category font
2291   "Set font size to default.
2292
2293 @lilypond[verbatim,quote]
2294 \\markup {
2295   \\teeny {
2296     this is very small
2297     \\hspace #2
2298     \\normalsize {
2299       normal size
2300     }
2301     \\hspace #2
2302     teeny again
2303   }
2304 }
2305 @end lilypond"
2306   (interpret-markup layout (prepend-alist-chain 'font-size 0 props) arg))
2307
2308 (define-markup-command (small layout props arg)
2309   (markup?)
2310   #:category font
2311   "Set font size to -1.
2312
2313 @lilypond[verbatim,quote]
2314 \\markup {
2315   default
2316   \\hspace #2
2317   \\small
2318   small
2319 }
2320 @end lilypond"
2321   (interpret-markup layout (prepend-alist-chain 'font-size -1 props) arg))
2322
2323 (define-markup-command (tiny layout props arg)
2324   (markup?)
2325   #:category font
2326   "Set font size to -2.
2327
2328 @lilypond[verbatim,quote]
2329 \\markup {
2330   default
2331   \\hspace #2
2332   \\tiny
2333   tiny
2334 }
2335 @end lilypond"
2336   (interpret-markup layout (prepend-alist-chain 'font-size -2 props) arg))
2337
2338 (define-markup-command (teeny layout props arg)
2339   (markup?)
2340   #:category font
2341   "Set font size to -3.
2342
2343 @lilypond[verbatim,quote]
2344 \\markup {
2345   default
2346   \\hspace #2
2347   \\teeny
2348   teeny
2349 }
2350 @end lilypond"
2351   (interpret-markup layout (prepend-alist-chain 'font-size -3 props) arg))
2352
2353 (define-markup-command (fontCaps layout props arg)
2354   (markup?)
2355   #:category font
2356   "Set @code{font-shape} to @code{caps}
2357
2358 Note: @code{\\fontCaps} requires the installation and selection of
2359 fonts which support the @code{caps} font shape."
2360   (interpret-markup layout (prepend-alist-chain 'font-shape 'caps props) arg))
2361
2362 ;; Poor man's caps
2363 (define-markup-command (smallCaps layout props arg)
2364   (markup?)
2365   #:category font
2366   "Emit @var{arg} as small caps.
2367
2368 Note: @code{\\smallCaps} does not support accented characters.
2369
2370 @lilypond[verbatim,quote]
2371 \\markup {
2372   default
2373   \\hspace #2
2374   \\smallCaps {
2375     Text in small caps
2376   }
2377 }
2378 @end lilypond"
2379   (define (char-list->markup chars lower)
2380     (let ((final-string (string-upcase (reverse-list->string chars))))
2381       (if lower
2382           (markup #:fontsize -2 final-string)
2383           final-string)))
2384   (define (make-small-caps rest-chars currents current-is-lower prev-result)
2385     (if (null? rest-chars)
2386         (make-concat-markup
2387           (reverse! (cons (char-list->markup currents current-is-lower)
2388                           prev-result)))
2389         (let* ((ch (car rest-chars))
2390                (is-lower (char-lower-case? ch)))
2391           (if (or (and current-is-lower is-lower)
2392                   (and (not current-is-lower) (not is-lower)))
2393               (make-small-caps (cdr rest-chars)
2394                                (cons ch currents)
2395                                is-lower
2396                                prev-result)
2397               (make-small-caps (cdr rest-chars)
2398                                (list ch)
2399                                is-lower
2400                                (if (null? currents)
2401                                    prev-result
2402                                    (cons (char-list->markup
2403                                             currents current-is-lower)
2404                                          prev-result)))))))
2405   (interpret-markup layout props
2406     (if (string? arg)
2407         (make-small-caps (string->list arg) (list) #f (list))
2408         arg)))
2409
2410 (define-markup-command (caps layout props arg)
2411   (markup?)
2412   #:category font
2413   "Copy of the @code{\\smallCaps} command.
2414
2415 @lilypond[verbatim,quote]
2416 \\markup {
2417   default
2418   \\hspace #2
2419   \\caps {
2420     Text in small caps
2421   }
2422 }
2423 @end lilypond"
2424   (interpret-markup layout props (make-smallCaps-markup arg)))
2425
2426 (define-markup-command (dynamic layout props arg)
2427   (markup?)
2428   #:category font
2429   "Use the dynamic font.  This font only contains @b{s}, @b{f}, @b{m},
2430 @b{z}, @b{p}, and @b{r}.  When producing phrases, like
2431 @q{pi@`{u}@tie{}@b{f}}, the normal words (like @q{pi@`{u}}) should be
2432 done in a different font.  The recommended font for this is bold and italic.
2433 @lilypond[verbatim,quote]
2434 \\markup {
2435   \\dynamic {
2436     sfzp
2437   }
2438 }
2439 @end lilypond"
2440   (interpret-markup
2441    layout (prepend-alist-chain 'font-encoding 'fetaText props) arg))
2442
2443 (define-markup-command (text layout props arg)
2444   (markup?)
2445   #:category font
2446   "Use a text font instead of music symbol or music alphabet font.
2447
2448 @lilypond[verbatim,quote]
2449 \\markup {
2450   \\number {
2451     1, 2,
2452     \\text {
2453       three, four,
2454     }
2455     5
2456   }
2457 }
2458 @end lilypond"
2459
2460   ;; ugh - latin1
2461   (interpret-markup layout (prepend-alist-chain 'font-encoding 'latin1 props)
2462                     arg))
2463
2464 (define-markup-command (italic layout props arg)
2465   (markup?)
2466   #:category font
2467   "Use italic @code{font-shape} for @var{arg}.
2468
2469 @lilypond[verbatim,quote]
2470 \\markup {
2471   default
2472   \\hspace #2
2473   \\italic
2474   italic
2475 }
2476 @end lilypond"
2477   (interpret-markup layout (prepend-alist-chain 'font-shape 'italic props) arg))
2478
2479 (define-markup-command (typewriter layout props arg)
2480   (markup?)
2481   #:category font
2482   "Use @code{font-family} typewriter for @var{arg}.
2483
2484 @lilypond[verbatim,quote]
2485 \\markup {
2486   default
2487   \\hspace #2
2488   \\typewriter
2489   typewriter
2490 }
2491 @end lilypond"
2492   (interpret-markup
2493    layout (prepend-alist-chain 'font-family 'typewriter props) arg))
2494
2495 (define-markup-command (upright layout props arg)
2496   (markup?)
2497   #:category font
2498   "Set @code{font-shape} to @code{upright}.  This is the opposite
2499 of @code{italic}.
2500
2501 @lilypond[verbatim,quote]
2502 \\markup {
2503   \\italic {
2504     italic text
2505     \\hspace #2
2506     \\upright {
2507       upright text
2508     }
2509     \\hspace #2
2510     italic again
2511   }
2512 }
2513 @end lilypond"
2514   (interpret-markup
2515    layout (prepend-alist-chain 'font-shape 'upright props) arg))
2516
2517 (define-markup-command (medium layout props arg)
2518   (markup?)
2519   #:category font
2520   "Switch to medium font-series (in contrast to bold).
2521
2522 @lilypond[verbatim,quote]
2523 \\markup {
2524   \\bold {
2525     some bold text
2526     \\hspace #2
2527     \\medium {
2528       medium font series
2529     }
2530     \\hspace #2
2531     bold again
2532   }
2533 }
2534 @end lilypond"
2535   (interpret-markup layout (prepend-alist-chain 'font-series 'medium props)
2536                     arg))
2537
2538 (define-markup-command (normal-text layout props arg)
2539   (markup?)
2540   #:category font
2541   "Set all font related properties (except the size) to get the default
2542 normal text font, no matter what font was used earlier.
2543
2544 @lilypond[verbatim,quote]
2545 \\markup {
2546   \\huge \\bold \\sans \\caps {
2547     huge bold sans caps
2548     \\hspace #2
2549     \\normal-text {
2550       huge normal
2551     }
2552     \\hspace #2
2553     as before
2554   }
2555 }
2556 @end lilypond"
2557   ;; ugh - latin1
2558   (interpret-markup layout
2559                     (cons '((font-family . roman) (font-shape . upright)
2560                             (font-series . medium) (font-encoding . latin1))
2561                           props)
2562                     arg))
2563
2564 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2565 ;; symbols.
2566 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2567
2568 (define-markup-command (musicglyph layout props glyph-name)
2569   (string?)
2570   #:category music
2571   "@var{glyph-name} is converted to a musical symbol; for example,
2572 @code{\\musicglyph #\"accidentals.natural\"} selects the natural sign from
2573 the music font.  See @ruser{The Feta font} for a complete listing of
2574 the possible glyphs.
2575
2576 @lilypond[verbatim,quote]
2577 \\markup {
2578   \\musicglyph #\"f\"
2579   \\musicglyph #\"rests.2\"
2580   \\musicglyph #\"clefs.G_change\"
2581 }
2582 @end lilypond"
2583   (let* ((font (ly:paper-get-font layout
2584                                   (cons '((font-encoding . fetaMusic)
2585                                           (font-name . #f))
2586
2587                                                  props)))
2588          (glyph (ly:font-get-glyph font glyph-name)))
2589     (if (null? (ly:stencil-expr glyph))
2590         (ly:warning (_ "Cannot find glyph ~a") glyph-name))
2591
2592     glyph))
2593
2594 (define-markup-command (doublesharp layout props)
2595   ()
2596   #:category music
2597   "Draw a double sharp symbol.
2598
2599 @lilypond[verbatim,quote]
2600 \\markup {
2601   \\doublesharp
2602 }
2603 @end lilypond"
2604   (interpret-markup layout props (markup #:musicglyph (assoc-get 1 standard-alteration-glyph-name-alist ""))))
2605
2606 (define-markup-command (sesquisharp layout props)
2607   ()
2608   #:category music
2609   "Draw a 3/2 sharp symbol.
2610
2611 @lilypond[verbatim,quote]
2612 \\markup {
2613   \\sesquisharp
2614 }
2615 @end lilypond"
2616   (interpret-markup layout props (markup #:musicglyph (assoc-get 3/4 standard-alteration-glyph-name-alist ""))))
2617
2618 (define-markup-command (sharp layout props)
2619   ()
2620   #:category music
2621   "Draw a sharp symbol.
2622
2623 @lilypond[verbatim,quote]
2624 \\markup {
2625   \\sharp
2626 }
2627 @end lilypond"
2628   (interpret-markup layout props (markup #:musicglyph (assoc-get 1/2 standard-alteration-glyph-name-alist ""))))
2629
2630 (define-markup-command (semisharp layout props)
2631   ()
2632   #:category music
2633   "Draw a semisharp symbol.
2634
2635 @lilypond[verbatim,quote]
2636 \\markup {
2637   \\semisharp
2638 }
2639 @end lilypond"
2640   (interpret-markup layout props (markup #:musicglyph (assoc-get 1/4 standard-alteration-glyph-name-alist ""))))
2641
2642 (define-markup-command (natural layout props)
2643   ()
2644   #:category music
2645   "Draw a natural symbol.
2646
2647 @lilypond[verbatim,quote]
2648 \\markup {
2649   \\natural
2650 }
2651 @end lilypond"
2652   (interpret-markup layout props (markup #:musicglyph (assoc-get 0 standard-alteration-glyph-name-alist ""))))
2653
2654 (define-markup-command (semiflat layout props)
2655   ()
2656   #:category music
2657   "Draw a semiflat symbol.
2658
2659 @lilypond[verbatim,quote]
2660 \\markup {
2661   \\semiflat
2662 }
2663 @end lilypond"
2664   (interpret-markup layout props (markup #:musicglyph (assoc-get -1/4 standard-alteration-glyph-name-alist ""))))
2665
2666 (define-markup-command (flat layout props)
2667   ()
2668   #:category music
2669   "Draw a flat symbol.
2670
2671 @lilypond[verbatim,quote]
2672 \\markup {
2673   \\flat
2674 }
2675 @end lilypond"
2676   (interpret-markup layout props (markup #:musicglyph (assoc-get -1/2 standard-alteration-glyph-name-alist ""))))
2677
2678 (define-markup-command (sesquiflat layout props)
2679   ()
2680   #:category music
2681   "Draw a 3/2 flat symbol.
2682
2683 @lilypond[verbatim,quote]
2684 \\markup {
2685   \\sesquiflat
2686 }
2687 @end lilypond"
2688   (interpret-markup layout props (markup #:musicglyph (assoc-get -3/4 standard-alteration-glyph-name-alist ""))))
2689
2690 (define-markup-command (doubleflat layout props)
2691   ()
2692   #:category music
2693   "Draw a double flat symbol.
2694
2695 @lilypond[verbatim,quote]
2696 \\markup {
2697   \\doubleflat
2698 }
2699 @end lilypond"
2700   (interpret-markup layout props (markup #:musicglyph (assoc-get -1 standard-alteration-glyph-name-alist ""))))
2701
2702 (define-markup-command (with-color layout props color arg)
2703   (color? markup?)
2704   #:category other
2705   "
2706 @cindex coloring text
2707
2708 Draw @var{arg} in color specified by @var{color}.
2709
2710 @lilypond[verbatim,quote]
2711 \\markup {
2712   \\with-color #red
2713   red
2714   \\hspace #2
2715   \\with-color #green
2716   green
2717   \\hspace #2
2718   \\with-color #blue
2719   blue
2720 }
2721 @end lilypond"
2722   (let ((stil (interpret-markup layout props arg)))
2723     (ly:make-stencil (list 'color color (ly:stencil-expr stil))
2724                      (ly:stencil-extent stil X)
2725                      (ly:stencil-extent stil Y))))
2726
2727 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2728 ;; glyphs
2729 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2730
2731 (define-markup-command (arrow-head layout props axis dir filled)
2732   (integer? ly:dir? boolean?)
2733   #:category graphic
2734   "Produce an arrow head in specified direction and axis.
2735 Use the filled head if @var{filled} is specified.
2736 @lilypond[verbatim,quote]
2737 \\markup {
2738   \\fontsize #5 {
2739     \\general-align #Y #DOWN {
2740       \\arrow-head #Y #UP ##t
2741       \\arrow-head #Y #DOWN ##f
2742       \\hspace #2
2743       \\arrow-head #X #RIGHT ##f
2744       \\arrow-head #X #LEFT ##f
2745     }
2746   }
2747 }
2748 @end lilypond"
2749   (let*
2750       ((name (format #f "arrowheads.~a.~a~a"
2751                      (if filled
2752                          "close"
2753                          "open")
2754                      axis
2755                      dir)))
2756     (ly:font-get-glyph
2757      (ly:paper-get-font layout (cons '((font-encoding . fetaMusic))
2758                                      props))
2759      name)))
2760
2761 (define-markup-command (lookup layout props glyph-name)
2762   (string?)
2763   #:category other
2764   "Lookup a glyph by name.
2765
2766 @lilypond[verbatim,quote]
2767 \\markup {
2768   \\override #'(font-encoding . fetaBraces) {
2769     \\lookup #\"brace200\"
2770     \\hspace #2
2771     \\rotate #180
2772     \\lookup #\"brace180\"
2773   }
2774 }
2775 @end lilypond"
2776   (ly:font-get-glyph (ly:paper-get-font layout props)
2777                      glyph-name))
2778
2779 (define-markup-command (char layout props num)
2780   (integer?)
2781   #:category other
2782   "Produce a single character.  Characters encoded in hexadecimal
2783 format require the prefix @code{#x}.
2784
2785 @lilypond[verbatim,quote]
2786 \\markup {
2787   \\char #65 \\char ##x00a9
2788 }
2789 @end lilypond"
2790   (ly:text-interface::interpret-markup layout props (ly:wide-char->utf-8 num)))
2791
2792 (define number->mark-letter-vector (make-vector 25 #\A))
2793
2794 (do ((i 0 (1+ i))
2795      (j 0 (1+ j)))
2796     ((>= i 26))
2797   (if (= i (- (char->integer #\I) (char->integer #\A)))
2798       (set! i (1+ i)))
2799   (vector-set! number->mark-letter-vector j
2800                (integer->char (+ i (char->integer #\A)))))
2801
2802 (define number->mark-alphabet-vector (list->vector
2803   (map (lambda (i) (integer->char (+ i (char->integer #\A)))) (iota 26))))
2804
2805 (define (number->markletter-string vec n)
2806   "Double letters for big marks."
2807   (let* ((lst (vector-length vec)))
2808
2809     (if (>= n lst)
2810         (string-append (number->markletter-string vec (1- (quotient n lst)))
2811                        (number->markletter-string vec (remainder n lst)))
2812         (make-string 1 (vector-ref vec n)))))
2813
2814 (define-markup-command (markletter layout props num)
2815   (integer?)
2816   #:category other
2817   "Make a markup letter for @var{num}.  The letters start with A to@tie{}Z
2818 (skipping letter@tie{}I), and continue with double letters.
2819
2820 @lilypond[verbatim,quote]
2821 \\markup {
2822   \\markletter #8
2823   \\hspace #2
2824   \\markletter #26
2825 }
2826 @end lilypond"
2827   (ly:text-interface::interpret-markup layout props
2828     (number->markletter-string number->mark-letter-vector num)))
2829
2830 (define-markup-command (markalphabet layout props num)
2831   (integer?)
2832   #:category other
2833    "Make a markup letter for @var{num}.  The letters start with A to@tie{}Z
2834 and continue with double letters.
2835
2836 @lilypond[verbatim,quote]
2837 \\markup {
2838   \\markalphabet #8
2839   \\hspace #2
2840   \\markalphabet #26
2841 }
2842 @end lilypond"
2843    (ly:text-interface::interpret-markup layout props
2844      (number->markletter-string number->mark-alphabet-vector num)))
2845
2846 (define-public (horizontal-slash-interval num forward number-interval mag)
2847   (if forward
2848     (cond ;((= num 6) (interval-widen number-interval (* mag 0.5)))
2849           ;((= num 5) (interval-widen number-interval (* mag 0.5)))
2850           (else (interval-widen number-interval (* mag 0.25))))
2851     (cond ((= num 6) (interval-widen number-interval (* mag 0.5)))
2852           ;((= num 5) (interval-widen number-interval (* mag 0.5)))
2853           (else (interval-widen number-interval (* mag 0.25))))
2854   ))
2855
2856 (define-public (adjust-slash-stencil num forward stencil mag)
2857   (if forward
2858     (cond ((= num 2)
2859               (ly:stencil-translate stencil (cons (* mag -0.00) (* mag 0.2))))
2860           ((= num 3)
2861               (ly:stencil-translate stencil (cons (* mag -0.00) (* mag 0.2))))
2862           ;((= num 5)
2863               ;(ly:stencil-translate stencil (cons (* mag -0.00) (* mag -0.07))))
2864           ;((= num 7)
2865           ;    (ly:stencil-translate stencil (cons (* mag -0.00) (* mag -0.15))))
2866           (else stencil))
2867     (cond ((= num 6)
2868               (ly:stencil-translate stencil (cons (* mag -0.00) (* mag 0.15))))
2869           ;((= num 8)
2870           ;    (ly:stencil-translate stencil (cons (* mag -0.00) (* mag -0.15))))
2871           (else stencil))
2872   )
2873 )
2874
2875 (define (slashed-digit-internal layout props num forward font-size thickness)
2876   (let* ((mag (magstep font-size))
2877          (thickness (* mag
2878                        (ly:output-def-lookup layout 'line-thickness)
2879                        thickness))
2880          ; backward slashes might use slope and point in the other direction!
2881          (dy (* mag (if forward 0.4 -0.4)))
2882          (number-stencil (interpret-markup layout
2883                                            (prepend-alist-chain 'font-encoding 'fetaText props)
2884                                            (number->string num)))
2885          (num-x (horizontal-slash-interval num forward (ly:stencil-extent number-stencil X) mag))
2886          (center (interval-center (ly:stencil-extent number-stencil Y)))
2887          ; Use the real extents of the slash, not the whole number, because we
2888          ; might translate the slash later on!
2889          (num-y (interval-widen (cons center center) (abs dy)))
2890          (is-sane (and (interval-sane? num-x) (interval-sane? num-y)))
2891          (slash-stencil (if is-sane
2892                             (make-line-stencil thickness
2893                                          (car num-x) (- (interval-center num-y) dy)
2894                                          (cdr num-x) (+ (interval-center num-y) dy))
2895                             #f)))
2896     (if (ly:stencil? slash-stencil)
2897       (begin
2898         ; for some numbers we need to shift the slash/backslash up or down to make
2899         ; the slashed digit look better
2900         (set! slash-stencil (adjust-slash-stencil num forward slash-stencil mag))
2901         (set! number-stencil
2902           (ly:stencil-add number-stencil slash-stencil)))
2903       (ly:warning "Unable to create slashed digit ~a" num))
2904     number-stencil))
2905
2906
2907 (define-markup-command (slashed-digit layout props num)
2908   (integer?)
2909   #:category other
2910   #:properties ((font-size 0)
2911                 (thickness 1.6))
2912   "
2913 @cindex slashed digits
2914
2915 A feta number, with slash.  This is for use in the context of
2916 figured bass notation.
2917 @lilypond[verbatim,quote]
2918 \\markup {
2919   \\slashed-digit #5
2920   \\hspace #2
2921   \\override #'(thickness . 3)
2922   \\slashed-digit #7
2923 }
2924 @end lilypond"
2925   (slashed-digit-internal layout props num #t font-size thickness))
2926
2927 (define-markup-command (backslashed-digit layout props num)
2928   (integer?)
2929   #:category other
2930   #:properties ((font-size 0)
2931                 (thickness 1.6))
2932   "
2933 @cindex backslashed digits
2934
2935 A feta number, with backslash.  This is for use in the context of
2936 figured bass notation.
2937 @lilypond[verbatim,quote]
2938 \\markup {
2939   \\backslashed-digit #5
2940   \\hspace #2
2941   \\override #'(thickness . 3)
2942   \\backslashed-digit #7
2943 }
2944 @end lilypond"
2945   (slashed-digit-internal layout props num #f font-size thickness))
2946
2947 ;; eyeglasses
2948 (define eyeglassespath
2949   '((moveto 0.42 0.77)
2950     (rcurveto 0 0.304 -0.246 0.55 -0.55 0.55)
2951     (rcurveto -0.304 0 -0.55 -0.246 -0.55 -0.55)
2952     (rcurveto 0 -0.304 0.246 -0.55 0.55 -0.55)
2953     (rcurveto 0.304 0 0.55 0.246 0.55 0.55)
2954     (closepath)
2955     (moveto 2.07 0.77)
2956     (rcurveto 0 0.304 -0.246 0.55 -0.55 0.55)
2957     (rcurveto -0.304 0 -0.55 -0.246 -0.55 -0.55)
2958     (rcurveto 0 -0.304 0.246 -0.55 0.55 -0.55)
2959     (rcurveto 0.304 0 0.55 0.246 0.55 0.55)
2960     (closepath)
2961     (moveto 1.025 0.935)
2962     (rcurveto 0 0.182 -0.148 0.33 -0.33 0.33)
2963     (rcurveto -0.182 0 -0.33 -0.148 -0.33 -0.33)
2964     (moveto -0.68 0.77)
2965     (rlineto 0.66 1.43)
2966     (rcurveto 0.132 0.286 0.55 0.44 0.385 -0.33)
2967     (moveto 2.07 0.77)
2968     (rlineto 0.66 1.43)
2969     (rcurveto 0.132 0.286 0.55 0.44 0.385 -0.33)))
2970
2971 (define-markup-command (eyeglasses layout props)
2972   ()
2973   #:category other
2974   "Prints out eyeglasses, indicating strongly to look at the conductor.
2975 @lilypond[verbatim,quote]
2976 \\markup { \\eyeglasses }
2977 @end lilypond"
2978   (interpret-markup layout props
2979     (make-override-markup '(line-cap-style . butt)
2980       (make-path-markup 0.15 eyeglassespath))))
2981
2982 (define-markup-command (left-brace layout props size)
2983   (number?)
2984   #:category other
2985   "
2986 A feta brace in point size @var{size}.
2987
2988 @lilypond[verbatim,quote]
2989 \\markup {
2990   \\left-brace #35
2991   \\hspace #2
2992   \\left-brace #45
2993 }
2994 @end lilypond"
2995   (let* ((font (ly:paper-get-font layout
2996                                   (cons '((font-encoding . fetaBraces)
2997                                           (font-name . #f))
2998                                         props)))
2999          (glyph-count (1- (ly:otf-glyph-count font)))
3000          (scale (ly:output-def-lookup layout 'output-scale))
3001          (scaled-size (/ (ly:pt size) scale))
3002          (glyph (lambda (n)
3003                   (ly:font-get-glyph font (string-append "brace"
3004                                                          (number->string n)))))
3005          (get-y-from-brace (lambda (brace)
3006                              (interval-length
3007                               (ly:stencil-extent (glyph brace) Y))))
3008          (find-brace (binary-search 0 glyph-count get-y-from-brace scaled-size))
3009          (glyph-found (glyph find-brace)))
3010
3011     (if (or (null? (ly:stencil-expr glyph-found))
3012             (< scaled-size (interval-length (ly:stencil-extent (glyph 0) Y)))
3013             (> scaled-size (interval-length
3014                             (ly:stencil-extent (glyph glyph-count) Y))))
3015         (begin
3016           (ly:warning (_ "no brace found for point size ~S ") size)
3017           (ly:warning (_ "defaulting to ~S pt")
3018                       (/ (* scale (interval-length
3019                                    (ly:stencil-extent glyph-found Y)))
3020                          (ly:pt 1)))))
3021     glyph-found))
3022
3023 (define-markup-command (right-brace layout props size)
3024   (number?)
3025   #:category other
3026   "
3027 A feta brace in point size @var{size}, rotated 180 degrees.
3028
3029 @lilypond[verbatim,quote]
3030 \\markup {
3031   \\right-brace #45
3032   \\hspace #2
3033   \\right-brace #35
3034 }
3035 @end lilypond"
3036   (interpret-markup layout props (markup #:rotate 180 #:left-brace size)))
3037
3038 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3039 ;; the note command.
3040 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3041
3042 ;; TODO: better syntax.
3043
3044 (define-markup-command (note-by-number layout props log dot-count dir)
3045   (number? number? number?)
3046   #:category music
3047   #:properties ((font-size 0)
3048                 (style '()))
3049   "
3050 @cindex notes within text by log and dot-count
3051
3052 Construct a note symbol, with stem.  By using fractional values for
3053 @var{dir}, longer or shorter stems can be obtained.
3054
3055 @lilypond[verbatim,quote]
3056 \\markup {
3057   \\note-by-number #3 #0 #DOWN
3058   \\hspace #2
3059   \\note-by-number #1 #2 #0.8
3060 }
3061 @end lilypond"
3062   (define (get-glyph-name-candidates dir log style)
3063     (map (lambda (dir-name)
3064            (format #f "noteheads.~a~a" dir-name
3065                    (if (and (symbol? style)
3066                             (not (equal? 'default style)))
3067                        (select-head-glyph style (min log 2))
3068                        (min log 2))))
3069          (list (if (= dir UP) "u" "d")
3070                "s")))
3071
3072   (define (get-glyph-name font cands)
3073     (if (null? cands)
3074         ""
3075         (if (ly:stencil-empty? (ly:font-get-glyph font (car cands)))
3076             (get-glyph-name font (cdr cands))
3077             (car cands))))
3078
3079   (let* ((font (ly:paper-get-font layout (cons '((font-encoding . fetaMusic))
3080                                                props)))
3081          (size-factor (magstep font-size))
3082          (stem-length (* size-factor (max 3 (- log 1))))
3083          (head-glyph-name
3084           (let ((result (get-glyph-name font (get-glyph-name-candidates
3085                                               (sign dir) log style))))
3086             (if (string-null? result)
3087                 ;; If no glyph name can be found, select default heads.  Though
3088                 ;; this usually means an unsupported style has been chosen, it
3089                 ;; also prevents unrelated 'style settings from other grobs
3090                 ;; (e.g., TextSpanner and TimeSignature) leaking into markup.
3091                 (get-glyph-name font (get-glyph-name-candidates
3092                                       (sign dir) log 'default))
3093                 result)))
3094          (head-glyph (ly:font-get-glyph font head-glyph-name))
3095          (attach-indices (ly:note-head::stem-attachment font head-glyph-name))
3096          (stem-thickness (* size-factor 0.13))
3097          (stemy (* dir stem-length))
3098          (attach-off (cons (interval-index
3099                             (ly:stencil-extent head-glyph X)
3100                             (* (sign dir) (car attach-indices)))
3101                            (* (sign dir) ; fixme, this is inconsistent between X & Y.
3102                               (interval-index
3103                                (ly:stencil-extent head-glyph Y)
3104                                (cdr attach-indices)))))
3105          (stem-glyph (and (> log 0)
3106                           (ly:round-filled-box
3107                            (ordered-cons (car attach-off)
3108                                          (+ (car attach-off)
3109                                             (* (- (sign dir)) stem-thickness)))
3110                            (cons (min stemy (cdr attach-off))
3111                                  (max stemy (cdr attach-off)))
3112                            (/ stem-thickness 3))))
3113
3114          (dot (ly:font-get-glyph font "dots.dot"))
3115          (dotwid (interval-length (ly:stencil-extent dot X)))
3116          (dots (and (> dot-count 0)
3117                     (apply ly:stencil-add
3118                            (map (lambda (x)
3119                                   (ly:stencil-translate-axis
3120                                    dot (* 2 x dotwid) X))
3121                                 (iota dot-count)))))
3122          (flaggl (and (> log 2)
3123                       (ly:stencil-translate
3124                        (ly:font-get-glyph font
3125                                           (string-append "flags."
3126                                                          (if (> dir 0) "u" "d")
3127                                                          (number->string log)))
3128                        (cons (+ (car attach-off) (if (< dir 0)
3129                                                      stem-thickness 0))
3130                              stemy)))))
3131
3132     ;; If there is a flag on an upstem and the stem is short, move the dots
3133     ;; to avoid the flag.  16th notes get a special case because their flags
3134     ;; hang lower than any other flags.
3135     (if (and dots (> dir 0) (> log 2)
3136              (or (< dir 1.15) (and (= log 4) (< dir 1.3))))
3137         (set! dots (ly:stencil-translate-axis dots 0.5 X)))
3138     (if flaggl
3139         (set! stem-glyph (ly:stencil-add flaggl stem-glyph)))
3140     (if (ly:stencil? stem-glyph)
3141         (set! stem-glyph (ly:stencil-add stem-glyph head-glyph))
3142         (set! stem-glyph head-glyph))
3143     (if (ly:stencil? dots)
3144         (set! stem-glyph
3145               (ly:stencil-add
3146                (ly:stencil-translate-axis
3147                 dots
3148                 (+ (cdr (ly:stencil-extent head-glyph X)) dotwid)
3149                 X)
3150                stem-glyph)))
3151     stem-glyph))
3152
3153 (define-public log2
3154   (let ((divisor (log 2)))
3155     (lambda (z) (inexact->exact (/ (log z) divisor)))))
3156
3157 (define (parse-simple-duration duration-string)
3158   "Parse the `duration-string', e.g. ''4..'' or ''breve.'',
3159 and return a (log dots) list."
3160   (let ((match (regexp-exec (make-regexp "(breve|longa|maxima|[0-9]+)(\\.*)")
3161                             duration-string)))
3162     (if (and match (string=? duration-string (match:substring match 0)))
3163         (let ((len (match:substring match 1))
3164               (dots (match:substring match 2)))
3165           (list (cond ((string=? len "breve") -1)
3166                       ((string=? len "longa") -2)
3167                       ((string=? len "maxima") -3)
3168                       (else (log2 (string->number len))))
3169                 (if dots (string-length dots) 0)))
3170         (ly:error (_ "not a valid duration string: ~a") duration-string))))
3171
3172 (define-markup-command (note layout props duration dir)
3173   (string? number?)
3174   #:category music
3175   #:properties (note-by-number-markup)
3176   "
3177 @cindex notes within text by string
3178
3179 This produces a note with a stem pointing in @var{dir} direction, with
3180 the @var{duration} for the note head type and augmentation dots.  For
3181 example, @code{\\note #\"4.\" #-0.75} creates a dotted quarter note, with
3182 a shortened down stem.
3183
3184 @lilypond[verbatim,quote]
3185 \\markup {
3186   \\override #'(style . cross) {
3187     \\note #\"4..\" #UP
3188   }
3189   \\hspace #2
3190   \\note #\"breve\" #0
3191 }
3192 @end lilypond"
3193   (let ((parsed (parse-simple-duration duration)))
3194     (note-by-number-markup layout props (car parsed) (cadr parsed) dir)))
3195
3196 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3197 ;; translating.
3198 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3199
3200 (define-markup-command (lower layout props amount arg)
3201   (number? markup?)
3202   #:category align
3203   "
3204 @cindex lowering text
3205
3206 Lower @var{arg} by the distance @var{amount}.
3207 A negative @var{amount} indicates raising; see also @code{\\raise}.
3208
3209 @lilypond[verbatim,quote]
3210 \\markup {
3211   one
3212   \\lower #3
3213   two
3214   three
3215 }
3216 @end lilypond"
3217   (ly:stencil-translate-axis (interpret-markup layout props arg)
3218                              (- amount) Y))
3219
3220 (define-markup-command (translate-scaled layout props offset arg)
3221   (number-pair? markup?)
3222   #:category align
3223   #:properties ((font-size 0))
3224   "
3225 @cindex translating text
3226 @cindex scaling text
3227
3228 Translate @var{arg} by @var{offset}, scaling the offset by the
3229 @code{font-size}.
3230
3231 @lilypond[verbatim,quote]
3232 \\markup {
3233   \\fontsize #5 {
3234     * \\translate #'(2 . 3) translate
3235     \\hspace #2
3236     * \\translate-scaled #'(2 . 3) translate-scaled
3237   }
3238 }
3239 @end lilypond"
3240   (let* ((factor (magstep font-size))
3241          (scaled (cons (* factor (car offset))
3242                        (* factor (cdr offset)))))
3243     (ly:stencil-translate (interpret-markup layout props arg)
3244                           scaled)))
3245
3246 (define-markup-command (raise layout props amount arg)
3247   (number? markup?)
3248   #:category align
3249   "
3250 @cindex raising text
3251
3252 Raise @var{arg} by the distance @var{amount}.
3253 A negative @var{amount} indicates lowering, see also @code{\\lower}.
3254
3255 The argument to @code{\\raise} is the vertical displacement amount,
3256 measured in (global) staff spaces.  @code{\\raise} and @code{\\super}
3257 raise objects in relation to their surrounding markups.
3258
3259 If the text object itself is positioned above or below the staff, then
3260 @code{\\raise} cannot be used to move it, since the mechanism that
3261 positions it next to the staff cancels any shift made with
3262 @code{\\raise}.  For vertical positioning, use the @code{padding}
3263 and/or @code{extra-offset} properties.
3264
3265 @lilypond[verbatim,quote]
3266 \\markup {
3267   C
3268   \\small
3269   \\bold
3270   \\raise #1.0
3271   9/7+
3272 }
3273 @end lilypond"
3274   (ly:stencil-translate-axis (interpret-markup layout props arg) amount Y))
3275
3276 (define-markup-command (fraction layout props arg1 arg2)
3277   (markup? markup?)
3278   #:category other
3279   #:properties ((font-size 0))
3280   "
3281 @cindex creating text fractions
3282
3283 Make a fraction of two markups.
3284 @lilypond[verbatim,quote]
3285 \\markup {
3286   Ï€ â‰ˆ
3287   \\fraction 355 113
3288 }
3289 @end lilypond"
3290   (let* ((m1 (interpret-markup layout props arg1))
3291          (m2 (interpret-markup layout props arg2))
3292          (factor (magstep font-size))
3293          (boxdimen (cons (* factor -0.05) (* factor 0.05)))
3294          (padding (* factor 0.2))
3295          (baseline (* factor 0.6))
3296          (offset (* factor 0.75)))
3297     (set! m1 (ly:stencil-aligned-to m1 X CENTER))
3298     (set! m2 (ly:stencil-aligned-to m2 X CENTER))
3299     (let* ((x1 (ly:stencil-extent m1 X))
3300            (x2 (ly:stencil-extent m2 X))
3301            (line (ly:round-filled-box (interval-union x1 x2) boxdimen 0.0))
3302            ;; should stack mols separately, to maintain LINE on baseline
3303            (stack (stack-lines DOWN padding baseline (list m1 line m2))))
3304       (set! stack
3305             (ly:stencil-aligned-to stack Y CENTER))
3306       (set! stack
3307             (ly:stencil-aligned-to stack X LEFT))
3308       ;; should have EX dimension
3309       ;; empirical anyway
3310       (ly:stencil-translate-axis stack offset Y))))
3311
3312 (define-markup-command (normal-size-super layout props arg)
3313   (markup?)
3314   #:category font
3315   #:properties ((baseline-skip))
3316   "
3317 @cindex setting superscript in standard font size
3318
3319 Set @var{arg} in superscript with a normal font size.
3320
3321 @lilypond[verbatim,quote]
3322 \\markup {
3323   default
3324   \\normal-size-super {
3325     superscript in standard size
3326   }
3327 }
3328 @end lilypond"
3329   (ly:stencil-translate-axis
3330    (interpret-markup layout props arg)
3331    (* 0.5 baseline-skip) Y))
3332
3333 (define-markup-command (super layout props arg)
3334   (markup?)
3335   #:category font
3336   #:properties ((font-size 0)
3337                 (baseline-skip))
3338   "
3339 @cindex superscript text
3340
3341 Set @var{arg} in superscript.
3342
3343 @lilypond[verbatim,quote]
3344 \\markup {
3345   E =
3346   \\concat {
3347     mc
3348     \\super
3349     2
3350   }
3351 }
3352 @end lilypond"
3353   (ly:stencil-translate-axis
3354    (interpret-markup
3355     layout
3356     (cons `((font-size . ,(- font-size 3))) props)
3357     arg)
3358    (* 0.5 baseline-skip)
3359    Y))
3360
3361 (define-markup-command (translate layout props offset arg)
3362   (number-pair? markup?)
3363   #:category align
3364   "
3365 @cindex translating text
3366
3367 Translate @var{arg} relative to its surroundings.  @var{offset}
3368 is a pair of numbers representing the displacement in the X and Y axis.
3369
3370 @lilypond[verbatim,quote]
3371 \\markup {
3372   *
3373   \\translate #'(2 . 3)
3374   \\line { translated two spaces right, three up }
3375 }
3376 @end lilypond"
3377   (ly:stencil-translate (interpret-markup layout props arg)
3378                         offset))
3379
3380 (define-markup-command (sub layout props arg)
3381   (markup?)
3382   #:category font
3383   #:properties ((font-size 0)
3384                 (baseline-skip))
3385   "
3386 @cindex subscript text
3387
3388 Set @var{arg} in subscript.
3389
3390 @lilypond[verbatim,quote]
3391 \\markup {
3392   \\concat {
3393     H
3394     \\sub {
3395       2
3396     }
3397     O
3398   }
3399 }
3400 @end lilypond"
3401   (ly:stencil-translate-axis
3402    (interpret-markup
3403     layout
3404     (cons `((font-size . ,(- font-size 3))) props)
3405     arg)
3406    (* -0.5 baseline-skip)
3407    Y))
3408
3409 (define-markup-command (normal-size-sub layout props arg)
3410   (markup?)
3411   #:category font
3412   #:properties ((baseline-skip))
3413   "
3414 @cindex setting subscript in standard font size
3415
3416 Set @var{arg} in subscript with a normal font size.
3417
3418 @lilypond[verbatim,quote]
3419 \\markup {
3420   default
3421   \\normal-size-sub {
3422     subscript in standard size
3423   }
3424 }
3425 @end lilypond"
3426   (ly:stencil-translate-axis
3427    (interpret-markup layout props arg)
3428    (* -0.5 baseline-skip)
3429    Y))
3430
3431 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3432 ;; brackets.
3433 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3434
3435 (define-markup-command (hbracket layout props arg)
3436   (markup?)
3437   #:category graphic
3438   "
3439 @cindex placing horizontal brackets around text
3440
3441 Draw horizontal brackets around @var{arg}.
3442
3443 @lilypond[verbatim,quote]
3444 \\markup {
3445   \\hbracket {
3446     \\line {
3447       one two three
3448     }
3449   }
3450 }
3451 @end lilypond"
3452   (let ((th 0.1) ;; todo: take from GROB.
3453         (m (interpret-markup layout props arg)))
3454     (bracketify-stencil m X th (* 2.5 th) th)))
3455
3456 (define-markup-command (bracket layout props arg)
3457   (markup?)
3458   #:category graphic
3459   "
3460 @cindex placing vertical brackets around text
3461
3462 Draw vertical brackets around @var{arg}.
3463
3464 @lilypond[verbatim,quote]
3465 \\markup {
3466   \\bracket {
3467     \\note #\"2.\" #UP
3468   }
3469 }
3470 @end lilypond"
3471   (let ((th 0.1) ;; todo: take from GROB.
3472         (m (interpret-markup layout props arg)))
3473     (bracketify-stencil m Y th (* 2.5 th) th)))
3474
3475 (define-markup-command (parenthesize layout props arg)
3476   (markup?)
3477   #:category graphic
3478   #:properties ((angularity 0)
3479                 (padding)
3480                 (size 1)
3481                 (thickness 1)
3482                 (width 0.25))
3483   "
3484 @cindex placing parentheses around text
3485
3486 Draw parentheses around @var{arg}.  This is useful for parenthesizing
3487 a column containing several lines of text.
3488
3489 @lilypond[verbatim,quote]
3490 \\markup {
3491   \\line {
3492     \\parenthesize {
3493       \\column {
3494         foo
3495         bar
3496       }
3497     }
3498     \\override #'(angularity . 2) {
3499       \\parenthesize {
3500         \\column {
3501           bah
3502           baz
3503         }
3504       }
3505     }
3506   }
3507 }
3508 @end lilypond"
3509   (let* ((markup (interpret-markup layout props arg))
3510          (scaled-width (* size width))
3511          (scaled-thickness
3512           (* (chain-assoc-get 'line-thickness props 0.1)
3513              thickness))
3514          (half-thickness
3515           (min (* size 0.5 scaled-thickness)
3516                (* (/ 4 3.0) scaled-width)))
3517          (padding (chain-assoc-get 'padding props half-thickness)))
3518     (parenthesize-stencil
3519      markup half-thickness scaled-width angularity padding)))
3520
3521
3522 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3523 ;; Delayed markup evaluation
3524 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3525
3526 (define-markup-command (page-ref layout props label gauge default)
3527   (symbol? markup? markup?)
3528   #:category other
3529   "
3530 @cindex referencing page numbers in text
3531
3532 Reference to a page number.  @var{label} is the label set on the referenced
3533 page (using the @code{\\label} command), @var{gauge} a markup used to estimate
3534 the maximum width of the page number, and @var{default} the value to display
3535 when @var{label} is not found."
3536   (let* ((gauge-stencil (interpret-markup layout props gauge))
3537          (x-ext (ly:stencil-extent gauge-stencil X))
3538          (y-ext (ly:stencil-extent gauge-stencil Y)))
3539     (ly:make-stencil
3540      `(delay-stencil-evaluation
3541        ,(delay (ly:stencil-expr
3542                 (let* ((table (ly:output-def-lookup layout 'label-page-table))
3543                        (page-number (if (list? table)
3544                                         (assoc-get label table)
3545                                         #f))
3546                        (page-markup (if page-number (format #f "~a" page-number) default))
3547                        (page-stencil (interpret-markup layout props page-markup))
3548                        (gap (- (interval-length x-ext)
3549                                (interval-length (ly:stencil-extent page-stencil X)))))
3550                   (interpret-markup layout props
3551                                     (markup #:concat (#:hspace gap page-markup)))))))
3552      x-ext
3553      y-ext)))
3554
3555 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3556 ;; scaling
3557 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3558
3559 (define-markup-command (scale layout props factor-pair arg)
3560   (number-pair? markup?)
3561   #:category graphic
3562   "
3563 @cindex scaling markup
3564 @cindex mirroring markup
3565
3566 Scale @var{arg}.  @var{factor-pair} is a pair of numbers
3567 representing the scaling-factor in the X and Y axes.
3568 Negative values may be used to produce mirror images.
3569
3570 @lilypond[verbatim,quote]
3571 \\markup {
3572   \\line {
3573     \\scale #'(2 . 1)
3574     stretched
3575     \\scale #'(1 . -1)
3576     mirrored
3577   }
3578 }
3579 @end lilypond"
3580   (let ((stil (interpret-markup layout props arg))
3581         (sx (car factor-pair))
3582         (sy (cdr factor-pair)))
3583     (ly:stencil-scale stil sx sy)))
3584
3585 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3586 ;; Repeating
3587 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3588
3589 (define-markup-command (pattern layout props count axis space pattern)
3590   (integer? integer? number? markup?)
3591   #:category other
3592   "
3593 Prints @var{count} times a @var{pattern} markup.
3594 Patterns are spaced apart by @var{space}.
3595 Patterns are distributed on @var{axis}.
3596
3597 @lilypond[verbatim, quote]
3598 \\markup \\column {
3599   \"Horizontally repeated :\"
3600   \\pattern #7 #X #2 \\flat
3601   \\null
3602   \"Vertically repeated :\"
3603   \\pattern #3 #Y #0.5 \\flat
3604 }
3605 @end lilypond"
3606   (let ((pattern-width (interval-length
3607                          (ly:stencil-extent (interpret-markup layout props pattern) X)))
3608         (new-props (prepend-alist-chain 'word-space 0 (prepend-alist-chain 'baseline-skip 0 props))))
3609     (let loop ((i (1- count)) (patterns (markup)))
3610       (if (zero? i)
3611           (interpret-markup
3612             layout
3613             new-props
3614             (if (= axis X)
3615                 (markup patterns pattern)
3616                 (markup #:column (patterns pattern))))
3617           (loop (1- i)
3618             (if (= axis X)
3619                 (markup patterns pattern #:hspace space)
3620                 (markup #:column (patterns pattern #:vspace space))))))))
3621
3622 (define-markup-command (fill-with-pattern layout props space dir pattern left right)
3623   (number? ly:dir? markup? markup? markup?)
3624   #:category align
3625   #:properties ((word-space)
3626                 (line-width))
3627   "
3628 Put @var{left} and @var{right} in a horizontal line of width @code{line-width}
3629 with a line of markups @var{pattern} in between.
3630 Patterns are spaced apart by @var{space}.
3631 Patterns are aligned to the @var{dir} markup.
3632
3633 @lilypond[verbatim, quote]
3634 \\markup \\column {
3635   \"right-aligned :\"
3636   \\fill-with-pattern #1 #RIGHT . first right
3637   \\fill-with-pattern #1 #RIGHT . second right
3638   \\null
3639   \"center-aligned :\"
3640   \\fill-with-pattern #1.5 #CENTER - left right
3641   \\null
3642   \"left-aligned :\"
3643   \\override #'(line-width . 50)
3644   \\fill-with-pattern #2 #LEFT : left first
3645   \\override #'(line-width . 50)
3646   \\fill-with-pattern #2 #LEFT : left second
3647 }
3648 @end lilypond"
3649   (let* ((pattern-x-extent (ly:stencil-extent (interpret-markup layout props pattern) X))
3650          (pattern-width (interval-length pattern-x-extent))
3651          (left-width (interval-length (ly:stencil-extent (interpret-markup layout props left) X)))
3652          (right-width (interval-length (ly:stencil-extent (interpret-markup layout props right) X)))
3653          (middle-width (max 0 (- line-width (+ (+ left-width right-width) (* word-space 2)))))
3654          (period (+ space pattern-width))
3655          (count (truncate (/ (- middle-width pattern-width) period)))
3656          (x-offset (+ (* (- (- middle-width (* count period)) pattern-width) (/ (1+ dir) 2)) (abs (car pattern-x-extent)))))
3657     (interpret-markup layout props
3658                       (markup left
3659                               #:with-dimensions (cons 0 middle-width) '(0 . 0)
3660                               #:translate (cons x-offset 0)
3661                               #:pattern (1+ count) X space pattern
3662                               right))))
3663
3664 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3665 ;; Markup list commands
3666 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3667
3668 (define-public (space-lines baseline stils)
3669   (let space-stil ((stils stils)
3670                    (result (list)))
3671     (if (null? stils)
3672         (reverse! result)
3673         (let* ((stil (car stils))
3674                (dy-top (max (- (/ baseline 1.5)
3675                                (interval-bound (ly:stencil-extent stil Y) UP))
3676                             0.0))
3677                (dy-bottom (max (+ (/ baseline 3.0)
3678                                   (interval-bound (ly:stencil-extent stil Y) DOWN))
3679                                0.0))
3680                (new-stil (ly:make-stencil
3681                           (ly:stencil-expr stil)
3682                           (ly:stencil-extent stil X)
3683                           (cons (- (interval-bound (ly:stencil-extent stil Y) DOWN)
3684                                    dy-bottom)
3685                                 (+ (interval-bound (ly:stencil-extent stil Y) UP)
3686                                    dy-top)))))
3687           (space-stil (cdr stils) (cons new-stil result))))))
3688
3689 (define-markup-list-command (justified-lines layout props args)
3690   (markup-list?)
3691   #:properties ((baseline-skip)
3692                 wordwrap-internal-markup-list)
3693   "
3694 @cindex justifying lines of text
3695
3696 Like @code{\\justify}, but return a list of lines instead of a single markup.
3697 Use @code{\\override-lines #'(line-width . @var{X})} to set the line width;
3698 @var{X}@tie{}is the number of staff spaces."
3699   (space-lines baseline-skip
3700                (interpret-markup-list layout props
3701                                       (make-wordwrap-internal-markup-list #t args))))
3702
3703 (define-markup-list-command (wordwrap-lines layout props args)
3704   (markup-list?)
3705   #:properties ((baseline-skip)
3706                 wordwrap-internal-markup-list)
3707   "Like @code{\\wordwrap}, but return a list of lines instead of a single markup.
3708 Use @code{\\override-lines #'(line-width . @var{X})} to set the line width,
3709 where @var{X} is the number of staff spaces."
3710   (space-lines baseline-skip
3711                (interpret-markup-list layout props
3712                                       (make-wordwrap-internal-markup-list #f args))))
3713
3714 (define-markup-list-command (column-lines layout props args)
3715   (markup-list?)
3716   #:properties ((baseline-skip))
3717   "Like @code{\\column}, but return a list of lines instead of a single markup.
3718 @code{baseline-skip} determines the space between each markup in @var{args}."
3719   (space-lines baseline-skip
3720                (interpret-markup-list layout props args)))
3721
3722 (define-markup-list-command (override-lines layout props new-prop args)
3723   (pair? markup-list?)
3724   "Like @code{\\override}, for markup lists."
3725   (interpret-markup-list layout (cons (list new-prop) props) args))