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