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