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
38 ;; TODO: use font recoding.
40 ;; (map make-word-markup (string-tokenize str)))))
42 (define-public empty-markup
43 (make-simple-markup ""))
45 ;;(def-markup-command (fill-line paper props line-width markups)
46 ;; (number? markup-list?)
47 ;; no parser tag -- should make number? markuk-list? thingy
48 (def-markup-command (fill-line paper props markups)
50 "Put @var{markups} in a horizontal line of width @var{line-width}.
51 The markups are spaced/flushed to fill the entire line."
53 (let* ((stencils (map (lambda (x) (interpret-markup paper props x))
55 (text-width (apply + (map interval-length
57 (ly:stencil-extent x X))
59 (word-count (length markups))
60 (word-space (chain-assoc-get 'word-space props))
61 (line-width (chain-assoc-get 'linewidth props))
62 (fill-space (if (< line-width text-width)
64 (/ (- line-width text-width)
65 (if (= word-count 1) 2 (- word-count 1)))))
66 (line-stencils (if (= word-count 1)
67 (map (lambda (x) (interpret-markup paper props x))
68 (list (make-simple-markup "")
69 (make-stencil-markup (car stencils))
70 (make-simple-markup "")))
72 (stack-stencil-line fill-space line-stencils)))
74 (define (font-markup qualifier value)
75 (lambda (paper props arg)
76 (interpret-markup paper
77 (prepend-alist-chain qualifier value props)
80 (def-markup-command (line paper props args) (markup-list?)
81 "Put @var{args} in a horizontal line. The property @code{word-space}
82 determines the space between each markup in @var{args}."
84 (chain-assoc-get 'word-space props)
85 (map (lambda (m) (interpret-markup paper props m)) args)))
87 (def-markup-command (combine paper props m1 m2) (markup? markup?)
88 "Print two markups on top of each other."
90 ((s1 (interpret-markup paper props m1))
91 (s2 (interpret-markup paper props m2)))
93 (ly:stencil-add s1 s2)))
95 (def-markup-command (finger paper props arg) (markup?)
96 "Set the argument as small numbers."
97 (interpret-markup paper
98 (cons '((font-size . -5) (font-family . number)) props)
101 (def-markup-command (fontsize paper props mag arg) (number? markup?)
102 "This sets the relative font size, e.g.
104 A \\fontsize #2 @{ B C @} D
108 This will enlarge the B and the C by two steps.
112 (prepend-alist-chain 'font-size mag props)
115 (def-markup-command (magnify paper props sz arg) (number? markup?)
116 "This sets the font magnification for the its argument. In the following
117 example, the middle A will be 10% larger:
119 A \\magnify #1.1 @{ A @} A
122 Note: magnification only works if a font-name is explicitly selected.
123 Use @code{\\fontsize} otherwise."
127 (prepend-alist-chain 'font-magnification sz props)
130 (def-markup-command (bold paper props arg) (markup?)
131 "Switch to bold font-series"
132 (interpret-markup paper (prepend-alist-chain 'font-series 'bold props) arg))
134 (def-markup-command (sans paper props arg) (markup?)
135 "Switch to the sans-serif family"
136 (interpret-markup paper (prepend-alist-chain 'font-family 'sans props) arg))
138 (def-markup-command (number paper props arg) (markup?)
139 "Set font family to @code{number}, which yields the font used for
140 time signatures and fingerings. This font only contains numbers and
141 some punctuation. It doesn't have any letters. "
142 (interpret-markup paper (prepend-alist-chain 'font-encoding 'fetaNumber props) arg))
144 (def-markup-command (roman paper props arg) (markup?)
145 "Set font family to @code{roman}."
146 (interpret-markup paper (prepend-alist-chain 'font-family 'roman props) arg))
148 (def-markup-command (huge paper props arg) (markup?)
149 "Set font size to +2."
150 (interpret-markup paper (prepend-alist-chain 'font-size 2 props) arg))
152 (def-markup-command (large paper props arg) (markup?)
153 "Set font size to +1."
154 (interpret-markup paper (prepend-alist-chain 'font-size 1 props) arg))
156 (def-markup-command (normalsize paper props arg) (markup?)
157 "Set font size to default."
158 (interpret-markup paper (prepend-alist-chain 'font-size 0 props) arg))
160 (def-markup-command (small paper props arg) (markup?)
161 "Set font size to -1."
162 (interpret-markup paper (prepend-alist-chain 'font-size -1 props) arg))
164 (def-markup-command (tiny paper props arg) (markup?)
165 "Set font size to -2."
166 (interpret-markup paper (prepend-alist-chain 'font-size -2 props) arg))
168 (def-markup-command (teeny paper props arg) (markup?)
169 "Set font size to -3."
170 (interpret-markup paper (prepend-alist-chain 'font-size -3 props) arg))
172 (def-markup-command (caps paper props arg) (markup?)
173 "Set font shape to @code{caps}."
174 (interpret-markup paper (prepend-alist-chain 'font-shape 'caps props) arg))
176 (def-markup-command (latin-i paper props arg) (markup?)
177 "TEST latin1 encoding."
178 (interpret-markup paper (prepend-alist-chain 'font-shape 'latin1 props) arg))
180 (def-markup-command (dynamic paper props arg) (markup?)
181 "Use the dynamic font. This font only contains @b{s}, @b{f}, @b{m},
182 @b{z}, @b{p}, and @b{r}. When producing phrases, like ``pi@`{u} @b{f}'', the
183 normal words (like ``pi@`{u}'') should be done in a different font. The
184 recommend font for this is bold and italic"
186 paper (prepend-alist-chain 'font-encoding 'fetaDynamic props) arg))
188 (def-markup-command (italic paper props arg) (markup?)
189 "Use italic @code{font-shape} for @var{arg}. "
190 (interpret-markup paper (prepend-alist-chain 'font-shape 'italic props) arg))
192 (def-markup-command (typewriter paper props arg) (markup?)
193 "Use @code{font-family} typewriter for @var{arg}."
195 paper (prepend-alist-chain 'font-family 'typewriter props) arg))
197 (def-markup-command (upright paper props arg) (markup?)
198 "Set font shape to @code{upright}."
200 paper (prepend-alist-chain 'font-shape 'upright props) arg))
202 (def-markup-command (doublesharp paper props) ()
203 "Draw a double sharp symbol."
205 (interpret-markup paper props (markup #:musicglyph "accidentals-4")))
206 (def-markup-command (sesquisharp paper props) ()
207 "Draw a 3/2 sharp symbol."
208 (interpret-markup paper props (markup #:musicglyph "accidentals-3")))
210 (def-markup-command (sharp paper props) ()
211 "Draw a sharp symbol."
212 (interpret-markup paper props (markup #:musicglyph "accidentals-2")))
213 (def-markup-command (semisharp paper props) ()
214 "Draw a semi sharp symbol."
215 (interpret-markup paper props (markup #:musicglyph "accidentals-1")))
216 (def-markup-command (natural paper props) ()
217 "Draw a natural symbol."
219 (interpret-markup paper props (markup #:musicglyph "accidentals-0")))
220 (def-markup-command (semiflat paper props) ()
222 (interpret-markup paper props (markup #:musicglyph "accidentals--1")))
223 (def-markup-command (flat paper props) ()
224 "Draw a flat symbol."
226 (interpret-markup paper props (markup #:musicglyph "accidentals--2")))
227 (def-markup-command (sesquiflat paper props) ()
228 "Draw a 3/2 flat symbol."
230 (interpret-markup paper props (markup #:musicglyph "accidentals--3")))
231 (def-markup-command (doubleflat paper props) ()
232 "Draw a double flat symbol."
234 (interpret-markup paper props (markup #:musicglyph "accidentals--4")))
237 (def-markup-command (column paper props args) (markup-list?)
238 "Stack the markups in @var{args} vertically."
240 -1 0.0 (chain-assoc-get 'baseline-skip props)
241 (map (lambda (m) (interpret-markup paper props m)) args)))
243 (def-markup-command (dir-column paper props args) (markup-list?)
244 "Make a column of args, going up or down, depending on the setting
245 of the @code{#'direction} layout property."
246 (let* ((dir (chain-assoc-get 'direction props)))
248 (if (number? dir) dir -1)
250 (chain-assoc-get 'baseline-skip props)
251 (map (lambda (x) (interpret-markup paper props x)) args))))
253 (def-markup-command (center-align paper props args) (markup-list?)
254 "Put @code{args} in a centered column. "
255 (let* ((mols (map (lambda (x) (interpret-markup paper props x)) args))
256 (cmols (map (lambda (x) (ly:stencil-align-to! x X CENTER)) mols)))
257 (stack-lines -1 0.0 (chain-assoc-get 'baseline-skip props) mols)))
259 (def-markup-command (vcenter paper props arg) (markup?)
260 "Align @code{arg} to its center. "
261 (let* ((mol (interpret-markup paper props arg)))
262 (ly:stencil-align-to! mol Y CENTER)
265 (def-markup-command (right-align paper props arg) (markup?)
266 (let* ((m (interpret-markup paper props arg)))
267 (ly:stencil-align-to! m X RIGHT)
270 (def-markup-command (left-align paper props arg) (markup?)
271 "Align @var{arg} on its left edge. "
273 (let* ((m (interpret-markup paper props arg)))
274 (ly:stencil-align-to! m X LEFT)
277 (def-markup-command (general-align paper props axis dir arg) (integer? number? markup?)
278 "Align @var{arg} in @var{axis} direction to the @var{dir} side."
279 (let* ((m (interpret-markup paper props arg)))
281 (ly:stencil-align-to! m axis dir)
285 (def-markup-command (halign paper props dir arg) (number? markup?)
286 "Set horizontal alignment. If @var{dir} is -1, then it is
287 left-aligned, while+1 is right. Values in between interpolate alignment
291 (let* ((m (interpret-markup paper props arg)))
292 (ly:stencil-align-to! m X dir)
295 (def-markup-command (musicglyph paper props glyph-name) (string?)
296 "This is converted to a musical symbol, e.g. @code{\\musicglyph
297 #\"accidentals-0\"} will select the natural sign from the music font.
298 See @usermanref{The Feta font} for a complete listing of the possible glyphs.
300 (ly:find-glyph-by-name
301 (ly:paper-get-font paper (cons '((font-encoding . fetaMusic))
306 (def-markup-command (lookup paper props glyph-name) (string?)
307 "Lookup a glyph by name."
308 (ly:find-glyph-by-name (ly:paper-get-font paper props)
311 (def-markup-command (char paper props num) (integer?)
312 "This produces a single character, e.g. @code{\\char #65} produces the
314 (ly:get-glyph (ly:paper-get-font paper props) num))
316 (def-markup-command (raise paper props amount arg) (number? markup?)
318 This raises @var{arg}, by the distance @var{amount}.
319 A negative @var{amount} indicates lowering:
321 @lilypond[verbatim,fragment,relative=1]
322 c1^\\markup { C \\small \\raise #1.0 \\bold { \"9/7+\" }}
324 The argument to @code{\\raise} is the vertical displacement amount,
325 measured in (global) staff spaces. @code{\\raise} and @code{\\super}
326 raise objects in relation to their surrounding markups.
328 If the text object itself is positioned above or below the staff, then
329 @code{\\raise} cannot be used to move it, since the mechanism that
330 positions it next to the staff cancels any shift made with
331 @code{\\raise}. For vertical positioning, use the @code{padding}
332 and/or @code{extra-offset} properties. "
335 (ly:stencil-translate-axis (interpret-markup paper props arg)
338 (def-markup-command (fraction paper props arg1 arg2) (markup? markup?)
339 "Make a fraction of two markups."
341 (let* ((m1 (interpret-markup paper props arg1))
342 (m2 (interpret-markup paper props arg2)))
343 (ly:stencil-align-to! m1 X CENTER)
344 (ly:stencil-align-to! m2 X CENTER)
345 (let* ((x1 (ly:stencil-extent m1 X))
346 (x2 (ly:stencil-extent m2 X))
347 (line (ly:round-filled-box (interval-union x1 x2) '(-0.05 . 0.05) 0.0))
348 ;; should stack mols separately, to maintain LINE on baseline
349 (stack (stack-lines -1 0.2 0.6 (list m1 line m2))))
350 (ly:stencil-align-to! stack Y CENTER)
351 (ly:stencil-align-to! stack X LEFT)
352 ;; should have EX dimension
354 (ly:stencil-translate-axis stack 0.75 Y))))
357 ;; TODO: better syntax.
359 (def-markup-command (note-by-number paper props log dot-count dir) (number? number? number?)
360 "Construct a note symbol, with stem. By using fractional values for
361 @var{dir}, you can obtain longer or shorter stems."
363 (let* ((font (ly:paper-get-font paper (cons '((font-encoding . fetaMusic)) props)))
364 (stemlen (max 3 (- log 1)))
365 (headgl (ly:find-glyph-by-name
367 (string-append "noteheads-" (number->string (min log 2)))))
369 (stemy (* dir stemlen))
370 (attachx (if (> dir 0)
371 (- (cdr (ly:stencil-extent headgl X)) stemth)
373 (attachy (* dir 0.28))
374 (stemgl (and (> log 0)
376 (cons attachx (+ attachx stemth))
377 (cons (min stemy attachy)
380 (dot (ly:find-glyph-by-name font "dots-dot"))
381 (dotwid (interval-length (ly:stencil-extent dot X)))
382 (dots (and (> dot-count 0)
383 (apply ly:stencil-add
385 (ly:stencil-translate-axis
386 dot (* (+ 1 (* 2 x)) dotwid) X) )
387 (iota dot-count 1)))))
388 (flaggl (and (> log 2)
389 (ly:stencil-translate
390 (ly:find-glyph-by-name font
391 (string-append "flags-"
392 (if (> dir 0) "u" "d")
393 (number->string log)))
394 (cons (+ attachx (/ stemth 2)) stemy)))))
396 (set! stemgl (ly:stencil-add flaggl stemgl)))
397 (if (ly:stencil? stemgl)
398 (set! stemgl (ly:stencil-add stemgl headgl))
399 (set! stemgl headgl))
400 (if (ly:stencil? dots)
403 (ly:stencil-translate-axis dots
404 (+ (if (and (> dir 0) (> log 2))
407 ;; huh ? why not necessary?
408 ;;(cdr (ly:stencil-extent headgl X))
414 (use-modules (ice-9 regex))
417 (let ((divisor (log 2)))
418 (lambda (z) (inexact->exact (/ (log z) divisor)))))
420 (define (parse-simple-duration duration-string)
421 "Parse the `duration-string', e.g. ''4..'' or ''breve.'', and return a (log dots) list."
422 (let ((match (regexp-exec (make-regexp "(breve|longa|maxima|[0-9]+)(\\.*)") duration-string)))
423 (if (and match (string=? duration-string (match:substring match 0)))
424 (let ((len (match:substring match 1))
425 (dots (match:substring match 2)))
426 (list (cond ((string=? len "breve") -1)
427 ((string=? len "longa") -2)
428 ((string=? len "maxima") -3)
429 (else (log2 (string->number len))))
430 (if dots (string-length dots) 0)))
431 (error "This is not a valid duration string:" duration-string))))
433 (def-markup-command (note paper props duration dir) (string? number?)
434 "This produces a note with a stem pointing in @var{dir} direction, with
435 the @var{duration} for the note head type and augmentation dots. For
436 example, @code{\\note #\"4.\" #-0.75} creates a dotted quarter note, with
437 a shortened down stem."
439 (let ((parsed (parse-simple-duration duration)))
440 (note-by-number-markup paper props (car parsed) (cadr parsed) dir)))
442 (def-markup-command (normal-size-super paper props arg) (markup?)
443 "A superscript which does not use a smaller font."
445 (ly:stencil-translate-axis (interpret-markup
448 (* 0.5 (chain-assoc-get 'baseline-skip props))
451 (def-markup-command (super paper props arg) (markup?)
454 @cindex lowering text
456 @cindex translating text
458 @cindex @code{\\super}
461 Raising and lowering texts can be done with @code{\\super} and
464 @lilypond[verbatim,fragment,relative=1]
465 c1^\\markup { E \"=\" mc \\super \"2\" }
470 (ly:stencil-translate-axis
473 (cons `((font-size . ,(- (chain-assoc-get 'font-size props 0) 3))) props)
475 (* 0.5 (chain-assoc-get 'baseline-skip props))
478 (def-markup-command (translate paper props offset arg) (number-pair? markup?)
479 "This translates an object. Its first argument is a cons of numbers
481 A \\translate #(cons 2 -3) @{ B C @} D
483 This moves `B C' 2 spaces to the right, and 3 down, relative to its
484 surroundings. This command cannot be used to move isolated scripts
485 vertically, for the same reason that @code{\\raise} cannot be used for
489 (ly:stencil-translate (interpret-markup paper props arg)
492 (def-markup-command (sub paper props arg) (markup?)
493 "Set @var{arg} in subscript."
495 (ly:stencil-translate-axis
498 (cons `((font-size . ,(- (chain-assoc-get 'font-size props 0) 3))) props)
500 (* -0.5 (chain-assoc-get 'baseline-skip props))
503 (def-markup-command (normal-size-sub paper props arg) (markup?)
504 "Set @var{arg} in subscript, in a normal font size."
506 (ly:stencil-translate-axis
507 (interpret-markup paper props arg)
508 (* -0.5 (chain-assoc-get 'baseline-skip props))
511 (def-markup-command (hbracket paper props arg) (markup?)
512 "Draw horizontal brackets around @var{arg}."
513 (let ((th 0.1) ;; todo: take from GROB.
514 (m (interpret-markup paper props arg)))
515 (bracketify-stencil m X th (* 2.5 th) th)))
517 (def-markup-command (bracket paper props arg) (markup?)
518 "Draw vertical brackets around @var{arg}."
519 (let ((th 0.1) ;; todo: take from GROB.
520 (m (interpret-markup paper props arg)))
521 (bracketify-stencil m Y th (* 2.5 th) th)))
523 ;; todo: fix negative space
524 (def-markup-command (hspace paper props amount) (number?)
525 "This produces a invisible object taking horizontal space.
527 \\markup @{ A \\hspace #2.0 B @}
529 will put extra space between A and B, on top of the space that is
530 normally inserted before elements on a line.
533 (ly:make-stencil "" (cons 0 amount) '(-1 . 1) )
534 (ly:make-stencil "" (cons amount amount) '(-1 . 1))))
536 (def-markup-command (override paper props new-prop arg) (pair? markup?)
537 "Add the first argument in to the property list. Properties may be
538 any sort of property supported by @internalsref{font-interface} and
539 @internalsref{text-interface}, for example
542 \\override #'(font-family . married) \"bla\"
546 (interpret-markup paper (cons (list new-prop) props) arg))
548 (def-markup-command (smaller paper props arg) (markup?)
549 "Decrease the font size relative to current setting"
550 (let* ((fs (chain-assoc-get 'font-size props 0))
551 (entry (cons 'font-size (- fs 1))))
552 (interpret-markup paper (cons (list entry) props) arg)))
555 (def-markup-command (bigger paper props arg) (markup?)
556 "Increase the font size relative to current setting"
557 (let* ((fs (chain-assoc-get 'font-size props 0))
558 (entry (cons 'font-size (+ fs 1))))
559 (interpret-markup paper (cons (list entry) props) arg)))
561 (def-markup-command larger (markup?)
564 (def-markup-command (box paper props arg) (markup?)
565 "Draw a box round @var{arg}"
569 (m (interpret-markup paper props arg)))
570 (box-stencil m th pad)))
572 (def-markup-command (strut paper props) ()
574 "Create a box of the same height as the space in the current font.
576 FIXME: is this working?
579 (let ((m (Text_item::interpret_markup paper props " ")))
580 (ly:stencil-set-extent! m X '(1000 . -1000))
583 (define number->mark-letter-vector (make-vector 25 #\A))
588 (if (= i (- (char->integer #\I) (char->integer #\A)))
590 (vector-set! number->mark-letter-vector j
591 (integer->char (+ i (char->integer #\A)))))
593 (define (number->markletter-string n)
594 "Double letters for big marks."
596 ((l (vector-length number->mark-letter-vector)))
599 (string-append (number->markletter-string (1- (quotient n l)))
600 (number->markletter-string (remainder n l)))
601 (make-string 1 (vector-ref number->mark-letter-vector n)))))
604 (def-markup-command (markletter paper props num) (integer?)
605 "Make a markup letter for @var{num}. The letters start with A to Z
606 (skipping I), and continues with double letters."
608 (Text_item::interpret_markup paper props (number->markletter-string num)))
613 (def-markup-command (bracketed-y-column paper props indices args)
615 "Make a column of the markups in @var{args}, putting brackets around
616 the elements marked in @var{indices}, which is a list of numbers."
618 (define (sublist l start stop)
619 (take (drop l start) (- (1+ stop) start)) )
621 (define (stencil-list-extent ss axis)
623 (apply min (map (lambda (x) (car (ly:stencil-extent x axis))) ss))
624 (apply max (map (lambda (x) (cdr (ly:stencil-extent x axis))) ss))))
626 (define (stack-stencils stencils bskip last-stencil)
628 ((null? stencils) '())
631 (stack-stencils (cdr stencils) bskip (car stencils))))
634 ((orig (car stencils))
635 (dir (chain-assoc-get 'direction props DOWN))
636 (new (ly:stencil-moved-to-edge last-stencil Y dir
641 (cons new (stack-stencils (cdr stencils) bskip new))))
644 (define (make-brackets stencils indices acc)
647 (pair? (cdr indices)))
649 ((encl (sublist stencils (car indices) (cadr indices)))
650 (x-ext (stencil-list-extent encl X))
651 (y-ext (stencil-list-extent encl Y))
654 (protusion (* 2.5 thick))
656 (ly:stencil-translate-axis
657 (ly:bracket Y y-ext thick protusion)
658 (- (car x-ext) pad) X))
659 (rb (ly:stencil-translate-axis
660 (ly:bracket Y y-ext thick (- protusion))
661 (+ (cdr x-ext) pad) X))
665 stencils (cddr indices)
679 (chain-assoc-get 'baseline-skip props))
680 (stacked (stack-stencils stencils 1.25 #f))
681 (brackets (make-brackets stacked indices '()))
684 (apply ly:stencil-add
685 (append stacked brackets)