]> git.donarmstrong.com Git - lilypond.git/blob - lily/measure-grouping-engraver.cc
* scm/page.scm (make-page): make it friendlier to call (esp. from C++)
[lilypond.git] / lily / measure-grouping-engraver.cc
1 /*
2   measure-grouping-engraver.cc -- implement Measure_grouping_engraver
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 2002--2006 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
8
9 #include "warn.hh"
10 #include "side-position-interface.hh"
11 #include "global-context.hh"
12 #include "engraver.hh"
13
14 #include "translator.icc"
15
16 class Measure_grouping_engraver : public Engraver
17 {
18 public:
19   TRANSLATOR_DECLARATIONS (Measure_grouping_engraver);
20
21 protected:
22   Spanner *grouping_;
23   Rational stop_grouping_mom_;
24
25   void process_music ();
26   virtual void finalize ();
27   DECLARE_ACKNOWLEDGER (note_column);
28 };
29
30 void
31 Measure_grouping_engraver::finalize ()
32 {
33   if (grouping_)
34     {
35       grouping_->set_bound (RIGHT, unsmob_grob (get_property ("currentCommandColumn")));
36       grouping_->suicide ();
37       grouping_ = 0;
38     }
39 }
40
41 void
42 Measure_grouping_engraver::acknowledge_note_column (Grob_info gi)
43 {
44   if (grouping_)
45     Side_position_interface::add_support (grouping_, gi.grob ());
46 }
47
48 void
49 Measure_grouping_engraver::process_music ()
50 {
51   Moment now = now_mom ();
52   if (grouping_ && now.main_part_ >= stop_grouping_mom_ && !now.grace_part_)
53     {
54       grouping_->set_bound (RIGHT,
55                             unsmob_grob (get_property ("currentMusicalColumn")));
56
57       grouping_ = 0;
58     }
59
60   if (now.grace_part_)
61     return;
62
63   SCM grouping = get_property ("beatGrouping");
64   if (scm_is_pair (grouping))
65     {
66       Moment *measpos = unsmob_moment (get_property ("measurePosition"));
67       Rational mp = measpos->main_part_;
68
69       Moment *beatlen_mom = unsmob_moment (get_property ("beatLength"));
70       Rational beat_length = beatlen_mom->main_part_;
71
72       Rational where (0);
73       for (SCM s = grouping; scm_is_pair (s);
74            where += Rational ((int) scm_to_int (scm_car (s))) * beat_length,
75              s = scm_cdr (s))
76         {
77           int grouplen = scm_to_int (scm_car (s));
78           if (where == mp)
79             {
80               if (grouping_)
81                 {
82                   programming_error ("last grouping not finished yet");
83                   continue;
84                 }
85
86               grouping_ = make_spanner ("MeasureGrouping", SCM_EOL);
87               grouping_->set_bound (LEFT, unsmob_grob (get_property ("currentMusicalColumn")));
88
89               stop_grouping_mom_ = now.main_part_ + Rational (grouplen - 1) * beat_length;
90               get_global_context ()->add_moment_to_process (Moment (stop_grouping_mom_));
91
92               if (grouplen == 3)
93                 grouping_->set_property ("style", ly_symbol2scm ("triangle"));
94               else
95                 grouping_->set_property ("style", ly_symbol2scm ("bracket"));
96
97               break;
98             }
99         }
100     }
101 }
102
103 Measure_grouping_engraver::Measure_grouping_engraver ()
104 {
105   grouping_ = 0;
106 }
107
108 ADD_ACKNOWLEDGER (Measure_grouping_engraver, note_column);
109 ADD_TRANSLATOR (Measure_grouping_engraver,
110                 /* doc */
111                 "Creates MeasureGrouping to indicate beat subdivision.",
112                 /* create */
113                 "MeasureGrouping",
114                 /* accept */
115                 "",
116                 /* read */
117                 "beatLength "
118                 "currentMusicalColumn "
119                 "measurePosition "
120                 "beatGrouping "
121                 ,
122                 /* write */
123                 "");