]> git.donarmstrong.com Git - lilypond.git/blob - lily/tuplet-engraver.cc
bugfix.
[lilypond.git] / lily / tuplet-engraver.cc
1 /*   
2   plet-engraver.cc --  implement Tuplet_engraver
3   
4   source file of the GNU LilyPond music typesetter
5   
6   (c) 1998--2004 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7   
8  */
9
10
11
12 #include "tuplet-bracket.hh"
13 #include "note-column.hh"
14 #include "time-scaled-music.hh"
15 #include "beam.hh"
16 #include "music-list.hh"
17 #include "engraver.hh"
18 #include "spanner.hh"
19
20 class Tuplet_engraver : public Engraver
21 {
22 public:
23   TRANSLATOR_DECLARATIONS (Tuplet_engraver);
24
25 protected:
26   Link_array<Music> time_scaled_musics_;
27   /// when does the scaled music stop? Array order is synced with time_scaled_musics_
28   Array<Rational> stop_moments_;
29   /// when does the current spanner stop? Array order is synced with time_scaled_musics_
30   Array<Rational> span_stop_moments_;
31   
32   /// The spanners. Array order is synced with time_scaled_musics_
33   Link_array<Spanner> started_spanners_;
34
35   virtual void acknowledge_grob (Grob_info);
36   virtual bool try_music (Music*r);
37   virtual void start_translation_timestep ();
38   virtual void process_acknowledged_grobs ();
39 };
40
41 bool
42 Tuplet_engraver::try_music (Music *c)
43 {
44   if (c->is_mus_type ("time-scaled-music"))
45     {
46       Music *el = unsmob_music (c->get_property ("element"));
47       if (el && !el->is_mus_type ("event-chord"))
48         {
49           time_scaled_musics_.push (c);
50           Rational m = now_mom ().main_part_ + c->get_length ().main_part_;
51           stop_moments_.push (m);
52
53           SCM s = get_property ("tupletSpannerDuration");
54           if (unsmob_moment (s))
55             m = m <? (now_mom () + *unsmob_moment (s)).main_part_;
56           
57           span_stop_moments_.push (m);
58         }
59       return true;
60     }
61   return false;
62 }
63
64 void
65 Tuplet_engraver::process_acknowledged_grobs ()
66 {
67   for (int i= 0; i < time_scaled_musics_.size (); i++)
68     {
69       if (i < started_spanners_.size () && started_spanners_[i])
70         continue;
71
72       Spanner* glep = make_spanner ("TupletBracket", time_scaled_musics_ [i]->self_scm ());
73
74       if (i >= started_spanners_.size ())
75         started_spanners_.push (glep);
76       else
77         started_spanners_[i] = glep;
78       
79
80       SCM proc = get_property ("tupletNumberFormatFunction");
81       if (ly_c_procedure_p (proc))
82         {
83           SCM t = scm_apply_0 (proc, scm_list_n (time_scaled_musics_[i]->self_scm (), SCM_UNDEFINED));
84           glep->set_property ("text", t);
85         }
86       
87     }
88 }
89
90 void
91 Tuplet_engraver::acknowledge_grob (Grob_info i)
92 {
93   if (Note_column::has_interface (i.grob_))
94     {
95       for (int j =0; j < started_spanners_.size (); j++)
96         if (started_spanners_[j]) 
97           Tuplet_bracket::add_column (started_spanners_[j], 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= started_spanners_.size (); i--;)
112     {
113       if (now.main_part_ >= span_stop_moments_[i])
114         {
115           if (Spanner *sp = started_spanners_[i])
116             {
117               if (!sp->get_bound (RIGHT))
118                 sp->set_bound (RIGHT, sp->get_bound (LEFT));
119               
120               started_spanners_[i] =0;
121             }
122           
123           if (tsd.to_bool ())
124             span_stop_moments_[i] += tsd.main_part_;
125         }
126
127       if (now.main_part_ >= stop_moments_[i])
128         {
129           started_spanners_.del (i);
130           stop_moments_.del (i);
131           span_stop_moments_.del (i);
132           time_scaled_musics_.del (i);
133         }
134     }
135 }
136
137 Tuplet_engraver::Tuplet_engraver ()
138 {
139 }
140
141 ENTER_DESCRIPTION (Tuplet_engraver,
142 /* descr */       "Catch Time_scaled_music and generate appropriate bracket  ",
143 /* creats*/       "TupletBracket",
144 /* accepts */     "time-scaled-music",
145 /* acks  */      "note-column-interface",
146 /* reads */       "tupletNumberFormatFunction tupletSpannerDuration",
147 /* write */       "");