]> git.donarmstrong.com Git - lilypond.git/blob - lily/tie-engraver.cc
(stop_translation_timestep): remember tie
[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 "protected-scm.hh"
15 #include "spanner.hh"
16 #include "tie-column.hh"
17 #include "engraver.hh"
18 #include "item.hh"
19 #include "grob-pitch-tuple.hh"
20 #include "warn.hh"
21 #include "note-head.hh"
22 #include "staff-symbol-referencer.hh"
23
24 /**
25    Manufacture ties.  Acknowledge noteheads, and put them into a
26    priority queue. If we have a TieEvent, connect the notes that finish
27    just at this time, and note that start at this time.
28
29    TODO: Remove the dependency on musical info. We should tie on the
30    basis of position and duration-log of the heads (not of the events).
31 */
32 class Tie_engraver : public Engraver
33 {
34   Music *event_;
35   Music *last_event_;
36   Link_array<Grob> now_heads_;
37   Link_array<Grob> heads_to_tie_;
38   Link_array<Grob> ties_;
39   Protected_scm tie_start_definition_;
40   
41   Spanner * tie_column_;
42   
43 protected:
44   virtual void stop_translation_timestep ();
45   virtual void start_translation_timestep ();
46   virtual void acknowledge_grob (Grob_info);
47   virtual bool try_music (Music*);
48   virtual void process_music ();
49   void typeset_tie (Grob*);
50 public:
51   TRANSLATOR_DECLARATIONS (Tie_engraver);
52 };
53
54
55
56 Tie_engraver::Tie_engraver ()
57 {
58   event_ = 0;
59   last_event_  = 0;
60   tie_column_ = 0;
61 }
62
63
64 bool
65 Tie_engraver::try_music (Music *mus)
66 {
67   if (mus->is_mus_type ("tie-event"))
68     {
69       event_ = mus;
70     }
71   
72   return true;
73 }
74
75 void
76 Tie_engraver::process_music ()
77 {
78   if (event_)
79     {
80       context ()->set_property ("tieMelismaBusy", SCM_BOOL_T);
81     }
82 }
83
84 void
85 Tie_engraver::acknowledge_grob (Grob_info i)
86 {
87   if (Note_head::has_interface (i.grob_))
88     {
89       Grob * h  = i.grob_;
90       now_heads_.push (h);
91       for  (int i = heads_to_tie_.size (); i--;)
92         {
93           Grob *th =  heads_to_tie_[i];
94           Music * right_mus = unsmob_music (h->get_property ("cause"));
95           Music * left_mus = unsmob_music (th->get_property ("cause"));
96
97           /*
98             maybe should check positions too.
99            */
100           if (right_mus && left_mus
101               && ly_c_equal_p (right_mus->get_property ("pitch"),
102                              left_mus->get_property ("pitch")))
103             {
104               Grob * p = new Spanner  (tie_start_definition_);
105               announce_grob (p, last_event_->self_scm ());
106               Tie::set_interface (p); // cannot remove yet!
107           
108               Tie::set_head (p, LEFT, th);
109               Tie::set_head (p, RIGHT, h);
110           
111               ties_.push (p);
112             }
113         }
114
115       if (ties_.size () && ! tie_column_)
116         {
117           tie_column_ = make_spanner ("TieColumn", SCM_EOL);
118           
119         }
120
121       if (tie_column_)
122         for (int i = ties_.size (); i--;)
123           Tie_column::add_tie (tie_column_,ties_ [i]);
124     }
125 }
126
127 void
128 Tie_engraver::start_translation_timestep ()
129 {
130   context ()->set_property ("tieMelismaBusy",
131                               ly_bool2scm (heads_to_tie_.size ()));
132       
133 }
134
135 void
136 Tie_engraver::stop_translation_timestep ()
137 {
138   if (ties_.size ())
139     {
140       heads_to_tie_.clear ();
141       for (int i=0; i<  ties_.size (); i++)
142         {
143           typeset_tie (ties_[i]);
144         }
145
146       ties_.clear ();
147       last_event_ = 0;
148       tie_column_ =0;
149     }
150   
151   if (event_)
152     {
153       tie_start_definition_ = updated_grob_properties (context (), ly_symbol2scm ("Tie"));
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_property ("head-pair"), LEFT, new_head_drul[LEFT]->self_scm ());
177   index_set_cell (her->get_property ("head-pair"), RIGHT, new_head_drul[RIGHT]->self_scm ());
178
179 }
180
181
182 ENTER_DESCRIPTION (Tie_engraver,
183 /* descr */       "Generate ties between noteheads of equal pitch.",
184 /* creats*/       "Tie TieColumn",
185 /* accepts */     "tie-event",
186 /* acks  */      "rhythmic-head-interface",
187 /* reads */       "tieMelismaBusy",
188 /* write */       "");