Showing posts with label context definition. Show all posts
Showing posts with label context definition. Show all posts

Thursday, May 12, 2011

Accidentally creating a new context

Consider the following layout block:

\layout {
\context {
\type "Engraver_group"
\consists "Time_signature_engraver"
\consists "Axis_group_engraver"
\name "TimeSig"
\override TimeSignature #'font-size = #3
\override TimeSignature #'break-align-symbol = ##f
\override TimeSignature #'X-offset =
#ly:self-alignment-interface::x-aligned-on-self
\override TimeSignature #'self-alignment-X = #CENTER
\override TimeSignature #'after-line-breaking =
#shift-right-at-line-begin
}
\context {
\Score
\accepts TimeSig
}
\context {
\Staff
\remove "Time_signature_engraver"
}
}

The layout comes from the LilyPond snippet repository and allows for time signatures above staves:

\new Score <<
\new TimeSig {
\time 2/4 s2
\time 3/4 s2.
}
\new StaffGroup <<
\new Staff { c'4 c'4 c'4 c'4 c'4 }
\new Staff { c'4 c'4 c'4 c'4 c'4 }
>>
>>


But this can get messed up:

\new Score <<
\new TimeSig {
#(set-accidental-style 'forget)
\time 2/4 s2
\time 3/4 s2.
}
\new StaffGroup <<
\new Staff { c'4 c'4 c'4 c'4 c'4 }
\new Staff { c'4 c'4 c'4 c'4 c'4 }
>>
>>


The accidental style setting causes an unwanted new staff to be instantiated.

Make sure to keep Scheme commands out of nonsemantic contexts to avoid this behavior.

Monday, May 12, 2008

Making LilyPond GrandStaff accept RhythmicStaff

GrandStaff does not by default accept RhythmicStaves. Context modification fixes this.



\new GrandStaff <<
\new RhythmicStaff { c'4 }
\new RhythmicStaff { c'4 }
\new RhythmicStaff { c'4 }
\new RhythmicStaff { c'4 }
>>

\layout { \context { \GrandStaff \accepts RhythmicStaff } }

Thanks to Rune.

Tuesday, May 6, 2008

Smart LilyPond context definition

LilyPond makes it easy to define new contexts with special attributes.

\layout {
\context {
\Staff
\type Engraver_group
\name TwoLineStaff
\alias Staff
\override StaffSymbol #'line-positions = #'(-2 2)
}
\context {
\Score
\accepts TwoLineStaff
}
}

This layout block defines a special two-line staff.

\new TwoLineStaff { c'4 d'4 e'4 f'4 }




Red and blue versions of our special TwoLineStaff are available like this.

\layout {
\context {
\Staff
\type Engraver_group
\name TwoLineStaff
\alias Staff
\override StaffSymbol #'line-positions = #'(-2 2)
}
\context {
\TwoLineStaff
\type Engraver_group
\name RedTwoLineStaff
\alias TwoLineStaff
\override NoteHead #'color = #red
\override Stem #'color = #red
\override Dots #'color = #red
}
\context {
\TwoLineStaff
\type Engraver_group
\name BlueTwoLineStaff
\alias TwoLineStaff
\override NoteHead #'color = #blue
\override Stem #'color = #blue
\override Dots #'color = #blue
}
\context {
\Score
\accepts RedTwoLineStaff
\accepts BlueTwoLineStaff
}
}

RedTwoLineStaff and BlueTwoLineStaff here extend TwoLineStaff in the manner of concrete classes extending an abstract base class.

\new Score <<
\new RedTwoLineStaff { c'4 d'4 e'4 f'4 }
\new BlueTwoLineStaff { c'4 d'4 e'4 f'4 }
>>




These definitions of RedTwoLineStaff and BlueTwoLineStaff avoid code duplication and let us have both types of staff in a single score. In a later post we'll look at a different way to define red and blue two-line staves for different editions of the same score.