Hello list,

to my grief I noticed that David Nalesnik's id-aware spanner implementation in scheme no longer works in 2.39.48. It gives me errors like "warning: No spanner to end!!" and "warning: incomplete spanner removed!" if there are overlapping spanners. No

For reference, I again attach David N's (most recent) textspanner implementation. The \score block is a minimal example.

Note: this worked fine with 2.19.39, and fails with 2.19.48. I didn't look at intermediate versions.


\version "2.19"

%% Incorporating some code from the rewrite in Scheme of
%% Text_spanner_engraver in input/regression/scheme-text-spanner.ly

#(define (add-bound-item spanner item)
   (if (null? (ly:spanner-bound spanner LEFT))
       (ly:spanner-set-bound! spanner LEFT item)
       (ly:spanner-set-bound! spanner RIGHT item)))

#(define (axis-offset-symbol axis)
   (if (eq? axis X) 'X-offset 'Y-offset))

#(define (set-axis! grob axis)
   (if (not (number? (ly:grob-property grob 'side-axis)))
        (set! (ly:grob-property grob 'side-axis) axis)
         (if (eq? axis X)
         (axis-offset-symbol axis)))))

#(define (assign-spanner-index spanner orig-ls)
   "Determine the position of a new spanner in an ordered sequence
of spanners.  The goal is for the sequence to begin with zero and
contain no gaps.  Return the index representing the spanner's position."
   (if (null? orig-ls)
       (let loop ((ls orig-ls) (insert? #t) (result 0))
          ((null? ls) result)
          ;; position at head of list
          ((and insert? (> (caar orig-ls) 0))
           (loop ls #f 0))
          ;; no gaps, put at end of list
          ((and insert? (null? (cdr ls)))
           (loop (cdr ls) #f (1+ (caar ls))))
          ;; fill lowest position of gap
          ((and insert?
                (> (caadr ls) (1+ (caar ls))))
           (loop (cdr ls) #f (1+ (caar ls))))
          (else (loop (cdr ls) insert? result))))))

alternateTextSpannerEngraver =
#(lambda (context)
   (let (;; a list of pairs comprising a spanner index
          ;;  (not spanner-id) and a spanner which has been begun
          (spanners '())
          (finished '()) ; list of spanners in completion stage
          (start-events '()) ; list of START events
          (stop-events '())) ; list of STOP events
      ;; \startTextSpan, \stopTextSpan, and the like create events
      ;; which we collect here.
       ((text-span-event engraver event)
        (if (= START (ly:event-property event 'span-direction))
            (set! start-events (cons event start-events))
            (set! stop-events (cons event stop-events)))))
      ;; Populate 'note-columns property of spanners.  Bounds are
      ;; set to note columns, and each spanner keeps a record of
      ;; the note columns it traverses.
       ((note-column-interface engraver grob source-engraver)
        (for-each (lambda (s)
                     (cdr s) 'note-columns grob)
                    (if (null? (ly:spanner-bound (cdr s) LEFT))
                        (add-bound-item (cdr s) grob)))
        ;; finished only contains spanners, no indices
        (for-each (lambda (f)
                     f 'note-columns grob)
                    (if (null? (ly:spanner-bound f RIGHT))
                        (add-bound-item f grob)))

      ((process-music trans)
       ;; Move begun spanners from 'span' to 'finished'.  We do this
       ;; on the basis of 'spanner-id.  If we find a match--either
       ;; the strings are the same, or both are unset--a transfer
       ;; can be made.  Return a warning if we find no match: spanner
       ;; hasn't been properly begun.
        (lambda (es)
          (let ((es-id (ly:event-property es 'spanner-id)))
            (let loop ((sp spanners))
              (if (null? sp)
                  (ly:warning "No spanner to end!!")
                  (let ((sp-id (ly:event-property
                                (event-cause (cdar sp)) 'spanner-id)))
                        (string? sp-id)
                        (string? es-id)
                        (string=? sp-id es-id))
                       ;; deal with \startTextSpan, \stopTextSpan
                        (null? sp-id)
                        (null? es-id)))
                      (set! finished (cons (cdar sp) finished))
                      (set! spanners (remove (lambda (s) (eq? s (car sp))) spanners)))
                     (else (loop (cdr sp)))))))))

       ;; The end of our spanners can be acknowledged by other engravers.
        (lambda (f)
          (ly:engraver-announce-end-grob trans f (event-cause f)))

       ;; Make spanners in response to START events.
       ;; Each new spanner is assigned an index denoting its position relative to
       ;; other active spanners.  This is enforced (for the moment) by adding
       ;; a small amount to the spanner's 'outside-staff-priority proportional to
       ;; this index.  This is unlikely to result in conflicts, though a better
       ;; solution may be to organize the spanners by a new alignment grob.
       ;; Also, add any existing spanners to the 'side-support-elements array of
       ;; the new spanner.  This ensures correct ordering over line breaks when
       ;; 'outside-staff-priority is set to #f (which means that it is no longer
       ;; an outside-staff-object--not the default).
        (lambda (es)
          (let* ((new (ly:engraver-make-grob trans 'TextSpanner es))
                 (new-idx (assign-spanner-index new spanners))
                 (new-osp (ly:grob-property new 'outside-staff-priority))
                 (new-osp (if (number? new-osp)
                              (+ new-osp (/ new-idx 1000.0))
            (set! (ly:grob-property new 'outside-staff-priority) new-osp)
            (set-axis! new Y)
            ;; Add spanners with a lower index than new spanner to
            ;; its 'side-support-elements.  This allows new spanners
            ;; to fill gaps under the topmost spanner.
             (lambda (sp)
               (if (< (car sp) new-idx)
                   (ly:pointer-group-interface::add-grob new
                     'side-support-elements (cdr sp))))
            (set! spanners (cons (cons new-idx new) spanners))
            (set! spanners
                  (sort spanners (lambda (x y) (< (car x) (car y)))))))
       ;; Events have served their purpose for this timestep.  Clear
       ;; the way for new events in later timesteps.
       (set! start-events '())
       (set! stop-events '()))

      ((stop-translation-timestep trans)
       ;; Set bounds of spanners to PaperColumns if they haven't been set.
       ;; This allows spanners to be drawn between spacers.  Other uses?
       ;; Doesn't appear to affect whether spanners can de drawn between
       ;; rests.
        (lambda (s)
          (if (null? (ly:spanner-bound (cdr s) LEFT))
              (ly:spanner-set-bound! (cdr s) LEFT
                (ly:context-property context 'currentMusicalColumn))))

        (lambda (f)
          (if (null? (ly:spanner-bound f RIGHT))
              (ly:spanner-set-bound! f RIGHT
                (ly:context-property context 'currentMusicalColumn))))

       (set! finished '()))
       ((finalize trans)
             ;; If spanner ends on spacer at end of context?
              (lambda (f)
                (if (null? (ly:spanner-bound f RIGHT))
                    (ly:spanner-set-bound! f RIGHT
                      (ly:context-property context 'currentMusicalColumn))))
            (set! finished '())
            ;; User didn't end spanner.
             (lambda (sp)
               (ly:warning "incomplete spanner removed!")
               (ly:grob-suicide! (cdr sp)))
            (set! spanners '())))))

startTextSpanOne =
#(make-music 'TextSpanEvent 'span-direction START 'spanner-id "1")

stopTextSpanOne =
#(make-music 'TextSpanEvent 'span-direction STOP 'spanner-id "1")

startTextSpanTwo =
#(make-music 'TextSpanEvent 'span-direction START 'spanner-id "2")

stopTextSpanTwo =
#(make-music 'TextSpanEvent 'span-direction STOP 'spanner-id "2")

startTextSpanThree =
#(make-music 'TextSpanEvent 'span-direction START 'spanner-id "3")

stopTextSpanThree =
#(make-music 'TextSpanEvent 'span-direction STOP 'spanner-id "3")

% %{
%%%%%%%%%%%%%%%%%%%%%%%%%%%%% EXAMPLE %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\layout {
  \context {
    \remove #"Text_spanner_engraver"
    \consists \alternateTextSpannerEngraver

% David Kastrup on -user 2012-10-22
after =
#(define-music-function (time event music)
  (ly:duration? ly:music? ly:music?)
  "Place @var{event} at a relative duration @var{time} in relation to @var{music}."
  #{ \context Bottom << { \skip $time <>$event } $music >> #})

\score {
    \new Staff <<
      \relative c'' {
        % a1^"Cannot leave this out" |
        \after 4*0
            -\tweak bound-details.left.text "ON"
            -\tweak bound-details.right.text "OFF"
        \after 4*3
            -\tweak bound-details.left.text "NO"
            -\tweak bound-details.right.text "FFO"
            -\tweak direction #DOWN
        % << a1 { s4 <>\=1\stopTextSpan s2 <>\=2\stopTextSpan } >>
        \after 4 \=1\stopTextSpan \after 4*3 \=2\stopTextSpan a1
    \new Staff {
      \repeat unfold 12 c''4
% %}

\relative c' {
  \override TextSpanner.style = ##f
  \override TextSpanner.thickness = 10
  %\override TextSpanner.to-barline = ##t
  -\tweak color #red
  -\tweak color #green
  -\tweak color #blue

  -\tweak color #red
  -\tweak color #green
  -\tweak color #blue

  b c d

  a4 b c d

lilypond-user mailing list

Reply via email to