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