lilypond-user
[Top][All Lists]
Advanced

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

Re: Distance of grob to barlines


From: David Nalesnik
Subject: Re: Distance of grob to barlines
Date: Sat, 22 Aug 2015 11:55:10 -0500

Hi Andrew,

On Fri, Aug 21, 2015 at 6:51 PM, Andrew Bernard <address@hidden> wrote:
Can anybody kindly point me to code examples showing how to find the distance of a grob to its nearest enclosing barlines, in Scheme?

I don't know of any examples out there, be this will do it.

This uses the horizontal coordinate of the grob and barline as a simple demonstration.  Depending on what precisely you mean by "distance" (right edge to left edge, left edge to right edge, etc.) you may want to use ly:grob-extent.

HTH,
David

%%%%%%%%%%%%%%
 \version "2.19.23"

#(define (distance-to-nearest-barline dir)
   (lambda (grob)
     (let* ((refp (ly:grob-system grob))
            (all-elts (ly:grob-array->list (ly:grob-object refp 'all-elements)))
            (barlines (filter (lambda (elt)
                                (grob::has-interface elt 'bar-line-interface))
                              all-elts))
            (me-pos (grob::rhythmic-location grob))
            (pred? (if (= LEFT dir) rhythmic-location>=? rhythmic-location<?))
            (closest (filter (lambda (b)
                               (pred? me-pos (grob::rhythmic-location b)))
                             barlines))
            (closest (reduce
                      (lambda (elem prev)
                        (if (pred?
                             (grob::rhythmic-location prev) (grob::rhythmic-location elem))
                            prev
                            elem))

                      #f
                      closest)))
       
       (if closest
           (let* ((me-X (ly:grob-relative-coordinate grob refp X))
                  (closest-X (ly:grob-relative-coordinate closest refp X))
                  (dist (- closest-X me-X)))
             (ly:grob-set-property! grob 'color darkgreen) ; for testing
             (ly:grob-set-property! closest 'color red) ; for testing
             (format #t "distance between ~a and ~a: ~a~%"
               grob closest dist))
           (ly:message "No closest barline!")))))

{
  
  c'4 d' e' 
  \once \override NoteHead.after-line-breaking = #(distance-to-nearest-barline RIGHT)
  f'
  d'1
  c'4 d' e'
  \once \override Stem.after-line-breaking = #(distance-to-nearest-barline LEFT)
  f'
  d'1
}

reply via email to

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