\version "2.19.50" %% and higher %% https://lists.gnu.org/archive/html/lilypond-user/2016-12/msg00382.html %% http://lsr.di.unimi.it/LSR/Item?id=888 #(define space-set (list->char-set (string->list ".?-;,:„“‚‘«»‹›『』「」“”‘’–— */()[]{}|<>!`~&…†‡"))) #(define (width grob text) (let* ((X-extent (ly:stencil-extent (grob-interpret-markup grob text) X))) (if (interval-empty? X-extent) 0 (cdr X-extent)))) #(define (remove-suspended-note-heads stem note-heads) (let* ((nc (ly:grob-common-refpoint stem (car note-heads) X)) (stem-coord (ly:grob-relative-coordinate stem stem X)) (half-stem-thick (/ (ly:grob-property stem 'thickness) 2)) (stem-dir (ly:grob-property stem 'direction))) (remove (lambda (nh) (if (positive? stem-dir) (> (ly:grob-relative-coordinate nh nc X) stem-coord) (< (ly:grob-relative-coordinate nh nc X) (- stem-coord half-stem-thick)))) note-heads))) #(define (make-center-on-word-callback grob center-on-chords) (let* ((text (ly:grob-property-data grob 'text)) (syllable (markup->string text)) (word-position (if (string-skip syllable space-set) (string-skip syllable space-set) 0)) (word-end (if (string-skip-right syllable space-set) (+ (string-skip-right syllable space-set) 1) (string-length syllable))) (preword (substring syllable 0 word-position)) (word (substring syllable word-position word-end)) (preword-width (width grob preword)) (word-width (width grob (if (string-null? syllable) text word))) (note-column (ly:grob-parent grob X)) (stem (ly:grob-object note-column 'stem)) (stem-dir (ly:grob-property stem 'direction)) (sys (ly:grob-system grob)) (nh-ls (if (ly:grob-array? (ly:grob-object note-column 'note-heads)) (ly:grob-array->list (ly:grob-object note-column 'note-heads)) '())) (full-column-width (interval-length (ly:relative-group-extent nh-ls note-column X))) (note-column-width (interval-length (ly:relative-group-extent (remove-suspended-note-heads stem nh-ls) note-column X)))) (- (* (/ (if center-on-chords (if (positive? stem-dir) (- full-column-width word-width) (- (* 2 note-column-width) full-column-width word-width)) (- note-column-width word-width)) 2) (1+ (ly:grob-property-data grob 'self-alignment-X))) preword-width))) #(define (center-on-word grob) (make-center-on-word-callback grob #f)) #(define (center-on-word-on-chords grob) (make-center-on-word-callback grob #t)) \layout { \context { \Lyrics \override LyricText #'X-offset = #center-on-word-on-chords } }