]> git.donarmstrong.com Git - lilypond.git/blob - lily/tie-engraver.cc
release: 1.1.33
[lilypond.git] / lily / tie-engraver.cc
1 /*   
2   ctie-engraver.cc --  implement Tie_engraver
3   
4   source file of the GNU LilyPond music typesetter
5   
6   (c) 1998--1999 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7   
8  */
9
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
16 Tie_engraver::Tie_engraver()
17 {
18   req_l_ = 0;
19 }
20
21
22 bool
23 Tie_engraver::do_try_music (Music *m)
24 {
25   if (Tie_req * c = dynamic_cast<Tie_req*> (m))
26     {
27       req_l_ = c;
28       return true;
29     }
30   return false;
31 }
32
33 void
34 Tie_engraver::acknowledge_element (Score_element_info i)
35 {
36   if (Note_head *nh = dynamic_cast<Note_head *> (i.elem_l_))
37     {
38       Note_req * m = dynamic_cast<Note_req* > (i.req_l_);
39       if (!m)
40         return;
41       now_heads_.push (CHead_melodic_tuple (nh, m, now_mom()+ m->length_mom ()));
42     }
43 }
44
45 void
46 Tie_engraver::do_process_requests ()
47 {
48   if (req_l_)
49     {
50       Moment now = now_mom ();
51       Link_array<Note_head> nharr;
52       
53       stopped_heads_.clear ();
54       while (past_notes_pq_.size ()
55              && past_notes_pq_.front ().end_ == now)
56         stopped_heads_.push (past_notes_pq_.get ());
57     }
58 }
59
60 void
61 Tie_engraver::process_acknowledged ()
62 {
63   bool old_behavior = get_property ("oldTieBehavior", 0).to_bool ();
64   
65   if (req_l_)
66     {
67       if (old_behavior)
68         {
69           if (now_heads_.size () != stopped_heads_.size ())
70             {
71               req_l_->warning ("Unequal number of note heads for tie");
72             }
73           int sz = now_heads_.size () <? stopped_heads_.size ();
74
75           /* hmm. Should do something more sensible.
76        because, we assume no more noteheads come along after the 1st pass.
77           */
78           if (sz <= tie_p_arr_.size ())
79             return;
80
81           now_heads_.sort (CHead_melodic_tuple::pitch_compare);
82           stopped_heads_.sort(CHead_melodic_tuple::pitch_compare);
83
84           for (int i=0; i < sz; i++)
85             {
86               Tie * p = new Tie;
87               p->set_head (LEFT, stopped_heads_[i].head_l_);
88               p->set_head (RIGHT, now_heads_[i].head_l_);
89               tie_p_arr_.push (p);
90               announce_element (Score_element_info (p, req_l_));
91             }
92         }
93       else
94         {
95           now_heads_.sort (CHead_melodic_tuple::pitch_compare);
96           stopped_heads_.sort(CHead_melodic_tuple::pitch_compare);
97           int i=0;
98           int j=0;
99           int tie_count=0;
100           while  ( i < now_heads_.size () && j < stopped_heads_.size ())
101             {
102               int comp
103                 = Musical_pitch::compare (now_heads_[i].req_l_->pitch_ ,
104                                           stopped_heads_[j].req_l_->pitch_);
105
106               if (comp)
107                 {
108                   (comp < 0) ? i ++ : j++;
109                   continue;
110                 }
111               else
112                 {
113                   tie_count ++;
114
115                   /* don't go around recreating ties that were already
116                   made. Not infallible. Due to reordering in sort (),
117                   we will make the wrong ties when noteheads are
118                   added.  */
119                   if (tie_count > tie_p_arr_.size ())
120                     {
121                       Tie * p = new Tie;
122                       p->set_head (LEFT, stopped_heads_[j].head_l_);
123                       p->set_head (RIGHT, now_heads_[i].head_l_);
124                       tie_p_arr_.push (p);
125                       announce_element (Score_element_info (p, req_l_));
126                     }
127                   i++;
128                   j++;
129
130                 }
131             }
132         }
133     }
134 }
135
136 void
137 Tie_engraver::do_pre_move_processing ()
138 {
139   for (int i=0; i < now_heads_.size (); i++)
140     {
141       past_notes_pq_.insert (now_heads_[i]);
142     }
143   now_heads_.clear ();
144
145   Scalar dir (get_property ("tieydirection", 0));
146   Scalar dir2 (get_property ("ydirection", 0));
147
148   Direction tie_dir = CENTER;
149   if (dir.length_i () && dir.isnum_b ())
150     tie_dir = (Direction) sign (int(dir));
151   else if (dir2.length_i () && dir2.isnum_b ())
152     tie_dir = (Direction) sign (int (dir2));
153   
154   for (int i=0; i<  tie_p_arr_.size (); i++)
155    {
156       tie_p_arr_[i]->dir_ = tie_dir;
157       typeset_element (tie_p_arr_[i]);
158     }
159   tie_p_arr_.clear ();
160 }
161
162 void
163 Tie_engraver::do_post_move_processing ()
164 {
165   req_l_ =0;
166   Moment now = now_mom ();
167   while (past_notes_pq_.size () && past_notes_pq_.front ().end_ < now)
168     past_notes_pq_.delmin ();
169 }
170
171 ADD_THIS_TRANSLATOR(Tie_engraver);
172
173
174 CHead_melodic_tuple::CHead_melodic_tuple ()
175 {
176   head_l_ =0;
177   req_l_ =0;
178   end_ = 0;
179 }
180
181 CHead_melodic_tuple::CHead_melodic_tuple (Note_head *h, Melodic_req*m, Moment mom)
182 {
183   head_l_ = h;
184   req_l_ = m;
185   end_ = mom;
186 }
187
188 int
189 CHead_melodic_tuple::pitch_compare (CHead_melodic_tuple const&h1,
190                              CHead_melodic_tuple const &h2)
191 {
192   return Melodic_req::compare (*h1.req_l_, *h2.req_l_);
193 }
194
195 int
196 CHead_melodic_tuple::time_compare (CHead_melodic_tuple const&h1,
197                              CHead_melodic_tuple const &h2)
198 {
199   return (h1.end_ - h2.end_ ).sign ();
200 }