]> git.donarmstrong.com Git - lilypond.git/blob - lily/partial-iterator.cc
Web-ja: update introduction
[lilypond.git] / lily / partial-iterator.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2010--2015 Neil Puttock <n.puttock@gmail.com>
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 "context.hh"
21 #include "global-context.hh"
22 #include "input.hh"
23 #include "international.hh"
24 #include "moment.hh"
25 #include "music.hh"
26 #include "simple-music-iterator.hh"
27 #include "lily-imports.hh"
28
29 class Partial_iterator : public Simple_music_iterator
30 {
31 public:
32   DECLARE_SCHEME_CALLBACK (constructor, ());
33   DECLARE_SCHEME_CALLBACK (finalization, (SCM, SCM));
34 protected:
35   virtual void process (Moment);
36 };
37
38 void
39 Partial_iterator::process (Moment m)
40 {
41   if (Duration * dur
42       = unsmob<Duration> (get_music ()->get_property ("duration")))
43     {
44       Moment length = Moment (dur->get_length ());
45
46       // Partial_iterator is an iterator rather than an engraver,
47       // so the active context it is getting called in does not
48       // depend on which context definition the engraver might be
49       // defined.
50       //
51       // Using where_defined to find the context where
52       // measurePosition should be overwritten does not actually
53       // work since the Timing_translator does not set
54       // measurePosition when initializing.
55
56       Context *timing = unsmob<Context>
57         (Lily::ly_context_find (get_outlet ()->self_scm (),
58                                 ly_symbol2scm ("Timing")));
59
60       if (!timing)
61         programming_error ("missing Timing in \\partial");
62       else if (get_outlet ()->now_mom () > 0)
63         {
64           timing->set_property ("partialBusy", ly_bool2scm (true));
65           Global_context *tg = get_outlet ()->get_global_context ();
66           tg->add_finalization (scm_list_3 (finalization_proc,
67                                             get_outlet ()->self_scm (),
68                                             length.smobbed_copy ()));
69         }
70       else
71         {
72           Moment mp = robust_scm2moment
73                       (timing->get_property ("measurePosition"),
74                        Rational (0));
75           mp.main_part_ = 0;
76           timing->set_property
77           ("measurePosition", (mp - length).smobbed_copy ());
78         }
79     }
80   else
81     programming_error ("invalid duration in \\partial");
82
83   Simple_music_iterator::process (m);
84 }
85
86 IMPLEMENT_CTOR_CALLBACK (Partial_iterator);
87
88 MAKE_SCHEME_CALLBACK (Partial_iterator, finalization, 2);
89 SCM
90 Partial_iterator::finalization (SCM ctx, SCM length)
91 {
92   LY_ASSERT_SMOB (Context, ctx, 1);
93   LY_ASSERT_SMOB (Moment, length, 2);
94   Context *timing = unsmob<Context>
95     (Lily::ly_context_find (ctx, ly_symbol2scm ("Timing")));
96   if (!timing) {
97     programming_error ("missing Timing in \\partial");
98     return SCM_UNSPECIFIED;
99   }
100   Moment mp = robust_scm2moment (timing->get_property ("measurePosition"),
101                                  Rational (0));
102   mp.main_part_ = measure_length (timing);
103   timing->set_property ("measurePosition",
104                         (mp - *unsmob<Moment> (length)).smobbed_copy ());
105   timing->unset_property (ly_symbol2scm ("partialBusy"));
106
107   return SCM_UNSPECIFIED;
108 }