]> git.donarmstrong.com Git - lilypond.git/blob - lily/measure-grouping-engraver.cc
Merge branch 'lilypond/translation' of ssh://git.sv.gnu.org/srv/git/lilypond
[lilypond.git] / lily / measure-grouping-engraver.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2002--2010 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 "warn.hh"
21 #include "side-position-interface.hh"
22 #include "global-context.hh"
23 #include "engraver.hh"
24 #include "spanner.hh"
25 #include "beam-settings.hh"
26
27 #include "translator.icc"
28
29 class Measure_grouping_engraver : public Engraver
30 {
31 public:
32   TRANSLATOR_DECLARATIONS (Measure_grouping_engraver);
33
34 protected:
35   Spanner *grouping_;
36   Rational stop_grouping_mom_;
37
38   void process_music ();
39   virtual void finalize ();
40   DECLARE_ACKNOWLEDGER (note_column);
41 };
42
43 void
44 Measure_grouping_engraver::finalize ()
45 {
46   if (grouping_)
47     {
48       grouping_->set_bound (RIGHT, unsmob_grob (get_property ("currentCommandColumn")));
49       grouping_->suicide ();
50       grouping_ = 0;
51     }
52 }
53
54 void
55 Measure_grouping_engraver::acknowledge_note_column (Grob_info gi)
56 {
57   if (grouping_)
58     Side_position_interface::add_support (grouping_, gi.grob ());
59 }
60
61 void
62 Measure_grouping_engraver::process_music ()
63 {
64   Moment now = now_mom ();
65   if (grouping_ && now.main_part_ >= stop_grouping_mom_ && !now.grace_part_)
66     {
67       grouping_->set_bound (RIGHT,
68                             unsmob_grob (get_property ("currentMusicalColumn")));
69
70       grouping_ = 0;
71     }
72
73   if (now.grace_part_)
74     return;
75
76   SCM settings = get_property ("beamSettings");
77   SCM grouping = SCM_EOL;
78   if (scm_is_pair (settings))
79     {
80       SCM time_signature_fraction = get_property ("timeSignatureFraction");
81       grouping = ly_beam_grouping (settings,
82                                    time_signature_fraction,
83                                    ly_symbol2scm ("end"),
84                                    ly_symbol2scm ("*"));
85     }
86   if (scm_is_pair (grouping))
87     {
88       Moment *measpos = unsmob_moment (get_property ("measurePosition"));
89       Rational mp = measpos->main_part_;
90
91       Moment *beatlen_mom = unsmob_moment (get_property ("beatLength"));
92       Rational beat_length = beatlen_mom->main_part_;
93
94       Rational where (0);
95       for (SCM s = grouping; scm_is_pair (s);
96            where += Rational ((int) scm_to_int (scm_car (s))) * beat_length,
97              s = scm_cdr (s))
98         {
99           int grouplen = scm_to_int (scm_car (s));
100           if (where == mp)
101             {
102               if (grouping_)
103                 {
104                   programming_error ("last grouping not finished yet");
105                   continue;
106                 }
107               if (grouplen > 1)
108                {
109                  grouping_ = make_spanner ("MeasureGrouping", SCM_EOL);
110                  grouping_->set_bound (LEFT, unsmob_grob (get_property ("currentMusicalColumn")));
111
112                  stop_grouping_mom_ = now.main_part_ + Rational (grouplen - 1) * beat_length;
113                  get_global_context ()->add_moment_to_process (Moment (stop_grouping_mom_));
114
115                  if (grouplen == 3)
116                    grouping_->set_property ("style", ly_symbol2scm ("triangle"));
117                  else
118                    grouping_->set_property ("style", ly_symbol2scm ("bracket"));
119
120                  break;
121                }
122             }
123         }
124     }
125 }
126
127 Measure_grouping_engraver::Measure_grouping_engraver ()
128 {
129   grouping_ = 0;
130 }
131
132 ADD_ACKNOWLEDGER (Measure_grouping_engraver, note_column);
133 ADD_TRANSLATOR (Measure_grouping_engraver,
134                 /* doc */
135                 "Create @code{MeasureGrouping} to indicate beat subdivision.",
136
137                 /* create */
138                 "MeasureGrouping ",
139
140                 /* read */
141                 "beatLength "
142                 "currentMusicalColumn "
143                 "measurePosition "
144                 "beamSettings ",
145
146                 /* write */
147                 ""
148                 );