]> git.donarmstrong.com Git - lilypond.git/blob - lily/tie-engraver.cc
release commit
[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--2003 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7   
8  */
9
10 #include "event.hh"
11 #include "tie.hh"
12 #include "translator-group.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    TODO: support sparseTies.
31
32    TODO: melismata will fuck up now:
33
34    < { c8 ~ c8 }
35      { c16 c c c  } >
36
37    melisma is after the 2nd 8th note, but will now be signaled as
38    lasting till the 3rd 16th.
39 */
40 class Tie_engraver : public Engraver
41 {
42   Music *event_;
43   Music *last_event_;
44   Link_array<Grob> now_heads_;
45   Link_array<Grob> heads_to_tie_;
46   Link_array<Grob> ties_;
47   
48   Spanner * tie_column_;
49   
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_acknowledged_grobs ();
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::acknowledge_grob (Grob_info i)
85 {
86   if (Note_head::has_interface (i.grob_))
87     {
88       Grob * h  = i.grob_;
89       now_heads_.push (h);
90       for  (int i = heads_to_tie_.size (); i--;)
91         {
92           Grob *th =  heads_to_tie_[i];
93           Music * right_mus = unsmob_music (h->get_grob_property ("cause"));
94           Music * left_mus = unsmob_music (th->get_grob_property ("cause"));
95
96           /*
97             maybe should check positions too.
98            */
99           if (right_mus && left_mus
100               && gh_equal_p (right_mus->get_mus_property ("pitch"),
101                              left_mus->get_mus_property ("pitch")))
102             {
103               Grob * p = new Spanner (get_property ("Tie"));
104               Tie::set_interface (p); // cannot remove yet!
105           
106               Tie::set_head (p, LEFT, th);
107               Tie::set_head (p, RIGHT, h);
108           
109               ties_.push (p);
110               announce_grob(p, last_event_->self_scm());
111             }
112         }
113     }
114 }
115
116 void
117 Tie_engraver::process_acknowledged_grobs ()
118 {
119   if (ties_.size () > 1 && !tie_column_)
120     {
121       tie_column_ = new Spanner (get_property ("TieColumn"));
122       
123       for (int i = ties_.size (); i--;)
124         Tie_column::add_tie (tie_column_,ties_ [i]);
125
126       announce_grob(tie_column_, SCM_EOL);
127     }
128 }
129
130 void
131 Tie_engraver::start_translation_timestep ()
132 {
133   if (to_boolean (get_property ("automaticMelismata")))
134       daddy_trans_->set_property ("tieMelismaBusy",
135                                   gh_bool2scm (heads_to_tie_.size ()));
136       
137 }
138
139 void
140 Tie_engraver::stop_translation_timestep ()
141 {
142   if (ties_.size ())
143     {
144       heads_to_tie_.clear ();
145       for (int i=0; i<  ties_.size (); i++)
146         {
147           typeset_tie (ties_[i]);
148         }
149
150       ties_.clear();
151       last_event_ = 0;
152       if (tie_column_)
153         {
154           typeset_grob (tie_column_);
155           tie_column_ =0;
156         }
157     }
158   
159   if (event_)
160     {
161       heads_to_tie_ = now_heads_;
162       last_event_ = event_;
163     }
164   event_ = 0;
165   now_heads_.clear ();
166 }
167
168 void
169 Tie_engraver::typeset_tie (Grob *her)
170 {
171   if (! (Tie::head (her,LEFT) && Tie::head (her,RIGHT)))
172     warning (_ ("lonely tie"));
173
174   Direction d = LEFT;
175   Drul_array<Grob *> new_head_drul;
176   new_head_drul[LEFT] = Tie::head (her,LEFT);
177   new_head_drul[RIGHT] = Tie::head (her,RIGHT);  
178   do {
179     if (!Tie::head (her,d))
180       new_head_drul[d] = Tie::head (her, (Direction)-d);
181   } while (flip (&d) != LEFT);
182
183   index_set_cell (her->get_grob_property ("heads"), LEFT, new_head_drul[LEFT]->self_scm ());
184   index_set_cell (her->get_grob_property ("heads"), RIGHT, new_head_drul[RIGHT]->self_scm ());
185
186   typeset_grob (her);
187 }
188
189
190 ENTER_DESCRIPTION(Tie_engraver,
191 /* descr */       "Generate ties between noteheads of equal pitch.",
192 /* creats*/       "Tie TieColumn",
193 /* accepts */     "tie-event",
194 /* acks  */      "rhythmic-head-interface",
195 /* reads */       "tieMelismaBusy",
196 /* write */       "");