]> git.donarmstrong.com Git - lilypond.git/blob - lily/tie-engraver.cc
(DECLARE_EVENT_SWALLOWER): ENTER_DESCRIPTION -> ADD_TRANSLATOR
[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 "tie.hh"
11 #include "context.hh"
12 #include "protected-scm.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 class Tie_engraver : public Engraver
31 {
32   Music *event_;
33   Music *last_event_;
34   Link_array<Grob> now_heads_;
35   Link_array<Grob> heads_to_tie_;
36   Link_array<Grob> ties_;
37   SCM tie_start_definition_;
38   
39   Spanner * tie_column_;
40   
41 protected:
42   virtual void stop_translation_timestep ();
43   virtual void derived_mark () const;
44   virtual void start_translation_timestep ();
45   virtual void acknowledge_grob (Grob_info);
46   virtual bool try_music (Music*);
47   virtual void process_music ();
48   void typeset_tie (Grob*);
49 public:
50   TRANSLATOR_DECLARATIONS (Tie_engraver);
51 };
52
53
54
55 Tie_engraver::Tie_engraver ()
56 {
57   event_ = 0;
58   last_event_  = 0;
59   tie_column_ = 0;
60   tie_start_definition_ = SCM_EOL;
61 }
62
63 void
64 Tie_engraver::derived_mark () const
65 {
66   scm_gc_mark (tie_start_definition_);
67 }
68
69
70 bool
71 Tie_engraver::try_music (Music *mus)
72 {
73   if (mus->is_mus_type ("tie-event"))
74     {
75       event_ = mus;
76     }
77   
78   return true;
79 }
80
81 void
82 Tie_engraver::process_music ()
83 {
84   if (event_)
85     {
86       context ()->set_property ("tieMelismaBusy", SCM_BOOL_T);
87     }
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_property ("cause"));
101           Music * left_mus = unsmob_music (th->get_property ("cause"));
102
103           /*
104             maybe should check positions too.
105            */
106           if (right_mus && left_mus
107               && ly_c_equal_p (right_mus->get_property ("pitch"),
108                              left_mus->get_property ("pitch")))
109             {
110               Grob * p = new Spanner  (tie_start_definition_, context()->get_grob_key ("Tie"));
111               announce_grob (p, last_event_->self_scm ());
112               Tie::set_interface (p); // cannot remove yet!
113           
114               Tie::set_head (p, LEFT, th);
115               Tie::set_head (p, RIGHT, h);
116           
117               ties_.push (p);
118             }
119         }
120
121       if (ties_.size () && ! tie_column_)
122         {
123           tie_column_ = make_spanner ("TieColumn", SCM_EOL);
124           
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   context ()->set_property ("tieMelismaBusy",
137                               ly_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       tie_column_ = 0;
155     }
156   
157   if (event_)
158     {
159       tie_start_definition_ = updated_grob_properties (context (), ly_symbol2scm ("Tie"));
160       heads_to_tie_ = now_heads_;
161       last_event_ = event_;
162     }
163   event_ = 0;
164   now_heads_.clear ();
165 }
166
167 void
168 Tie_engraver::typeset_tie (Grob *her)
169 {
170   if (! (Tie::head (her,LEFT) && Tie::head (her,RIGHT)))
171     warning (_ ("lonely tie"));
172
173   Direction d = LEFT;
174   Drul_array<Grob *> new_head_drul;
175   new_head_drul[LEFT] = Tie::head (her,LEFT);
176   new_head_drul[RIGHT] = Tie::head (her,RIGHT);  
177   do {
178     if (!Tie::head (her,d))
179       new_head_drul[d] = Tie::head (her, (Direction)-d);
180   } while (flip (&d) != LEFT);
181
182   index_set_cell (her->get_property ("head-pair"), LEFT, new_head_drul[LEFT]->self_scm ());
183   index_set_cell (her->get_property ("head-pair"), RIGHT, new_head_drul[RIGHT]->self_scm ());
184
185 }
186
187
188 ADD_TRANSLATOR (Tie_engraver,
189 /* descr */       "Generate ties between noteheads of equal pitch.",
190 /* creats*/       "Tie TieColumn",
191 /* accepts */     "tie-event",
192 /* acks  */      "rhythmic-head-interface",
193 /* reads */       "tieMelismaBusy",
194 /* write */       "");