]> git.donarmstrong.com Git - lilypond.git/blob - Documentation/snippets/new/centering-markup-on-note-heads-automatically.ly
Docs: run convert-ly for 2.14.0.
[lilypond.git] / Documentation / snippets / new / centering-markup-on-note-heads-automatically.ly
1 \version "2.14.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
21     `((acknowledgers
22        (note-column-interface
23         . ,(lambda (trans grob source)
24              ;; cache NoteColumn in this Voice context
25              (set! note-column grob)))
26
27        (text-script-interface
28         . ,(lambda (trans grob source)
29              ;; whenever a TextScript is acknowledged,
30              ;; add it to `scripts' list
31              (set! scripts (cons grob scripts)))))
32
33       (stop-translation-timestep
34        . ,(lambda (trans)
35             ;; if any TextScript grobs exist,
36             ;; set NoteColumn as X-parent
37             (and (pair? scripts)
38                  (for-each (lambda (script)
39                              (set! (ly:grob-parent script X) note-column))
40                            scripts))
41             ;; clear scripts ready for next timestep
42             (set! scripts '()))))))
43
44 \layout {
45   \context {
46     \Voice
47     \consists #Text_align_engraver
48     \override TextScript #'X-offset =
49       #ly:self-alignment-interface::aligned-on-x-parent
50     \override TextScript #'self-alignment-X = #CENTER
51   }
52 }
53
54 \new Staff <<
55   \relative c'' {
56     \override NoteColumn #'force-hshift = #3
57     c1-\markup { \arrow-head #Y #DOWN ##t }
58   }
59   \\
60   \relative c' {
61     a4 a-\markup { \huge ^ } a a
62   }
63 >>