]> git.donarmstrong.com Git - lilypond.git/blob - lily/tuplet-engraver.cc
* lily/include/translator.icc: new file.
[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   virtual void acknowledge_grob (Grob_info);
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_grob (Grob_info i)
91 {
92   if (Note_column::has_interface (i.grob ()))
93     {
94       for (int j = 0; j < tuplets_.size (); j++)
95         if (tuplets_[j].spanner_)
96           Tuplet_bracket::add_column (tuplets_[j].spanner_,
97                                       dynamic_cast<Item *> (i.grob ()));
98     }
99 }
100
101 void
102 Tuplet_engraver::start_translation_timestep ()
103 {
104   Moment now = now_mom ();
105
106   Moment tsd;
107   SCM s = get_property ("tupletSpannerDuration");
108   if (unsmob_moment (s))
109     tsd = unsmob_moment (s)->main_part_;
110
111   for (int i = tuplets_.size (); i--;)
112     {
113       if (now.main_part_ >= tuplets_[i].span_stop_)
114         {
115           if (Spanner *sp = tuplets_[i].spanner_)
116             {
117               if (!sp->get_bound (RIGHT))
118                 sp->set_bound (RIGHT, sp->get_bound (LEFT));
119
120               tuplets_[i].spanner_ = 0;
121             }
122
123           if (tsd.to_bool ())
124             tuplets_[i].span_stop_ += tsd.main_part_;
125         }
126
127       if (now.main_part_ >= tuplets_[i].stop_)
128         {
129           tuplets_.del (i);
130         }
131     }
132 }
133
134 Tuplet_engraver::Tuplet_engraver ()
135 {
136 }
137
138 ADD_TRANSLATOR (Tuplet_engraver,
139                 /* descr */ "Catch Time_scaled_music and generate appropriate bracket  ",
140                 /* creats*/ "TupletBracket",
141                 /* accepts */ "time-scaled-music",
142                 /* acks  */ "note-column-interface",
143                 /* reads */ "tupletNumberFormatFunction tupletSpannerDuration",
144                 /* write */ "");