]> git.donarmstrong.com Git - lilypond.git/blob - lily/tie-engraver.cc
patch::: 1.1.26.jcn2: fixjes
[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 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       now_heads_.push (CHead_melodic_tuple (nh, m, now_moment()+ m->duration ()));
40     }
41 }
42
43 void
44 Tie_engraver::do_process_requests ()
45 {
46   if (req_l_)
47     {
48       Moment now = now_moment ();
49       Link_array<Note_head> nharr;
50       
51       stopped_heads_.clear ();
52       while (past_notes_pq_.size ()
53              && past_notes_pq_.front ().end_ == now)
54         stopped_heads_.push (past_notes_pq_.get ());
55
56     }
57 }
58
59 void
60 Tie_engraver::process_acknowledged ()
61 {
62   if (req_l_)
63     {
64       if (now_heads_.size () != stopped_heads_.size ())
65         {
66           req_l_->warning ("Unequal number of note heads for tie");
67         }
68       int sz = now_heads_.size () <? stopped_heads_.size ();
69
70       // hmm. Should do something more sensible.
71       // because, we assume no more noteheads come along after the 1st pass.
72       if (sz > tie_p_arr_.size ())
73         {
74           now_heads_.sort (CHead_melodic_tuple::pitch_compare);
75           stopped_heads_.sort(CHead_melodic_tuple::pitch_compare);
76
77           for (int i=0; i < sz; i++)
78             {
79               Tie * p = new Tie;
80               p->set_head (LEFT, stopped_heads_[i].head_l_);
81               p->set_head (RIGHT, now_heads_[i].head_l_);
82               tie_p_arr_.push (p);
83               announce_element (Score_element_info (p, req_l_));
84             }
85         }
86     }
87 }
88
89 void
90 Tie_engraver::do_pre_move_processing ()
91 {
92   for (int i=0; i < now_heads_.size (); i++)
93     {
94       past_notes_pq_.insert (now_heads_[i]);
95     }
96   now_heads_.clear ();
97
98   Scalar dir (get_property ("tieydirection", 0));
99   Scalar dir2 (get_property ("ydirection", 0));
100
101   Direction tie_dir = CENTER;
102   if (dir.length_i () && dir.isnum_b ())
103     tie_dir = (Direction) sign (int(dir));
104   else if (dir2.length_i () && dir2.isnum_b ())
105     tie_dir = (Direction) sign (int (dir2));
106   
107   for (int i=0; i<  tie_p_arr_.size (); i++)
108    {
109       tie_p_arr_[i]->dir_ = tie_dir;
110       typeset_element (tie_p_arr_[i]);
111     }
112   tie_p_arr_.clear ();
113 }
114
115 void
116 Tie_engraver::do_post_move_processing ()
117 {
118   req_l_ =0;
119   Moment now = now_moment ();
120   while (past_notes_pq_.size () && past_notes_pq_.front ().end_ < now)
121     past_notes_pq_.delmin ();
122 }
123
124 ADD_THIS_TRANSLATOR(Tie_engraver);
125
126
127 CHead_melodic_tuple::CHead_melodic_tuple ()
128 {
129   head_l_ =0;
130   mel_l_ =0;
131   end_ = 0;
132 }
133
134 CHead_melodic_tuple::CHead_melodic_tuple (Note_head *h, Melodic_req*m, Moment mom)
135 {
136   head_l_ = h;
137   mel_l_ = m;
138   end_ = mom;
139 }
140
141 int
142 CHead_melodic_tuple::pitch_compare (CHead_melodic_tuple const&h1,
143                              CHead_melodic_tuple const &h2)
144 {
145   return Melodic_req::compare (*h1.mel_l_, *h2.mel_l_);
146 }
147
148 int
149 CHead_melodic_tuple::time_compare (CHead_melodic_tuple const&h1,
150                              CHead_melodic_tuple const &h2)
151 {
152   return (h1.end_ - h2.end_ ).sign ();
153 }