]> git.donarmstrong.com Git - lilypond.git/blob - lily/tuplet-engraver.cc
release: 1.5.47
[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--2002 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7   
8  */
9
10
11 #include "command-request.hh"
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<Time_scaled_music> time_scaled_music_arr_;
27   /// when does the scaled music stop? Array order is synced with time_scaled_music_arr_
28   Array<Rational> stop_moments_;
29   /// when does the current spanner stop? Array order is synced with time_scaled_music_arr_
30   Array<Rational> span_stop_moments_;
31   
32   /// The spanners. Array order is synced with time_scaled_music_arr_
33   Link_array<Spanner> started_span_p_arr_;
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 create_grobs ();
40 };
41
42 bool
43 Tuplet_engraver::try_music (Music *r)
44 {
45   if (Time_scaled_music * c = dynamic_cast<Time_scaled_music *> (r))
46     {
47       Music *el = c->element ();
48       if (!dynamic_cast<Request_chord*> (el))
49         {
50           time_scaled_music_arr_.push (c);
51           Rational m = now_mom ().main_part_ + c->length_mom ().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::create_grobs ()
67 {
68   SCM v = get_property ("tupletInvisible");
69   if (to_boolean (v))
70     return;
71
72   for (int i= 0; i < time_scaled_music_arr_.size (); i++)
73     {
74       if (i < started_span_p_arr_.size () && started_span_p_arr_[i])
75         continue;
76
77       Spanner* glep = new Spanner (get_property ("TupletBracket"));
78
79       if (i >= started_span_p_arr_.size ())
80         started_span_p_arr_.push (glep);
81       else
82         started_span_p_arr_[i] = glep;
83       
84
85       SCM proc = get_property ("tupletNumberFormatFunction");
86       if (gh_procedure_p (proc))
87         {
88           SCM t = gh_apply (proc, scm_list_n (time_scaled_music_arr_[i]->self_scm (), SCM_UNDEFINED));
89           glep->set_grob_property ("text", t);
90         }
91       
92       announce_grob(glep, time_scaled_music_arr_ [i]->self_scm());
93     }
94 }
95
96 void
97 Tuplet_engraver::acknowledge_grob (Grob_info i)
98 {
99   if (Note_column::has_interface (i.grob_l_))
100     {
101       for (int j =0; j  <started_span_p_arr_.size (); j++)
102         if (started_span_p_arr_[j]) 
103           Tuplet_bracket::add_column (started_span_p_arr_[j], dynamic_cast<Item*> (i.grob_l_));
104     }
105 }
106
107 void
108 Tuplet_engraver::start_translation_timestep ()
109 {
110   Moment now = now_mom ();
111
112   Moment tsd;
113   SCM s = get_property ("tupletSpannerDuration");
114   if (unsmob_moment (s))
115     tsd = unsmob_moment (s)->main_part_;
116
117   for (int i= started_span_p_arr_.size (); i--;)
118     {
119       if (now.main_part_ >= span_stop_moments_[i])
120         {
121           if (started_span_p_arr_[i])
122             {
123               typeset_grob (started_span_p_arr_[i]);
124               started_span_p_arr_[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_span_p_arr_.del (i);
134           stop_moments_.del (i);
135           span_stop_moments_.del (i);
136           time_scaled_music_arr_.del (i);
137         }
138     }
139 }
140
141 void
142 Tuplet_engraver::finalize ()
143 {
144   for (int i=0; i < started_span_p_arr_.size (); i++)
145     {
146       if (started_span_p_arr_[i])
147         typeset_grob (started_span_p_arr_[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 /* acks  */       "note-column-interface",
159 /* reads */       "tupletNumberFormatFunction tupletSpannerDuration tupletInvisible",
160 /* write */       "");