1 ;;;; define-markup-commands.scm -- markup commands
3 ;;;; source file of the GNU LilyPond music typesetter
5 ;;;; (c) 2000--2004 Han-Wen Nienhuys <hanwen@cs.uu.nl>
6 ;;;; Jan Nieuwenhuizen <janneke@gnu.org>
10 ;;; * each markup function should have a doc string with
11 ;; syntax, description and example.
14 (def-markup-command (stencil layout props stil) (ly:stencil?)
19 (def-markup-command (score layout props score) (ly:score?)
21 ((systems (ly:score-embedded-format score layout)))
23 (if (= 0 (vector-length systems))
25 (ly:warn "No systems found in \\score markup. Did you forget \\layout?")
29 ((stencil (ly:paper-system-stencil (vector-ref systems 0))))
31 (ly:stencil-align-to! stencil Y CENTER)
34 (def-markup-command (simple layout props str) (string?)
35 "A simple text string; @code{\\markup @{ foo @}} is equivalent with
36 @code{\\markup @{ \\simple #\"foo\" @}}."
37 (interpret-markup layout props str))
39 (def-markup-command (encoded-simple layout props sym str) (symbol? string?)
40 "A text string, encoded with encoding @var{sym}. See
41 @usermanref{Text encoding} for more information."
42 (Text_interface::interpret_string layout
46 ;; TODO: use font recoding.
48 ;; (map make-word-markup (string-tokenize str)))))
50 (define-public empty-markup
51 (make-simple-markup ""))
54 (def-markup-command (postscript layout props str) (string?)
56 "This inserts @var{str} directly into the output as a PostScript
57 command string. Due to technicalities of the output backends,
58 different scales should be used for the @TeX{} and PostScript backend,
59 selected with @code{-f}.
62 For the TeX backend, the following string prints a rotated text
67 0 0 moveto /ecrm10 findfont
68 1.75 scalefont setfont 90 rotate (hello) show
72 The magical constant 1.75 scales from LilyPond units (staff spaces) to
75 For the postscript backend, use the following
78 gsave /ecrm10 findfont
80 scalefont setfont 90 rotate (hello) show grestore
86 (list 'embedded-ps str)
89 ;;(def-markup-command (fill-line layout props line-width markups)
90 ;; (number? markup-list?)
91 ;; no parser tag -- should make number? markuk-list? thingy
92 (def-markup-command (fill-line layout props markups)
94 "Put @var{markups} in a horizontal line of width @var{line-width}.
95 The markups are spaced/flushed to fill the entire line."
97 (let* ((stencils (map (lambda (x) (interpret-markup layout props x))
99 (text-width (apply + (map interval-length
101 (ly:stencil-extent x X))
103 (word-count (length markups))
104 (word-space (chain-assoc-get 'word-space props))
105 (line-width (chain-assoc-get 'linewidth props))
106 (fill-space (if (< line-width text-width)
108 (/ (- line-width text-width)
109 (if (= word-count 1) 2 (- word-count 1)))))
110 (line-stencils (if (= word-count 1)
111 (map (lambda (x) (interpret-markup layout props x))
112 (list (make-simple-markup "")
113 (make-stencil-markup (car stencils))
114 (make-simple-markup "")))
116 (stack-stencil-line fill-space line-stencils)))
118 (define (font-markup qualifier value)
119 (lambda (layout props arg)
120 (interpret-markup layout
121 (prepend-alist-chain qualifier value props)
124 (def-markup-command (line layout props args) (markup-list?)
125 "Put @var{args} in a horizontal line. The property @code{word-space}
126 determines the space between each markup in @var{args}."
128 (chain-assoc-get 'word-space props)
129 (map (lambda (m) (interpret-markup layout props m)) args)))
131 (def-markup-command (combine layout props m1 m2) (markup? markup?)
132 "Print two markups on top of each other."
134 ((s1 (interpret-markup layout props m1))
135 (s2 (interpret-markup layout props m2)))
137 (ly:stencil-add s1 s2)))
139 (def-markup-command (finger layout props arg) (markup?)
140 "Set the argument as small numbers."
141 (interpret-markup layout
142 (cons '((font-size . -5) (font-encoding . fetaNumber)) props)
145 (def-markup-command (fontsize layout props mag arg) (number? markup?)
146 "This sets the relative font size, e.g.
148 A \\fontsize #2 @{ B C @} D
152 This will enlarge the B and the C by two steps.
156 (prepend-alist-chain 'font-size mag props)
159 (def-markup-command (magnify layout props sz arg) (number? markup?)
160 "This sets the font magnification for the its argument. In the following
161 example, the middle A will be 10% larger:
163 A \\magnify #1.1 @{ A @} A
166 Note: magnification only works if a font-name is explicitly selected.
167 Use @code{\\fontsize} otherwise."
171 (prepend-alist-chain 'font-magnification sz props)
174 (def-markup-command (bold layout props arg) (markup?)
175 "Switch to bold font-series"
176 (interpret-markup layout (prepend-alist-chain 'font-series 'bold props) arg))
178 (def-markup-command (sans layout props arg) (markup?)
179 "Switch to the sans serif family"
180 (interpret-markup layout (prepend-alist-chain 'font-family 'sans props) arg))
182 (def-markup-command (number layout props arg) (markup?)
183 "Set font family to @code{number}, which yields the font used for
184 time signatures and fingerings. This font only contains numbers and
185 some punctuation. It doesn't have any letters. "
186 (interpret-markup layout (prepend-alist-chain 'font-encoding 'fetaNumber props) arg))
188 (def-markup-command (roman layout props arg) (markup?)
189 "Set font family to @code{roman}."
190 (interpret-markup layout (prepend-alist-chain 'font-family 'roman props) arg))
192 (def-markup-command (huge layout props arg) (markup?)
193 "Set font size to +2."
194 (interpret-markup layout (prepend-alist-chain 'font-size 2 props) arg))
196 (def-markup-command (large layout props arg) (markup?)
197 "Set font size to +1."
198 (interpret-markup layout (prepend-alist-chain 'font-size 1 props) arg))
200 (def-markup-command (normalsize layout props arg) (markup?)
201 "Set font size to default."
202 (interpret-markup layout (prepend-alist-chain 'font-size 0 props) arg))
204 (def-markup-command (small layout props arg) (markup?)
205 "Set font size to -1."
206 (interpret-markup layout (prepend-alist-chain 'font-size -1 props) arg))
208 (def-markup-command (tiny layout props arg) (markup?)
209 "Set font size to -2."
210 (interpret-markup layout (prepend-alist-chain 'font-size -2 props) arg))
212 (def-markup-command (teeny layout props arg) (markup?)
213 "Set font size to -3."
214 (interpret-markup layout (prepend-alist-chain 'font-size -3 props) arg))
216 (def-markup-command (caps layout props arg) (markup?)
217 "Set @code{font-shape} to @code{caps}."
218 (interpret-markup layout (prepend-alist-chain 'font-shape 'caps props) arg))
220 ;(def-markup-command (latin-i layout props arg) (markup?)
221 ; "TEST latin1 encoding."
222 ; (interpret-markup layout (prepend-alist-chain 'font-shape 'latin1 props) arg))
224 (def-markup-command (dynamic layout props arg) (markup?)
225 "Use the dynamic font. This font only contains @b{s}, @b{f}, @b{m},
226 @b{z}, @b{p}, and @b{r}. When producing phrases, like ``pi@`{u} @b{f}'', the
227 normal words (like ``pi@`{u}'') should be done in a different font. The
228 recommend font for this is bold and italic"
230 layout (prepend-alist-chain 'font-encoding 'fetaDynamic props) arg))
232 (def-markup-command (italic layout props arg) (markup?)
233 "Use italic @code{font-shape} for @var{arg}. "
234 (interpret-markup layout (prepend-alist-chain 'font-shape 'italic props) arg))
236 (def-markup-command (typewriter layout props arg) (markup?)
237 "Use @code{font-family} typewriter for @var{arg}."
239 layout (prepend-alist-chain 'font-family 'typewriter props) arg))
241 (def-markup-command (upright layout props arg) (markup?)
242 "Set font shape to @code{upright}."
244 layout (prepend-alist-chain 'font-shape 'upright props) arg))
246 (def-markup-command (doublesharp layout props) ()
247 "Draw a double sharp symbol."
249 (interpret-markup layout props (markup #:musicglyph "accidentals-4")))
250 (def-markup-command (sesquisharp layout props) ()
251 "Draw a 3/2 sharp symbol."
252 (interpret-markup layout props (markup #:musicglyph "accidentals-3")))
254 (def-markup-command (sharp layout props) ()
255 "Draw a sharp symbol."
256 (interpret-markup layout props (markup #:musicglyph "accidentals-2")))
257 (def-markup-command (semisharp layout props) ()
258 "Draw a semi sharp symbol."
259 (interpret-markup layout props (markup #:musicglyph "accidentals-1")))
260 (def-markup-command (natural layout props) ()
261 "Draw a natural symbol."
263 (interpret-markup layout props (markup #:musicglyph "accidentals-0")))
264 (def-markup-command (semiflat layout props) ()
266 (interpret-markup layout props (markup #:musicglyph "accidentals--1")))
267 (def-markup-command (flat layout props) ()
268 "Draw a flat symbol."
270 (interpret-markup layout props (markup #:musicglyph "accidentals--2")))
271 (def-markup-command (sesquiflat layout props) ()
272 "Draw a 3/2 flat symbol."
274 (interpret-markup layout props (markup #:musicglyph "accidentals--3")))
275 (def-markup-command (doubleflat layout props) ()
276 "Draw a double flat symbol."
278 (interpret-markup layout props (markup #:musicglyph "accidentals--4")))
281 (def-markup-command (column layout props args) (markup-list?)
282 "Stack the markups in @var{args} vertically."
284 -1 0.0 (chain-assoc-get 'baseline-skip props)
285 (map (lambda (m) (interpret-markup layout props m)) args)))
287 (def-markup-command (dir-column layout props args) (markup-list?)
288 "Make a column of args, going up or down, depending on the setting
289 of the @code{#'direction} layout property."
290 (let* ((dir (chain-assoc-get 'direction props)))
292 (if (number? dir) dir -1)
294 (chain-assoc-get 'baseline-skip props)
295 (map (lambda (x) (interpret-markup layout props x)) args))))
297 (def-markup-command (center-align layout props args) (markup-list?)
298 "Put @code{args} in a centered column. "
299 (let* ((mols (map (lambda (x) (interpret-markup layout props x)) args))
300 (cmols (map (lambda (x) (ly:stencil-align-to! x X CENTER)) mols)))
301 (stack-lines -1 0.0 (chain-assoc-get 'baseline-skip props) mols)))
303 (def-markup-command (vcenter layout props arg) (markup?)
304 "Align @code{arg} to its center. "
305 (let* ((mol (interpret-markup layout props arg)))
306 (ly:stencil-align-to! mol Y CENTER)
309 (def-markup-command (right-align layout props arg) (markup?)
310 (let* ((m (interpret-markup layout props arg)))
311 (ly:stencil-align-to! m X RIGHT)
314 (def-markup-command (left-align layout props arg) (markup?)
315 "Align @var{arg} on its left edge. "
317 (let* ((m (interpret-markup layout props arg)))
318 (ly:stencil-align-to! m X LEFT)
321 (def-markup-command (general-align layout props axis dir arg) (integer? number? markup?)
322 "Align @var{arg} in @var{axis} direction to the @var{dir} side."
323 (let* ((m (interpret-markup layout props arg)))
325 (ly:stencil-align-to! m axis dir)
329 (def-markup-command (halign layout props dir arg) (number? markup?)
330 "Set horizontal alignment. If @var{dir} is @code{-1}, then it is
331 left-aligned, while @code{+1} is right. Values in between interpolate
332 alignment accordingly."
335 (let* ((m (interpret-markup layout props arg)))
336 (ly:stencil-align-to! m X dir)
339 (def-markup-command (musicglyph layout props glyph-name) (string?)
340 "This is converted to a musical symbol, e.g. @code{\\musicglyph
341 #\"accidentals-0\"} will select the natural sign from the music font.
342 See @usermanref{The Feta font} for a complete listing of the possible glyphs.
344 (ly:find-glyph-by-name
345 (ly:paper-get-font layout (cons '((font-encoding . fetaMusic))
350 (def-markup-command (lookup layout props glyph-name) (string?)
351 "Lookup a glyph by name."
352 (ly:find-glyph-by-name (ly:paper-get-font layout props)
355 (def-markup-command (char layout props num) (integer?)
356 "Produce a single character, e.g. @code{\\char #65} produces the
358 (ly:get-glyph (ly:paper-get-font layout props) num))
360 (def-markup-command (raise layout props amount arg) (number? markup?)
362 This raises @var{arg}, by the distance @var{amount}.
363 A negative @var{amount} indicates lowering:
365 @lilypond[verbatim,fragment,relative=1]
366 c1^\\markup { C \\small \\raise #1.0 \\bold { \"9/7+\" }}
368 The argument to @code{\\raise} is the vertical displacement amount,
369 measured in (global) staff spaces. @code{\\raise} and @code{\\super}
370 raise objects in relation to their surrounding markups.
372 If the text object itself is positioned above or below the staff, then
373 @code{\\raise} cannot be used to move it, since the mechanism that
374 positions it next to the staff cancels any shift made with
375 @code{\\raise}. For vertical positioning, use the @code{padding}
376 and/or @code{extra-offset} properties. "
379 (ly:stencil-translate-axis (interpret-markup layout props arg)
382 (def-markup-command (fraction layout props arg1 arg2) (markup? markup?)
383 "Make a fraction of two markups."
385 (let* ((m1 (interpret-markup layout props arg1))
386 (m2 (interpret-markup layout props arg2)))
387 (ly:stencil-align-to! m1 X CENTER)
388 (ly:stencil-align-to! m2 X CENTER)
389 (let* ((x1 (ly:stencil-extent m1 X))
390 (x2 (ly:stencil-extent m2 X))
391 (line (ly:round-filled-box (interval-union x1 x2) '(-0.05 . 0.05) 0.0))
392 ;; should stack mols separately, to maintain LINE on baseline
393 (stack (stack-lines -1 0.2 0.6 (list m1 line m2))))
394 (ly:stencil-align-to! stack Y CENTER)
395 (ly:stencil-align-to! stack X LEFT)
396 ;; should have EX dimension
398 (ly:stencil-translate-axis stack 0.75 Y))))
401 ;; TODO: better syntax.
403 (def-markup-command (note-by-number layout props log dot-count dir) (number? number? number?)
404 "Construct a note symbol, with stem. By using fractional values for
405 @var{dir}, you can obtain longer or shorter stems."
407 (let* ((font (ly:paper-get-font layout (cons '((font-encoding . fetaMusic)) props)))
408 (size (chain-assoc-get 'font-size props 0))
409 (stem-length (* (magstep size) (max 3 (- log 1))))
410 (head-glyph (ly:find-glyph-by-name
412 (string-append "noteheads-" (number->string (min log 2)))))
413 (stem-thickness 0.13)
414 (stemy (* dir stem-length))
415 (attachx (if (> dir 0)
416 (- (cdr (ly:stencil-extent head-glyph X)) stem-thickness)
418 (attachy (* dir 0.28))
419 (stem-glyph (and (> log 0)
421 (cons attachx (+ attachx stem-thickness))
422 (cons (min stemy attachy)
424 (/ stem-thickness 3))))
425 (dot (ly:find-glyph-by-name font "dots-dot"))
426 (dotwid (interval-length (ly:stencil-extent dot X)))
427 (dots (and (> dot-count 0)
428 (apply ly:stencil-add
430 (ly:stencil-translate-axis
431 dot (* (+ 1 (* 2 x)) dotwid) X) )
432 (iota dot-count 1)))))
433 (flaggl (and (> log 2)
434 (ly:stencil-translate
435 (ly:find-glyph-by-name font
436 (string-append "flags-"
437 (if (> dir 0) "u" "d")
438 (number->string log)))
439 (cons (+ attachx (/ stem-thickness 2)) stemy)))))
441 (set! stem-glyph (ly:stencil-add flaggl stem-glyph)))
442 (if (ly:stencil? stem-glyph)
443 (set! stem-glyph (ly:stencil-add stem-glyph head-glyph))
444 (set! stem-glyph head-glyph))
445 (if (ly:stencil? dots)
448 (ly:stencil-translate-axis dots
449 (+ (if (and (> dir 0) (> log 2))
452 ;; huh ? why not necessary?
453 ;;(cdr (ly:stencil-extent head-glyph X))
459 (use-modules (ice-9 regex))
462 (let ((divisor (log 2)))
463 (lambda (z) (inexact->exact (/ (log z) divisor)))))
465 (define (parse-simple-duration duration-string)
466 "Parse the `duration-string', e.g. ''4..'' or ''breve.'', and return a (log dots) list."
467 (let ((match (regexp-exec (make-regexp "(breve|longa|maxima|[0-9]+)(\\.*)") duration-string)))
468 (if (and match (string=? duration-string (match:substring match 0)))
469 (let ((len (match:substring match 1))
470 (dots (match:substring match 2)))
471 (list (cond ((string=? len "breve") -1)
472 ((string=? len "longa") -2)
473 ((string=? len "maxima") -3)
474 (else (log2 (string->number len))))
475 (if dots (string-length dots) 0)))
476 (error "This is not a valid duration string:" duration-string))))
478 (def-markup-command (note layout props duration dir) (string? number?)
479 "This produces a note with a stem pointing in @var{dir} direction, with
480 the @var{duration} for the note head type and augmentation dots. For
481 example, @code{\\note #\"4.\" #-0.75} creates a dotted quarter note, with
482 a shortened down stem."
484 (let ((parsed (parse-simple-duration duration)))
485 (note-by-number-markup layout props (car parsed) (cadr parsed) dir)))
487 (def-markup-command (normal-size-super layout props arg) (markup?)
488 "Set @var{arg} in superscript with a normal font size."
490 (ly:stencil-translate-axis (interpret-markup
493 (* 0.5 (chain-assoc-get 'baseline-skip props))
496 (def-markup-command (super layout props arg) (markup?)
499 @cindex lowering text
501 @cindex translating text
503 @cindex @code{\\super}
506 Raising and lowering texts can be done with @code{\\super} and
509 @lilypond[verbatim,fragment,relative=1]
510 c1^\\markup { E \"=\" mc \\super \"2\" }
515 (ly:stencil-translate-axis
518 (cons `((font-size . ,(- (chain-assoc-get 'font-size props 0) 3))) props)
520 (* 0.5 (chain-assoc-get 'baseline-skip props))
523 (def-markup-command (translate layout props offset arg) (number-pair? markup?)
524 "This translates an object. Its first argument is a cons of numbers
526 A \\translate #(cons 2 -3) @{ B C @} D
528 This moves `B C' 2 spaces to the right, and 3 down, relative to its
529 surroundings. This command cannot be used to move isolated scripts
530 vertically, for the same reason that @code{\\raise} cannot be used for
535 (ly:stencil-translate (interpret-markup layout props arg)
538 (def-markup-command (sub layout props arg) (markup?)
539 "Set @var{arg} in subscript."
541 (ly:stencil-translate-axis
544 (cons `((font-size . ,(- (chain-assoc-get 'font-size props 0) 3))) props)
546 (* -0.5 (chain-assoc-get 'baseline-skip props))
549 (def-markup-command (normal-size-sub layout props arg) (markup?)
550 "Set @var{arg} in subscript, in a normal font size."
552 (ly:stencil-translate-axis
553 (interpret-markup layout props arg)
554 (* -0.5 (chain-assoc-get 'baseline-skip props))
557 (def-markup-command (hbracket layout props arg) (markup?)
558 "Draw horizontal brackets around @var{arg}."
559 (let ((th 0.1) ;; todo: take from GROB.
560 (m (interpret-markup layout props arg)))
561 (bracketify-stencil m X th (* 2.5 th) th)))
563 (def-markup-command (bracket layout props arg) (markup?)
564 "Draw vertical brackets around @var{arg}."
565 (let ((th 0.1) ;; todo: take from GROB.
566 (m (interpret-markup layout props arg)))
567 (bracketify-stencil m Y th (* 2.5 th) th)))
569 ;; todo: fix negative space
570 (def-markup-command (hspace layout props amount) (number?)
571 "This produces a invisible object taking horizontal space.
573 \\markup @{ A \\hspace #2.0 B @}
575 will put extra space between A and B, on top of the space that is
576 normally inserted before elements on a line.
579 (ly:make-stencil "" (cons 0 amount) '(-1 . 1) )
580 (ly:make-stencil "" (cons amount amount) '(-1 . 1))))
582 (def-markup-command (override layout props new-prop arg) (pair? markup?)
583 "Add the first argument in to the property list. Properties may be
584 any sort of property supported by @internalsref{font-interface} and
585 @internalsref{text-interface}, for example
588 \\override #'(font-family . married) \"bla\"
592 (interpret-markup layout (cons (list new-prop) props) arg))
594 (def-markup-command (smaller layout props arg) (markup?)
595 "Decrease the font size relative to current setting"
596 (let* ((fs (chain-assoc-get 'font-size props 0))
597 (entry (cons 'font-size (- fs 1))))
598 (interpret-markup layout (cons (list entry) props) arg)))
601 (def-markup-command (bigger layout props arg) (markup?)
602 "Increase the font size relative to current setting"
603 (let* ((fs (chain-assoc-get 'font-size props 0))
604 (entry (cons 'font-size (+ fs 1))))
605 (interpret-markup layout (cons (list entry) props) arg)))
607 (def-markup-command larger (markup?)
611 (def-markup-command (box layout props arg) (markup?)
612 "Draw a box round @var{arg}. Looks at @code{thickness} and
613 @code{box-padding} properties to determine line thickness and padding
615 (let ((th (chain-assoc-get 'thickness props 0.1))
616 (pad (chain-assoc-get 'box-padding props 0.2))
617 (m (interpret-markup layout props arg)))
618 (box-stencil m th pad)))
620 ;FIXME: is this working?
621 (def-markup-command (strut layout props) ()
623 "Create a box of the same height as the space in the current font."
625 (let ((m (Text_interface::interpret_markup layout props " ")))
626 (ly:stencil-set-extent! m X '(1000 . -1000))
629 (define number->mark-letter-vector (make-vector 25 #\A))
634 (if (= i (- (char->integer #\I) (char->integer #\A)))
636 (vector-set! number->mark-letter-vector j
637 (integer->char (+ i (char->integer #\A)))))
639 (define (number->markletter-string n)
640 "Double letters for big marks."
642 ((l (vector-length number->mark-letter-vector)))
645 (string-append (number->markletter-string (1- (quotient n l)))
646 (number->markletter-string (remainder n l)))
647 (make-string 1 (vector-ref number->mark-letter-vector n)))))
650 (def-markup-command (markletter layout props num) (integer?)
651 "Make a markup letter for @var{num}. The letters start with A to Z
652 (skipping I), and continues with double letters."
654 (Text_interface::interpret_markup layout props (number->markletter-string num)))
659 (def-markup-command (bracketed-y-column layout props indices args)
661 "Make a column of the markups in @var{args}, putting brackets around
662 the elements marked in @var{indices}, which is a list of numbers."
664 (define (sublist l start stop)
665 (take (drop l start) (- (1+ stop) start)) )
667 (define (stencil-list-extent ss axis)
669 (apply min (map (lambda (x) (car (ly:stencil-extent x axis))) ss))
670 (apply max (map (lambda (x) (cdr (ly:stencil-extent x axis))) ss))))
672 (define (stack-stencils stencils bskip last-stencil)
674 ((null? stencils) '())
677 (stack-stencils (cdr stencils) bskip (car stencils))))
680 ((orig (car stencils))
681 (dir (chain-assoc-get 'direction props DOWN))
682 (new (ly:stencil-moved-to-edge last-stencil Y dir
687 (cons new (stack-stencils (cdr stencils) bskip new))))
690 (define (make-brackets stencils indices acc)
693 (pair? (cdr indices)))
695 ((encl (sublist stencils (car indices) (cadr indices)))
696 (x-ext (stencil-list-extent encl X))
697 (y-ext (stencil-list-extent encl Y))
700 (protusion (* 2.5 thick))
702 (ly:stencil-translate-axis
703 (ly:bracket Y y-ext thick protusion)
704 (- (car x-ext) pad) X))
705 (rb (ly:stencil-translate-axis
706 (ly:bracket Y y-ext thick (- protusion))
707 (+ (cdr x-ext) pad) X))
711 stencils (cddr indices)
725 (chain-assoc-get 'baseline-skip props))
726 (stacked (stack-stencils stencils 1.25 #f))
727 (brackets (make-brackets stacked indices '()))
730 (apply ly:stencil-add
731 (append stacked brackets)