]> git.donarmstrong.com Git - lilypond.git/blob - lily/tuplet-engraver.cc
*** empty log message ***
[lilypond.git] / lily / tuplet-engraver.cc
1 /*
2   tuplet-engraver.cc -- implement Tuplet_engraver
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1998--2005 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8
9 #include "tuplet-bracket.hh"
10 #include "note-column.hh"
11 #include "beam.hh"
12 #include "engraver.hh"
13 #include "spanner.hh"
14
15 #include "translator.icc"
16
17 struct Tuplet_description
18 {
19   Music *music_;
20   Rational stop_;
21   Rational span_stop_;
22   Spanner *spanner_;
23   Tuplet_description ()
24   {
25     music_ = 0;
26     spanner_ = 0;
27   }
28 };
29
30 class Tuplet_engraver : public Engraver
31 {
32 public:
33   TRANSLATOR_DECLARATIONS (Tuplet_engraver);
34
35 protected:
36   Array<Tuplet_description> tuplets_;
37
38   DECLARE_ACKNOWLEDGER(note_column);
39   virtual bool try_music (Music *r);
40   PRECOMPUTED_VIRTUAL void start_translation_timestep ();
41   PRECOMPUTED_VIRTUAL void process_music ();
42 };
43
44 bool
45 Tuplet_engraver::try_music (Music *music)
46 {
47   if (music->is_mus_type ("time-scaled-music"))
48     {
49       Music *el = unsmob_music (music->get_property ("element"));
50       if (el && !el->is_mus_type ("event-chord"))
51         {
52           Tuplet_description d;
53           d.music_ = music;
54           d.stop_ = now_mom ().main_part_ + music->get_length ().main_part_;
55           d.span_stop_ = d.stop_;
56
57           SCM s = get_property ("tupletSpannerDuration");
58           if (unsmob_moment (s))
59             d.span_stop_ = min (d.span_stop_, (now_mom () + *unsmob_moment (s)).main_part_);
60
61           tuplets_.push (d);
62         }
63       return true;
64     }
65   return false;
66 }
67
68 void
69 Tuplet_engraver::process_music ()
70 {
71   for (int i = 0; i < tuplets_.size (); i++)
72     {
73       if (tuplets_[i].spanner_)
74         continue;
75
76       Spanner *spanner = make_spanner ("TupletBracket",
77                                        tuplets_[i].music_->self_scm ());
78       tuplets_[i].spanner_ = spanner;
79
80       SCM proc = get_property ("tupletNumberFormatFunction");
81       if (ly_is_procedure (proc))
82         {
83           SCM t = scm_apply_0 (proc, scm_list_1 (tuplets_[i].music_->self_scm ()));
84           spanner->set_property ("text", t);
85         }
86     }
87 }
88
89 void
90 Tuplet_engraver::acknowledge_note_column (Grob_info i)
91 {
92   for (int j = 0; j < tuplets_.size (); j++)
93     if (tuplets_[j].spanner_)
94       Tuplet_bracket::add_column (tuplets_[j].spanner_,
95                                   dynamic_cast<Item *> (i.grob ()));
96 }
97
98 void
99 Tuplet_engraver::start_translation_timestep ()
100 {
101   Moment now = now_mom ();
102
103   Moment tsd;
104   SCM s = get_property ("tupletSpannerDuration");
105   if (unsmob_moment (s))
106     tsd = unsmob_moment (s)->main_part_;
107
108   for (int i = tuplets_.size (); i--;)
109     {
110       if (now.main_part_ >= tuplets_[i].span_stop_)
111         {
112           if (Spanner *sp = tuplets_[i].spanner_)
113             {
114               if (!sp->get_bound (RIGHT))
115                 sp->set_bound (RIGHT, sp->get_bound (LEFT));
116
117               tuplets_[i].spanner_ = 0;
118             }
119
120           if (tsd.to_bool ())
121             tuplets_[i].span_stop_ += tsd.main_part_;
122         }
123
124       if (now.main_part_ >= tuplets_[i].stop_)
125         {
126           tuplets_.del (i);
127         }
128     }
129 }
130
131 Tuplet_engraver::Tuplet_engraver ()
132 {
133 }
134 ADD_ACKNOWLEDGER(Tuplet_engraver,note_column);
135 ADD_TRANSLATOR (Tuplet_engraver,
136                 /* descr */ "Catch Time_scaled_music and generate appropriate bracket  ",
137                 /* creats*/ "TupletBracket",
138                 /* accepts */ "time-scaled-music",
139                 /* acks  */ "",
140                 /* reads */ "tupletNumberFormatFunction tupletSpannerDuration",
141                 /* write */ "");