X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=Documentation%2Fsnippets%2Fnew%2Fcentering-markup-on-note-heads-automatically.ly;fp=Documentation%2Fsnippets%2Fnew%2Fcentering-markup-on-note-heads-automatically.ly;h=47169a559bd1c801a0e18a3336793f2869e5b813;hb=e90f0536f9be39ada0bef0aeb0d275dec3b2fb5b;hp=0000000000000000000000000000000000000000;hpb=a8c9e8a7ca320ab0df5fd32e717fd62cd7635ce6;p=lilypond.git diff --git a/Documentation/snippets/new/centering-markup-on-note-heads-automatically.ly b/Documentation/snippets/new/centering-markup-on-note-heads-automatically.ly new file mode 100644 index 0000000000..47169a559b --- /dev/null +++ b/Documentation/snippets/new/centering-markup-on-note-heads-automatically.ly @@ -0,0 +1,63 @@ +\version "2.14.0" + +\header { + lsrtags = "text, tweaks-and-overrides, contexts-and-engravers" + texidoc = " +For technical reasons, text scripts attached to note heads cannot +easily be centered on a note head's width, unlike articulations. + +Instead of using trial-and-error offset tweaks, this snippet uses a +Scheme engraver to reset the horizontal parent of each markup to a +@code{NoteColumn}. This also allows text to follow note heads which have +been shifted via @code{force-hshift}. +" + doctitle = "Centering markup on note heads automatically" +} + +#(define (Text_align_engraver ctx) + (let ((scripts '()) + (note-column #f)) + + `((acknowledgers + (note-column-interface + . ,(lambda (trans grob source) + ;; cache NoteColumn in this Voice context + (set! note-column grob))) + + (text-script-interface + . ,(lambda (trans grob source) + ;; whenever a TextScript is acknowledged, + ;; add it to `scripts' list + (set! scripts (cons grob scripts))))) + + (stop-translation-timestep + . ,(lambda (trans) + ;; if any TextScript grobs exist, + ;; set NoteColumn as X-parent + (and (pair? scripts) + (for-each (lambda (script) + (set! (ly:grob-parent script X) note-column)) + scripts)) + ;; clear scripts ready for next timestep + (set! scripts '())))))) + +\layout { + \context { + \Voice + \consists #Text_align_engraver + \override TextScript #'X-offset = + #ly:self-alignment-interface::aligned-on-x-parent + \override TextScript #'self-alignment-X = #CENTER + } +} + +\new Staff << + \relative c'' { + \override NoteColumn #'force-hshift = #3 + c1-\markup { \arrow-head #Y #DOWN ##t } + } + \\ + \relative c' { + a4 a-\markup { \huge ^ } a a + } +>>