]> git.donarmstrong.com Git - lilypond.git/blob - lily/tie-engraver.cc
* lily/translator.cc (derived_mark): new function.
[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   SCM tie_start_definition_;
40   
41   Spanner * tie_column_;
42   
43 protected:
44   virtual void stop_translation_timestep ();
45   virtual void derived_mark () const;
46   virtual void start_translation_timestep ();
47   virtual void acknowledge_grob (Grob_info);
48   virtual bool try_music (Music*);
49   virtual void process_music ();
50   void typeset_tie (Grob*);
51 public:
52   TRANSLATOR_DECLARATIONS (Tie_engraver);
53 };
54
55
56
57 Tie_engraver::Tie_engraver ()
58 {
59   event_ = 0;
60   last_event_  = 0;
61   tie_column_ = 0;
62   tie_start_definition_ = SCM_EOL;
63 }
64
65 void
66 Tie_engraver::derived_mark () const
67 {
68   scm_gc_mark (tie_start_definition_);
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::process_music ()
85 {
86   if (event_)
87     {
88       context ()->set_property ("tieMelismaBusy", SCM_BOOL_T);
89     }
90 }
91
92 void
93 Tie_engraver::acknowledge_grob (Grob_info i)
94 {
95   if (Note_head::has_interface (i.grob_))
96     {
97       Grob * h  = i.grob_;
98       now_heads_.push (h);
99       for  (int i = heads_to_tie_.size (); i--;)
100         {
101           Grob *th =  heads_to_tie_[i];
102           Music * right_mus = unsmob_music (h->get_property ("cause"));
103           Music * left_mus = unsmob_music (th->get_property ("cause"));
104
105           /*
106             maybe should check positions too.
107            */
108           if (right_mus && left_mus
109               && ly_c_equal_p (right_mus->get_property ("pitch"),
110                              left_mus->get_property ("pitch")))
111             {
112               Grob * p = new Spanner  (tie_start_definition_);
113               announce_grob (p, last_event_->self_scm ());
114               Tie::set_interface (p); // cannot remove yet!
115           
116               Tie::set_head (p, LEFT, th);
117               Tie::set_head (p, RIGHT, h);
118           
119               ties_.push (p);
120             }
121         }
122
123       if (ties_.size () && ! tie_column_)
124         {
125           tie_column_ = make_spanner ("TieColumn", SCM_EOL);
126           
127         }
128
129       if (tie_column_)
130         for (int i = ties_.size (); i--;)
131           Tie_column::add_tie (tie_column_,ties_ [i]);
132     }
133 }
134
135 void
136 Tie_engraver::start_translation_timestep ()
137 {
138   context ()->set_property ("tieMelismaBusy",
139                               ly_bool2scm (heads_to_tie_.size ()));
140       
141 }
142
143 void
144 Tie_engraver::stop_translation_timestep ()
145 {
146   if (ties_.size ())
147     {
148       heads_to_tie_.clear ();
149       for (int i=0; i<  ties_.size (); i++)
150         {
151           typeset_tie (ties_[i]);
152         }
153
154       ties_.clear ();
155       last_event_ = 0;
156       tie_column_ =0;
157     }
158   
159   if (event_)
160     {
161       tie_start_definition_ = updated_grob_properties (context (), ly_symbol2scm ("Tie"));
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_property ("head-pair"), LEFT, new_head_drul[LEFT]->self_scm ());
185   index_set_cell (her->get_property ("head-pair"), RIGHT, new_head_drul[RIGHT]->self_scm ());
186
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 */       "");