X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=Documentation%2Fsnippets%2Fcentering-markup-on-note-heads-automatically.ly;h=512cabdff4be89dd736a4802d68458c4487a0483;hb=057106293b07b74b00553fe4dc3dfac5c1f3b682;hp=509534a91b52878a72145a66ae105ac0012138cf;hpb=ff8573ef9104b7104d943c54385df3a5eecde5d2;p=lilypond.git diff --git a/Documentation/snippets/centering-markup-on-note-heads-automatically.ly b/Documentation/snippets/centering-markup-on-note-heads-automatically.ly index 509534a91b..512cabdff4 100644 --- a/Documentation/snippets/centering-markup-on-note-heads-automatically.ly +++ b/Documentation/snippets/centering-markup-on-note-heads-automatically.ly @@ -1,53 +1,68 @@ -%% Do not edit this file; it is automatically -%% generated from LSR http://lsr.dsi.unimi.it -%% This file is in the public domain. -\version "2.13.4" +% Do not edit this file; it is automatically +% generated from Documentation/snippets/new +% This file is in the public domain. +%% Note: this file works from version 2.13.36 +\version "2.13.36" \header { - lsrtags = "text, tweaks-and-overrides" - + 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 accesses a -note head (or rest) from the @code{TextScript} object's horizontal -parent (a paper column), using its extent to correct the positioning. - - - +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" } % begin verbatim -textScriptCenterOnNote = \override TextScript #'X-offset = -#(lambda (grob) - (let* ((paper-col (ly:grob-parent grob X)) - (elts (ly:grob-object paper-col 'elements)) - (rhythmic-head - (if (ly:grob-array? elts) - (let loop ((array-idx 0)) - (call/cc - (lambda (return) - (let ((array-len (ly:grob-array-length elts))) - (if (< array-idx (1- array-len)) - (let ((elt (ly:grob-array-ref elts array-idx))) - (if (grob::has-interface elt - 'rhythmic-head-interface) - (return elt) - (loop (1+ array-idx))))) - grob)))) - grob))) - - (+ - (ly:self-alignment-interface::x-aligned-on-self grob) - (interval-center - (ly:grob-robust-relative-extent rhythmic-head rhythmic-head X))))) - -\relative c' { - \override TextScript #'self-alignment-X = #CENTER - \textScriptCenterOnNote - 1-\markup \arrow-head #Y #UP ##t - 1-\markup \huge "^" + +#(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 + } +>>