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 (general-align paper props axis dir arg) (integer? number? markup?)
282 "Align @var{arg} in @var{axis} direction to the @var{dir} side."
283 (let* ((m (interpret-markup paper props arg)))
285 (ly:stencil-align-to! m axis dir)
289 (def-markup-command (halign paper props dir arg) (number? markup?)
290 "Set horizontal alignment. If @var{dir} is -1, then it is
291 left-aligned, while+1 is right. Values in between interpolate alignment
295 (let* ((m (interpret-markup paper props arg)))
296 (ly:stencil-align-to! m X dir)
299 (def-markup-command (musicglyph paper props glyph-name) (string?)
300 "This is converted to a musical symbol, e.g. @code{\\musicglyph
301 #\"accidentals-0\"} will select the natural sign from the music font.
302 See @usermanref{The Feta font} for a complete listing of the possible glyphs.
304 (ly:find-glyph-by-name
305 (ly:paper-get-font paper (cons '((font-encoding . fetaMusic))
310 (def-markup-command (lookup paper props glyph-name) (string?)
311 "Lookup a glyph by name."
312 (ly:find-glyph-by-name (ly:paper-get-font paper props)
315 (def-markup-command (char paper props num) (integer?)
316 "This produces a single character, e.g. @code{\\char #65} produces the
318 (ly:get-glyph (ly:paper-get-font paper props) num))
320 (def-markup-command (raise paper props amount arg) (number? markup?)
322 This raises @var{arg}, by the distance @var{amount}.
323 A negative @var{amount} indicates lowering:
325 @lilypond[verbatim,fragment,relative=1]
326 c1^\\markup { C \\small \\raise #1.0 \\bold { \"9/7+\" }}
328 The argument to @code{\\raise} is the vertical displacement amount,
329 measured in (global) staff spaces. @code{\\raise} and @code{\\super}
330 raise objects in relation to their surrounding markups.
332 If the text object itself is positioned above or below the staff, then
333 @code{\\raise} cannot be used to move it, since the mechanism that
334 positions it next to the staff cancels any shift made with
335 @code{\\raise}. For vertical positioning, use the @code{padding}
336 and/or @code{extra-offset} properties. "
339 (ly:stencil-translate-axis (interpret-markup paper props arg)
342 (def-markup-command (fraction paper props arg1 arg2) (markup? markup?)
343 "Make a fraction of two markups."
345 (let* ((m1 (interpret-markup paper props arg1))
346 (m2 (interpret-markup paper props arg2)))
347 (ly:stencil-align-to! m1 X CENTER)
348 (ly:stencil-align-to! m2 X CENTER)
349 (let* ((x1 (ly:stencil-extent m1 X))
350 (x2 (ly:stencil-extent m2 X))
351 (line (ly:round-filled-box (interval-union x1 x2) '(-0.05 . 0.05) 0.0))
352 ;; should stack mols separately, to maintain LINE on baseline
353 (stack (stack-lines -1 0.2 0.6 (list m1 line m2))))
354 (ly:stencil-align-to! stack Y CENTER)
355 (ly:stencil-align-to! stack X LEFT)
356 ;; should have EX dimension
358 (ly:stencil-translate-axis stack 0.75 Y))))
361 ;; TODO: better syntax.
363 (def-markup-command (note-by-number paper props log dot-count dir) (number? number? number?)
364 "Construct a note symbol, with stem. By using fractional values for
365 @var{dir}, you can obtain longer or shorter stems."
367 (let* ((font (ly:paper-get-font paper (cons '((font-encoding . fetaMusic)) props)))
368 (stemlen (max 3 (- log 1)))
369 (headgl (ly:find-glyph-by-name
371 (string-append "noteheads-" (number->string (min log 2)))))
373 (stemy (* dir stemlen))
374 (attachx (if (> dir 0)
375 (- (cdr (ly:stencil-extent headgl X)) stemth)
377 (attachy (* dir 0.28))
378 (stemgl (and (> log 0)
380 (cons attachx (+ attachx stemth))
381 (cons (min stemy attachy)
384 (dot (ly:find-glyph-by-name font "dots-dot"))
385 (dotwid (interval-length (ly:stencil-extent dot X)))
386 (dots (and (> dot-count 0)
387 (apply ly:stencil-add
389 (ly:stencil-translate-axis
390 dot (* (+ 1 (* 2 x)) dotwid) X) )
391 (iota dot-count 1)))))
392 (flaggl (and (> log 2)
393 (ly:stencil-translate
394 (ly:find-glyph-by-name font
395 (string-append "flags-"
396 (if (> dir 0) "u" "d")
397 (number->string log)))
398 (cons (+ attachx (/ stemth 2)) stemy)))))
400 (set! stemgl (ly:stencil-add flaggl stemgl)))
401 (if (ly:stencil? stemgl)
402 (set! stemgl (ly:stencil-add stemgl headgl))
403 (set! stemgl headgl))
404 (if (ly:stencil? dots)
407 (ly:stencil-translate-axis dots
408 (+ (if (and (> dir 0) (> log 2))
411 ;; huh ? why not necessary?
412 ;;(cdr (ly:stencil-extent headgl X))
418 (use-modules (ice-9 regex))
421 (let ((divisor (log 2)))
422 (lambda (z) (inexact->exact (/ (log z) divisor)))))
424 (define (parse-simple-duration duration-string)
425 "Parse the `duration-string', e.g. ''4..'' or ''breve.'', and return a (log dots) list."
426 (let ((match (regexp-exec (make-regexp "(breve|longa|maxima|[0-9]+)(\\.*)") duration-string)))
427 (if (and match (string=? duration-string (match:substring match 0)))
428 (let ((len (match:substring match 1))
429 (dots (match:substring match 2)))
430 (list (cond ((string=? len "breve") -1)
431 ((string=? len "longa") -2)
432 ((string=? len "maxima") -3)
433 (else (log2 (string->number len))))
434 (if dots (string-length dots) 0)))
435 (error "This is not a valid duration string:" duration-string))))
437 (def-markup-command (note paper props duration dir) (string? number?)
438 "This produces a note with a stem pointing in @var{dir} direction, with
439 the @var{duration} for the note head type and augmentation dots. For
440 example, @code{\\note #\"4.\" #-0.75} creates a dotted quarter note, with
441 a shortened down stem."
443 (let ((parsed (parse-simple-duration duration)))
444 (note-by-number-markup paper props (car parsed) (cadr parsed) dir)))
446 (def-markup-command (normal-size-super paper props arg) (markup?)
447 "A superscript which does not use a smaller font."
449 (ly:stencil-translate-axis (interpret-markup
452 (* 0.5 (chain-assoc-get 'baseline-skip props))
455 (def-markup-command (super paper props arg) (markup?)
458 @cindex lowering text
460 @cindex translating text
462 @cindex @code{\\super}
465 Raising and lowering texts can be done with @code{\\super} and
468 @lilypond[verbatim,fragment,relative=1]
469 c1^\\markup { E \"=\" mc \\super \"2\" }
474 (ly:stencil-translate-axis
477 (cons `((font-size . ,(- (chain-assoc-get 'font-size props 0) 3))) props)
479 (* 0.5 (chain-assoc-get 'baseline-skip props))
482 (def-markup-command (translate paper props offset arg) (number-pair? markup?)
483 "This translates an object. Its first argument is a cons of numbers
485 A \\translate #(cons 2 -3) @{ B C @} D
487 This moves `B C' 2 spaces to the right, and 3 down, relative to its
488 surroundings. This command cannot be used to move isolated scripts
489 vertically, for the same reason that @code{\\raise} cannot be used for
493 (ly:stencil-translate (interpret-markup paper props arg)
496 (def-markup-command (sub paper props arg) (markup?)
497 "Set @var{arg} in subscript."
499 (ly:stencil-translate-axis
502 (cons `((font-size . ,(- (chain-assoc-get 'font-size props 0) 3))) props)
504 (* -0.5 (chain-assoc-get 'baseline-skip props))
507 (def-markup-command (normal-size-sub paper props arg) (markup?)
508 "Set @var{arg} in subscript, in a normal font size."
510 (ly:stencil-translate-axis
511 (interpret-markup paper props arg)
512 (* -0.5 (chain-assoc-get 'baseline-skip props))
515 (def-markup-command (hbracket paper props arg) (markup?)
516 "Draw horizontal brackets around @var{arg}."
517 (let ((th 0.1) ;; todo: take from GROB.
518 (m (interpret-markup paper props arg)))
519 (bracketify-stencil m X th (* 2.5 th) th)))
521 (def-markup-command (bracket paper props arg) (markup?)
522 "Draw vertical brackets around @var{arg}."
523 (let ((th 0.1) ;; todo: take from GROB.
524 (m (interpret-markup paper props arg)))
525 (bracketify-stencil m Y th (* 2.5 th) th)))
527 ;; todo: fix negative space
528 (def-markup-command (hspace paper props amount) (number?)
529 "This produces a invisible object taking horizontal space.
531 \\markup @{ A \\hspace #2.0 B @}
533 will put extra space between A and B, on top of the space that is
534 normally inserted before elements on a line.
537 (ly:make-stencil "" (cons 0 amount) '(-1 . 1) )
538 (ly:make-stencil "" (cons amount amount) '(-1 . 1))))
540 (def-markup-command (override paper props new-prop arg) (pair? markup?)
541 "Add the first argument in to the property list. Properties may be
542 any sort of property supported by @internalsref{font-interface} and
543 @internalsref{text-interface}, for example
546 \\override #'(font-family . married) \"bla\"
550 (interpret-markup paper (cons (list new-prop) props) arg))
552 (def-markup-command (smaller paper props arg) (markup?)
553 "Decrease the font size relative to current setting"
554 (let* ((fs (chain-assoc-get 'font-size props 0))
555 (entry (cons 'font-size (- fs 1))))
556 (interpret-markup paper (cons (list entry) props) arg)))
559 (def-markup-command (bigger paper props arg) (markup?)
560 "Increase the font size relative to current setting"
561 (let* ((fs (chain-assoc-get 'font-size props 0))
562 (entry (cons 'font-size (+ fs 1))))
563 (interpret-markup paper (cons (list entry) props) arg)))
565 (def-markup-command larger (markup?)
568 (def-markup-command (box paper props arg) (markup?)
569 "Draw a box round @var{arg}"
573 (m (interpret-markup paper props arg)))
574 (box-stencil m th pad)))
576 (def-markup-command (strut paper props) ()
578 "Create a box of the same height as the space in the current font.
580 FIXME: is this working?
583 (let ((m (Text_item::interpret_markup paper props " ")))
584 (ly:stencil-set-extent! m X '(1000 . -1000))
587 (define number->mark-letter-vector (make-vector 25 #\A))
592 (if (= i (- (char->integer #\I) (char->integer #\A)))
594 (vector-set! number->mark-letter-vector j
595 (integer->char (+ i (char->integer #\A)))))
597 (define (number->markletter-string n)
598 "Double letters for big marks."
600 ((l (vector-length number->mark-letter-vector)))
603 (string-append (number->markletter-string (1- (quotient n l)))
604 (number->markletter-string (remainder n l)))
605 (make-string 1 (vector-ref number->mark-letter-vector n)))))
608 (def-markup-command (markletter paper props num) (integer?)
609 "Make a markup letter for @var{num}. The letters start with A to Z
610 (skipping I), and continues with double letters."
612 (Text_item::interpret_markup paper props (number->markletter-string num)))
617 (def-markup-command (bracketed-y-column paper props indices args)
619 "Make a column of the markups in @var{args}, putting brackets around
620 the elements marked in @var{indices}, which is a list of numbers."
622 (define (sublist l start stop)
623 (take (drop l start) (- (1+ stop) start)) )
625 (define (stencil-list-extent ss axis)
627 (apply min (map (lambda (x) (car (ly:stencil-extent x axis))) ss))
628 (apply max (map (lambda (x) (cdr (ly:stencil-extent x axis))) ss))))
630 (define (stack-stencils stencils bskip last-stencil)
632 ((null? stencils) '())
635 (stack-stencils (cdr stencils) bskip (car stencils))))
638 ((orig (car stencils))
639 (dir (chain-assoc-get 'direction props DOWN))
640 (new (ly:stencil-moved-to-edge last-stencil Y dir
645 (cons new (stack-stencils (cdr stencils) bskip new))))
648 (define (make-brackets stencils indices acc)
651 (pair? (cdr indices)))
653 ((encl (sublist stencils (car indices) (cadr indices)))
654 (x-ext (stencil-list-extent encl X))
655 (y-ext (stencil-list-extent encl Y))
658 (protusion (* 2.5 thick))
660 (ly:stencil-translate-axis
661 (ly:bracket Y y-ext thick protusion)
662 (- (car x-ext) pad) X))
663 (rb (ly:stencil-translate-axis
664 (ly:bracket Y y-ext thick (- protusion))
665 (+ (cdr x-ext) pad) X))
669 stencils (cddr indices)
683 (chain-assoc-get 'baseline-skip props))
684 (stacked (stack-stencils stencils 1.25 #f))
685 (brackets (make-brackets stacked indices '()))
688 (apply ly:stencil-add
689 (append stacked brackets)