lilypond-1.3.12
[lilypond.git] / lily / tie-engraver.cc
blob876ca6070fcac5541743bfda711b1dc602fa539c
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);
85 int i=0;
86 int j=0;
87 int tie_count=0;
88 while ( i < now_heads_.size () && j < stopped_heads_.size ())
90 int comp
91 = Musical_pitch::compare (now_heads_[i].req_l_->pitch_ ,
92 stopped_heads_[j].req_l_->pitch_);
94 if (comp)
96 (comp < 0) ? i ++ : j++;
97 continue;
99 else
101 tie_count ++;
103 /* don't go around recreating ties that were already
104 made. Not infallible. Due to reordering in sort (),
105 we will make the wrong ties when noteheads are
106 added. */
107 if (tie_count > tie_p_arr_.size ())
109 Tie * p = new Tie;
110 p->set_head (LEFT, stopped_heads_[j].head_l_);
111 p->set_head (RIGHT, now_heads_[i].head_l_);
112 tie_p_arr_.push (p);
113 announce_element (Score_element_info (p, req_l_));
115 i++;
116 j++;
121 if (!tie_p_arr_.size ())
123 req_l_->warning (_ ("No ties were created!"));
130 void
131 Tie_engraver::do_pre_move_processing ()
133 for (int i=0; i < now_heads_.size (); i++)
135 past_notes_pq_.insert (now_heads_[i]);
137 now_heads_.clear ();
139 for (int i=0; i< tie_p_arr_.size (); i++)
141 typeset_element (tie_p_arr_[i]);
143 tie_p_arr_.clear ();
146 void
147 Tie_engraver::do_post_move_processing ()
149 SCM m = get_property ("automaticMelismata",0);
150 if (gh_boolean_p (m) && gh_scm2bool (m))
152 set_melisma (false);
154 req_l_ = 0;
155 Moment now = now_mom ();
156 while (past_notes_pq_.size () && past_notes_pq_.front ().end_ < now)
157 past_notes_pq_.delmin ();
160 ADD_THIS_TRANSLATOR(Tie_engraver);
163 CHead_melodic_tuple::CHead_melodic_tuple ()
165 head_l_ =0;
166 req_l_ =0;
167 end_ = 0;
170 CHead_melodic_tuple::CHead_melodic_tuple (Note_head *h, Melodic_req*m, Moment mom)
172 head_l_ = h;
173 req_l_ = m;
174 end_ = mom;
178 CHead_melodic_tuple::pitch_compare (CHead_melodic_tuple const&h1,
179 CHead_melodic_tuple const &h2)
181 return Melodic_req::compare (*h1.req_l_, *h2.req_l_);
185 CHead_melodic_tuple::time_compare (CHead_melodic_tuple const&h1,
186 CHead_melodic_tuple const &h2)
188 return (h1.end_ - h2.end_ ).sign ();