Skip to content

Instantly share code, notes, and snippets.

@lucaswerkmeister
Last active March 15, 2018 17:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lucaswerkmeister/5ae87eaa3695b8a39f5d1e05bc9b077c to your computer and use it in GitHub Desktop.
Save lucaswerkmeister/5ae87eaa3695b8a39f5d1e05bc9b077c to your computer and use it in GitHub Desktop.
Workaround for \displaceHeads on notes with beams

The \displaceHeads command from LilyPond snippet 861 doesn’t work correctly with notes that have beams – it removes the beam, probably due to a LilyPond bug (see here for details). This version can be used instead – it’s not as correct (the width of the beam is hard-coded), but it seems to work. (Only use it if you’re actually affected by this problem, of course, otherwise stick with the original!)

#(define ((shift offsets) grob)
"Defines how NoteHeads should be moved according to the given list of offsets."
(let* (
;; NoteHeads
;; Get the NoteHeads of the NoteColumn
(note-heads (ly:grob-array->list (ly:grob-object grob 'note-heads)))
;; Get their durations
(nh-duration-log
(map
(lambda (note-head-grobs)
(ly:grob-property note-head-grobs 'duration-log))
note-heads))
;; Get the stencils of the NoteHeads
(nh-stencils
(map
(lambda (note-head-grobs)
(ly:grob-property note-head-grobs 'stencil))
note-heads))
;; Get their length in X-axis-direction
(stencils-x-lengths
(map
(lambda (x)
(let* ((stencil (ly:grob-property x 'stencil))
(stencil-X-exts (ly:stencil-extent stencil X))
(stencil-lengths (interval-length stencil-X-exts)))
stencil-lengths))
note-heads))
)
;; Final Calculation for moving the NoteHeads
(for-each
(lambda (nh nh-x-length off)
(if (= off 0)
#f
(ly:grob-translate-axis! nh (* off (- nh-x-length 0.065)) X)))
note-heads stencils-x-lengths offsets)))
displaceHeads =
#(define-music-function (parser location offsets) (list?)
"
Moves the NoteHeads, using (shift offsets)
"
#{
\once \override NoteColumn.before-line-breaking = #(shift offsets)
#})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment