2 Sequential_iterator.cc -- implement Sequential_iterator
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2004 Han-Wen Nienhuys <hanwen@cs.uu.nl>
9 #include "translator-group.hh"
11 #include "sequential-iterator.hh"
12 #include "music-list.hh"
14 Grace_fixup *copy_grace_fixups (Grace_fixup* src);
15 Grace_fixup *get_grace_fixups (SCM cursor);
19 TODO: handling of grace notes is exquisite pain. This handling
20 should be formally specified and then the implementation verified.
25 Invariant for the data structure.
28 if (gh_pair_p (cursor_))
29 iter_->music_ == unsmob_music (ly_car (cursor_))
33 The length of musiclist from start to up to cursor_ (cursor_ not
36 here_mom_ = sum (length (musiclist [start ... cursor>)) %)
39 Sequential_iterator::Sequential_iterator ()
41 here_mom_ = Moment (0);
49 Sequential_iterator::get_music_list () const
55 Sequential_iterator::do_quit ()
65 Sequential_iterator::derived_mark ()const
68 scm_gc_mark (iter_->self_scm ());
70 scm_gc_mark (cursor_);
75 Sequential_iterator::derived_substitute (Context *f,Context *t)
78 iter_->substitute_outlet (f,t);
83 get_grace_fixups (SCM cursor)
87 Grace_fixup *head = 0;
88 Grace_fixup **tail = &head;
90 for (; gh_pair_p (cursor); cursor = ly_cdr (cursor))
92 Music * mus = unsmob_music (ly_car (cursor));
93 Moment s = mus->start_mom ();
94 Moment l =mus->get_length () - s;
98 if (last != Moment (-1))
100 Grace_fixup *p =new Grace_fixup;
102 p->length_ = here - last;
103 p->grace_start_ = s.grace_part_;
106 tail = &(*tail)->next_;
109 here.grace_part_ = s.grace_part_;
122 copy_grace_fixups (Grace_fixup* src)
124 Grace_fixup * head = 0;
125 Grace_fixup **dest = &head;
129 *dest = new Grace_fixup (*src);
130 dest = & (*dest)->next_;
138 Sequential_iterator::construct_children ()
140 list_ = get_music_list ();
144 if (gh_pair_p (cursor_))
146 Music *m =unsmob_music (ly_car (cursor_));
147 iter_ = unsmob_iterator (get_iterator (m));
150 while (iter_ && !iter_->ok ())
155 here_mom_ = get_music ()->start_mom ();
156 grace_fixups_ = get_grace_fixups (cursor_);
159 iter_->ok () is tautology, but what the heck.
161 if (iter_ && iter_->ok ())
167 maintain invariants: change cursor, iter and here_mom_ in one fell
171 Sequential_iterator::next_element (bool)
173 Moment len =iter_->music_get_length () - iter_->music_start_mom ();
174 assert (!grace_fixups_ || grace_fixups_->start_ >= here_mom_);
176 if (len.main_part_ && grace_fixups_ &&
177 grace_fixups_->start_ == here_mom_)
179 here_mom_ += grace_fixups_->length_;
180 here_mom_.grace_part_ += grace_fixups_->grace_start_;
182 Grace_fixup * n =grace_fixups_->next_;
183 delete grace_fixups_;
186 else if (len.grace_part_ && !len.main_part_)
188 here_mom_.grace_part_ =0;
193 !len.grace_part_ || len.main_part_
195 We skip over a big chunk (mainpart != 0). Any starting graces
196 in that chunk should be in len.grace_part_
202 cursor_ = ly_cdr (cursor_);
205 if (gh_pair_p (cursor_))
206 iter_ = unsmob_iterator (get_iterator (unsmob_music (ly_car (cursor_))));
212 move to context of child iterator if it is deeper down in the
216 Sequential_iterator::descend_to_child ()
218 Context * child_report = child_report = iter_->get_outlet ();
219 Context * me_report = get_outlet ();
221 Context * c = child_report;
222 while (c && c != me_report)
224 c= c->daddy_context_;
228 set_translator (child_report);
234 Sequential_iterator::process (Moment until)
239 grace_fixups_->start_ == here_mom_
240 && (grace_fixups_->start_ + grace_fixups_->length_
241 + Moment (Rational (0), grace_fixups_->grace_start_) == until))
244 do the stuff/note/rest preceding a grace.
246 iter_->process (iter_->music_get_length ());
250 Moment w = until - here_mom_ + iter_->music_start_mom ();
251 // if (w >= Moment (0))
256 if the iter is still OK, there must be events left that have
270 Sequential_iterator::pending_moment () const
272 Moment cp = iter_->pending_moment ();
275 Fix-up a grace note halfway in the music.
277 if (grace_fixups_ && here_mom_ == grace_fixups_->start_
278 && grace_fixups_->length_ + iter_->music_start_mom () == cp)
280 return here_mom_ + grace_fixups_->length_ + Moment (0, grace_fixups_->grace_start_);
284 Fix-up a grace note at the start of the music.
286 return cp + here_mom_ - iter_->music_start_mom ();
291 Sequential_iterator::ok () const
297 Sequential_iterator::try_music_in_children (Music *m) const
299 return iter_ ? iter_->try_music (m) : 0;
302 IMPLEMENT_CTOR_CALLBACK (Sequential_iterator);
305 Sequential_iterator::run_always () const
307 return iter_ ? iter_->run_always () : false;