lilypond-1.3.19
[lilypond.git] / lily / tie-engraver.cc
blob0948237b0e152886cb2bc16c6ed77e7d9d7215c5
1 /*
2 ctie-engraver.cc -- implement Tie_engraver
4 source file of the GNU LilyPond music typesetter
6 (c) 1998--1999 Han-Wen Nienhuys <hanwen@cs.uu.nl>
8 */
10 #include "tie-engraver.hh"
11 #include "command-request.hh"
12 #include "note-head.hh"
13 #include "musical-request.hh"
14 #include "tie.hh"
15 #include "translator-group.hh"
17 Tie_engraver::Tie_engraver()
19 req_l_ = 0;
23 bool
24 Tie_engraver::do_try_music (Music *m)
26 if (Tie_req * c = dynamic_cast<Tie_req*> (m))
28 req_l_ = c;
29 SCM m = get_property ("automaticMelismata",0);
30 bool am = gh_boolean_p (m) &&gh_scm2bool (m);
31 if (am)
33 set_melisma (true);
35 return true;
37 return false;
40 void
41 Tie_engraver::set_melisma (bool m)
43 Translator_group *where = daddy_trans_l_;
44 get_property ("tieMelismaBusy", &where);
45 if (!where)
46 where = daddy_trans_l_;
48 daddy_trans_l_->set_property ("tieMelismaBusy", m ? SCM_BOOL_T : SCM_BOOL_F);
51 void
52 Tie_engraver::acknowledge_element (Score_element_info i)
54 if (Note_head *nh = dynamic_cast<Note_head *> (i.elem_l_))
56 Note_req * m = dynamic_cast<Note_req* > (i.req_l_);
57 if (!m)
58 return;
59 now_heads_.push (CHead_melodic_tuple (nh, m, now_mom()+ m->length_mom ()));
63 void
64 Tie_engraver::do_process_requests ()
66 if (req_l_)
68 Moment now = now_mom ();
69 Link_array<Note_head> nharr;
71 stopped_heads_.clear ();
72 while (past_notes_pq_.size ()
73 && past_notes_pq_.front ().end_ == now)
74 stopped_heads_.push (past_notes_pq_.get ());
78 void
79 Tie_engraver::process_acknowledged ()
81 if (req_l_)
83 now_heads_.sort (CHead_melodic_tuple::pitch_compare);
84 stopped_heads_.sort(CHead_melodic_tuple::pitch_compare);
86 SCM head_list = SCM_EOL;
88 int j = stopped_heads_.size ()-1;
89 int i = now_heads_.size ()-1;
91 while (i >= 0 && j >=0)
93 int comp
94 = Musical_pitch::compare (now_heads_[i].req_l_->pitch_ ,
95 stopped_heads_[j].req_l_->pitch_);
97 if (comp)
99 (comp < 0) ? j -- : i--;
100 continue;
102 else
104 head_list = gh_cons (gh_cons (stopped_heads_[j].head_l_->self_scm_,
105 now_heads_[i].head_l_->self_scm_),
106 head_list);
108 past_notes_pq_. insert (now_heads_[i]);
109 now_heads_.del (i);
110 stopped_heads_.del (j);
111 i--;
112 j--;
117 SCM sparse = get_property ("sparseTies", 0);
118 if (to_boolean (sparse))
120 int i = scm_ilength (head_list);
122 if (!i)
123 return;
125 SCM pair = gh_list_ref (head_list, gh_int2scm (i/2));
127 Tie * p = new Tie;
128 p->set_head (LEFT, dynamic_cast<Item*> (unsmob_element (gh_car (pair))));
129 p->set_head (RIGHT, dynamic_cast<Item*> (unsmob_element (gh_cdr (pair))));
131 tie_p_arr_.push (p);
132 announce_element (Score_element_info (p, req_l_));
134 else for (SCM s = head_list; gh_pair_p (s); s = gh_cdr (s))
136 Tie * p = new Tie;
137 p->set_head (LEFT, dynamic_cast<Item*> (unsmob_element (gh_caar (s))));
138 p->set_head (RIGHT, dynamic_cast<Item*> (unsmob_element (gh_cdar (s))));
140 tie_p_arr_.push (p);
141 announce_element (Score_element_info (p, req_l_));
144 if (!tie_p_arr_.size ())
146 req_l_->warning (_ ("No ties were created!"));
153 void
154 Tie_engraver::do_pre_move_processing ()
156 for (int i=0; i < now_heads_.size (); i++)
158 past_notes_pq_.insert (now_heads_[i]);
160 now_heads_.clear ();
162 for (int i=0; i< tie_p_arr_.size (); i++)
164 typeset_element (tie_p_arr_[i]);
166 tie_p_arr_.clear ();
169 void
170 Tie_engraver::do_post_move_processing ()
172 SCM m = get_property ("automaticMelismata",0);
173 if (to_boolean (m))
175 set_melisma (false);
177 req_l_ = 0;
178 Moment now = now_mom ();
179 while (past_notes_pq_.size () && past_notes_pq_.front ().end_ < now)
180 past_notes_pq_.delmin ();
183 ADD_THIS_TRANSLATOR(Tie_engraver);
186 CHead_melodic_tuple::CHead_melodic_tuple ()
188 head_l_ =0;
189 req_l_ =0;
190 end_ = 0;
193 CHead_melodic_tuple::CHead_melodic_tuple (Note_head *h, Melodic_req*m, Moment mom)
195 head_l_ = h;
196 req_l_ = m;
197 end_ = mom;
201 CHead_melodic_tuple::pitch_compare (CHead_melodic_tuple const&h1,
202 CHead_melodic_tuple const &h2)
204 return Melodic_req::compare (*h1.req_l_, *h2.req_l_);
208 CHead_melodic_tuple::time_compare (CHead_melodic_tuple const&h1,
209 CHead_melodic_tuple const &h2)
211 return (h1.end_ - h2.end_ ).sign ();