[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Adding text to slurs
From: |
Urs Liska |
Subject: |
Re: Adding text to slurs |
Date: |
Mon, 5 Nov 2018 11:05:46 +0100 |
User-agent: |
Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.9.1 |
Hi Pablo,
Am 04.11.2018 um 09:36 schrieb Pablo Cordal:
Hi to everyone from Spain,
I have got a special need with lilypond. I need to add text to slurs
for educational purposes, I'm a music teacher and I'd like to add
information to musical phrases.
I have recently written a function (with extensive help from this list)
that should be exactly what you need. It places a markup centered above
or below (depending on the direction) of a slur:
\version "2.19.82"
% "Annotate" a slur with a horizontally centered markup.
% The vertical position of the markup is determined both by
% (uses the greater of) the mozart.markup-staff-padding option and
% the min-padding argument.
%
% NOTE: As this function is an after-line-breaking callback it can't
% provide any collision handling for the added markup. This may result
% in collisions with other score elements, or it can cause the added
% markup to be cut off at the paper border. For the latter case the
% function \forceTopCropping from the paper-top-padding tool can be used.
annotatedSlur =
#(define-event-function (min-padding text)((number? 1) markup?)
"Create a slur with an attached markup."
#{
-\tweak after-line-breaking
#(lambda (grob)
(let*
((dir (ly:grob-property grob 'direction))
;; staff-padding from the options
(staff-padding (getOption '(mozart markup-staff-padding)))
;; The original slur stencil
(stencil (ly:slur::print grob))
;; A new stencil for the markup
(markup-stencil (grob-interpret-markup grob text))
;; Y-extent of the original slur for later reference
(slur-extent (ly:grob-property grob 'Y-extent))
;; total height of slur
(slur-height (- (cdr slur-extent) (car slur-extent)))
;; absolute distance of outer slur edge from middle staffline
(outer-slur-edge-abs
(abs (if (> dir 0) (cdr slur-extent) (car slur-extent))))
;; Y-extent of the added markup stencil
(markup-extent (ly:stencil-extent markup-stencil Y))
;; total height of markup
(markup-height (- (cdr markup-extent) (car markup-extent)))
;; height of lower extender or X height, depending on direction
(markup-inner-height
(if (> dir 0)
(* -1 (car markup-extent))
(cdr markup-extent)))
;; offset because staff-padding *below* staff is increased by 1
(direction-offset (if (> dir 0) 0 -1))
;; padding between slur and markup, based on both the min-padding
;; argument and the global mozart.markup-staff-padding option
(effective-padding
(max
(- (+ staff-padding 2)
markup-inner-height
direction-offset
outer-slur-edge-abs)
min-padding))
;; necessary horizontal shift to center the markup against the slur
(hshift (- (interval-center (ly:stencil-extent stencil X))
(interval-center (ly:stencil-extent markup-stencil X))))
;; combined stencil from slur and text
(new-stencil
(ly:stencil-combine-at-edge stencil Y dir
(ly:stencil-translate-axis markup-stencil hshift X)
effective-padding)))
;; enclose the *new* stencil by the modified Y-extent
;; NOTE: this only makes the whole thing *clickable*, it doesn't
;; help with pushing the staff towards the bottom of the page.
(ly:grob-set-property! grob 'Y-extent
(if (> dir 0)
(cons
(car slur-extent)
(+ (car slur-extent) slur-height effective-padding
markup-height))
(cons
(- (car slur-extent) effective-padding markup-height)
(cdr slur-extent))))
(ly:grob-set-property! grob 'stencil new-stencil)))
% NOTE: this is a slur-event, not a misplaced parenthesis
(
#})
You would use it like
{
c'1 ^\annotatedSlur "My text" c' ) c' _\annotatedSlur 4 "My padded text" c' )
}
HTH
Urs