]> git.donarmstrong.com Git - lilypond.git/blob - lily/ligature-engraver.cc
* The grand 2005-2006 replace.
[lilypond.git] / lily / ligature-engraver.cc
1 /*
2   ligature-engraver.cc -- implement Ligature_engraver
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 2002--2006 Juergen Reuter <reuter@ipd.uka.de>
7 */
8
9 #include "ligature-engraver.hh"
10
11 #include "spanner.hh"
12 #include "score-engraver.hh"
13 #include "note-head.hh"
14 #include "rest.hh"
15 #include "warn.hh"
16 #include "context.hh"
17
18 #include "translator.icc"
19
20 /*
21  * This abstract class provides the general framework for ligatures of
22  * any kind.  It cares for handling start/stop ligatures events and
23  * collecting all noteheads inbetween, but delegates creation of a
24  * ligature spanner for each start/stop pair and typesetting of the
25  * ligature spanner to a concrete subclass.
26  *
27  * A concrete ligature engraver must subclass this class and provide
28  * functions create_ligature_spanner () and typeset_ligature
29  * (Spanner *, Array<Grob_info>).  Subclasses of this class basically
30  * fall into two categories.
31  *
32  * The first category consists of engravers that engrave ligatures in
33  * a way that really deserves the name ligature.  That is, they
34  * produce a single connected graphical object of fixed width,
35  * consisting of noteheads and other primitives.  Space may be
36  * inserted only after each ligature, if necessary, but in no case
37  * between the primitives of the ligature.  Accidentals have to be put
38  * to the left of the ligature, and not to the left of individual
39  * noteheads.  Class Coherent_ligature_engraver is the common
40  * superclass for all of these engravers.
41  *
42  * The second category is for engravers that are relaxed in the sense
43  * that they do not require to produce a single connected graphical
44  * object.  For example, in contemporary editions, ligatures are often
45  * marked, but otherwise use contemporary notation and spacing.  In
46  * this category, there is currently only a single class,
47  * Ligature_bracket_engraver, which marks each ligature with a
48  * horizontal sqare bracket, but otherwise leaves the appearance
49  * untouched.
50  */
51
52 /*
53  * TODO: lyrics/melisma/syllables: there should be at most one
54  * syllable of lyrics per ligature (i.e. for the lyrics context, a
55  * ligature should count as a single note, regardless of how many
56  * heads the ligature consists of).
57  *
58  * TODO: currently, you have to add/remove the proper
59  * Ligature_engraver (Ligature_bracket_engraver,
60  * Mensural_ligature_engraver) to the proper translator
61  * (e.g. VoiceContext) to choose between various representations.
62  * Since adding/removing an engraver to a translator is a global
63  * action in the layout block, you cannot mix various representations
64  * _within_ the same score.  Hence, for selecting a representation,
65  * one would rather like to have a property that can be set e.g. for
66  * several staves individually.  However, it seems that this approach
67  * would require to have a single, complicated Ligature_engraver that
68  * consists of all the code...  This needs further thoughts.
69  */
70 Ligature_engraver::Ligature_engraver ()
71 {
72   ligature_ = 0;
73   finished_ligature_ = 0;
74   events_drul_[LEFT] = events_drul_[RIGHT] = 0;
75   prev_start_event_ = 0;
76   last_bound_ = 0;
77   brew_ligature_primitive_proc = SCM_EOL;
78 }
79
80 bool
81 Ligature_engraver::try_music (Music *m)
82 {
83   if (m->is_mus_type ("ligature-event"))
84     {
85       Direction d = to_dir (m->get_property ("span-direction"));
86       events_drul_[d] = m;
87       return true;
88     }
89   return false;
90 }
91
92 void
93 Ligature_engraver::process_music ()
94 {
95   if (events_drul_[STOP])
96     {
97       if (!ligature_)
98         {
99           events_drul_[STOP]->origin ()->warning (_ ("can't find start of ligature"));
100           return;
101         }
102
103       if (!last_bound_)
104         events_drul_[STOP]->origin ()->warning (_ ("no right bound"));
105       else
106         ligature_->set_bound (RIGHT, last_bound_);
107
108       prev_start_event_ = 0;
109       finished_primitives_ = primitives_;
110       finished_ligature_ = ligature_;
111       primitives_.clear ();
112       ligature_ = 0;
113     }
114   last_bound_ = unsmob_grob (get_property ("currentMusicalColumn"));
115
116   if (ligature_)
117     {
118       // TODO: maybe forbid breaks only if not transcribing
119       get_score_engraver ()->forbid_breaks ();
120     }
121
122   if (events_drul_[START])
123     {
124       if (ligature_)
125         {
126           events_drul_[START]->origin ()->warning (_ ("already have a ligature"));
127           return;
128         }
129
130       prev_start_event_ = events_drul_[START];
131       ligature_ = create_ligature_spanner ();
132
133       Grob *bound = unsmob_grob (get_property ("currentMusicalColumn"));
134       if (!bound)
135         events_drul_[START]->origin ()->warning (_ ("no left bound"));
136       else
137         ligature_->set_bound (LEFT, bound);
138
139       ligature_start_mom_ = now_mom ();
140
141       // TODO: dump cause into make_item/spanner. 
142       // announce_grob (ligature_, events_drul_[START]->self_scm ());
143     }
144 }
145
146 void
147 Ligature_engraver::stop_translation_timestep ()
148 {
149   if (finished_ligature_)
150     {
151       if (!finished_primitives_.size ())
152         {
153           finished_ligature_->programming_error ("Ligature_engraver::stop_translation_timestep (): "
154                                                  "junking empty ligature");
155         }
156       else
157         {
158           typeset_ligature (finished_ligature_, finished_primitives_);
159           finished_primitives_.clear ();
160         }
161       finished_ligature_ = 0;
162     }
163
164   events_drul_[START] = 0;
165   events_drul_[STOP] = 0;
166 }
167
168 void
169 Ligature_engraver::finalize ()
170 {
171   if (finished_ligature_)
172     {
173       typeset_ligature (finished_ligature_, finished_primitives_);
174       finished_primitives_.clear ();
175       finished_ligature_ = 0;
176     }
177   if (ligature_)
178     {
179       prev_start_event_->origin ()->warning (_ ("unterminated ligature"));
180       ligature_->suicide ();
181     }
182 }
183
184 Spanner *
185 Ligature_engraver::current_ligature ()
186 {
187   return ligature_;
188 }
189
190 void
191 Ligature_engraver::acknowledge_note_head (Grob_info info)
192 {
193   if (ligature_)
194     {
195       primitives_.push (info);
196       if (info.grob () && (brew_ligature_primitive_proc != SCM_EOL))
197         {
198           info.grob ()->set_property ("stencil", brew_ligature_primitive_proc);
199         }
200     }
201 }
202
203 void
204 Ligature_engraver::acknowledge_rest (Grob_info info)
205 {
206   if (ligature_)
207     {
208       info.music_cause ()->origin ()->warning (_ ("ignoring rest: ligature may not contain rest"));
209       prev_start_event_->origin ()->warning (_ ("ligature was started here"));
210       // TODO: maybe better should stop ligature here rather than
211       // ignoring the rest?
212     }
213 }
214
215 // no ADD_ACKNOWLEDGER / ADD_ACKNOWLEDGER / ADD_TRANSLATOR macro calls
216 // since this class is abstract