]> git.donarmstrong.com Git - lilypond.git/blob - lily/tie-engraver.cc
a73e90bf50d99d5a27fa61b1585377a0c8e2cf31
[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 protected:
51   virtual void stop_translation_timestep ();
52   virtual void start_translation_timestep ();
53   virtual void acknowledge_grob (Grob_info);
54   virtual bool try_music (Music*);
55   virtual void process_music ();
56   void typeset_tie (Grob*);
57 public:
58   TRANSLATOR_DECLARATIONS(Tie_engraver);
59 };
60
61
62
63 Tie_engraver::Tie_engraver ()
64 {
65   event_ = 0;
66   last_event_  = 0;
67   tie_column_ = 0;
68 }
69
70
71 bool
72 Tie_engraver::try_music (Music *mus)
73 {
74   if (mus->is_mus_type ("tie-event"))
75     {
76       event_ = mus;
77     }
78   
79   return true;
80 }
81
82 void
83 Tie_engraver::process_music ()
84 {
85   if (event_)
86     daddy_trans_->set_property ("tieMelismaBusy", SCM_BOOL_T);
87 }
88
89 void
90 Tie_engraver::acknowledge_grob (Grob_info i)
91 {
92   if (Note_head::has_interface (i.grob_))
93     {
94       Grob * h  = i.grob_;
95       now_heads_.push (h);
96       for  (int i = heads_to_tie_.size (); i--;)
97         {
98           Grob *th =  heads_to_tie_[i];
99           Music * right_mus = unsmob_music (h->get_grob_property ("cause"));
100           Music * left_mus = unsmob_music (th->get_grob_property ("cause"));
101
102           /*
103             maybe should check positions too.
104            */
105           if (right_mus && left_mus
106               && gh_equal_p (right_mus->get_mus_property ("pitch"),
107                              left_mus->get_mus_property ("pitch")))
108             {
109               Grob * p = make_spanner ("Tie");
110               Tie::set_interface (p); // cannot remove yet!
111           
112               Tie::set_head (p, LEFT, th);
113               Tie::set_head (p, RIGHT, h);
114           
115               ties_.push (p);
116               announce_grob(p, last_event_->self_scm());
117             }
118         }
119
120       if (ties_.size () && ! tie_column_)
121         {
122           tie_column_ = make_spanner ("TieColumn");
123           announce_grob(tie_column_, SCM_EOL);
124         }
125
126       if (tie_column_)
127         for (int i = ties_.size (); i--;)
128           Tie_column::add_tie (tie_column_,ties_ [i]);
129     }
130 }
131
132 void
133 Tie_engraver::start_translation_timestep ()
134 {
135   daddy_trans_->set_property ("tieMelismaBusy",
136                               gh_bool2scm (heads_to_tie_.size ()));
137       
138 }
139
140 void
141 Tie_engraver::stop_translation_timestep ()
142 {
143   if (ties_.size ())
144     {
145       heads_to_tie_.clear ();
146       for (int i=0; i<  ties_.size (); i++)
147         {
148           typeset_tie (ties_[i]);
149         }
150
151       ties_.clear();
152       last_event_ = 0;
153       if (tie_column_)
154         {
155           typeset_grob (tie_column_);
156           tie_column_ =0;
157         }
158     }
159   
160   if (event_)
161     {
162       heads_to_tie_ = now_heads_;
163       last_event_ = event_;
164     }
165   event_ = 0;
166   now_heads_.clear ();
167 }
168
169 void
170 Tie_engraver::typeset_tie (Grob *her)
171 {
172   if (! (Tie::head (her,LEFT) && Tie::head (her,RIGHT)))
173     warning (_ ("lonely tie"));
174
175   Direction d = LEFT;
176   Drul_array<Grob *> new_head_drul;
177   new_head_drul[LEFT] = Tie::head (her,LEFT);
178   new_head_drul[RIGHT] = Tie::head (her,RIGHT);  
179   do {
180     if (!Tie::head (her,d))
181       new_head_drul[d] = Tie::head (her, (Direction)-d);
182   } while (flip (&d) != LEFT);
183
184   index_set_cell (her->get_grob_property ("head-pair"), LEFT, new_head_drul[LEFT]->self_scm ());
185   index_set_cell (her->get_grob_property ("head-pair"), RIGHT, new_head_drul[RIGHT]->self_scm ());
186
187   typeset_grob (her);
188 }
189
190
191 ENTER_DESCRIPTION(Tie_engraver,
192 /* descr */       "Generate ties between noteheads of equal pitch.",
193 /* creats*/       "Tie TieColumn",
194 /* accepts */     "tie-event",
195 /* acks  */      "rhythmic-head-interface",
196 /* reads */       "tieMelismaBusy",
197 /* write */       "");