]> git.donarmstrong.com Git - lilypond.git/blob - lily/tuplet-engraver.cc
* lily/include/lily-guile.hh: is_x -> ly_c_X_p naming.
[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 finalize ();
36   virtual void acknowledge_grob (Grob_info);
37   virtual bool try_music (Music*r);
38   virtual void start_translation_timestep ();
39   virtual void process_acknowledged_grobs ();
40 };
41
42 bool
43 Tuplet_engraver::try_music (Music *c)
44 {
45   if (c->is_mus_type ("time-scaled-music"))
46     {
47       Music *el = unsmob_music (c->get_property ("element"));
48       if (el && !el->is_mus_type ("event-chord"))
49         {
50           time_scaled_musics_.push (c);
51           Rational m = now_mom ().main_part_ + c->get_length ().main_part_;
52           stop_moments_.push (m);
53
54           SCM s = get_property ("tupletSpannerDuration");
55           if (unsmob_moment (s))
56             m = m <? (now_mom () + *unsmob_moment (s)).main_part_;
57           
58           span_stop_moments_.push (m);
59         }
60       return true;
61     }
62   return false;
63 }
64
65 void
66 Tuplet_engraver::process_acknowledged_grobs ()
67 {
68   for (int i= 0; i < time_scaled_musics_.size (); i++)
69     {
70       if (i < started_spanners_.size () && started_spanners_[i])
71         continue;
72
73       Spanner* glep = make_spanner ("TupletBracket");
74
75       if (i >= started_spanners_.size ())
76         started_spanners_.push (glep);
77       else
78         started_spanners_[i] = glep;
79       
80
81       SCM proc = get_property ("tupletNumberFormatFunction");
82       if (ly_c_procedure_p (proc))
83         {
84           SCM t = scm_apply_0 (proc, scm_list_n (time_scaled_musics_[i]->self_scm (), SCM_UNDEFINED));
85           glep->set_property ("text", t);
86         }
87       
88       announce_grob (glep, time_scaled_musics_ [i]->self_scm ());
89     }
90 }
91
92 void
93 Tuplet_engraver::acknowledge_grob (Grob_info i)
94 {
95   if (Note_column::has_interface (i.grob_))
96     {
97       for (int j =0; j  <started_spanners_.size (); j++)
98         if (started_spanners_[j]) 
99           Tuplet_bracket::add_column (started_spanners_[j], dynamic_cast<Item*> (i.grob_));
100     }
101 }
102
103 void
104 Tuplet_engraver::start_translation_timestep ()
105 {
106   Moment now = now_mom ();
107
108   Moment tsd;
109   SCM s = get_property ("tupletSpannerDuration");
110   if (unsmob_moment (s))
111     tsd = unsmob_moment (s)->main_part_;
112
113   for (int i= started_spanners_.size (); i--;)
114     {
115       if (now.main_part_ >= span_stop_moments_[i])
116         {
117           if (Spanner *sp = started_spanners_[i])
118             {
119               if (!sp->get_bound (RIGHT))
120                 sp->set_bound (RIGHT, sp->get_bound (LEFT));
121               
122               typeset_grob (sp);
123
124               started_spanners_[i] =0;
125             }
126           
127           if (tsd.to_bool ())
128             span_stop_moments_[i] += tsd.main_part_;
129         }
130
131       if (now.main_part_ >= stop_moments_[i])
132         {
133           started_spanners_.del (i);
134           stop_moments_.del (i);
135           span_stop_moments_.del (i);
136           time_scaled_musics_.del (i);
137         }
138     }
139 }
140
141 void
142 Tuplet_engraver::finalize ()
143 {
144   for (int i=0; i < started_spanners_.size (); i++)
145     {
146       if (started_spanners_[i])
147         typeset_grob (started_spanners_[i]);
148     }  
149 }
150
151
152
153 Tuplet_engraver::Tuplet_engraver (){}
154
155 ENTER_DESCRIPTION (Tuplet_engraver,
156 /* descr */       "Catch Time_scaled_music and generate appropriate bracket  ",
157 /* creats*/       "TupletBracket",
158 /* accepts */     "time-scaled-music",
159 /* acks  */      "note-column-interface",
160 /* reads */       "tupletNumberFormatFunction tupletSpannerDuration",
161 /* write */       "");