]> git.donarmstrong.com Git - lilypond.git/blob - lily/tie-engraver.cc
* input/regression/quote-tie.ly: new file.
[lilypond.git] / lily / tie-engraver.cc
1 /*
2   tie-engraver.cc -- implement Tie_engraver
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1998--2006 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
8
9 #include "engraver.hh"
10
11 #include "context.hh"
12 #include "grob-pitch-tuple.hh"
13 #include "international.hh"
14 #include "item.hh"
15 #include "note-head.hh"
16 #include "protected-scm.hh"
17 #include "spanner.hh"
18 #include "staff-symbol-referencer.hh"
19 #include "tie-column.hh"
20 #include "tie.hh"
21 #include "warn.hh"
22
23 /**
24    Manufacture ties.  Acknowledge noteheads, and put them into a
25    priority queue. If we have a TieEvent, connect the notes that finish
26    just at this time, and note that start at this time.
27
28    TODO: Remove the dependency on musical info. We should tie on the
29    basis of position and duration-log of the heads (not of the events).
30 */
31
32 struct Head_event_tuple
33 {
34   Grob *head_;
35   Moment end_moment_;
36   SCM tie_definition_;
37   Music *event_;
38   
39   Head_event_tuple ()
40   {
41     event_ = 0;
42     head_ = 0;
43     tie_definition_ = SCM_EOL;
44   }
45 };
46
47 class Tie_engraver : public Engraver
48 {
49   Music *event_;
50   vector<Grob*> now_heads_;
51   vector<Head_event_tuple> heads_to_tie_;
52   vector<Grob*> ties_;
53
54   Spanner *tie_column_;
55
56 protected:
57   void stop_translation_timestep ();
58   virtual void derived_mark () const;
59   void start_translation_timestep ();
60   DECLARE_ACKNOWLEDGER (note_head);
61   virtual bool try_music (Music *);
62   void process_music ();
63   void typeset_tie (Grob *);
64 public:
65   TRANSLATOR_DECLARATIONS (Tie_engraver);
66 };
67
68 void
69 Tie_engraver::derived_mark () const
70 {
71   Engraver::derived_mark ();
72   for (vsize i = 0; i < heads_to_tie_.size (); i++)
73     scm_gc_mark (heads_to_tie_[i].tie_definition_);
74 }
75
76 Tie_engraver::Tie_engraver ()
77 {
78   event_ = 0;
79   tie_column_ = 0;
80 }
81
82 bool
83 Tie_engraver::try_music (Music *mus)
84 {
85   if (mus->is_mus_type ("tie-event"))
86     event_ = mus;
87
88   return true;
89 }
90
91 void
92 Tie_engraver::process_music ()
93 {
94   if (event_)
95     context ()->set_property ("tieMelismaBusy", SCM_BOOL_T);
96 }
97
98 void
99 Tie_engraver::acknowledge_note_head (Grob_info i)
100 {
101   Grob *h = i.grob ();
102   now_heads_.push_back (h);
103   for (vsize i = heads_to_tie_.size (); i--;)
104     {
105       Grob *th = heads_to_tie_[i].head_;
106       Music *right_mus = unsmob_music (h->get_property ("cause"));
107       Music *left_mus = unsmob_music (th->get_property ("cause"));
108
109       /*
110         maybe should check positions too.
111       */
112       if (right_mus && left_mus
113           && ly_is_equal (right_mus->get_property ("pitch"),
114                           left_mus->get_property ("pitch")))
115         {
116           Grob *p = new Spanner (heads_to_tie_[i].tie_definition_,
117                                  context ()->get_grob_key ("Tie"));
118           announce_grob (p, heads_to_tie_[i].event_->self_scm ());
119           Tie::set_head (p, LEFT, th);
120           Tie::set_head (p, RIGHT, h);
121
122           ties_.push_back (p);
123           heads_to_tie_.erase (heads_to_tie_.begin () + i);
124         }
125     }
126
127   if (ties_.size () && ! tie_column_)
128     tie_column_ = make_spanner ("TieColumn", ties_[0]->self_scm ());
129
130   if (tie_column_)
131     for (vsize i = ties_.size (); i--;)
132       Tie_column::add_tie (tie_column_, ties_[i]);
133 }
134
135 void
136 Tie_engraver::start_translation_timestep ()
137 {
138   context ()->set_property ("tieMelismaBusy",
139                             ly_bool2scm (heads_to_tie_.size ()));
140   
141   
142   if (!to_boolean (get_property ("tieWaitForNote")))
143     {
144       Moment now = now_mom ();
145       for (vsize i = heads_to_tie_.size ();  i--; )
146         {
147           if (now > heads_to_tie_[i].end_moment_)
148             heads_to_tie_.erase (heads_to_tie_.begin () + i);
149         }
150     }
151 }
152
153 void
154 Tie_engraver::stop_translation_timestep ()
155 {
156   if (ties_.size ())
157     {
158       if (!to_boolean (get_property ("tieWaitForNote")))
159         heads_to_tie_.clear ();
160
161       for (vsize i = 0; i < ties_.size (); i++)
162         typeset_tie (ties_[i]);
163
164       ties_.clear ();
165       tie_column_ = 0;
166     }
167
168   if (event_)
169     {
170       SCM start_definition
171         = updated_grob_properties (context (), ly_symbol2scm ("Tie"));
172
173       if (!to_boolean (get_property ("tieWaitForNote")))
174         heads_to_tie_.clear ();
175
176       for (vsize i = 0; i < now_heads_.size (); i++)
177         {
178           Grob *head = now_heads_[i];
179           Music *left_mus = unsmob_music (head->get_property ("cause"));
180           if (left_mus)
181             {
182               Head_event_tuple event_tup;
183
184               event_tup.head_ = head;
185               event_tup.tie_definition_ = start_definition;
186               event_tup.event_ = event_;
187
188               Moment end = now_mom ();
189               if (end.grace_part_)
190                 {
191                   end.grace_part_ += left_mus->get_length ().main_part_;
192                 }
193               else
194                 {
195                   end += left_mus->get_length (); 
196                 }
197               event_tup.end_moment_ = end;
198
199               heads_to_tie_.push_back (event_tup);
200             }
201         }
202     }
203
204   event_ = 0;
205   now_heads_.clear ();
206 }
207
208 void
209 Tie_engraver::typeset_tie (Grob *her)
210 {
211   if (! (Tie::head (her, LEFT) && Tie::head (her, RIGHT)))
212     warning (_ ("lonely tie"));
213
214   Direction d = LEFT;
215   Drul_array<Grob *> new_head_drul;
216   new_head_drul[LEFT] = Tie::head (her, LEFT);
217   new_head_drul[RIGHT] = Tie::head (her, RIGHT);
218   do
219     {
220       if (!Tie::head (her, d))
221         new_head_drul[d] = Tie::head (her, (Direction) - d);
222     }
223   while (flip (&d) != LEFT);
224
225   Spanner *sp = dynamic_cast<Spanner*> (her);
226   sp->set_bound (LEFT, new_head_drul[LEFT]);
227   sp->set_bound (RIGHT, new_head_drul[RIGHT]);
228 }
229
230 #include "translator.icc"
231
232 ADD_ACKNOWLEDGER (Tie_engraver, note_head);
233 ADD_TRANSLATOR (Tie_engraver,
234                 /* doc */ "Generate ties between noteheads of equal pitch.",
235                 /* create */
236                 "Tie "
237                 "TieColumn",
238
239                 /* accept */ "tie-event",
240                 /* read */ "tieWaitForNote",
241                 /* write */ "tieMelismaBusy");