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