]> git.donarmstrong.com Git - lilypond.git/blob - lily/time-signature-engraver.cc
Issue 4151: implicitTimeSignatureVisibility->initialTimeSignatureVisibility
[lilypond.git] / lily / time-signature-engraver.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1997--2014 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 "engraver-group.hh"
21
22 #include "item.hh"
23 #include "international.hh"
24 #include "misc.hh"
25 #include "moment.hh"
26 #include "stream-event.hh"
27 #include "time-signature.hh"
28 #include "warn.hh"
29
30 #include "translator.icc"
31
32 /**
33    generate time_signatures.
34 */
35 class Time_signature_engraver : public Engraver
36 {
37   Item *time_signature_;
38   SCM last_time_fraction_;
39   SCM time_cause_;
40
41 protected:
42   virtual void derived_mark () const;
43   void stop_translation_timestep ();
44   void process_music ();
45 public:
46   TRANSLATOR_DECLARATIONS (Time_signature_engraver);
47   DECLARE_TRANSLATOR_LISTENER (time_signature);
48 };
49
50 void
51 Time_signature_engraver::derived_mark () const
52 {
53   scm_gc_mark (last_time_fraction_);
54   scm_gc_mark (time_cause_);
55 }
56
57 Time_signature_engraver::Time_signature_engraver ()
58 {
59   time_signature_ = 0;
60   time_cause_ = SCM_EOL;
61   last_time_fraction_ = SCM_BOOL_F;
62 }
63
64 IMPLEMENT_TRANSLATOR_LISTENER (Time_signature_engraver, time_signature);
65 void
66 Time_signature_engraver::listen_time_signature (Stream_event *ev)
67 {
68   time_cause_ = ev->self_scm ();
69 }
70
71 void
72 Time_signature_engraver::process_music ()
73 {
74   if (time_signature_)
75     return;
76
77   SCM fr = get_property ("timeSignatureFraction");
78   if (last_time_fraction_ != fr
79       && scm_is_pair (fr))
80     {
81       time_signature_ = make_item ("TimeSignature", time_cause_);
82       time_signature_->set_property ("fraction", fr);
83
84       if (last_time_fraction_ == SCM_BOOL_F)
85         time_signature_->set_property ("break-visibility",
86                                        get_property ("initialTimeSignatureVisibility"));
87
88       int den = scm_to_int (scm_cdr (fr));
89       if (den != (1 << intlog2 (den)))
90         {
91           /*
92             Todo: should make typecheck?
93
94             OTOH, Tristan Keuris writes 8/20 in his Intermezzi.
95           */
96           time_signature_->warning (_f ("strange time signature found: %d/%d",
97                                         int (scm_to_int (scm_car (fr))),
98                                         den));
99         }
100
101       last_time_fraction_ = fr;
102     }
103 }
104
105 void
106 Time_signature_engraver::stop_translation_timestep ()
107 {
108   if (time_signature_ && (time_cause_ != SCM_EOL))
109     {
110       Moment *mp = Moment::unsmob (get_property ("measurePosition"));
111       if (mp && (mp->main_part_ > Rational (0))
112           && !to_boolean (get_property ("partialBusy")))
113         time_signature_->warning ("mid-measure time signature without \\partial");
114     }
115
116   time_signature_ = 0;
117   time_cause_ = SCM_EOL;
118 }
119
120 #include "translator.icc"
121
122 ADD_TRANSLATOR (Time_signature_engraver,
123                 /* doc */
124                 "Create a @ref{TimeSignature} whenever"
125                 " @code{timeSignatureFraction} changes.",
126
127                 /* create */
128                 "TimeSignature ",
129
130                 /* read */
131                 "initialTimeSignatureVisibility "
132                 "partialBusy "
133                 "timeSignatureFraction ",
134
135                 /* write */
136                 ""
137                );