]> git.donarmstrong.com Git - lilypond.git/blob - lily/tie-engraver.cc
* The grand 2005-2006 replace.
[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--2006 Han-Wen Nienhuys <hanwen@xs4all.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   void stop_translation_timestep ();
57   virtual void derived_mark () const;
58   void start_translation_timestep ();
59   DECLARE_ACKNOWLEDGER (note_head);
60   virtual bool try_music (Music *);
61   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     event_ = mus;
86
87   return true;
88 }
89
90 void
91 Tie_engraver::process_music ()
92 {
93   if (event_)
94     context ()->set_property ("tieMelismaBusy", SCM_BOOL_T);
95 }
96
97 void
98 Tie_engraver::acknowledge_note_head (Grob_info i)
99 {
100   Grob *h = i.grob ();
101   now_heads_.push (h);
102   for (int i = heads_to_tie_.size (); i--;)
103     {
104       Grob *th = heads_to_tie_[i].head_;
105       Music *right_mus = unsmob_music (h->get_property ("cause"));
106       Music *left_mus = unsmob_music (th->get_property ("cause"));
107
108       /*
109         maybe should check positions too.
110       */
111       if (right_mus && left_mus
112           && ly_is_equal (right_mus->get_property ("pitch"),
113                           left_mus->get_property ("pitch")))
114         {
115           Grob *p = new Spanner (heads_to_tie_[i].tie_definition_,
116                                  context ()->get_grob_key ("Tie"));
117           announce_grob (p, heads_to_tie_[i].event_->self_scm ());
118           Tie::set_head (p, LEFT, th);
119           Tie::set_head (p, RIGHT, h);
120
121           ties_.push (p);
122           heads_to_tie_.del (i);
123         }
124     }
125
126   if (ties_.size () && ! tie_column_)
127     tie_column_ = make_spanner ("TieColumn", ties_[0]->self_scm ());
128
129   if (tie_column_)
130     for (int i = ties_.size (); i--;)
131       Tie_column::add_tie (tie_column_, ties_[i]);
132 }
133
134 void
135 Tie_engraver::start_translation_timestep ()
136 {
137   context ()->set_property ("tieMelismaBusy",
138                             ly_bool2scm (heads_to_tie_.size ()));
139 }
140
141 void
142 Tie_engraver::stop_translation_timestep ()
143 {
144   if (ties_.size ())
145     {
146       if (!to_boolean (get_property ("tieWaitForNote")))
147         heads_to_tie_.clear ();
148
149       for (int i = 0; i < ties_.size (); i++)
150         typeset_tie (ties_[i]);
151
152       ties_.clear ();
153       tie_column_ = 0;
154     }
155
156   if (event_)
157     {
158       SCM start_definition
159         = updated_grob_properties (context (), ly_symbol2scm ("Tie"));
160
161       if (!to_boolean (get_property ("tieWaitForNote")))
162         heads_to_tie_.clear ();
163
164       for (int i = 0; i < now_heads_.size (); i++)
165         {
166           heads_to_tie_.push (Head_event_tuple (now_heads_[i], event_,
167                                                 start_definition));
168         }
169     }
170
171   event_ = 0;
172   now_heads_.clear ();
173 }
174
175 void
176 Tie_engraver::typeset_tie (Grob *her)
177 {
178   if (! (Tie::head (her, LEFT) && Tie::head (her, RIGHT)))
179     warning (_ ("lonely tie"));
180
181   Direction d = LEFT;
182   Drul_array<Grob *> new_head_drul;
183   new_head_drul[LEFT] = Tie::head (her, LEFT);
184   new_head_drul[RIGHT] = Tie::head (her, RIGHT);
185   do
186     {
187       if (!Tie::head (her, d))
188         new_head_drul[d] = Tie::head (her, (Direction) - d);
189     }
190   while (flip (&d) != LEFT);
191
192   Spanner *sp = dynamic_cast<Spanner*> (her);
193   sp->set_bound (LEFT, new_head_drul[LEFT]);
194   sp->set_bound (RIGHT, new_head_drul[RIGHT]);
195 }
196
197 #include "translator.icc"
198
199 ADD_ACKNOWLEDGER (Tie_engraver, note_head);
200 ADD_TRANSLATOR (Tie_engraver,
201                 /* doc */ "Generate ties between noteheads of equal pitch.",
202                 /* create */
203                 "Tie "
204                 "TieColumn",
205
206                 /* accept */ "tie-event",
207                 /* read */ "tieWaitForNote",
208                 /* write */ "tieMelismaBusy");