]> git.donarmstrong.com Git - lilypond.git/blob - lily/tie-engraver.cc
Fix some bugs in the dynamic engraver and PostScript backend
[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 "engraver.hh"
10
11 #include "context.hh"
12 #include "grob-pitch-tuple.hh"
13 #include "international.hh"
14 #include "item.hh"
15 #include "note-head.hh"
16 #include "protected-scm.hh"
17 #include "spanner.hh"
18 #include "staff-symbol-referencer.hh"
19 #include "tie-column.hh"
20 #include "tie.hh"
21 #include "warn.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
32 struct Head_event_tuple
33 {
34   Grob *head_;
35   SCM tie_definition_;
36   Music *event_;
37   Head_event_tuple ()
38   {
39   }
40   Head_event_tuple (Grob *h, Music *m, SCM def)
41   {
42     head_ = h;
43     event_ = m;
44     tie_definition_ = def;
45   }
46 };
47
48 class Tie_engraver : public Engraver
49 {
50   Music *event_;
51   vector<Grob*> now_heads_;
52   vector<Head_event_tuple> heads_to_tie_;
53   vector<Grob*> ties_;
54
55   Spanner *tie_column_;
56
57 protected:
58   void stop_translation_timestep ();
59   virtual void derived_mark () const;
60   void start_translation_timestep ();
61   DECLARE_ACKNOWLEDGER (note_head);
62   virtual bool try_music (Music *);
63   void process_music ();
64   void typeset_tie (Grob *);
65 public:
66   TRANSLATOR_DECLARATIONS (Tie_engraver);
67 };
68
69 void
70 Tie_engraver::derived_mark () const
71 {
72   Engraver::derived_mark ();
73   for (vsize i = 0; i < heads_to_tie_.size (); i++)
74     scm_gc_mark (heads_to_tie_[i].tie_definition_);
75 }
76
77 Tie_engraver::Tie_engraver ()
78 {
79   event_ = 0;
80   tie_column_ = 0;
81 }
82
83 bool
84 Tie_engraver::try_music (Music *mus)
85 {
86   if (mus->is_mus_type ("tie-event"))
87     event_ = mus;
88
89   return true;
90 }
91
92 void
93 Tie_engraver::process_music ()
94 {
95   if (event_)
96     context ()->set_property ("tieMelismaBusy", SCM_BOOL_T);
97 }
98
99 void
100 Tie_engraver::acknowledge_note_head (Grob_info i)
101 {
102   Grob *h = i.grob ();
103   now_heads_.push_back (h);
104   for (vsize i = heads_to_tie_.size (); i--;)
105     {
106       Grob *th = heads_to_tie_[i].head_;
107       Music *right_mus = unsmob_music (h->get_property ("cause"));
108       Music *left_mus = unsmob_music (th->get_property ("cause"));
109
110       /*
111         maybe should check positions too.
112       */
113       if (right_mus && left_mus
114           && ly_is_equal (right_mus->get_property ("pitch"),
115                           left_mus->get_property ("pitch")))
116         {
117           Grob *p = new Spanner (heads_to_tie_[i].tie_definition_,
118                                  context ()->get_grob_key ("Tie"));
119           announce_grob (p, heads_to_tie_[i].event_->self_scm ());
120           Tie::set_head (p, LEFT, th);
121           Tie::set_head (p, RIGHT, h);
122
123           ties_.push_back (p);
124           heads_to_tie_.erase (heads_to_tie_.begin () + i);
125         }
126     }
127
128   if (ties_.size () && ! tie_column_)
129     tie_column_ = make_spanner ("TieColumn", ties_[0]->self_scm ());
130
131   if (tie_column_)
132     for (vsize i = ties_.size (); i--;)
133       Tie_column::add_tie (tie_column_, ties_[i]);
134 }
135
136 void
137 Tie_engraver::start_translation_timestep ()
138 {
139   context ()->set_property ("tieMelismaBusy",
140                             ly_bool2scm (heads_to_tie_.size ()));
141 }
142
143 void
144 Tie_engraver::stop_translation_timestep ()
145 {
146   if (ties_.size ())
147     {
148       if (!to_boolean (get_property ("tieWaitForNote")))
149         heads_to_tie_.clear ();
150
151       for (vsize i = 0; i < ties_.size (); i++)
152         typeset_tie (ties_[i]);
153
154       ties_.clear ();
155       tie_column_ = 0;
156     }
157
158   if (event_)
159     {
160       SCM start_definition
161         = updated_grob_properties (context (), ly_symbol2scm ("Tie"));
162
163       if (!to_boolean (get_property ("tieWaitForNote")))
164         heads_to_tie_.clear ();
165
166       for (vsize i = 0; i < now_heads_.size (); i++)
167         {
168           heads_to_tie_.push_back (Head_event_tuple (now_heads_[i], event_,
169                                                 start_definition));
170         }
171     }
172
173   event_ = 0;
174   now_heads_.clear ();
175 }
176
177 void
178 Tie_engraver::typeset_tie (Grob *her)
179 {
180   if (! (Tie::head (her, LEFT) && Tie::head (her, RIGHT)))
181     warning (_ ("lonely tie"));
182
183   Direction d = LEFT;
184   Drul_array<Grob *> new_head_drul;
185   new_head_drul[LEFT] = Tie::head (her, LEFT);
186   new_head_drul[RIGHT] = Tie::head (her, RIGHT);
187   do
188     {
189       if (!Tie::head (her, d))
190         new_head_drul[d] = Tie::head (her, (Direction) - d);
191     }
192   while (flip (&d) != LEFT);
193
194   Spanner *sp = dynamic_cast<Spanner*> (her);
195   sp->set_bound (LEFT, new_head_drul[LEFT]);
196   sp->set_bound (RIGHT, new_head_drul[RIGHT]);
197 }
198
199 #include "translator.icc"
200
201 ADD_ACKNOWLEDGER (Tie_engraver, note_head);
202 ADD_TRANSLATOR (Tie_engraver,
203                 /* doc */ "Generate ties between noteheads of equal pitch.",
204                 /* create */
205                 "Tie "
206                 "TieColumn",
207
208                 /* accept */ "tie-event",
209                 /* read */ "tieWaitForNote",
210                 /* write */ "tieMelismaBusy");