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)
26 (display (ly:stencil-extent stencil X))
27 (display (ly:stencil-extent stencil Y))
28 ; (set! stencil (ly:stencil-translate-axis stencil -20 X))
29 ; (set! stencil (ly:stencil-translate-axis stencil 20 Y))
32 (def-markup-command (simple paper props str) (string?)
33 "A simple text string; @code{\\markup @{ foo @}} is equivalent with
34 @code{\\markup @{ \\simple #\"foo\" @}}."
35 (interpret-markup paper props str))
37 (def-markup-command (encoded-simple paper props sym str) (symbol? string?)
38 "A text string, encoded with encoding @var{sym}. "
39 (Text_item::interpret_string paper
42 ;; TODO: use font recoding.
44 ;; (map make-word-markup (string-tokenize str)))))
46 (define-public empty-markup
47 (make-simple-markup ""))
49 ;;(def-markup-command (fill-line paper props line-width markups)
50 ;; (number? markup-list?)
51 ;; no parser tag -- should make number? markuk-list? thingy
52 (def-markup-command (fill-line paper props markups)
54 "Put @var{markups} in a horizontal line of width @var{line-width}.
55 The markups are spaced/flushed to fill the entire line."
57 (let* ((stencils (map (lambda (x) (interpret-markup paper props x))
59 (text-width (apply + (map interval-length
61 (ly:stencil-extent x X))
63 (word-count (length markups))
64 (word-space (chain-assoc-get 'word-space props))
65 (line-width (chain-assoc-get 'linewidth props))
66 (fill-space (if (< line-width text-width)
68 (/ (- line-width text-width)
69 (if (= word-count 1) 2 (- word-count 1)))))
70 (line-stencils (if (= word-count 1)
71 (map (lambda (x) (interpret-markup paper props x))
72 (list (make-simple-markup "")
73 (make-stencil-markup (car stencils))
74 (make-simple-markup "")))
76 (stack-stencil-line fill-space line-stencils)))
78 (define (font-markup qualifier value)
79 (lambda (paper props arg)
80 (interpret-markup paper
81 (prepend-alist-chain qualifier value props)
84 (def-markup-command (line paper props args) (markup-list?)
85 "Put @var{args} in a horizontal line. The property @code{word-space}
86 determines the space between each markup in @var{args}."
88 (chain-assoc-get 'word-space props)
89 (map (lambda (m) (interpret-markup paper props m)) args)))
91 (def-markup-command (combine paper props m1 m2) (markup? markup?)
92 "Print two markups on top of each other."
94 ((s1 (interpret-markup paper props m1))
95 (s2 (interpret-markup paper props m2)))
97 (ly:stencil-add s1 s2)))
99 (def-markup-command (finger paper props arg) (markup?)
100 "Set the argument as small numbers."
101 (interpret-markup paper
102 (cons '((font-size . -5) (font-family . number)) props)
105 (def-markup-command (fontsize paper props mag arg) (number? markup?)
106 "This sets the relative font size, e.g.
108 A \\fontsize #2 @{ B C @} D
112 This will enlarge the B and the C by two steps.
116 (prepend-alist-chain 'font-size mag props)
119 (def-markup-command (magnify paper props sz arg) (number? markup?)
120 "This sets the font magnification for the its argument. In the following
121 example, the middle A will be 10% larger:
123 A \\magnify #1.1 @{ A @} A
126 Note: magnification only works if a font-name is explicitly selected.
127 Use @code{\\fontsize} otherwise."
131 (prepend-alist-chain 'font-magnification sz props)
134 (def-markup-command (bold paper props arg) (markup?)
135 "Switch to bold font-series"
136 (interpret-markup paper (prepend-alist-chain 'font-series 'bold props) arg))
138 (def-markup-command (sans paper props arg) (markup?)
139 "Switch to the sans-serif family"
140 (interpret-markup paper (prepend-alist-chain 'font-family 'sans props) arg))
142 (def-markup-command (number paper props arg) (markup?)
143 "Set font family to @code{number}, which yields the font used for
144 time signatures and fingerings. This font only contains numbers and
145 some punctuation. It doesn't have any letters. "
146 (interpret-markup paper (prepend-alist-chain 'font-encoding 'fetaNumber props) arg))
148 (def-markup-command (roman paper props arg) (markup?)
149 "Set font family to @code{roman}."
150 (interpret-markup paper (prepend-alist-chain 'font-family 'roman props) arg))
152 (def-markup-command (huge 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 (large paper props arg) (markup?)
157 "Set font size to +1."
158 (interpret-markup paper (prepend-alist-chain 'font-size 1 props) arg))
160 (def-markup-command (normalsize paper props arg) (markup?)
161 "Set font size to default."
162 (interpret-markup paper (prepend-alist-chain 'font-size 0 props) arg))
164 (def-markup-command (small paper props arg) (markup?)
165 "Set font size to -1."
166 (interpret-markup paper (prepend-alist-chain 'font-size -1 props) arg))
168 (def-markup-command (tiny paper props arg) (markup?)
169 "Set font size to -2."
170 (interpret-markup paper (prepend-alist-chain 'font-size -2 props) arg))
172 (def-markup-command (teeny paper props arg) (markup?)
173 "Set font size to -3."
174 (interpret-markup paper (prepend-alist-chain 'font-size -3 props) arg))
176 (def-markup-command (caps paper props arg) (markup?)
177 "Set font shape to @code{caps}."
178 (interpret-markup paper (prepend-alist-chain 'font-shape 'caps props) arg))
180 (def-markup-command (latin-i paper props arg) (markup?)
181 "TEST latin1 encoding."
182 (interpret-markup paper (prepend-alist-chain 'font-shape 'latin1 props) arg))
184 (def-markup-command (dynamic paper props arg) (markup?)
185 "Use the dynamic font. This font only contains @b{s}, @b{f}, @b{m},
186 @b{z}, @b{p}, and @b{r}. When producing phrases, like ``pi@`{u} @b{f}'', the
187 normal words (like ``pi@`{u}'') should be done in a different font. The
188 recommend font for this is bold and italic"
190 paper (prepend-alist-chain 'font-encoding 'fetaDynamic props) arg))
192 (def-markup-command (italic paper props arg) (markup?)
193 "Use italic @code{font-shape} for @var{arg}. "
194 (interpret-markup paper (prepend-alist-chain 'font-shape 'italic props) arg))
196 (def-markup-command (typewriter paper props arg) (markup?)
197 "Use @code{font-family} typewriter for @var{arg}."
199 paper (prepend-alist-chain 'font-family 'typewriter props) arg))
201 (def-markup-command (upright paper props arg) (markup?)
202 "Set font shape to @code{upright}."
204 paper (prepend-alist-chain 'font-shape 'upright props) arg))
206 (def-markup-command (doublesharp paper props) ()
207 "Draw a double sharp symbol."
209 (interpret-markup paper props (markup #:musicglyph "accidentals-4")))
210 (def-markup-command (sesquisharp paper props) ()
211 "Draw a 3/2 sharp symbol."
212 (interpret-markup paper props (markup #:musicglyph "accidentals-3")))
214 (def-markup-command (sharp paper props) ()
215 "Draw a sharp symbol."
216 (interpret-markup paper props (markup #:musicglyph "accidentals-2")))
217 (def-markup-command (semisharp paper props) ()
218 "Draw a semi sharp symbol."
219 (interpret-markup paper props (markup #:musicglyph "accidentals-1")))
220 (def-markup-command (natural paper props) ()
221 "Draw a natural symbol."
223 (interpret-markup paper props (markup #:musicglyph "accidentals-0")))
224 (def-markup-command (semiflat paper props) ()
226 (interpret-markup paper props (markup #:musicglyph "accidentals--1")))
227 (def-markup-command (flat paper props) ()
228 "Draw a flat symbol."
230 (interpret-markup paper props (markup #:musicglyph "accidentals--2")))
231 (def-markup-command (sesquiflat paper props) ()
232 "Draw a 3/2 flat symbol."
234 (interpret-markup paper props (markup #:musicglyph "accidentals--3")))
235 (def-markup-command (doubleflat paper props) ()
236 "Draw a double flat symbol."
238 (interpret-markup paper props (markup #:musicglyph "accidentals--4")))
241 (def-markup-command (column paper props args) (markup-list?)
242 "Stack the markups in @var{args} vertically."
244 -1 0.0 (chain-assoc-get 'baseline-skip props)
245 (map (lambda (m) (interpret-markup paper props m)) args)))
247 (def-markup-command (dir-column paper props args) (markup-list?)
248 "Make a column of args, going up or down, depending on the setting
249 of the @code{#'direction} layout property."
250 (let* ((dir (chain-assoc-get 'direction props)))
252 (if (number? dir) dir -1)
254 (chain-assoc-get 'baseline-skip props)
255 (map (lambda (x) (interpret-markup paper props x)) args))))
257 (def-markup-command (center-align paper props args) (markup-list?)
258 "Put @code{args} in a centered column. "
259 (let* ((mols (map (lambda (x) (interpret-markup paper props x)) args))
260 (cmols (map (lambda (x) (ly:stencil-align-to! x X CENTER)) mols)))
261 (stack-lines -1 0.0 (chain-assoc-get 'baseline-skip props) mols)))
263 (def-markup-command (vcenter paper props arg) (markup?)
264 "Align @code{arg} to its center. "
265 (let* ((mol (interpret-markup paper props arg)))
266 (ly:stencil-align-to! mol Y CENTER)
269 (def-markup-command (right-align paper props arg) (markup?)
270 (let* ((m (interpret-markup paper props arg)))
271 (ly:stencil-align-to! m X RIGHT)
274 (def-markup-command (left-align paper props arg) (markup?)
275 "Align @var{arg} on its left edge. "
277 (let* ((m (interpret-markup paper props arg)))
278 (ly:stencil-align-to! m X LEFT)
281 (def-markup-command (halign paper props dir arg) (number? markup?)
282 "Set horizontal alignment. If @var{dir} is -1, then it is
283 left-aligned, while+1 is right. Values in between interpolate alignment
287 (let* ((m (interpret-markup paper props arg)))
288 (ly:stencil-align-to! m X dir)
291 (def-markup-command (musicglyph paper props glyph-name) (string?)
292 "This is converted to a musical symbol, e.g. @code{\\musicglyph
293 #\"accidentals-0\"} will select the natural sign from the music font.
294 See @usermanref{The Feta font} for a complete listing of the possible glyphs.
296 (ly:find-glyph-by-name
297 (ly:paper-get-font paper (cons '((font-encoding . fetaMusic))
302 (def-markup-command (lookup paper props glyph-name) (string?)
303 "Lookup a glyph by name."
304 (ly:find-glyph-by-name (ly:paper-get-font paper props)
307 (def-markup-command (char paper props num) (integer?)
308 "This produces a single character, e.g. @code{\\char #65} produces the
310 (ly:get-glyph (ly:paper-get-font paper props) num))
312 (def-markup-command (raise paper props amount arg) (number? markup?)
314 This raises @var{arg}, by the distance @var{amount}.
315 A negative @var{amount} indicates lowering:
317 @lilypond[verbatim,fragment,relative=1]
318 c1^\\markup { C \\small \\raise #1.0 \\bold { \"9/7+\" }}
320 The argument to @code{\\raise} is the vertical displacement amount,
321 measured in (global) staff spaces. @code{\\raise} and @code{\\super}
322 raise objects in relation to their surrounding markups.
324 If the text object itself is positioned above or below the staff, then
325 @code{\\raise} cannot be used to move it, since the mechanism that
326 positions it next to the staff cancels any shift made with
327 @code{\\raise}. For vertical positioning, use the @code{padding}
328 and/or @code{extra-offset} properties. "
331 (ly:stencil-translate-axis (interpret-markup paper props arg)
334 (def-markup-command (fraction paper props arg1 arg2) (markup? markup?)
335 "Make a fraction of two markups."
337 (let* ((m1 (interpret-markup paper props arg1))
338 (m2 (interpret-markup paper props arg2)))
339 (ly:stencil-align-to! m1 X CENTER)
340 (ly:stencil-align-to! m2 X CENTER)
341 (let* ((x1 (ly:stencil-extent m1 X))
342 (x2 (ly:stencil-extent m2 X))
343 (line (ly:round-filled-box (interval-union x1 x2) '(-0.05 . 0.05) 0.0))
344 ;; should stack mols separately, to maintain LINE on baseline
345 (stack (stack-lines -1 0.2 0.6 (list m1 line m2))))
346 (ly:stencil-align-to! stack Y CENTER)
347 (ly:stencil-align-to! stack X LEFT)
348 ;; should have EX dimension
350 (ly:stencil-translate-axis stack 0.75 Y))))
353 ;; TODO: better syntax.
355 (def-markup-command (note-by-number paper props log dot-count dir) (number? number? number?)
356 "Construct a note symbol, with stem. By using fractional values for
357 @var{dir}, you can obtain longer or shorter stems."
359 (let* ((font (ly:paper-get-font paper (cons '((font-encoding . fetaMusic)) props)))
360 (stemlen (max 3 (- log 1)))
361 (headgl (ly:find-glyph-by-name
363 (string-append "noteheads-" (number->string (min log 2)))))
365 (stemy (* dir stemlen))
366 (attachx (if (> dir 0)
367 (- (cdr (ly:stencil-extent headgl X)) stemth)
369 (attachy (* dir 0.28))
370 (stemgl (and (> log 0)
372 (cons attachx (+ attachx stemth))
373 (cons (min stemy attachy)
376 (dot (ly:find-glyph-by-name font "dots-dot"))
377 (dotwid (interval-length (ly:stencil-extent dot X)))
378 (dots (and (> dot-count 0)
379 (apply ly:stencil-add
381 (ly:stencil-translate-axis
382 dot (* (+ 1 (* 2 x)) dotwid) X) )
383 (iota dot-count 1)))))
384 (flaggl (and (> log 2)
385 (ly:stencil-translate
386 (ly:find-glyph-by-name font
387 (string-append "flags-"
388 (if (> dir 0) "u" "d")
389 (number->string log)))
390 (cons (+ attachx (/ stemth 2)) stemy)))))
392 (set! stemgl (ly:stencil-add flaggl stemgl)))
393 (if (ly:stencil? stemgl)
394 (set! stemgl (ly:stencil-add stemgl headgl))
395 (set! stemgl headgl))
396 (if (ly:stencil? dots)
399 (ly:stencil-translate-axis dots
400 (+ (if (and (> dir 0) (> log 2))
403 ;; huh ? why not necessary?
404 ;;(cdr (ly:stencil-extent headgl X))
410 (use-modules (ice-9 regex))
413 (let ((divisor (log 2)))
414 (lambda (z) (inexact->exact (/ (log z) divisor)))))
416 (define (parse-simple-duration duration-string)
417 "Parse the `duration-string', e.g. ''4..'' or ''breve.'', and return a (log dots) list."
418 (let ((match (regexp-exec (make-regexp "(breve|longa|maxima|[0-9]+)(\\.*)") duration-string)))
419 (if (and match (string=? duration-string (match:substring match 0)))
420 (let ((len (match:substring match 1))
421 (dots (match:substring match 2)))
422 (list (cond ((string=? len "breve") -1)
423 ((string=? len "longa") -2)
424 ((string=? len "maxima") -3)
425 (else (log2 (string->number len))))
426 (if dots (string-length dots) 0)))
427 (error "This is not a valid duration string:" duration-string))))
429 (def-markup-command (note paper props duration dir) (string? number?)
430 "This produces a note with a stem pointing in @var{dir} direction, with
431 the @var{duration} for the note head type and augmentation dots. For
432 example, @code{\\note #\"4.\" #-0.75} creates a dotted quarter note, with
433 a shortened down stem."
435 (let ((parsed (parse-simple-duration duration)))
436 (note-by-number-markup paper props (car parsed) (cadr parsed) dir)))
438 (def-markup-command (normal-size-super paper props arg) (markup?)
439 "A superscript which does not use a smaller font."
441 (ly:stencil-translate-axis (interpret-markup
444 (* 0.5 (chain-assoc-get 'baseline-skip props))
447 (def-markup-command (super paper props arg) (markup?)
450 @cindex lowering text
452 @cindex translating text
454 @cindex @code{\\super}
457 Raising and lowering texts can be done with @code{\\super} and
460 @lilypond[verbatim,fragment,relative=1]
461 c1^\\markup { E \"=\" mc \\super \"2\" }
466 (ly:stencil-translate-axis
469 (cons `((font-size . ,(- (chain-assoc-get 'font-size props 0) 3))) props)
471 (* 0.5 (chain-assoc-get 'baseline-skip props))
474 (def-markup-command (translate paper props offset arg) (number-pair? markup?)
475 "This translates an object. Its first argument is a cons of numbers
477 A \\translate #(cons 2 -3) @{ B C @} D
479 This moves `B C' 2 spaces to the right, and 3 down, relative to its
480 surroundings. This command cannot be used to move isolated scripts
481 vertically, for the same reason that @code{\\raise} cannot be used for
485 (ly:stencil-translate (interpret-markup paper props arg)
488 (def-markup-command (sub paper props arg) (markup?)
489 "Set @var{arg} in subscript."
491 (ly:stencil-translate-axis
494 (cons `((font-size . ,(- (chain-assoc-get 'font-size props 0) 3))) props)
496 (* -0.5 (chain-assoc-get 'baseline-skip props))
499 (def-markup-command (normal-size-sub paper props arg) (markup?)
500 "Set @var{arg} in subscript, in a normal font size."
502 (ly:stencil-translate-axis
503 (interpret-markup paper props arg)
504 (* -0.5 (chain-assoc-get 'baseline-skip props))
507 (def-markup-command (hbracket paper props arg) (markup?)
508 "Draw horizontal brackets around @var{arg}."
509 (let ((th 0.1) ;; todo: take from GROB.
510 (m (interpret-markup paper props arg)))
511 (bracketify-stencil m X th (* 2.5 th) th)))
513 (def-markup-command (bracket paper props arg) (markup?)
514 "Draw vertical brackets around @var{arg}."
515 (let ((th 0.1) ;; todo: take from GROB.
516 (m (interpret-markup paper props arg)))
517 (bracketify-stencil m Y th (* 2.5 th) th)))
519 ;; todo: fix negative space
520 (def-markup-command (hspace paper props amount) (number?)
521 "This produces a invisible object taking horizontal space.
523 \\markup @{ A \\hspace #2.0 B @}
525 will put extra space between A and B, on top of the space that is
526 normally inserted before elements on a line.
529 (ly:make-stencil "" (cons 0 amount) '(-1 . 1) )
530 (ly:make-stencil "" (cons amount amount) '(-1 . 1))))
532 (def-markup-command (override paper props new-prop arg) (pair? markup?)
533 "Add the first argument in to the property list. Properties may be
534 any sort of property supported by @internalsref{font-interface} and
535 @internalsref{text-interface}, for example
538 \\override #'(font-family . married) \"bla\"
542 (interpret-markup paper (cons (list new-prop) props) arg))
544 (def-markup-command (smaller paper props arg) (markup?)
545 "Decrease the font size relative to current setting"
546 (let* ((fs (chain-assoc-get 'font-size props 0))
547 (entry (cons 'font-size (- fs 1))))
548 (interpret-markup paper (cons (list entry) props) arg)))
551 (def-markup-command (bigger paper props arg) (markup?)
552 "Increase the font size relative to current setting"
553 (let* ((fs (chain-assoc-get 'font-size props 0))
554 (entry (cons 'font-size (+ fs 1))))
555 (interpret-markup paper (cons (list entry) props) arg)))
557 (def-markup-command larger (markup?)
560 (def-markup-command (box paper props arg) (markup?)
561 "Draw a box round @var{arg}"
565 (m (interpret-markup paper props arg)))
566 (box-stencil m th pad)))
568 (def-markup-command (strut paper props) ()
570 "Create a box of the same height as the space in the current font.
572 FIXME: is this working?
575 (let ((m (Text_item::interpret_markup paper props " ")))
576 (ly:stencil-set-extent! m X '(1000 . -1000))
579 (define number->mark-letter-vector (make-vector 25 #\A))
584 (if (= i (- (char->integer #\I) (char->integer #\A)))
586 (vector-set! number->mark-letter-vector j
587 (integer->char (+ i (char->integer #\A)))))
589 (define (number->markletter-string n)
590 "Double letters for big marks."
592 ((l (vector-length number->mark-letter-vector)))
595 (string-append (number->markletter-string (1- (quotient n l)))
596 (number->markletter-string (remainder n l)))
597 (make-string 1 (vector-ref number->mark-letter-vector n)))))
600 (def-markup-command (markletter paper props num) (integer?)
601 "Make a markup letter for @var{num}. The letters start with A to Z
602 (skipping I), and continues with double letters."
604 (Text_item::interpret_markup paper props (number->markletter-string num)))
609 (def-markup-command (bracketed-y-column paper props indices args)
611 "Make a column of the markups in @var{args}, putting brackets around
612 the elements marked in @var{indices}, which is a list of numbers."
614 (define (sublist l start stop)
615 (take (drop l start) (- (1+ stop) start)) )
617 (define (stencil-list-extent ss axis)
619 (apply min (map (lambda (x) (car (ly:stencil-extent x axis))) ss))
620 (apply max (map (lambda (x) (cdr (ly:stencil-extent x axis))) ss))))
622 (define (stack-stencils stencils bskip last-stencil)
624 ((null? stencils) '())
627 (stack-stencils (cdr stencils) bskip (car stencils))))
630 ((orig (car stencils))
631 (dir (chain-assoc-get 'direction props DOWN))
632 (new (ly:stencil-moved-to-edge last-stencil Y dir
637 (cons new (stack-stencils (cdr stencils) bskip new))))
640 (define (make-brackets stencils indices acc)
643 (pair? (cdr indices)))
645 ((encl (sublist stencils (car indices) (cadr indices)))
646 (x-ext (stencil-list-extent encl X))
647 (y-ext (stencil-list-extent encl Y))
650 (protusion (* 2.5 thick))
652 (ly:stencil-translate-axis
653 (ly:bracket Y y-ext thick protusion)
654 (- (car x-ext) pad) X))
655 (rb (ly:stencil-translate-axis
656 (ly:bracket Y y-ext thick (- protusion))
657 (+ (cdr x-ext) pad) X))
661 stencils (cddr indices)
675 (chain-assoc-get 'baseline-skip props))
676 (stacked (stack-stencils stencils 1.25 #f))
677 (brackets (make-brackets stacked indices '()))
680 (apply ly:stencil-add
681 (append stacked brackets)