]> git.donarmstrong.com Git - lilypond.git/blob - Documentation/snippets/new/centering-markup-on-note-heads-automatically.ly
Run scripts/auxiliar/update-with-convert-ly.sh
[lilypond.git] / Documentation / snippets / new / centering-markup-on-note-heads-automatically.ly
1 \version "2.16.0"
2
3 \header {
4   lsrtags = "text, tweaks-and-overrides, contexts-and-engravers"
5   texidoc = "
6 For technical reasons, text scripts attached to note heads cannot
7 easily be centered on a note head's width, unlike articulations.
8
9 Instead of using trial-and-error offset tweaks, this snippet uses a
10 Scheme engraver to reset the horizontal parent of each markup to a
11 @code{NoteColumn}.  This also allows text to follow note heads which have
12 been shifted via @code{force-hshift}.
13 "
14   doctitle = "Centering markup on note heads automatically"
15 }
16
17 #(define (Text_align_engraver ctx)
18   (let ((scripts '())
19         (note-column #f))
20     (make-engraver
21      (acknowledgers
22       ((note-column-interface trans grob source)
23        ;; cache NoteColumn in this Voice context
24        (set! note-column grob))
25       ((text-script-interface trans grob source)
26        ;; whenever a TextScript is acknowledged,
27        ;; add it to `scripts' list
28        (set! scripts (cons grob scripts))))
29      ((stop-translation-timestep trans)
30       ;; if any TextScript grobs exist,
31       ;; set NoteColumn as X-parent
32       (for-each (lambda (script)
33                   (set! (ly:grob-parent script X) note-column))
34                 scripts)
35       ;; clear scripts ready for next timestep
36       (set! scripts '())))))
37
38 \layout {
39   \context {
40     \Voice
41     \consists #Text_align_engraver
42     \override TextScript #'X-offset =
43       #ly:self-alignment-interface::aligned-on-x-parent
44     \override TextScript #'self-alignment-X = #CENTER
45   }
46 }
47
48 \new Staff <<
49   \relative c'' {
50     \override NoteColumn #'force-hshift = #3
51     c1-\markup { \arrow-head #Y #DOWN ##t }
52   }
53   \\
54   \relative c' {
55     a4 a-\markup { \huge ^ } a a
56   }
57 >>