lilypond-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Center Lyric Syllables (ignoring punctuation)


From: Kieren MacMillan
Subject: Re: Center Lyric Syllables (ignoring punctuation)
Date: Mon, 3 Mar 2014 21:22:26 -0500

Hi everyone,

> attached is a modified version which works on 2.17.x and 2.18. Could you 
> update the LSR snippet? I'm not allowed to. Thanks!

Has anyone else noticed that this [amazing] function results in it being 
impossible to tweak the LyricText.self-alignment-X property? (See example, 
below.)
Is there a fix?

Thanks,
Kieren.

%%%%%  SNIPPET BEGINS

\version "2.19"
\language "english"

#(define space-set
  (list->char-set (string->list ".?-;,:“”‘’–— */()[]{}|<>!`~&…")))

#(define (width grob text-string)
  (let* ((layout (ly:grob-layout grob))
         (props
          (ly:grob-alist-chain
           grob
           (ly:output-def-lookup layout 'text-font-defaults)))
         (X-extent (ly:stencil-extent
                    (ly:text-interface::interpret-markup layout props
                     (markup text-string)) X)))
   (if (interval-empty? X-extent)
    0 (cdr X-extent))))

#(define (center-on-word grob)
  (let* ((text (ly:grob-property-data grob 'text))
         (syllable (if (string? text) text ""))
         (word-position
          (if (integer? (string-skip syllable space-set))
           (string-skip syllable space-set)
           0))
         (word-end
          (if (integer? (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 (if (string? text) (width grob preword) 0))
         (word-width (if (string? text) (width grob word) (width grob text)))
         (notehead (ly:grob-parent grob X))
         (refp (ly:grob-common-refpoint notehead grob X))
         (note-extent (ly:grob-extent notehead refp X))
         (note-width (- (cdr note-extent) (car note-extent))))

   (if (= -1 (ly:grob-property-data grob 'self-alignment-X))
    (- 0 preword-width)
    (- (/ (- note-width word-width) 2) preword-width))))


\layout {
  \context {
    \Lyrics
    \override LyricText #'X-offset = #center-on-word
  }
}

notes = \repeat unfold 5 { \repeat unfold 4 c'' \break }
words = \lyricmode { \repeat unfold 10 { foo bar } }
tweaks = { s1*2 s4 \once \override Score.LyricText.self-alignment-X = #RIGHT 
\once \override Score.LyricText.color = #red }

\score {
  <<
    \new Staff << \notes \tweaks >>
    \new Lyrics \words
  >>
}

%%%%%   SNIPPET ENDS


reply via email to

[Prev in Thread] Current Thread [Next in Thread]