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)))
23 (if (= 0 (vector-length systems))
25 (ly:warn "No systems found in \\score markup. Did you forget \\paper?")
29 ((stencil (ly:paper-system-stencil (vector-ref systems 0))))
31 (ly:stencil-align-to! stencil Y CENTER)
34 (def-markup-command (simple paper props str) (string?)
35 "A simple text string; @code{\\markup @{ foo @}} is equivalent with
36 @code{\\markup @{ \\simple #\"foo\" @}}."
37 (interpret-markup paper props str))
39 (def-markup-command (encoded-simple paper props sym str) (symbol? string?)
40 "A text string, encoded with encoding @var{sym}. "
41 (Text_item::interpret_string paper
45 ;; TODO: use font recoding.
47 ;; (map make-word-markup (string-tokenize str)))))
49 (define-public empty-markup
50 (make-simple-markup ""))
53 (def-markup-command (postscript paper props str) (string?)
55 "This inserts @var{str} directly into the output as a PostScript command string. For example,
59 { c^\\markup { \\postscript #\" /ecrm10 findfont 10 output-scale div scalefont 90 rotate (hello) show \" } }
63 (list 'embedded-ps str)
66 ;;(def-markup-command (fill-line paper props line-width markups)
67 ;; (number? markup-list?)
68 ;; no parser tag -- should make number? markuk-list? thingy
69 (def-markup-command (fill-line paper props markups)
71 "Put @var{markups} in a horizontal line of width @var{line-width}.
72 The markups are spaced/flushed to fill the entire line."
74 (let* ((stencils (map (lambda (x) (interpret-markup paper props x))
76 (text-width (apply + (map interval-length
78 (ly:stencil-extent x X))
80 (word-count (length markups))
81 (word-space (chain-assoc-get 'word-space props))
82 (line-width (chain-assoc-get 'linewidth props))
83 (fill-space (if (< line-width text-width)
85 (/ (- line-width text-width)
86 (if (= word-count 1) 2 (- word-count 1)))))
87 (line-stencils (if (= word-count 1)
88 (map (lambda (x) (interpret-markup paper props x))
89 (list (make-simple-markup "")
90 (make-stencil-markup (car stencils))
91 (make-simple-markup "")))
93 (stack-stencil-line fill-space line-stencils)))
95 (define (font-markup qualifier value)
96 (lambda (paper props arg)
97 (interpret-markup paper
98 (prepend-alist-chain qualifier value props)
101 (def-markup-command (line paper props args) (markup-list?)
102 "Put @var{args} in a horizontal line. The property @code{word-space}
103 determines the space between each markup in @var{args}."
105 (chain-assoc-get 'word-space props)
106 (map (lambda (m) (interpret-markup paper props m)) args)))
108 (def-markup-command (combine paper props m1 m2) (markup? markup?)
109 "Print two markups on top of each other."
111 ((s1 (interpret-markup paper props m1))
112 (s2 (interpret-markup paper props m2)))
114 (ly:stencil-add s1 s2)))
116 (def-markup-command (finger paper props arg) (markup?)
117 "Set the argument as small numbers."
118 (interpret-markup paper
119 (cons '((font-size . -5) (font-family . number)) props)
122 (def-markup-command (fontsize paper props mag arg) (number? markup?)
123 "This sets the relative font size, e.g.
125 A \\fontsize #2 @{ B C @} D
129 This will enlarge the B and the C by two steps.
133 (prepend-alist-chain 'font-size mag props)
136 (def-markup-command (magnify paper props sz arg) (number? markup?)
137 "This sets the font magnification for the its argument. In the following
138 example, the middle A will be 10% larger:
140 A \\magnify #1.1 @{ A @} A
143 Note: magnification only works if a font-name is explicitly selected.
144 Use @code{\\fontsize} otherwise."
148 (prepend-alist-chain 'font-magnification sz props)
151 (def-markup-command (bold paper props arg) (markup?)
152 "Switch to bold font-series"
153 (interpret-markup paper (prepend-alist-chain 'font-series 'bold props) arg))
155 (def-markup-command (sans paper props arg) (markup?)
156 "Switch to the sans-serif family"
157 (interpret-markup paper (prepend-alist-chain 'font-family 'sans props) arg))
159 (def-markup-command (number paper props arg) (markup?)
160 "Set font family to @code{number}, which yields the font used for
161 time signatures and fingerings. This font only contains numbers and
162 some punctuation. It doesn't have any letters. "
163 (interpret-markup paper (prepend-alist-chain 'font-encoding 'fetaNumber props) arg))
165 (def-markup-command (roman paper props arg) (markup?)
166 "Set font family to @code{roman}."
167 (interpret-markup paper (prepend-alist-chain 'font-family 'roman props) arg))
169 (def-markup-command (huge paper props arg) (markup?)
170 "Set font size to +2."
171 (interpret-markup paper (prepend-alist-chain 'font-size 2 props) arg))
173 (def-markup-command (large paper props arg) (markup?)
174 "Set font size to +1."
175 (interpret-markup paper (prepend-alist-chain 'font-size 1 props) arg))
177 (def-markup-command (normalsize paper props arg) (markup?)
178 "Set font size to default."
179 (interpret-markup paper (prepend-alist-chain 'font-size 0 props) arg))
181 (def-markup-command (small paper props arg) (markup?)
182 "Set font size to -1."
183 (interpret-markup paper (prepend-alist-chain 'font-size -1 props) arg))
185 (def-markup-command (tiny paper props arg) (markup?)
186 "Set font size to -2."
187 (interpret-markup paper (prepend-alist-chain 'font-size -2 props) arg))
189 (def-markup-command (teeny paper props arg) (markup?)
190 "Set font size to -3."
191 (interpret-markup paper (prepend-alist-chain 'font-size -3 props) arg))
193 (def-markup-command (caps paper props arg) (markup?)
194 "Set font shape to @code{caps}."
195 (interpret-markup paper (prepend-alist-chain 'font-shape 'caps props) arg))
197 (def-markup-command (latin-i paper props arg) (markup?)
198 "TEST latin1 encoding."
199 (interpret-markup paper (prepend-alist-chain 'font-shape 'latin1 props) arg))
201 (def-markup-command (dynamic paper props arg) (markup?)
202 "Use the dynamic font. This font only contains @b{s}, @b{f}, @b{m},
203 @b{z}, @b{p}, and @b{r}. When producing phrases, like ``pi@`{u} @b{f}'', the
204 normal words (like ``pi@`{u}'') should be done in a different font. The
205 recommend font for this is bold and italic"
207 paper (prepend-alist-chain 'font-encoding 'fetaDynamic props) arg))
209 (def-markup-command (italic paper props arg) (markup?)
210 "Use italic @code{font-shape} for @var{arg}. "
211 (interpret-markup paper (prepend-alist-chain 'font-shape 'italic props) arg))
213 (def-markup-command (typewriter paper props arg) (markup?)
214 "Use @code{font-family} typewriter for @var{arg}."
216 paper (prepend-alist-chain 'font-family 'typewriter props) arg))
218 (def-markup-command (upright paper props arg) (markup?)
219 "Set font shape to @code{upright}."
221 paper (prepend-alist-chain 'font-shape 'upright props) arg))
223 (def-markup-command (doublesharp paper props) ()
224 "Draw a double sharp symbol."
226 (interpret-markup paper props (markup #:musicglyph "accidentals-4")))
227 (def-markup-command (sesquisharp paper props) ()
228 "Draw a 3/2 sharp symbol."
229 (interpret-markup paper props (markup #:musicglyph "accidentals-3")))
231 (def-markup-command (sharp paper props) ()
232 "Draw a sharp symbol."
233 (interpret-markup paper props (markup #:musicglyph "accidentals-2")))
234 (def-markup-command (semisharp paper props) ()
235 "Draw a semi sharp symbol."
236 (interpret-markup paper props (markup #:musicglyph "accidentals-1")))
237 (def-markup-command (natural paper props) ()
238 "Draw a natural symbol."
240 (interpret-markup paper props (markup #:musicglyph "accidentals-0")))
241 (def-markup-command (semiflat paper props) ()
243 (interpret-markup paper props (markup #:musicglyph "accidentals--1")))
244 (def-markup-command (flat paper props) ()
245 "Draw a flat symbol."
247 (interpret-markup paper props (markup #:musicglyph "accidentals--2")))
248 (def-markup-command (sesquiflat paper props) ()
249 "Draw a 3/2 flat symbol."
251 (interpret-markup paper props (markup #:musicglyph "accidentals--3")))
252 (def-markup-command (doubleflat paper props) ()
253 "Draw a double flat symbol."
255 (interpret-markup paper props (markup #:musicglyph "accidentals--4")))
258 (def-markup-command (column paper props args) (markup-list?)
259 "Stack the markups in @var{args} vertically."
261 -1 0.0 (chain-assoc-get 'baseline-skip props)
262 (map (lambda (m) (interpret-markup paper props m)) args)))
264 (def-markup-command (dir-column paper props args) (markup-list?)
265 "Make a column of args, going up or down, depending on the setting
266 of the @code{#'direction} layout property."
267 (let* ((dir (chain-assoc-get 'direction props)))
269 (if (number? dir) dir -1)
271 (chain-assoc-get 'baseline-skip props)
272 (map (lambda (x) (interpret-markup paper props x)) args))))
274 (def-markup-command (center-align paper props args) (markup-list?)
275 "Put @code{args} in a centered column. "
276 (let* ((mols (map (lambda (x) (interpret-markup paper props x)) args))
277 (cmols (map (lambda (x) (ly:stencil-align-to! x X CENTER)) mols)))
278 (stack-lines -1 0.0 (chain-assoc-get 'baseline-skip props) mols)))
280 (def-markup-command (vcenter paper props arg) (markup?)
281 "Align @code{arg} to its center. "
282 (let* ((mol (interpret-markup paper props arg)))
283 (ly:stencil-align-to! mol Y CENTER)
286 (def-markup-command (right-align paper props arg) (markup?)
287 (let* ((m (interpret-markup paper props arg)))
288 (ly:stencil-align-to! m X RIGHT)
291 (def-markup-command (left-align paper props arg) (markup?)
292 "Align @var{arg} on its left edge. "
294 (let* ((m (interpret-markup paper props arg)))
295 (ly:stencil-align-to! m X LEFT)
298 (def-markup-command (general-align paper props axis dir arg) (integer? number? markup?)
299 "Align @var{arg} in @var{axis} direction to the @var{dir} side."
300 (let* ((m (interpret-markup paper props arg)))
302 (ly:stencil-align-to! m axis dir)
306 (def-markup-command (halign paper props dir arg) (number? markup?)
307 "Set horizontal alignment. If @var{dir} is -1, then it is
308 left-aligned, while+1 is right. Values in between interpolate alignment
312 (let* ((m (interpret-markup paper props arg)))
313 (ly:stencil-align-to! m X dir)
316 (def-markup-command (musicglyph paper props glyph-name) (string?)
317 "This is converted to a musical symbol, e.g. @code{\\musicglyph
318 #\"accidentals-0\"} will select the natural sign from the music font.
319 See @usermanref{The Feta font} for a complete listing of the possible glyphs.
321 (ly:find-glyph-by-name
322 (ly:paper-get-font paper (cons '((font-encoding . fetaMusic))
327 (def-markup-command (lookup paper props glyph-name) (string?)
328 "Lookup a glyph by name."
329 (ly:find-glyph-by-name (ly:paper-get-font paper props)
332 (def-markup-command (char paper props num) (integer?)
333 "This produces a single character, e.g. @code{\\char #65} produces the
335 (ly:get-glyph (ly:paper-get-font paper props) num))
337 (def-markup-command (raise paper props amount arg) (number? markup?)
339 This raises @var{arg}, by the distance @var{amount}.
340 A negative @var{amount} indicates lowering:
342 @lilypond[verbatim,fragment,relative=1]
343 c1^\\markup { C \\small \\raise #1.0 \\bold { \"9/7+\" }}
345 The argument to @code{\\raise} is the vertical displacement amount,
346 measured in (global) staff spaces. @code{\\raise} and @code{\\super}
347 raise objects in relation to their surrounding markups.
349 If the text object itself is positioned above or below the staff, then
350 @code{\\raise} cannot be used to move it, since the mechanism that
351 positions it next to the staff cancels any shift made with
352 @code{\\raise}. For vertical positioning, use the @code{padding}
353 and/or @code{extra-offset} properties. "
356 (ly:stencil-translate-axis (interpret-markup paper props arg)
359 (def-markup-command (fraction paper props arg1 arg2) (markup? markup?)
360 "Make a fraction of two markups."
362 (let* ((m1 (interpret-markup paper props arg1))
363 (m2 (interpret-markup paper props arg2)))
364 (ly:stencil-align-to! m1 X CENTER)
365 (ly:stencil-align-to! m2 X CENTER)
366 (let* ((x1 (ly:stencil-extent m1 X))
367 (x2 (ly:stencil-extent m2 X))
368 (line (ly:round-filled-box (interval-union x1 x2) '(-0.05 . 0.05) 0.0))
369 ;; should stack mols separately, to maintain LINE on baseline
370 (stack (stack-lines -1 0.2 0.6 (list m1 line m2))))
371 (ly:stencil-align-to! stack Y CENTER)
372 (ly:stencil-align-to! stack X LEFT)
373 ;; should have EX dimension
375 (ly:stencil-translate-axis stack 0.75 Y))))
378 ;; TODO: better syntax.
380 (def-markup-command (note-by-number paper props log dot-count dir) (number? number? number?)
381 "Construct a note symbol, with stem. By using fractional values for
382 @var{dir}, you can obtain longer or shorter stems."
384 (let* ((font (ly:paper-get-font paper (cons '((font-encoding . fetaMusic)) props)))
385 (stemlen (max 3 (- log 1)))
386 (headgl (ly:find-glyph-by-name
388 (string-append "noteheads-" (number->string (min log 2)))))
390 (stemy (* dir stemlen))
391 (attachx (if (> dir 0)
392 (- (cdr (ly:stencil-extent headgl X)) stemth)
394 (attachy (* dir 0.28))
395 (stemgl (and (> log 0)
397 (cons attachx (+ attachx stemth))
398 (cons (min stemy attachy)
401 (dot (ly:find-glyph-by-name font "dots-dot"))
402 (dotwid (interval-length (ly:stencil-extent dot X)))
403 (dots (and (> dot-count 0)
404 (apply ly:stencil-add
406 (ly:stencil-translate-axis
407 dot (* (+ 1 (* 2 x)) dotwid) X) )
408 (iota dot-count 1)))))
409 (flaggl (and (> log 2)
410 (ly:stencil-translate
411 (ly:find-glyph-by-name font
412 (string-append "flags-"
413 (if (> dir 0) "u" "d")
414 (number->string log)))
415 (cons (+ attachx (/ stemth 2)) stemy)))))
417 (set! stemgl (ly:stencil-add flaggl stemgl)))
418 (if (ly:stencil? stemgl)
419 (set! stemgl (ly:stencil-add stemgl headgl))
420 (set! stemgl headgl))
421 (if (ly:stencil? dots)
424 (ly:stencil-translate-axis dots
425 (+ (if (and (> dir 0) (> log 2))
428 ;; huh ? why not necessary?
429 ;;(cdr (ly:stencil-extent headgl X))
435 (use-modules (ice-9 regex))
438 (let ((divisor (log 2)))
439 (lambda (z) (inexact->exact (/ (log z) divisor)))))
441 (define (parse-simple-duration duration-string)
442 "Parse the `duration-string', e.g. ''4..'' or ''breve.'', and return a (log dots) list."
443 (let ((match (regexp-exec (make-regexp "(breve|longa|maxima|[0-9]+)(\\.*)") duration-string)))
444 (if (and match (string=? duration-string (match:substring match 0)))
445 (let ((len (match:substring match 1))
446 (dots (match:substring match 2)))
447 (list (cond ((string=? len "breve") -1)
448 ((string=? len "longa") -2)
449 ((string=? len "maxima") -3)
450 (else (log2 (string->number len))))
451 (if dots (string-length dots) 0)))
452 (error "This is not a valid duration string:" duration-string))))
454 (def-markup-command (note paper props duration dir) (string? number?)
455 "This produces a note with a stem pointing in @var{dir} direction, with
456 the @var{duration} for the note head type and augmentation dots. For
457 example, @code{\\note #\"4.\" #-0.75} creates a dotted quarter note, with
458 a shortened down stem."
460 (let ((parsed (parse-simple-duration duration)))
461 (note-by-number-markup paper props (car parsed) (cadr parsed) dir)))
463 (def-markup-command (normal-size-super paper props arg) (markup?)
464 "A superscript which does not use a smaller font."
466 (ly:stencil-translate-axis (interpret-markup
469 (* 0.5 (chain-assoc-get 'baseline-skip props))
472 (def-markup-command (super paper props arg) (markup?)
475 @cindex lowering text
477 @cindex translating text
479 @cindex @code{\\super}
482 Raising and lowering texts can be done with @code{\\super} and
485 @lilypond[verbatim,fragment,relative=1]
486 c1^\\markup { E \"=\" mc \\super \"2\" }
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 (translate paper props offset arg) (number-pair? markup?)
500 "This translates an object. Its first argument is a cons of numbers
502 A \\translate #(cons 2 -3) @{ B C @} D
504 This moves `B C' 2 spaces to the right, and 3 down, relative to its
505 surroundings. This command cannot be used to move isolated scripts
506 vertically, for the same reason that @code{\\raise} cannot be used for
510 (ly:stencil-translate (interpret-markup paper props arg)
513 (def-markup-command (sub paper props arg) (markup?)
514 "Set @var{arg} in subscript."
516 (ly:stencil-translate-axis
519 (cons `((font-size . ,(- (chain-assoc-get 'font-size props 0) 3))) props)
521 (* -0.5 (chain-assoc-get 'baseline-skip props))
524 (def-markup-command (normal-size-sub paper props arg) (markup?)
525 "Set @var{arg} in subscript, in a normal font size."
527 (ly:stencil-translate-axis
528 (interpret-markup paper props arg)
529 (* -0.5 (chain-assoc-get 'baseline-skip props))
532 (def-markup-command (hbracket paper props arg) (markup?)
533 "Draw horizontal brackets around @var{arg}."
534 (let ((th 0.1) ;; todo: take from GROB.
535 (m (interpret-markup paper props arg)))
536 (bracketify-stencil m X th (* 2.5 th) th)))
538 (def-markup-command (bracket paper props arg) (markup?)
539 "Draw vertical brackets around @var{arg}."
540 (let ((th 0.1) ;; todo: take from GROB.
541 (m (interpret-markup paper props arg)))
542 (bracketify-stencil m Y th (* 2.5 th) th)))
544 ;; todo: fix negative space
545 (def-markup-command (hspace paper props amount) (number?)
546 "This produces a invisible object taking horizontal space.
548 \\markup @{ A \\hspace #2.0 B @}
550 will put extra space between A and B, on top of the space that is
551 normally inserted before elements on a line.
554 (ly:make-stencil "" (cons 0 amount) '(-1 . 1) )
555 (ly:make-stencil "" (cons amount amount) '(-1 . 1))))
557 (def-markup-command (override paper props new-prop arg) (pair? markup?)
558 "Add the first argument in to the property list. Properties may be
559 any sort of property supported by @internalsref{font-interface} and
560 @internalsref{text-interface}, for example
563 \\override #'(font-family . married) \"bla\"
567 (interpret-markup paper (cons (list new-prop) props) arg))
569 (def-markup-command (smaller paper props arg) (markup?)
570 "Decrease the font size relative to current setting"
571 (let* ((fs (chain-assoc-get 'font-size props 0))
572 (entry (cons 'font-size (- fs 1))))
573 (interpret-markup paper (cons (list entry) props) arg)))
576 (def-markup-command (bigger paper props arg) (markup?)
577 "Increase the font size relative to current setting"
578 (let* ((fs (chain-assoc-get 'font-size props 0))
579 (entry (cons 'font-size (+ fs 1))))
580 (interpret-markup paper (cons (list entry) props) arg)))
582 (def-markup-command larger (markup?)
585 (def-markup-command (box paper props arg) (markup?)
586 "Draw a box round @var{arg}"
590 (m (interpret-markup paper props arg)))
591 (box-stencil m th pad)))
593 (def-markup-command (strut paper props) ()
595 "Create a box of the same height as the space in the current font.
597 FIXME: is this working?
600 (let ((m (Text_item::interpret_markup paper props " ")))
601 (ly:stencil-set-extent! m X '(1000 . -1000))
604 (define number->mark-letter-vector (make-vector 25 #\A))
609 (if (= i (- (char->integer #\I) (char->integer #\A)))
611 (vector-set! number->mark-letter-vector j
612 (integer->char (+ i (char->integer #\A)))))
614 (define (number->markletter-string n)
615 "Double letters for big marks."
617 ((l (vector-length number->mark-letter-vector)))
620 (string-append (number->markletter-string (1- (quotient n l)))
621 (number->markletter-string (remainder n l)))
622 (make-string 1 (vector-ref number->mark-letter-vector n)))))
625 (def-markup-command (markletter paper props num) (integer?)
626 "Make a markup letter for @var{num}. The letters start with A to Z
627 (skipping I), and continues with double letters."
629 (Text_item::interpret_markup paper props (number->markletter-string num)))
634 (def-markup-command (bracketed-y-column paper props indices args)
636 "Make a column of the markups in @var{args}, putting brackets around
637 the elements marked in @var{indices}, which is a list of numbers."
639 (define (sublist l start stop)
640 (take (drop l start) (- (1+ stop) start)) )
642 (define (stencil-list-extent ss axis)
644 (apply min (map (lambda (x) (car (ly:stencil-extent x axis))) ss))
645 (apply max (map (lambda (x) (cdr (ly:stencil-extent x axis))) ss))))
647 (define (stack-stencils stencils bskip last-stencil)
649 ((null? stencils) '())
652 (stack-stencils (cdr stencils) bskip (car stencils))))
655 ((orig (car stencils))
656 (dir (chain-assoc-get 'direction props DOWN))
657 (new (ly:stencil-moved-to-edge last-stencil Y dir
662 (cons new (stack-stencils (cdr stencils) bskip new))))
665 (define (make-brackets stencils indices acc)
668 (pair? (cdr indices)))
670 ((encl (sublist stencils (car indices) (cadr indices)))
671 (x-ext (stencil-list-extent encl X))
672 (y-ext (stencil-list-extent encl Y))
675 (protusion (* 2.5 thick))
677 (ly:stencil-translate-axis
678 (ly:bracket Y y-ext thick protusion)
679 (- (car x-ext) pad) X))
680 (rb (ly:stencil-translate-axis
681 (ly:bracket Y y-ext thick (- protusion))
682 (+ (cdr x-ext) pad) X))
686 stencils (cddr indices)
700 (chain-assoc-get 'baseline-skip props))
701 (stacked (stack-stencils stencils 1.25 #f))
702 (brackets (make-brackets stacked indices '()))
705 (apply ly:stencil-add
706 (append stacked brackets)