]> git.donarmstrong.com Git - lilypond.git/blob - lily/volta-repeat-iterator.cc
Break slurs between alternative endings in repeats; issue 1698.
[lilypond.git] / lily / volta-repeat-iterator.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2002--2012 Han-Wen Nienhuys <hanwen@xs4all.nl>
5
6   LilyPond is free software: you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation, either version 3 of the License, or
9   (at your option) any later version.
10
11   LilyPond is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15
16   You should have received a copy of the GNU General Public License
17   along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "music.hh"
21 #include "sequential-iterator.hh"
22 #include "context.hh"
23
24 class Volta_repeat_iterator : public Sequential_iterator
25 {
26 public:
27   DECLARE_SCHEME_CALLBACK (constructor, ());
28   Volta_repeat_iterator ();
29
30   void add_repeat_command (SCM);
31 protected:
32   virtual SCM get_music_list () const;
33   virtual void next_element (bool);
34   virtual void construct_children ();
35   virtual void process (Moment);
36
37   bool first_time_;
38   int alt_count_;
39   int rep_count_;
40   int done_count_;
41 };
42
43 Volta_repeat_iterator::Volta_repeat_iterator ()
44 {
45   done_count_ = alt_count_ = rep_count_ = 0;
46   first_time_ = true;
47 }
48
49 SCM
50 Volta_repeat_iterator::get_music_list ()const
51 {
52   return scm_cons (get_music ()->get_property ("element"),
53                    Sequential_iterator::get_music_list ());
54 }
55
56 void
57 Volta_repeat_iterator::construct_children ()
58 {
59   Sequential_iterator::construct_children ();
60
61   SCM alts = get_music ()->get_property ("elements");
62
63   alt_count_ = int (scm_ilength (alts));
64   rep_count_ = scm_to_int (get_music ()->get_property ("repeat-count"));
65   done_count_ = 0;
66 }
67
68 /*
69   TODO: add source information for debugging
70 */
71 void
72 Volta_repeat_iterator::add_repeat_command (SCM what)
73 {
74   SCM reps = ly_symbol2scm ("repeatCommands");
75   SCM current_reps = SCM_EOL;
76   Context *where = get_outlet ()->where_defined (reps, &current_reps);
77
78   if (where
79       && (current_reps == SCM_EOL || scm_is_pair (current_reps)))
80     {
81       current_reps = scm_cons (what, current_reps);
82       where->set_property (reps, current_reps);
83     }
84 }
85
86 void
87 Volta_repeat_iterator::next_element (bool side_effect)
88 {
89   done_count_++;
90   if (done_count_ > 1
91       && done_count_ <= alt_count_
92       && !to_boolean (get_outlet ()->get_property ("slurOverRepeat")))
93     {
94       SCM ev_scm = scm_call_1 (ly_lily_module_constant ("make-music"),
95                                ly_symbol2scm ("BreakSlurEvent"));
96       Music *ev = unsmob_music (ev_scm);
97       ev->send_to_context (get_outlet ());
98
99       ev_scm = scm_call_1 (ly_lily_module_constant ("make-music"),
100                            ly_symbol2scm ("BreakPhrasingSlurEvent"));
101       ev = unsmob_music (ev_scm);
102       ev->send_to_context (get_outlet ());
103     }
104
105   Sequential_iterator::next_element (side_effect);
106
107   if (side_effect)
108     {
109       if (alt_count_)
110         {
111           string repstr = to_string (rep_count_ - alt_count_ + done_count_) + ".";
112           if (done_count_ > 1)
113             {
114               add_repeat_command (scm_list_n (ly_symbol2scm ("volta"), SCM_BOOL_F, SCM_UNDEFINED));
115
116               if (done_count_ - 1 < alt_count_)
117                 add_repeat_command (ly_symbol2scm ("end-repeat"));
118             }
119
120           if (done_count_ == 1 && alt_count_ < rep_count_)
121             repstr = "1.--" + to_string (rep_count_ - alt_count_ + done_count_) + ".";
122
123           if (done_count_ <= alt_count_)
124             add_repeat_command (scm_list_n (ly_symbol2scm ("volta"),
125                                             ly_string2scm (repstr), SCM_UNDEFINED));
126         }
127       else
128         add_repeat_command (ly_symbol2scm ("end-repeat"));
129     }
130 }
131
132 void
133 Volta_repeat_iterator::process (Moment m)
134 {
135   if (first_time_)
136     {
137       add_repeat_command (ly_symbol2scm ("start-repeat"));
138       first_time_ = false;
139     }
140   Sequential_iterator::process (m);
141 }
142
143 IMPLEMENT_CTOR_CALLBACK (Volta_repeat_iterator);