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?)
18 (def-markup-command (simple paper props str) (string?)
19 "A simple text string; @code{\\markup @{ foo @}} is equivalent with
20 @code{\\markup @{ \\simple #\"foo\" @}}."
21 (interpret-markup paper props str))
23 (def-markup-command (encoded-simple paper props sym str) (symbol? string?)
24 "A text string, encoded with encoding @var{sym}. "
25 (Text_item::interpret_string paper
28 ;; TODO: use font recoding.
30 ;; (map make-word-markup (string-tokenize str)))))
32 (define-public empty-markup
33 (make-simple-markup ""))
35 ;;(def-markup-command (fill-line paper props line-width markups)
36 ;; (number? markup-list?)
37 ;; no parser tag -- should make number? markuk-list? thingy
38 (def-markup-command (fill-line paper props markups)
40 "Put @var{markups} in a horizontal line of width @var{line-width}.
41 The markups are spaced/flushed to fill the entire line."
43 (let* ((stencils (map (lambda (x) (interpret-markup paper props x))
45 (text-width (apply + (map interval-length
47 (ly:stencil-extent x X))
49 (word-count (length markups))
50 (word-space (chain-assoc-get 'word-space props))
51 (line-width (chain-assoc-get 'linewidth props))
52 (fill-space (if (< line-width text-width)
54 (/ (- line-width text-width)
55 (if (= word-count 1) 2 (- word-count 1)))))
56 (line-stencils (if (= word-count 1)
57 (map (lambda (x) (interpret-markup paper props x))
58 (list (make-simple-markup "")
60 (make-simple-markup "")))
62 (stack-stencil-line fill-space line-stencils)))
64 (define (font-markup qualifier value)
65 (lambda (paper props arg)
66 (interpret-markup paper
67 (prepend-alist-chain qualifier value props)
70 (def-markup-command (line paper props args) (markup-list?)
71 "Put @var{args} in a horizontal line. The property @code{word-space}
72 determines the space between each markup in @var{args}."
74 (chain-assoc-get 'word-space props)
75 (map (lambda (m) (interpret-markup paper props m)) args)))
77 (def-markup-command (combine paper props m1 m2) (markup? markup?)
78 "Print two markups on top of each other."
80 (interpret-markup paper props m1)
81 (interpret-markup paper props m2)))
83 (def-markup-command (finger paper props arg) (markup?)
84 "Set the argument as small numbers."
85 (interpret-markup paper
86 (cons '((font-size . -5) (font-family . number)) props)
89 (def-markup-command (fontsize paper props mag arg) (number? markup?)
90 "This sets the relative font size, e.g.
92 A \\fontsize #2 @{ B C @} D
96 This will enlarge the B and the C by two steps.
100 (prepend-alist-chain 'font-size mag props)
103 (def-markup-command (magnify paper props sz arg) (number? markup?)
104 "This sets the font magnification for the its argument. In the following
105 example, the middle A will be 10% larger:
107 A \\magnify #1.1 @{ A @} A
110 Note: magnification only works if a font-name is explicitly selected.
111 Use @code{\\fontsize} otherwise."
115 (prepend-alist-chain 'font-magnification sz props)
118 (def-markup-command (bold paper props arg) (markup?)
119 "Switch to bold font-series"
120 (interpret-markup paper (prepend-alist-chain 'font-series 'bold props) arg))
122 (def-markup-command (sans paper props arg) (markup?)
123 "Switch to the sans-serif family"
124 (interpret-markup paper (prepend-alist-chain 'font-family 'sans props) arg))
126 (def-markup-command (number paper props arg) (markup?)
127 "Set font family to @code{number}, which yields the font used for
128 time signatures and fingerings. This font only contains numbers and
129 some punctuation. It doesn't have any letters. "
130 (interpret-markup paper (prepend-alist-chain 'font-encoding 'fetaNumber props) arg))
132 (def-markup-command (roman paper props arg) (markup?)
133 "Set font family to @code{roman}."
134 (interpret-markup paper (prepend-alist-chain 'font-family 'roman props) arg))
136 (def-markup-command (huge paper props arg) (markup?)
137 "Set font size to +2."
138 (interpret-markup paper (prepend-alist-chain 'font-size 2 props) arg))
140 (def-markup-command (large paper props arg) (markup?)
141 "Set font size to +1."
142 (interpret-markup paper (prepend-alist-chain 'font-size 1 props) arg))
144 (def-markup-command (normalsize paper props arg) (markup?)
145 "Set font size to default."
146 (interpret-markup paper (prepend-alist-chain 'font-size 0 props) arg))
148 (def-markup-command (small paper props arg) (markup?)
149 "Set font size to -1."
150 (interpret-markup paper (prepend-alist-chain 'font-size -1 props) arg))
152 (def-markup-command (tiny paper props arg) (markup?)
153 "Set font size to -2."
154 (interpret-markup paper (prepend-alist-chain 'font-size -2 props) arg))
156 (def-markup-command (teeny paper props arg) (markup?)
157 "Set font size to -3."
158 (interpret-markup paper (prepend-alist-chain 'font-size -3 props) arg))
160 (def-markup-command (caps paper props arg) (markup?)
161 "Set font shape to @code{caps}."
162 (interpret-markup paper (prepend-alist-chain 'font-shape 'caps props) arg))
164 (def-markup-command (latin-i paper props arg) (markup?)
165 "TEST latin1 encoding."
166 (interpret-markup paper (prepend-alist-chain 'font-shape 'latin1 props) arg))
168 (def-markup-command (dynamic paper props arg) (markup?)
169 "Use the dynamic font. This font only contains @b{s}, @b{f}, @b{m},
170 @b{z}, @b{p}, and @b{r}. When producing phrases, like ``pi@`{u} @b{f}'', the
171 normal words (like ``pi@`{u}'') should be done in a different font. The
172 recommend font for this is bold and italic"
174 paper (prepend-alist-chain 'font-encoding 'fetaDynamic props) arg))
176 (def-markup-command (italic paper props arg) (markup?)
177 "Use italic @code{font-shape} for @var{arg}. "
178 (interpret-markup paper (prepend-alist-chain 'font-shape 'italic props) arg))
180 (def-markup-command (typewriter paper props arg) (markup?)
181 "Use @code{font-family} typewriter for @var{arg}."
183 paper (prepend-alist-chain 'font-family 'typewriter props) arg))
185 (def-markup-command (upright paper props arg) (markup?)
186 "Set font shape to @code{upright}."
188 paper (prepend-alist-chain 'font-shape 'upright props) arg))
190 (def-markup-command (doublesharp paper props) ()
191 "Draw a double sharp symbol."
193 (interpret-markup paper props (markup #:musicglyph "accidentals-4")))
194 (def-markup-command (sesquisharp paper props) ()
195 "Draw a 3/2 sharp symbol."
196 (interpret-markup paper props (markup #:musicglyph "accidentals-3")))
198 (def-markup-command (sharp paper props) ()
199 "Draw a sharp symbol."
200 (interpret-markup paper props (markup #:musicglyph "accidentals-2")))
201 (def-markup-command (semisharp paper props) ()
202 "Draw a semi sharp symbol."
203 (interpret-markup paper props (markup #:musicglyph "accidentals-1")))
204 (def-markup-command (natural paper props) ()
205 "Draw a natural symbol."
207 (interpret-markup paper props (markup #:musicglyph "accidentals-0")))
208 (def-markup-command (semiflat paper props) ()
210 (interpret-markup paper props (markup #:musicglyph "accidentals--1")))
211 (def-markup-command (flat paper props) ()
212 "Draw a flat symbol."
214 (interpret-markup paper props (markup #:musicglyph "accidentals--2")))
215 (def-markup-command (sesquiflat paper props) ()
216 "Draw a 3/2 flat symbol."
218 (interpret-markup paper props (markup #:musicglyph "accidentals--3")))
219 (def-markup-command (doubleflat paper props) ()
220 "Draw a double flat symbol."
222 (interpret-markup paper props (markup #:musicglyph "accidentals--4")))
225 (def-markup-command (column paper props args) (markup-list?)
226 "Stack the markups in @var{args} vertically."
228 -1 0.0 (chain-assoc-get 'baseline-skip props)
229 (map (lambda (m) (interpret-markup paper props m)) args)))
231 (def-markup-command (dir-column paper props args) (markup-list?)
232 "Make a column of args, going up or down, depending on the setting
233 of the @code{#'direction} layout property."
234 (let* ((dir (chain-assoc-get 'direction props)))
236 (if (number? dir) dir -1)
238 (chain-assoc-get 'baseline-skip props)
239 (map (lambda (x) (interpret-markup paper props x)) args))))
241 (def-markup-command (center-align paper props args) (markup-list?)
242 "Put @code{args} in a centered column. "
243 (let* ((mols (map (lambda (x) (interpret-markup paper props x)) args))
244 (cmols (map (lambda (x) (ly:stencil-align-to! x X CENTER)) mols)))
245 (stack-lines -1 0.0 (chain-assoc-get 'baseline-skip props) mols)))
247 (def-markup-command (vcenter paper props arg) (markup?)
248 "Align @code{arg} to its center. "
249 (let* ((mol (interpret-markup paper props arg)))
250 (ly:stencil-align-to! mol Y CENTER)
253 (def-markup-command (right-align paper props arg) (markup?)
254 (let* ((m (interpret-markup paper props arg)))
255 (ly:stencil-align-to! m X RIGHT)
258 (def-markup-command (left-align paper props arg) (markup?)
259 "Align @var{arg} on its left edge. "
261 (let* ((m (interpret-markup paper props arg)))
262 (ly:stencil-align-to! m X LEFT)
265 (def-markup-command (halign paper props dir arg) (number? markup?)
266 "Set horizontal alignment. If @var{dir} is -1, then it is
267 left-aligned, while+1 is right. Values in between interpolate alignment
271 (let* ((m (interpret-markup paper props arg)))
272 (ly:stencil-align-to! m X dir)
275 (def-markup-command (musicglyph paper props glyph-name) (string?)
276 "This is converted to a musical symbol, e.g. @code{\\musicglyph
277 #\"accidentals-0\"} will select the natural sign from the music font.
278 See @usermanref{The Feta font} for a complete listing of the possible glyphs.
280 (ly:find-glyph-by-name
281 (ly:paper-get-font paper (cons '((font-encoding . fetaMusic))
286 (def-markup-command (lookup paper props glyph-name) (string?)
287 "Lookup a glyph by name."
288 (ly:find-glyph-by-name (ly:paper-get-font paper props)
291 (def-markup-command (char paper props num) (integer?)
292 "This produces a single character, e.g. @code{\\char #65} produces the
294 (ly:get-glyph (ly:paper-get-font paper props) num))
296 (def-markup-command (raise paper props amount arg) (number? markup?)
298 This raises @var{arg}, by the distance @var{amount}.
299 A negative @var{amount} indicates lowering:
301 @lilypond[verbatim,fragment,relative=1]
302 c1^\\markup { C \\small \\raise #1.0 \\bold { \"9/7+\" }}
304 The argument to @code{\\raise} is the vertical displacement amount,
305 measured in (global) staff spaces. @code{\\raise} and @code{\\super}
306 raise objects in relation to their surrounding markups.
308 If the text object itself is positioned above or below the staff, then
309 @code{\\raise} cannot be used to move it, since the mechanism that
310 positions it next to the staff cancels any shift made with
311 @code{\\raise}. For vertical positioning, use the @code{padding}
312 and/or @code{extra-offset} properties. "
315 (ly:stencil-translate-axis (interpret-markup paper props arg)
318 (def-markup-command (fraction paper props arg1 arg2) (markup? markup?)
319 "Make a fraction of two markups."
321 (let* ((m1 (interpret-markup paper props arg1))
322 (m2 (interpret-markup paper props arg2)))
323 (ly:stencil-align-to! m1 X CENTER)
324 (ly:stencil-align-to! m2 X CENTER)
325 (let* ((x1 (ly:stencil-extent m1 X))
326 (x2 (ly:stencil-extent m2 X))
327 (line (ly:round-filled-box (interval-union x1 x2) '(-0.05 . 0.05) 0.0))
328 ;; should stack mols separately, to maintain LINE on baseline
329 (stack (stack-lines -1 0.2 0.6 (list m1 line m2))))
330 (ly:stencil-align-to! stack Y CENTER)
331 (ly:stencil-align-to! stack X LEFT)
332 ;; should have EX dimension
334 (ly:stencil-translate-axis stack 0.75 Y))))
337 ;; TODO: better syntax.
339 (def-markup-command (note-by-number paper props log dot-count dir) (number? number? number?)
340 "Construct a note symbol, with stem. By using fractional values for
341 @var{dir}, you can obtain longer or shorter stems."
343 (let* ((font (ly:paper-get-font paper (cons '((font-encoding . fetaMusic)) props)))
344 (stemlen (max 3 (- log 1)))
345 (headgl (ly:find-glyph-by-name
347 (string-append "noteheads-" (number->string (min log 2)))))
349 (stemy (* dir stemlen))
350 (attachx (if (> dir 0)
351 (- (cdr (ly:stencil-extent headgl X)) stemth)
353 (attachy (* dir 0.28))
354 (stemgl (and (> log 0)
356 (cons attachx (+ attachx stemth))
357 (cons (min stemy attachy)
360 (dot (ly:find-glyph-by-name font "dots-dot"))
361 (dotwid (interval-length (ly:stencil-extent dot X)))
362 (dots (and (> dot-count 0)
363 (apply ly:stencil-add
365 (ly:stencil-translate-axis
366 dot (* (+ 1 (* 2 x)) dotwid) X) )
367 (iota dot-count 1)))))
368 (flaggl (and (> log 2)
369 (ly:stencil-translate
370 (ly:find-glyph-by-name font
371 (string-append "flags-"
372 (if (> dir 0) "u" "d")
373 (number->string log)))
374 (cons (+ attachx (/ stemth 2)) stemy)))))
376 (set! stemgl (ly:stencil-add flaggl stemgl)))
377 (if (ly:stencil? stemgl)
378 (set! stemgl (ly:stencil-add stemgl headgl))
379 (set! stemgl headgl))
380 (if (ly:stencil? dots)
383 (ly:stencil-translate-axis dots
384 (+ (if (and (> dir 0) (> log 2))
387 ;; huh ? why not necessary?
388 ;;(cdr (ly:stencil-extent headgl X))
394 (use-modules (ice-9 regex))
397 (let ((divisor (log 2)))
398 (lambda (z) (inexact->exact (/ (log z) divisor)))))
400 (define (parse-simple-duration duration-string)
401 "Parse the `duration-string', e.g. ''4..'' or ''breve.'', and return a (log dots) list."
402 (let ((match (regexp-exec (make-regexp "(breve|longa|maxima|[0-9]+)(\\.*)") duration-string)))
403 (if (and match (string=? duration-string (match:substring match 0)))
404 (let ((len (match:substring match 1))
405 (dots (match:substring match 2)))
406 (list (cond ((string=? len "breve") -1)
407 ((string=? len "longa") -2)
408 ((string=? len "maxima") -3)
409 (else (log2 (string->number len))))
410 (if dots (string-length dots) 0)))
411 (error "This is not a valid duration string:" duration-string))))
413 (def-markup-command (note paper props duration dir) (string? number?)
414 "This produces a note with a stem pointing in @var{dir} direction, with
415 the @var{duration} for the note head type and augmentation dots. For
416 example, @code{\\note #\"4.\" #-0.75} creates a dotted quarter note, with
417 a shortened down stem."
419 (let ((parsed (parse-simple-duration duration)))
420 (note-by-number-markup paper props (car parsed) (cadr parsed) dir)))
422 (def-markup-command (normal-size-super paper props arg) (markup?)
423 "A superscript which does not use a smaller font."
425 (ly:stencil-translate-axis (interpret-markup
428 (* 0.5 (chain-assoc-get 'baseline-skip props))
431 (def-markup-command (super paper props arg) (markup?)
434 @cindex lowering text
436 @cindex translating text
438 @cindex @code{\\super}
441 Raising and lowering texts can be done with @code{\\super} and
444 @lilypond[verbatim,fragment,relative=1]
445 c1^\\markup { E \"=\" mc \\super \"2\" }
450 (ly:stencil-translate-axis
453 (cons `((font-size . ,(- (chain-assoc-get 'font-size props 0) 3))) props)
455 (* 0.5 (chain-assoc-get 'baseline-skip props))
458 (def-markup-command (translate paper props offset arg) (number-pair? markup?)
459 "This translates an object. Its first argument is a cons of numbers
461 A \\translate #(cons 2 -3) @{ B C @} D
463 This moves `B C' 2 spaces to the right, and 3 down, relative to its
464 surroundings. This command cannot be used to move isolated scripts
465 vertically, for the same reason that @code{\\raise} cannot be used for
469 (ly:stencil-translate (interpret-markup paper props arg)
472 (def-markup-command (sub paper props arg) (markup?)
473 "Set @var{arg} in subscript."
475 (ly:stencil-translate-axis
478 (cons `((font-size . ,(- (chain-assoc-get 'font-size props 0) 3))) props)
480 (* -0.5 (chain-assoc-get 'baseline-skip props))
483 (def-markup-command (normal-size-sub paper props arg) (markup?)
484 "Set @var{arg} in subscript, in a normal font size."
486 (ly:stencil-translate-axis
487 (interpret-markup paper props arg)
488 (* -0.5 (chain-assoc-get 'baseline-skip props))
491 (def-markup-command (hbracket paper props arg) (markup?)
492 "Draw horizontal brackets around @var{arg}."
493 (let ((th 0.1) ;; todo: take from GROB.
494 (m (interpret-markup paper props arg)))
495 (bracketify-stencil m X th (* 2.5 th) th)))
497 (def-markup-command (bracket paper props arg) (markup?)
498 "Draw vertical brackets around @var{arg}."
499 (let ((th 0.1) ;; todo: take from GROB.
500 (m (interpret-markup paper props arg)))
501 (bracketify-stencil m Y th (* 2.5 th) th)))
503 ;; todo: fix negative space
504 (def-markup-command (hspace paper props amount) (number?)
505 "This produces a invisible object taking horizontal space.
507 \\markup @{ A \\hspace #2.0 B @}
509 will put extra space between A and B, on top of the space that is
510 normally inserted before elements on a line.
513 (ly:make-stencil "" (cons 0 amount) '(-1 . 1) )
514 (ly:make-stencil "" (cons amount amount) '(-1 . 1))))
516 (def-markup-command (override paper props new-prop arg) (pair? markup?)
517 "Add the first argument in to the property list. Properties may be
518 any sort of property supported by @internalsref{font-interface} and
519 @internalsref{text-interface}, for example
522 \\override #'(font-family . married) \"bla\"
526 (interpret-markup paper (cons (list new-prop) props) arg))
528 (def-markup-command (smaller paper props arg) (markup?)
529 "Decrease the font size relative to current setting"
530 (let* ((fs (chain-assoc-get 'font-size props 0))
531 (entry (cons 'font-size (- fs 1))))
532 (interpret-markup paper (cons (list entry) props) arg)))
535 (def-markup-command (bigger paper props arg) (markup?)
536 "Increase the font size relative to current setting"
537 (let* ((fs (chain-assoc-get 'font-size props 0))
538 (entry (cons 'font-size (+ fs 1))))
539 (interpret-markup paper (cons (list entry) props) arg)))
541 (def-markup-command larger (markup?)
544 (def-markup-command (box paper props arg) (markup?)
545 "Draw a box round @var{arg}"
549 (m (interpret-markup paper props arg)))
550 (box-stencil m th pad)))
552 (def-markup-command (strut paper props) ()
554 "Create a box of the same height as the space in the current font.
556 FIXME: is this working?
559 (let ((m (Text_item::interpret_markup paper props " ")))
560 (ly:stencil-set-extent! m X '(1000 . -1000))
563 (define number->mark-letter-vector (make-vector 25 #\A))
568 (if (= i (- (char->integer #\I) (char->integer #\A)))
570 (vector-set! number->mark-letter-vector j
571 (integer->char (+ i (char->integer #\A)))))
573 (define (number->markletter-string n)
574 "Double letters for big marks."
576 ((l (vector-length number->mark-letter-vector)))
579 (string-append (number->markletter-string (1- (quotient n l)))
580 (number->markletter-string (remainder n l)))
581 (make-string 1 (vector-ref number->mark-letter-vector n)))))
584 (def-markup-command (markletter paper props num) (integer?)
585 "Make a markup letter for @var{num}. The letters start with A to Z
586 (skipping I), and continues with double letters."
588 (Text_item::interpret_markup paper props (number->markletter-string num)))
593 (def-markup-command (bracketed-y-column paper props indices args)
595 "Make a column of the markups in @var{args}, putting brackets around
596 the elements marked in @var{indices}, which is a list of numbers."
598 (define (sublist l start stop)
599 (take (drop l start) (- (1+ stop) start)) )
601 (define (stencil-list-extent ss axis)
603 (apply min (map (lambda (x) (car (ly:stencil-extent x axis))) ss))
604 (apply max (map (lambda (x) (cdr (ly:stencil-extent x axis))) ss))))
606 (define (stack-stencils stencils bskip last-stencil)
608 ((null? stencils) '())
611 (stack-stencils (cdr stencils) bskip (car stencils))))
614 ((orig (car stencils))
615 (dir (chain-assoc-get 'direction props DOWN))
616 (new (ly:stencil-moved-to-edge last-stencil Y dir
621 (cons new (stack-stencils (cdr stencils) bskip new))))
624 (define (make-brackets stencils indices acc)
627 (pair? (cdr indices)))
629 ((encl (sublist stencils (car indices) (cadr indices)))
630 (x-ext (stencil-list-extent encl X))
631 (y-ext (stencil-list-extent encl Y))
634 (protusion (* 2.5 thick))
636 (ly:stencil-translate-axis
637 (ly:bracket Y y-ext thick protusion)
638 (- (car x-ext) pad) X))
639 (rb (ly:stencil-translate-axis
640 (ly:bracket Y y-ext thick (- protusion))
641 (+ (cdr x-ext) pad) X))
645 stencils (cddr indices)
659 (chain-assoc-get 'baseline-skip props))
660 (stacked (stack-stencils stencils 1.25 #f))
661 (brackets (make-brackets stacked indices '()))
664 (apply ly:stencil-add
665 (append stacked brackets)