Use ly:staff-symbol-staff-space.
[lilypond/mpolesky.git] / lily / tie-engraver.cc
blob1a397b3b7479d675dca9e4fe301f92a7503e5a2e
1 /*
2 tie-engraver.cc -- implement Tie_engraver
4 source file of the GNU LilyPond music typesetter
6 (c) 1998--2009 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
9 #include "engraver.hh"
11 #include "context.hh"
12 #include "international.hh"
13 #include "item.hh"
14 #include "note-head.hh"
15 #include "protected-scm.hh"
16 #include "spanner.hh"
17 #include "staff-symbol-referencer.hh"
18 #include "stream-event.hh"
19 #include "tie-column.hh"
20 #include "tie.hh"
21 #include "warn.hh"
23 #include "translator.icc"
25 /**
26 Manufacture ties. Acknowledge noteheads, and put them into a
27 priority queue. If we have a TieEvent, connect the notes that finish
28 just at this time, and note that start at this time.
30 TODO: Remove the dependency on musical info. We should tie on the
31 basis of position and duration-log of the heads (not of the events).
34 struct Head_event_tuple
36 Grob *head_;
37 Moment end_moment_;
38 SCM tie_definition_;
39 Stream_event *tie_stream_event_;
40 Stream_event *tie_event_;
42 Head_event_tuple ()
44 head_ = 0;
45 tie_definition_ = SCM_EOL;
46 tie_event_ = 0;
47 tie_stream_event_ = 0;
51 class Tie_engraver : public Engraver
53 Stream_event *event_;
54 vector<Grob*> now_heads_;
55 vector<Head_event_tuple> heads_to_tie_;
56 vector<Grob*> ties_;
58 Spanner *tie_column_;
60 protected:
61 void stop_translation_timestep ();
62 virtual void derived_mark () const;
63 void start_translation_timestep ();
64 DECLARE_ACKNOWLEDGER (note_head);
65 DECLARE_TRANSLATOR_LISTENER (tie);
66 void process_music ();
67 void typeset_tie (Grob *);
68 public:
69 TRANSLATOR_DECLARATIONS (Tie_engraver);
72 void
73 Tie_engraver::derived_mark () const
75 Engraver::derived_mark ();
76 for (vsize i = 0; i < heads_to_tie_.size (); i++)
77 scm_gc_mark (heads_to_tie_[i].tie_definition_);
80 Tie_engraver::Tie_engraver ()
82 event_ = 0;
83 tie_column_ = 0;
86 IMPLEMENT_TRANSLATOR_LISTENER (Tie_engraver, tie);
87 void
88 Tie_engraver::listen_tie (Stream_event *ev)
90 ASSIGN_EVENT_ONCE (event_, ev);
93 void
94 Tie_engraver::process_music ()
96 bool busy = event_;
97 for (vsize i = 0; !busy && i < heads_to_tie_.size (); i++)
98 busy |= (heads_to_tie_[i].tie_event_
99 || heads_to_tie_[i].tie_stream_event_);
101 if (busy)
102 context ()->set_property ("tieMelismaBusy", SCM_BOOL_T);
105 void
106 Tie_engraver::acknowledge_note_head (Grob_info i)
108 Grob *h = i.grob ();
110 now_heads_.push_back (h);
111 for (vsize i = heads_to_tie_.size (); i--;)
113 Grob *th = heads_to_tie_[i].head_;
114 Stream_event *right_ev = unsmob_stream_event (h->get_property ("cause"));
115 Stream_event *left_ev = unsmob_stream_event (th->get_property ("cause"));
118 maybe should check positions too.
120 if (!right_ev || !left_ev)
121 continue;
123 if (ly_is_equal (right_ev->get_property ("pitch"),
124 left_ev->get_property ("pitch")))
126 Grob *p = new Spanner (heads_to_tie_[i].tie_definition_);
128 SCM cause = heads_to_tie_[i].tie_event_
129 ? heads_to_tie_[i].tie_event_->self_scm ()
130 : heads_to_tie_[i].tie_stream_event_->self_scm ();
132 announce_grob (p, cause);
133 Tie::set_head (p, LEFT, th);
134 Tie::set_head (p, RIGHT, h);
137 if (is_direction (unsmob_stream_event (cause)->get_property ("direction")))
139 Direction d = to_dir (unsmob_stream_event (cause)->get_property ("direction"));
140 p->set_property ("direction", scm_from_int (d));
143 ties_.push_back (p);
144 heads_to_tie_.erase (heads_to_tie_.begin () + i);
148 if (ties_.size () && ! tie_column_)
149 tie_column_ = make_spanner ("TieColumn", ties_[0]->self_scm ());
151 if (tie_column_)
152 for (vsize i = ties_.size (); i--;)
153 Tie_column::add_tie (tie_column_, ties_[i]);
156 void
157 Tie_engraver::start_translation_timestep ()
159 context ()->set_property ("tieMelismaBusy",
160 ly_bool2scm (heads_to_tie_.size ()));
162 if (heads_to_tie_.size () && !to_boolean (get_property ("tieWaitForNote")))
164 Moment now = now_mom ();
165 for (vsize i = heads_to_tie_.size (); i--; )
167 if (now > heads_to_tie_[i].end_moment_)
168 heads_to_tie_.erase (heads_to_tie_.begin () + i);
173 void
174 Tie_engraver::stop_translation_timestep ()
176 bool wait = to_boolean (get_property ("tieWaitForNote"));
177 if (ties_.size ())
179 if (!wait)
180 heads_to_tie_.clear ();
182 for (vsize i = 0; i < ties_.size (); i++)
183 typeset_tie (ties_[i]);
185 ties_.clear ();
186 tie_column_ = 0;
189 vector<Head_event_tuple> new_heads_to_tie;
191 for (vsize i = 0; i < now_heads_.size (); i++)
193 Grob *head = now_heads_[i];
194 Stream_event *left_ev
195 = unsmob_stream_event (head->get_property ("cause"));
197 if (!left_ev)
199 // may happen for ambituses
200 continue;
204 SCM left_articulations = left_ev->get_property ("articulations");
206 Stream_event *tie_event = 0;
207 Stream_event *tie_stream_event = event_;
208 for (SCM s = left_articulations;
209 !tie_event && !tie_stream_event && scm_is_pair (s);
210 s = scm_cdr (s))
212 Stream_event *ev = unsmob_stream_event (scm_car (s));
213 if (!ev)
214 continue;
216 if (ev->in_event_class ("tie-event"))
217 tie_event = ev;
220 if (left_ev && (tie_event || tie_stream_event))
222 Head_event_tuple event_tup;
224 SCM start_definition
225 = updated_grob_properties (context (), ly_symbol2scm ("Tie"));
227 event_tup.head_ = head;
228 event_tup.tie_definition_ = start_definition;
229 event_tup.tie_event_ = tie_event;
230 event_tup.tie_stream_event_ = tie_stream_event;
232 Moment end = now_mom ();
233 if (end.grace_part_)
235 end.grace_part_ += get_event_length (left_ev).main_part_;
237 else
239 end += get_event_length (left_ev);
241 event_tup.end_moment_ = end;
243 new_heads_to_tie.push_back (event_tup);
247 if (!wait && new_heads_to_tie.size ())
248 heads_to_tie_.clear ();
250 // hmmm, how to do with copy () ?
251 for (vsize i = 0; i < new_heads_to_tie.size (); i++)
252 heads_to_tie_.push_back (new_heads_to_tie[i]);
254 event_ = 0;
255 now_heads_.clear ();
258 void
259 Tie_engraver::typeset_tie (Grob *her)
261 if (! (Tie::head (her, LEFT) && Tie::head (her, RIGHT)))
262 warning (_ ("lonely tie"));
264 Direction d = LEFT;
265 Drul_array<Grob *> new_head_drul;
266 new_head_drul[LEFT] = Tie::head (her, LEFT);
267 new_head_drul[RIGHT] = Tie::head (her, RIGHT);
270 if (!Tie::head (her, d))
271 new_head_drul[d] = Tie::head (her, (Direction) - d);
273 while (flip (&d) != LEFT);
275 Spanner *sp = dynamic_cast<Spanner*> (her);
276 sp->set_bound (LEFT, new_head_drul[LEFT]);
277 sp->set_bound (RIGHT, new_head_drul[RIGHT]);
280 ADD_ACKNOWLEDGER (Tie_engraver, note_head);
281 ADD_TRANSLATOR (Tie_engraver,
282 /* doc */
283 "Generate ties between note heads of equal pitch.",
285 /* create */
286 "Tie "
287 "TieColumn ",
289 /* read */
290 "tieWaitForNote ",
292 /* write */
293 "tieMelismaBusy "