]> git.donarmstrong.com Git - lilypond.git/blob - lily/tie-engraver.cc
* lily/include/grob-info.hh: origin_contexts() now does not
[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--2004 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7   
8  */
9
10 #include "event.hh"
11 #include "tie.hh"
12 #include "context.hh"
13
14 #include "spanner.hh"
15 #include "tie-column.hh"
16 #include "engraver.hh"
17 #include "item.hh"
18 #include "grob-pitch-tuple.hh"
19 #include "warn.hh"
20 #include "note-head.hh"
21 #include "staff-symbol-referencer.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    TODO: support sparseTies.
32
33    TODO: melismata will fuck up now:
34
35    < { c8 ~ c8 }
36      { c16 c c c  } >
37
38    melisma is after the 2nd 8th note, but will now be signaled as
39    lasting till the 3rd 16th.
40 */
41 class Tie_engraver : public Engraver
42 {
43   Music *event_;
44   Music *last_event_;
45   Link_array<Grob> now_heads_;
46   Link_array<Grob> heads_to_tie_;
47   Link_array<Grob> ties_;
48   
49   Spanner * tie_column_;
50   
51 protected:
52   virtual void stop_translation_timestep ();
53   virtual void start_translation_timestep ();
54   virtual void acknowledge_grob (Grob_info);
55   virtual bool try_music (Music*);
56   virtual void process_music ();
57   void typeset_tie (Grob*);
58 public:
59   TRANSLATOR_DECLARATIONS(Tie_engraver);
60 };
61
62
63
64 Tie_engraver::Tie_engraver ()
65 {
66   event_ = 0;
67   last_event_  = 0;
68   tie_column_ = 0;
69 }
70
71
72 bool
73 Tie_engraver::try_music (Music *mus)
74 {
75   if (mus->is_mus_type ("tie-event"))
76     {
77       event_ = mus;
78     }
79   
80   return true;
81 }
82
83 void
84 Tie_engraver::process_music ()
85 {
86   if (event_)
87     daddy_context_->set_property ("tieMelismaBusy", SCM_BOOL_T);
88 }
89
90 void
91 Tie_engraver::acknowledge_grob (Grob_info i)
92 {
93   if (Note_head::has_interface (i.grob_))
94     {
95       Grob * h  = i.grob_;
96       now_heads_.push (h);
97       for  (int i = heads_to_tie_.size (); i--;)
98         {
99           Grob *th =  heads_to_tie_[i];
100           Music * right_mus = unsmob_music (h->get_grob_property ("cause"));
101           Music * left_mus = unsmob_music (th->get_grob_property ("cause"));
102
103           /*
104             maybe should check positions too.
105            */
106           if (right_mus && left_mus
107               && gh_equal_p (right_mus->get_mus_property ("pitch"),
108                              left_mus->get_mus_property ("pitch")))
109             {
110               Grob * p = make_spanner ("Tie");
111               Tie::set_interface (p); // cannot remove yet!
112           
113               Tie::set_head (p, LEFT, th);
114               Tie::set_head (p, RIGHT, h);
115           
116               ties_.push (p);
117               announce_grob(p, last_event_->self_scm());
118             }
119         }
120
121       if (ties_.size () && ! tie_column_)
122         {
123           tie_column_ = make_spanner ("TieColumn");
124           announce_grob(tie_column_, SCM_EOL);
125         }
126
127       if (tie_column_)
128         for (int i = ties_.size (); i--;)
129           Tie_column::add_tie (tie_column_,ties_ [i]);
130     }
131 }
132
133 void
134 Tie_engraver::start_translation_timestep ()
135 {
136   daddy_context_->set_property ("tieMelismaBusy",
137                               gh_bool2scm (heads_to_tie_.size ()));
138       
139 }
140
141 void
142 Tie_engraver::stop_translation_timestep ()
143 {
144   if (ties_.size ())
145     {
146       heads_to_tie_.clear ();
147       for (int i=0; i<  ties_.size (); i++)
148         {
149           typeset_tie (ties_[i]);
150         }
151
152       ties_.clear();
153       last_event_ = 0;
154       if (tie_column_)
155         {
156           typeset_grob (tie_column_);
157           tie_column_ =0;
158         }
159     }
160   
161   if (event_)
162     {
163       heads_to_tie_ = now_heads_;
164       last_event_ = event_;
165     }
166   event_ = 0;
167   now_heads_.clear ();
168 }
169
170 void
171 Tie_engraver::typeset_tie (Grob *her)
172 {
173   if (! (Tie::head (her,LEFT) && Tie::head (her,RIGHT)))
174     warning (_ ("lonely tie"));
175
176   Direction d = LEFT;
177   Drul_array<Grob *> new_head_drul;
178   new_head_drul[LEFT] = Tie::head (her,LEFT);
179   new_head_drul[RIGHT] = Tie::head (her,RIGHT);  
180   do {
181     if (!Tie::head (her,d))
182       new_head_drul[d] = Tie::head (her, (Direction)-d);
183   } while (flip (&d) != LEFT);
184
185   index_set_cell (her->get_grob_property ("head-pair"), LEFT, new_head_drul[LEFT]->self_scm ());
186   index_set_cell (her->get_grob_property ("head-pair"), RIGHT, new_head_drul[RIGHT]->self_scm ());
187
188   typeset_grob (her);
189 }
190
191
192 ENTER_DESCRIPTION(Tie_engraver,
193 /* descr */       "Generate ties between noteheads of equal pitch.",
194 /* creats*/       "Tie TieColumn",
195 /* accepts */     "tie-event",
196 /* acks  */      "rhythmic-head-interface",
197 /* reads */       "tieMelismaBusy",
198 /* write */       "");