Hi Ming and Jun,

This should work for the special period character.  If there are any other
punctuation marks you need at the ends of words, add them as strings to the
variable called "punct-right."  Note: punctuation which goes to the left is
handled differently.  If you need additional characters, add them to the
variable "punct-L" as characters--i.e. preceded by #\

If you need any multi-byte characters for punctuation which goes to the
left, the function will need to be adapted.  Easy enough to do--just let me
know.

HTH,
David

%%%%%%%%%%%%%%%%%%%%%%%%%
\version "2.16"

#(define (drop-punctuation-right str)
  (let ((punct-right '("," ";" ":" "?" "\"" "'" "!" "。")))
    (let loop ((elt punct-right))
      (if (null? elt)
          #f
          (if (string-suffix? (car elt) str)
              (string-drop-right str (string-length (car elt)))
              (loop (cdr elt)))))))

#(define (drop-all-punctuation-right str)
  (let ((core (drop-punctuation-right str)))
    (if core
        (drop-all-punctuation-right core)
        str)))

#(define (align grob)
  (let* ((stil (ly:grob-property grob 'stencil))
         (stil-X (ly:stencil-extent stil X))
         (text (ly:grob-property grob 'text))
         (punct-L (char-set #\" #\`))
         ; determine X-extent of text stencil without punctuation at end
         (text-L (drop-all-punctuation-right text))
         (text-L-stil (grob-interpret-markup grob text-L))
         (text-L-stil-X (ly:stencil-extent text-L-stil X))
         (displacement (/ (- (cdr text-L-stil-X) (cdr stil-X)) 2))
         ; determine X-extent of text stencil without punctuation at start
         (text-R (string-trim text punct-L))
         (text-R-stil (grob-interpret-markup grob text-R))
         (text-R-stil-X (ly:stencil-extent text-R-stil X))
         (displacement (+ displacement
                          (/ (- (cdr stil-X) (cdr text-R-stil-X)) 2))))

     (- (ly:self-alignment-interface::aligned-on-x-parent grob)
        displacement)))

\paper {
  ragged-right = ##f
}

\relative c' {
  \time 3/4 e4 e4. e8
  d4 e d c2.
}

\addlyrics {
  \override LyricText #'X-offset = #align
  "\"I" am so lone -- "ly,\"" said she
}
\addlyrics {
  \override LyricText #'X-offset = #align
  樂無  極! 寶  名, 貝  名。 啊?
}
\addlyrics {
  \override LyricText #'X-offset = #align
  a b c d e f g
}
_______________________________________________
lilypond-user mailing list
lilypond-user@gnu.org
https://lists.gnu.org/mailman/listinfo/lilypond-user

Reply via email to