]> git.donarmstrong.com Git - lilypond.git/blob - lily/tie-engraver.cc
release: 1.3.0
[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 #include "translator-group.hh"
16
17 Tie_engraver::Tie_engraver()
18 {
19   req_l_ = 0;
20 }
21
22
23 bool
24 Tie_engraver::do_try_music (Music *m)
25 {
26   if (Tie_req * c = dynamic_cast<Tie_req*> (m))
27     {
28       req_l_ = c;
29       SCM m = get_property ("automaticMelismata",0);
30       bool am = gh_boolean_p (m) &&gh_scm2bool (m);
31       if (am)
32         {
33           set_melisma (true);
34         }
35       return true;
36     }
37   return false;
38 }
39
40 void
41 Tie_engraver::set_melisma (bool m)
42 {
43   Translator_group *where = daddy_trans_l_;
44   get_property ("tieMelismaBusy", &where);
45   if (!where)
46     where = daddy_trans_l_;
47     
48   daddy_trans_l_->set_property ("tieMelismaBusy", m ? SCM_BOOL_T : SCM_BOOL_F);
49 }
50
51 void
52 Tie_engraver::acknowledge_element (Score_element_info i)
53 {
54   if (Note_head *nh = dynamic_cast<Note_head *> (i.elem_l_))
55     {
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 ()));
60     }
61 }
62
63 void
64 Tie_engraver::do_process_requests ()
65 {
66   if (req_l_)
67     {
68       Moment now = now_mom ();
69       Link_array<Note_head> nharr;
70       
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 ());
75     }
76 }
77
78 void
79 Tie_engraver::process_acknowledged ()
80 {
81   if (req_l_)
82     {
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 ())
89         {
90           int comp
91             = Musical_pitch::compare (now_heads_[i].req_l_->pitch_ ,
92                                       stopped_heads_[j].req_l_->pitch_);
93
94           if (comp)
95             {
96               (comp < 0) ? i ++ : j++;
97               continue;
98             }
99           else
100             {
101               tie_count ++;
102
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 ())
108                 {
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_));
114                 }
115               i++;
116               j++;
117
118             }
119         }
120
121       if (!tie_p_arr_.size ())
122         {
123           req_l_->warning (_ ("No ties were created!"));
124         }
125           
126     }
127 }
128
129
130 void
131 Tie_engraver::do_pre_move_processing ()
132 {
133   for (int i=0; i < now_heads_.size (); i++)
134     {
135       past_notes_pq_.insert (now_heads_[i]);
136     }
137   now_heads_.clear ();
138
139   SCM dir (get_property ("tieVerticalDirection", 0));
140   SCM dir2 (get_property ("verticalDirection", 0));
141
142   Direction tie_dir = CENTER;
143   if (SCM_NUMBERP(dir))
144     tie_dir = to_dir (dir);
145   else if (isdir_b (dir2))
146     tie_dir = to_dir (dir2);
147   
148   for (int i=0; i<  tie_p_arr_.size (); i++)
149    {
150       tie_p_arr_[i]->dir_ = tie_dir;
151       typeset_element (tie_p_arr_[i]);
152     }
153   tie_p_arr_.clear ();
154 }
155
156 void
157 Tie_engraver::do_post_move_processing ()
158 {
159   SCM m = get_property ("automaticMelismata",0);
160   if (gh_boolean_p (m) && gh_scm2bool (m))
161     {
162       set_melisma (false);
163     }
164   req_l_ = 0;
165   Moment now = now_mom ();
166   while (past_notes_pq_.size () && past_notes_pq_.front ().end_ < now)
167     past_notes_pq_.delmin ();
168 }
169
170 ADD_THIS_TRANSLATOR(Tie_engraver);
171
172
173 CHead_melodic_tuple::CHead_melodic_tuple ()
174 {
175   head_l_ =0;
176   req_l_ =0;
177   end_ = 0;
178 }
179
180 CHead_melodic_tuple::CHead_melodic_tuple (Note_head *h, Melodic_req*m, Moment mom)
181 {
182   head_l_ = h;
183   req_l_ = m;
184   end_ = mom;
185 }
186
187 int
188 CHead_melodic_tuple::pitch_compare (CHead_melodic_tuple const&h1,
189                              CHead_melodic_tuple const &h2)
190 {
191   return Melodic_req::compare (*h1.req_l_, *h2.req_l_);
192 }
193
194 int
195 CHead_melodic_tuple::time_compare (CHead_melodic_tuple const&h1,
196                              CHead_melodic_tuple const &h2)
197 {
198   return (h1.end_ - h2.end_ ).sign ();
199 }