Hi Andrew,

On Fri, Aug 21, 2015 at 6:51 PM, Andrew Bernard <andrew.bern...@gmail.com>
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
}
_______________________________________________
lilypond-user mailing list
lilypond-user@gnu.org
https://lists.gnu.org/mailman/listinfo/lilypond-user

Reply via email to