lilypond-user
[Top][All Lists]
Advanced

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

Re: How to return markup conditionally?


From: Trevor Bača
Subject: Re: How to return markup conditionally?
Date: Thu, 20 Jul 2023 12:26:58 -0400



On Wed, Jul 19, 2023 at 4:32 PM Jean Abou Samra <jean@abou-samra.fr> wrote:
Le mercredi 19 juillet 2023 à 22:11 +0200, Valentin Petzel a écrit :
Good point, I didn’t think of that! Also I know that using a callback on
bound-details would work as well, but I’m not sure if this is the best way to
replace a single value in an alist (what if you want to change other parts of
the alist independently?).


That is indeed a problem, but note that the same problem exists with before-line-breaking (you can't write two independent before-line-breaking overrides, you need to combine them).


Maybe the whole transformer thing should be something that Lilypond supports
in it’s core? Like, any grob property may also have a list of transformers
that get evaluated on getting the property.


Yes, it would be quite nice to be able to stack transformers. Defining good semantics (e.g., interaction with \temporary and \revert) is not really trivial though.

Relatedly, it would be nice at some point to gain support for callbacks in subproperties.

Ah, so Jean's pattern transforms bound-details to a copy of bound-details (with bound-details.left.text changed):

%%% BEGIN %%%

\version "2.25.6"
\paper { indent = 0 tagline = ##f }

\markuplist \column-lines {
    "TextSpanner.bound-details.text.left = \"red\" when spanner color is red;"
    "TextSpanner.bound-details.text.left = \"other\" when spanner color is not red:"
    " "
}

{
  \override TextSpanner.bound-details =
    #(grob-transformer
      'bound-details
      (lambda (grob orig)
        (assoc-set! (alist-copy orig)
                    'left
                    (assoc-set! (alist-copy (assoc-ref orig 'left))
                                'text
                                (if (equal? red (ly:grob-property grob 'color))
                                    "red" "other")))))
  c'1
  - \tweak color #red
  \startTextSpan
  d'1
  e'1
  \stopTextSpan
  f'1
  - \tweak color #blue
  \startTextSpan
  g'1
  a'1
  \stopTextSpan
}

%%% END %%%

But because successive calls to grob-transformer effectively overwrite earlier calls to grob-transformer, we can't use this pattern to (say) override bound-details.left.text and bound-details.left-broken.text at the same time:

%%% BEGIN %%%

\version "2.25.6"

\paper { indent = 0 }

\markup "Results of first grob transformer are lost;"
\markup "second grob transformer overwrites results of first grob transformer:"
\markup \vspace #1

{
  \override TextSpanner.bound-details =
    #(grob-transformer
      'bound-details
      (lambda (grob orig)
        (assoc-set! (alist-copy orig)
                    'left
                    (assoc-set! (alist-copy (assoc-ref orig 'left))
                                'text
                                (if (equal? red (ly:grob-property grob 'color))
                                    "red" "other")))))
  \override TextSpanner.bound-details =
    #(grob-transformer
      'bound-details
      (lambda (grob orig)
        (assoc-set! (alist-copy orig)
                    'left-broken
                    (assoc-set! (alist-copy (assoc-ref orig 'left-broken))
                                'text
                                (if (equal? red (ly:grob-property grob 'color))
                                    "red-broken" "other-broken")))))
  c'1
  - \tweak color #red
  \startTextSpan
  d'1
  \break
  e'1
  \stopTextSpan
  f'1
  - \tweak color #blue
  \startTextSpan
  g'1
  \break
  a'1
  \stopTextSpan
}

%%% END %%%

Can both subproperties be changed in a single call to grob-transformer?

Or is it necessary to use something more complex (perhaps like Valentin proposes here)?

--

Attachment: text-spanner-bound-details-grob-transformer.png
Description: PNG image

Attachment: successive-grob-transformers.png
Description: PNG image


reply via email to

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