]> git.donarmstrong.com Git - lilypond.git/blob - lily/tie-engraver.cc
* lily/include/translator.icc: new file.
[lilypond.git] / lily / tie-engraver.cc
1 /*
2   tie-engraver.cc -- implement Tie_engraver
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1998--2005 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8
9 #include "tie.hh"
10 #include "context.hh"
11 #include "protected-scm.hh"
12 #include "spanner.hh"
13 #include "tie-column.hh"
14 #include "engraver.hh"
15 #include "item.hh"
16 #include "grob-pitch-tuple.hh"
17 #include "warn.hh"
18 #include "note-head.hh"
19 #include "staff-symbol-referencer.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
30 struct Head_event_tuple
31 {
32   Grob *head_;
33   SCM tie_definition_;
34   Music *event_;
35   Head_event_tuple ()
36   {
37   }
38   Head_event_tuple (Grob *h, Music *m, SCM def)
39   {
40     head_ = h;
41     event_ = m;
42     tie_definition_ = def;
43   }
44 };
45
46 class Tie_engraver : public Engraver
47 {
48   Music *event_;
49   Link_array<Grob> now_heads_;
50   Array<Head_event_tuple> heads_to_tie_;
51   Link_array<Grob> ties_;
52
53   Spanner *tie_column_;
54
55 protected:
56   PRECOMPUTED_VIRTUAL void stop_translation_timestep ();
57   virtual void derived_mark () const;
58   PRECOMPUTED_VIRTUAL void start_translation_timestep ();
59   virtual void acknowledge_grob (Grob_info);
60   virtual bool try_music (Music *);
61   PRECOMPUTED_VIRTUAL void process_music ();
62   void typeset_tie (Grob *);
63 public:
64   TRANSLATOR_DECLARATIONS (Tie_engraver);
65 };
66
67 void
68 Tie_engraver::derived_mark () const
69 {
70   Engraver::derived_mark ();
71   for (int i = 0; i < heads_to_tie_.size (); i++)
72     scm_gc_mark (heads_to_tie_[i].tie_definition_);
73 }
74
75 Tie_engraver::Tie_engraver ()
76 {
77   event_ = 0;
78   tie_column_ = 0;
79 }
80
81 bool
82 Tie_engraver::try_music (Music *mus)
83 {
84   if (mus->is_mus_type ("tie-event"))
85     {
86       event_ = mus;
87     }
88
89   return true;
90 }
91
92 void
93 Tie_engraver::process_music ()
94 {
95   if (event_)
96     {
97       context ()->set_property ("tieMelismaBusy", SCM_BOOL_T);
98     }
99 }
100
101 void
102 Tie_engraver::acknowledge_grob (Grob_info i)
103 {
104   if (Note_head::has_interface (i.grob ()))
105     {
106       Grob *h = i.grob ();
107       now_heads_.push (h);
108       for (int i = heads_to_tie_.size (); i--;)
109         {
110           Grob *th = heads_to_tie_[i].head_;
111           Music *right_mus = unsmob_music (h->get_property ("cause"));
112           Music *left_mus = unsmob_music (th->get_property ("cause"));
113
114           /*
115             maybe should check positions too.
116           */
117           if (right_mus && left_mus
118               && ly_is_equal (right_mus->get_property ("pitch"),
119                                left_mus->get_property ("pitch")))
120             {
121               Grob *p = new Spanner (heads_to_tie_[i].tie_definition_,
122                                      context ()->get_grob_key ("Tie"));
123               announce_grob (p, heads_to_tie_[i].event_->self_scm ());
124               Tie::set_interface (p); // cannot remove yet!
125
126               Tie::set_head (p, LEFT, th);
127               Tie::set_head (p, RIGHT, h);
128
129               ties_.push (p);
130               heads_to_tie_.del (i);
131             }
132         }
133
134       if (ties_.size () && ! tie_column_)
135         {
136           tie_column_ = make_spanner ("TieColumn", ties_[0]->self_scm ());
137         }
138
139       if (tie_column_)
140         for (int i = ties_.size (); i--;)
141           Tie_column::add_tie (tie_column_, ties_[i]);
142     }
143 }
144
145 void
146 Tie_engraver::start_translation_timestep ()
147 {
148   context ()->set_property ("tieMelismaBusy",
149                             ly_bool2scm (heads_to_tie_.size ()));
150 }
151
152 void
153 Tie_engraver::stop_translation_timestep ()
154 {
155   if (ties_.size ())
156     {
157       if (!to_boolean (get_property ("tieWaitForNote")))
158         {
159           heads_to_tie_.clear ();
160         }
161
162       for (int i = 0; i < ties_.size (); i++)
163         {
164           typeset_tie (ties_[i]);
165         }
166
167       ties_.clear ();
168       tie_column_ = 0;
169     }
170
171   if (event_)
172     {
173       SCM start_definition
174         = updated_grob_properties (context (), ly_symbol2scm ("Tie"));
175
176       if (!to_boolean (get_property ("tieWaitForNote")))
177         heads_to_tie_.clear ();
178
179       for (int i = 0; i < now_heads_.size (); i++)
180         {
181           heads_to_tie_.push (Head_event_tuple (now_heads_[i], event_,
182                                                 start_definition));
183         }
184     }
185
186   event_ = 0;
187   now_heads_.clear ();
188 }
189
190 void
191 Tie_engraver::typeset_tie (Grob *her)
192 {
193   if (! (Tie::head (her, LEFT) && Tie::head (her, RIGHT)))
194     warning (_ ("lonely tie"));
195
196   Direction d = LEFT;
197   Drul_array<Grob *> new_head_drul;
198   new_head_drul[LEFT] = Tie::head (her, LEFT);
199   new_head_drul[RIGHT] = Tie::head (her, RIGHT);
200   do
201     {
202       if (!Tie::head (her, d))
203         new_head_drul[d] = Tie::head (her, (Direction) - d);
204     }
205   while (flip (&d) != LEFT);
206
207   index_set_cell (her->get_property ("head-pair"), LEFT, new_head_drul[LEFT]->self_scm ());
208   index_set_cell (her->get_property ("head-pair"), RIGHT, new_head_drul[RIGHT]->self_scm ());
209 }
210
211 #include "translator.icc"
212
213 ADD_TRANSLATOR (Tie_engraver,
214                 /* descr */ "Generate ties between noteheads of equal pitch.",
215                 /* creats*/ "Tie TieColumn",
216                 /* accepts */ "tie-event",
217                 /* acks  */ "rhythmic-head-interface",
218                 /* reads */ "tieMelismaBusy",
219                 /* write */ "");