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 paper props stil) (ly:stencil?)
19 (def-markup-command (score paper props score) (ly:score?)
21 ((systems (ly:score-embedded-format score paper))
22 (1st (vector-ref systems 0))
23 (stencil (ly:paper-system-stencil 1st)) )
25 (ly:stencil-align-to! stencil Y CENTER)
28 (def-markup-command (simple paper props str) (string?)
29 "A simple text string; @code{\\markup @{ foo @}} is equivalent with
30 @code{\\markup @{ \\simple #\"foo\" @}}."
31 (interpret-markup paper props str))
33 (def-markup-command (encoded-simple paper props sym str) (symbol? string?)
34 "A text string, encoded with encoding @var{sym}. "
35 (Text_item::interpret_string paper
39 ;; TODO: use font recoding.
41 ;; (map make-word-markup (string-tokenize str)))))
43 (define-public empty-markup
44 (make-simple-markup ""))
47 (def-markup-command (postscript paper props str)
48 "This inserts @var{str} directly into the output as a PostScript command string."
51 (list 'embedded-ps str)
54 ;;(def-markup-command (fill-line paper props line-width markups)
55 ;; (number? markup-list?)
56 ;; no parser tag -- should make number? markuk-list? thingy
57 (def-markup-command (fill-line paper props markups)
59 "Put @var{markups} in a horizontal line of width @var{line-width}.
60 The markups are spaced/flushed to fill the entire line."
62 (let* ((stencils (map (lambda (x) (interpret-markup paper props x))
64 (text-width (apply + (map interval-length
66 (ly:stencil-extent x X))
68 (word-count (length markups))
69 (word-space (chain-assoc-get 'word-space props))
70 (line-width (chain-assoc-get 'linewidth props))
71 (fill-space (if (< line-width text-width)
73 (/ (- line-width text-width)
74 (if (= word-count 1) 2 (- word-count 1)))))
75 (line-stencils (if (= word-count 1)
76 (map (lambda (x) (interpret-markup paper props x))
77 (list (make-simple-markup "")
78 (make-stencil-markup (car stencils))
79 (make-simple-markup "")))
81 (stack-stencil-line fill-space line-stencils)))
83 (define (font-markup qualifier value)
84 (lambda (paper props arg)
85 (interpret-markup paper
86 (prepend-alist-chain qualifier value props)
89 (def-markup-command (line paper props args) (markup-list?)
90 "Put @var{args} in a horizontal line. The property @code{word-space}
91 determines the space between each markup in @var{args}."
93 (chain-assoc-get 'word-space props)
94 (map (lambda (m) (interpret-markup paper props m)) args)))
96 (def-markup-command (combine paper props m1 m2) (markup? markup?)
97 "Print two markups on top of each other."
99 ((s1 (interpret-markup paper props m1))
100 (s2 (interpret-markup paper props m2)))
102 (ly:stencil-add s1 s2)))
104 (def-markup-command (finger paper props arg) (markup?)
105 "Set the argument as small numbers."
106 (interpret-markup paper
107 (cons '((font-size . -5) (font-family . number)) props)
110 (def-markup-command (fontsize paper props mag arg) (number? markup?)
111 "This sets the relative font size, e.g.
113 A \\fontsize #2 @{ B C @} D
117 This will enlarge the B and the C by two steps.
121 (prepend-alist-chain 'font-size mag props)
124 (def-markup-command (magnify paper props sz arg) (number? markup?)
125 "This sets the font magnification for the its argument. In the following
126 example, the middle A will be 10% larger:
128 A \\magnify #1.1 @{ A @} A
131 Note: magnification only works if a font-name is explicitly selected.
132 Use @code{\\fontsize} otherwise."
136 (prepend-alist-chain 'font-magnification sz props)
139 (def-markup-command (bold paper props arg) (markup?)
140 "Switch to bold font-series"
141 (interpret-markup paper (prepend-alist-chain 'font-series 'bold props) arg))
143 (def-markup-command (sans paper props arg) (markup?)
144 "Switch to the sans-serif family"
145 (interpret-markup paper (prepend-alist-chain 'font-family 'sans props) arg))
147 (def-markup-command (number paper props arg) (markup?)
148 "Set font family to @code{number}, which yields the font used for
149 time signatures and fingerings. This font only contains numbers and
150 some punctuation. It doesn't have any letters. "
151 (interpret-markup paper (prepend-alist-chain 'font-encoding 'fetaNumber props) arg))
153 (def-markup-command (roman paper props arg) (markup?)
154 "Set font family to @code{roman}."
155 (interpret-markup paper (prepend-alist-chain 'font-family 'roman props) arg))
157 (def-markup-command (huge paper props arg) (markup?)
158 "Set font size to +2."
159 (interpret-markup paper (prepend-alist-chain 'font-size 2 props) arg))
161 (def-markup-command (large paper props arg) (markup?)
162 "Set font size to +1."
163 (interpret-markup paper (prepend-alist-chain 'font-size 1 props) arg))
165 (def-markup-command (normalsize paper props arg) (markup?)
166 "Set font size to default."
167 (interpret-markup paper (prepend-alist-chain 'font-size 0 props) arg))
169 (def-markup-command (small paper props arg) (markup?)
170 "Set font size to -1."
171 (interpret-markup paper (prepend-alist-chain 'font-size -1 props) arg))
173 (def-markup-command (tiny paper props arg) (markup?)
174 "Set font size to -2."
175 (interpret-markup paper (prepend-alist-chain 'font-size -2 props) arg))
177 (def-markup-command (teeny paper props arg) (markup?)
178 "Set font size to -3."
179 (interpret-markup paper (prepend-alist-chain 'font-size -3 props) arg))
181 (def-markup-command (caps paper props arg) (markup?)
182 "Set font shape to @code{caps}."
183 (interpret-markup paper (prepend-alist-chain 'font-shape 'caps props) arg))
185 (def-markup-command (latin-i paper props arg) (markup?)
186 "TEST latin1 encoding."
187 (interpret-markup paper (prepend-alist-chain 'font-shape 'latin1 props) arg))
189 (def-markup-command (dynamic paper props arg) (markup?)
190 "Use the dynamic font. This font only contains @b{s}, @b{f}, @b{m},
191 @b{z}, @b{p}, and @b{r}. When producing phrases, like ``pi@`{u} @b{f}'', the
192 normal words (like ``pi@`{u}'') should be done in a different font. The
193 recommend font for this is bold and italic"
195 paper (prepend-alist-chain 'font-encoding 'fetaDynamic props) arg))
197 (def-markup-command (italic paper props arg) (markup?)
198 "Use italic @code{font-shape} for @var{arg}. "
199 (interpret-markup paper (prepend-alist-chain 'font-shape 'italic props) arg))
201 (def-markup-command (typewriter paper props arg) (markup?)
202 "Use @code{font-family} typewriter for @var{arg}."
204 paper (prepend-alist-chain 'font-family 'typewriter props) arg))
206 (def-markup-command (upright paper props arg) (markup?)
207 "Set font shape to @code{upright}."
209 paper (prepend-alist-chain 'font-shape 'upright props) arg))
211 (def-markup-command (doublesharp paper props) ()
212 "Draw a double sharp symbol."
214 (interpret-markup paper props (markup #:musicglyph "accidentals-4")))
215 (def-markup-command (sesquisharp paper props) ()
216 "Draw a 3/2 sharp symbol."
217 (interpret-markup paper props (markup #:musicglyph "accidentals-3")))
219 (def-markup-command (sharp paper props) ()
220 "Draw a sharp symbol."
221 (interpret-markup paper props (markup #:musicglyph "accidentals-2")))
222 (def-markup-command (semisharp paper props) ()
223 "Draw a semi sharp symbol."
224 (interpret-markup paper props (markup #:musicglyph "accidentals-1")))
225 (def-markup-command (natural paper props) ()
226 "Draw a natural symbol."
228 (interpret-markup paper props (markup #:musicglyph "accidentals-0")))
229 (def-markup-command (semiflat paper props) ()
231 (interpret-markup paper props (markup #:musicglyph "accidentals--1")))
232 (def-markup-command (flat paper props) ()
233 "Draw a flat symbol."
235 (interpret-markup paper props (markup #:musicglyph "accidentals--2")))
236 (def-markup-command (sesquiflat paper props) ()
237 "Draw a 3/2 flat symbol."
239 (interpret-markup paper props (markup #:musicglyph "accidentals--3")))
240 (def-markup-command (doubleflat paper props) ()
241 "Draw a double flat symbol."
243 (interpret-markup paper props (markup #:musicglyph "accidentals--4")))
246 (def-markup-command (column paper props args) (markup-list?)
247 "Stack the markups in @var{args} vertically."
249 -1 0.0 (chain-assoc-get 'baseline-skip props)
250 (map (lambda (m) (interpret-markup paper props m)) args)))
252 (def-markup-command (dir-column paper props args) (markup-list?)
253 "Make a column of args, going up or down, depending on the setting
254 of the @code{#'direction} layout property."
255 (let* ((dir (chain-assoc-get 'direction props)))
257 (if (number? dir) dir -1)
259 (chain-assoc-get 'baseline-skip props)
260 (map (lambda (x) (interpret-markup paper props x)) args))))
262 (def-markup-command (center-align paper props args) (markup-list?)
263 "Put @code{args} in a centered column. "
264 (let* ((mols (map (lambda (x) (interpret-markup paper props x)) args))
265 (cmols (map (lambda (x) (ly:stencil-align-to! x X CENTER)) mols)))
266 (stack-lines -1 0.0 (chain-assoc-get 'baseline-skip props) mols)))
268 (def-markup-command (vcenter paper props arg) (markup?)
269 "Align @code{arg} to its center. "
270 (let* ((mol (interpret-markup paper props arg)))
271 (ly:stencil-align-to! mol Y CENTER)
274 (def-markup-command (right-align paper props arg) (markup?)
275 (let* ((m (interpret-markup paper props arg)))
276 (ly:stencil-align-to! m X RIGHT)
279 (def-markup-command (left-align paper props arg) (markup?)
280 "Align @var{arg} on its left edge. "
282 (let* ((m (interpret-markup paper props arg)))
283 (ly:stencil-align-to! m X LEFT)
286 (def-markup-command (general-align paper props axis dir arg) (integer? number? markup?)
287 "Align @var{arg} in @var{axis} direction to the @var{dir} side."
288 (let* ((m (interpret-markup paper props arg)))
290 (ly:stencil-align-to! m axis dir)
294 (def-markup-command (halign paper props dir arg) (number? markup?)
295 "Set horizontal alignment. If @var{dir} is -1, then it is
296 left-aligned, while+1 is right. Values in between interpolate alignment
300 (let* ((m (interpret-markup paper props arg)))
301 (ly:stencil-align-to! m X dir)
304 (def-markup-command (musicglyph paper props glyph-name) (string?)
305 "This is converted to a musical symbol, e.g. @code{\\musicglyph
306 #\"accidentals-0\"} will select the natural sign from the music font.
307 See @usermanref{The Feta font} for a complete listing of the possible glyphs.
309 (ly:find-glyph-by-name
310 (ly:paper-get-font paper (cons '((font-encoding . fetaMusic))
315 (def-markup-command (lookup paper props glyph-name) (string?)
316 "Lookup a glyph by name."
317 (ly:find-glyph-by-name (ly:paper-get-font paper props)
320 (def-markup-command (char paper props num) (integer?)
321 "This produces a single character, e.g. @code{\\char #65} produces the
323 (ly:get-glyph (ly:paper-get-font paper props) num))
325 (def-markup-command (raise paper props amount arg) (number? markup?)
327 This raises @var{arg}, by the distance @var{amount}.
328 A negative @var{amount} indicates lowering:
330 @lilypond[verbatim,fragment,relative=1]
331 c1^\\markup { C \\small \\raise #1.0 \\bold { \"9/7+\" }}
333 The argument to @code{\\raise} is the vertical displacement amount,
334 measured in (global) staff spaces. @code{\\raise} and @code{\\super}
335 raise objects in relation to their surrounding markups.
337 If the text object itself is positioned above or below the staff, then
338 @code{\\raise} cannot be used to move it, since the mechanism that
339 positions it next to the staff cancels any shift made with
340 @code{\\raise}. For vertical positioning, use the @code{padding}
341 and/or @code{extra-offset} properties. "
344 (ly:stencil-translate-axis (interpret-markup paper props arg)
347 (def-markup-command (fraction paper props arg1 arg2) (markup? markup?)
348 "Make a fraction of two markups."
350 (let* ((m1 (interpret-markup paper props arg1))
351 (m2 (interpret-markup paper props arg2)))
352 (ly:stencil-align-to! m1 X CENTER)
353 (ly:stencil-align-to! m2 X CENTER)
354 (let* ((x1 (ly:stencil-extent m1 X))
355 (x2 (ly:stencil-extent m2 X))
356 (line (ly:round-filled-box (interval-union x1 x2) '(-0.05 . 0.05) 0.0))
357 ;; should stack mols separately, to maintain LINE on baseline
358 (stack (stack-lines -1 0.2 0.6 (list m1 line m2))))
359 (ly:stencil-align-to! stack Y CENTER)
360 (ly:stencil-align-to! stack X LEFT)
361 ;; should have EX dimension
363 (ly:stencil-translate-axis stack 0.75 Y))))
366 ;; TODO: better syntax.
368 (def-markup-command (note-by-number paper props log dot-count dir) (number? number? number?)
369 "Construct a note symbol, with stem. By using fractional values for
370 @var{dir}, you can obtain longer or shorter stems."
372 (let* ((font (ly:paper-get-font paper (cons '((font-encoding . fetaMusic)) props)))
373 (stemlen (max 3 (- log 1)))
374 (headgl (ly:find-glyph-by-name
376 (string-append "noteheads-" (number->string (min log 2)))))
378 (stemy (* dir stemlen))
379 (attachx (if (> dir 0)
380 (- (cdr (ly:stencil-extent headgl X)) stemth)
382 (attachy (* dir 0.28))
383 (stemgl (and (> log 0)
385 (cons attachx (+ attachx stemth))
386 (cons (min stemy attachy)
389 (dot (ly:find-glyph-by-name font "dots-dot"))
390 (dotwid (interval-length (ly:stencil-extent dot X)))
391 (dots (and (> dot-count 0)
392 (apply ly:stencil-add
394 (ly:stencil-translate-axis
395 dot (* (+ 1 (* 2 x)) dotwid) X) )
396 (iota dot-count 1)))))
397 (flaggl (and (> log 2)
398 (ly:stencil-translate
399 (ly:find-glyph-by-name font
400 (string-append "flags-"
401 (if (> dir 0) "u" "d")
402 (number->string log)))
403 (cons (+ attachx (/ stemth 2)) stemy)))))
405 (set! stemgl (ly:stencil-add flaggl stemgl)))
406 (if (ly:stencil? stemgl)
407 (set! stemgl (ly:stencil-add stemgl headgl))
408 (set! stemgl headgl))
409 (if (ly:stencil? dots)
412 (ly:stencil-translate-axis dots
413 (+ (if (and (> dir 0) (> log 2))
416 ;; huh ? why not necessary?
417 ;;(cdr (ly:stencil-extent headgl X))
423 (use-modules (ice-9 regex))
426 (let ((divisor (log 2)))
427 (lambda (z) (inexact->exact (/ (log z) divisor)))))
429 (define (parse-simple-duration duration-string)
430 "Parse the `duration-string', e.g. ''4..'' or ''breve.'', and return a (log dots) list."
431 (let ((match (regexp-exec (make-regexp "(breve|longa|maxima|[0-9]+)(\\.*)") duration-string)))
432 (if (and match (string=? duration-string (match:substring match 0)))
433 (let ((len (match:substring match 1))
434 (dots (match:substring match 2)))
435 (list (cond ((string=? len "breve") -1)
436 ((string=? len "longa") -2)
437 ((string=? len "maxima") -3)
438 (else (log2 (string->number len))))
439 (if dots (string-length dots) 0)))
440 (error "This is not a valid duration string:" duration-string))))
442 (def-markup-command (note paper props duration dir) (string? number?)
443 "This produces a note with a stem pointing in @var{dir} direction, with
444 the @var{duration} for the note head type and augmentation dots. For
445 example, @code{\\note #\"4.\" #-0.75} creates a dotted quarter note, with
446 a shortened down stem."
448 (let ((parsed (parse-simple-duration duration)))
449 (note-by-number-markup paper props (car parsed) (cadr parsed) dir)))
451 (def-markup-command (normal-size-super paper props arg) (markup?)
452 "A superscript which does not use a smaller font."
454 (ly:stencil-translate-axis (interpret-markup
457 (* 0.5 (chain-assoc-get 'baseline-skip props))
460 (def-markup-command (super paper props arg) (markup?)
463 @cindex lowering text
465 @cindex translating text
467 @cindex @code{\\super}
470 Raising and lowering texts can be done with @code{\\super} and
473 @lilypond[verbatim,fragment,relative=1]
474 c1^\\markup { E \"=\" mc \\super \"2\" }
479 (ly:stencil-translate-axis
482 (cons `((font-size . ,(- (chain-assoc-get 'font-size props 0) 3))) props)
484 (* 0.5 (chain-assoc-get 'baseline-skip props))
487 (def-markup-command (translate paper props offset arg) (number-pair? markup?)
488 "This translates an object. Its first argument is a cons of numbers
490 A \\translate #(cons 2 -3) @{ B C @} D
492 This moves `B C' 2 spaces to the right, and 3 down, relative to its
493 surroundings. This command cannot be used to move isolated scripts
494 vertically, for the same reason that @code{\\raise} cannot be used for
498 (ly:stencil-translate (interpret-markup paper props arg)
501 (def-markup-command (sub paper props arg) (markup?)
502 "Set @var{arg} in subscript."
504 (ly:stencil-translate-axis
507 (cons `((font-size . ,(- (chain-assoc-get 'font-size props 0) 3))) props)
509 (* -0.5 (chain-assoc-get 'baseline-skip props))
512 (def-markup-command (normal-size-sub paper props arg) (markup?)
513 "Set @var{arg} in subscript, in a normal font size."
515 (ly:stencil-translate-axis
516 (interpret-markup paper props arg)
517 (* -0.5 (chain-assoc-get 'baseline-skip props))
520 (def-markup-command (hbracket paper props arg) (markup?)
521 "Draw horizontal brackets around @var{arg}."
522 (let ((th 0.1) ;; todo: take from GROB.
523 (m (interpret-markup paper props arg)))
524 (bracketify-stencil m X th (* 2.5 th) th)))
526 (def-markup-command (bracket paper props arg) (markup?)
527 "Draw vertical brackets around @var{arg}."
528 (let ((th 0.1) ;; todo: take from GROB.
529 (m (interpret-markup paper props arg)))
530 (bracketify-stencil m Y th (* 2.5 th) th)))
532 ;; todo: fix negative space
533 (def-markup-command (hspace paper props amount) (number?)
534 "This produces a invisible object taking horizontal space.
536 \\markup @{ A \\hspace #2.0 B @}
538 will put extra space between A and B, on top of the space that is
539 normally inserted before elements on a line.
542 (ly:make-stencil "" (cons 0 amount) '(-1 . 1) )
543 (ly:make-stencil "" (cons amount amount) '(-1 . 1))))
545 (def-markup-command (override paper props new-prop arg) (pair? markup?)
546 "Add the first argument in to the property list. Properties may be
547 any sort of property supported by @internalsref{font-interface} and
548 @internalsref{text-interface}, for example
551 \\override #'(font-family . married) \"bla\"
555 (interpret-markup paper (cons (list new-prop) props) arg))
557 (def-markup-command (smaller paper props arg) (markup?)
558 "Decrease the font size relative to current setting"
559 (let* ((fs (chain-assoc-get 'font-size props 0))
560 (entry (cons 'font-size (- fs 1))))
561 (interpret-markup paper (cons (list entry) props) arg)))
564 (def-markup-command (bigger paper props arg) (markup?)
565 "Increase the font size relative to current setting"
566 (let* ((fs (chain-assoc-get 'font-size props 0))
567 (entry (cons 'font-size (+ fs 1))))
568 (interpret-markup paper (cons (list entry) props) arg)))
570 (def-markup-command larger (markup?)
573 (def-markup-command (box paper props arg) (markup?)
574 "Draw a box round @var{arg}"
578 (m (interpret-markup paper props arg)))
579 (box-stencil m th pad)))
581 (def-markup-command (strut paper props) ()
583 "Create a box of the same height as the space in the current font.
585 FIXME: is this working?
588 (let ((m (Text_item::interpret_markup paper props " ")))
589 (ly:stencil-set-extent! m X '(1000 . -1000))
592 (define number->mark-letter-vector (make-vector 25 #\A))
597 (if (= i (- (char->integer #\I) (char->integer #\A)))
599 (vector-set! number->mark-letter-vector j
600 (integer->char (+ i (char->integer #\A)))))
602 (define (number->markletter-string n)
603 "Double letters for big marks."
605 ((l (vector-length number->mark-letter-vector)))
608 (string-append (number->markletter-string (1- (quotient n l)))
609 (number->markletter-string (remainder n l)))
610 (make-string 1 (vector-ref number->mark-letter-vector n)))))
613 (def-markup-command (markletter paper props num) (integer?)
614 "Make a markup letter for @var{num}. The letters start with A to Z
615 (skipping I), and continues with double letters."
617 (Text_item::interpret_markup paper props (number->markletter-string num)))
622 (def-markup-command (bracketed-y-column paper props indices args)
624 "Make a column of the markups in @var{args}, putting brackets around
625 the elements marked in @var{indices}, which is a list of numbers."
627 (define (sublist l start stop)
628 (take (drop l start) (- (1+ stop) start)) )
630 (define (stencil-list-extent ss axis)
632 (apply min (map (lambda (x) (car (ly:stencil-extent x axis))) ss))
633 (apply max (map (lambda (x) (cdr (ly:stencil-extent x axis))) ss))))
635 (define (stack-stencils stencils bskip last-stencil)
637 ((null? stencils) '())
640 (stack-stencils (cdr stencils) bskip (car stencils))))
643 ((orig (car stencils))
644 (dir (chain-assoc-get 'direction props DOWN))
645 (new (ly:stencil-moved-to-edge last-stencil Y dir
650 (cons new (stack-stencils (cdr stencils) bskip new))))
653 (define (make-brackets stencils indices acc)
656 (pair? (cdr indices)))
658 ((encl (sublist stencils (car indices) (cadr indices)))
659 (x-ext (stencil-list-extent encl X))
660 (y-ext (stencil-list-extent encl Y))
663 (protusion (* 2.5 thick))
665 (ly:stencil-translate-axis
666 (ly:bracket Y y-ext thick protusion)
667 (- (car x-ext) pad) X))
668 (rb (ly:stencil-translate-axis
669 (ly:bracket Y y-ext thick (- protusion))
670 (+ (cdr x-ext) pad) X))
674 stencils (cddr indices)
688 (chain-assoc-get 'baseline-skip props))
689 (stacked (stack-stencils stencils 1.25 #f))
690 (brackets (make-brackets stacked indices '()))
693 (apply ly:stencil-add
694 (append stacked brackets)