]> git.donarmstrong.com Git - lilypond.git/blob - lily/tie-engraver.cc
7cc71e4d5150a427017fd5bf84e7ac7a9095801d
[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
21 /**
22    Manufacture ties.  Acknowledge noteheads, and put them into a
23    priority queue. If we have a TieEvent, connect the notes that finish
24    just at this time, and note that start at this time.
25
26    TODO: Remove the dependency on musical info. We should tie on the
27    basis of position and duration-log of the heads (not of the events).
28
29    TODO: support sparseTies.
30
31    TODO: melismata will fuck up now:
32
33    < { c8 ~ c8 }
34      { c16 c c c  } >
35
36    melisma is after the 2nd 8th note, but will now be signaled as
37    lasting till the 3rd 16th.
38 */
39 class Tie_engraver : public Engraver
40 {
41   Music *event_;
42   Music *last_event_;
43   Link_array<Grob> now_heads_;
44   Link_array<Grob> heads_to_tie_;
45   Link_array<Grob> ties_;
46   
47   Spanner * tie_column_;
48   
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_acknowledged_grobs ();
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::acknowledge_grob (Grob_info i)
84 {
85   if (Note_head::has_interface (i.grob_))
86     {
87       Grob * h  = i.grob_;
88       now_heads_.push (h);
89       for  (int i = heads_to_tie_.size (); i--;)
90         {
91           Grob *th =  heads_to_tie_[i];
92           int staff_pos = gh_scm2int (h->get_grob_property ("staff-position"));
93           int left_staff_pos = gh_scm2int (th->get_grob_property ("staff-position"));
94           if (staff_pos == left_staff_pos)
95             {
96               Grob * p = new Spanner (get_property ("Tie"));
97               Tie::set_interface (p); // cannot remove yet!
98           
99               Tie::set_head (p, LEFT, th);
100               Tie::set_head (p, RIGHT, h);
101           
102               ties_.push (p);
103               announce_grob(p, last_event_->self_scm());
104             }
105         }
106     }
107 }
108
109 void
110 Tie_engraver::process_acknowledged_grobs ()
111 {
112   if (ties_.size () > 1 && !tie_column_)
113     {
114       tie_column_ = new Spanner (get_property ("TieColumn"));
115       
116       for (int i = ties_.size (); i--;)
117         Tie_column::add_tie (tie_column_,ties_ [i]);
118
119       announce_grob(tie_column_, SCM_EOL);
120     }
121 }
122
123 void
124 Tie_engraver::start_translation_timestep ()
125 {
126   if (to_boolean (get_property ("automaticMelismata")))
127       daddy_trans_->set_property ("tieMelismaBusy",
128                                   gh_bool2scm (heads_to_tie_.size ()));
129       
130 }
131
132 void
133 Tie_engraver::stop_translation_timestep ()
134 {
135   if (ties_.size ())
136     {
137       heads_to_tie_.clear ();
138       for (int i=0; i<  ties_.size (); i++)
139         {
140           typeset_tie (ties_[i]);
141         }
142
143       ties_.clear();
144       last_event_ = 0;
145       if (tie_column_)
146         {
147           typeset_grob (tie_column_);
148           tie_column_ =0;
149         }
150     }
151   
152   if (event_)
153     {
154       heads_to_tie_ = now_heads_;
155       last_event_ = event_;
156     }
157   event_ = 0;
158   now_heads_.clear ();
159 }
160
161 void
162 Tie_engraver::typeset_tie (Grob *her)
163 {
164   if (! (Tie::head (her,LEFT) && Tie::head (her,RIGHT)))
165     warning (_ ("lonely tie"));
166
167   Direction d = LEFT;
168   Drul_array<Grob *> new_head_drul;
169   new_head_drul[LEFT] = Tie::head (her,LEFT);
170   new_head_drul[RIGHT] = Tie::head (her,RIGHT);  
171   do {
172     if (!Tie::head (her,d))
173       new_head_drul[d] = Tie::head (her, (Direction)-d);
174   } while (flip (&d) != LEFT);
175
176   index_set_cell (her->get_grob_property ("heads"), LEFT, new_head_drul[LEFT]->self_scm ());
177   index_set_cell (her->get_grob_property ("heads"), RIGHT, new_head_drul[RIGHT]->self_scm ());
178
179   typeset_grob (her);
180 }
181
182
183 ENTER_DESCRIPTION(Tie_engraver,
184 /* descr */       "Generate ties between noteheads of equal pitch.",
185 /* creats*/       "Tie TieColumn",
186 /* accepts */     "tie-event",
187 /* acks  */      "rhythmic-head-interface",
188 /* reads */       "tieMelismaBusy",
189 /* write */       "");