]> git.donarmstrong.com Git - lilypond.git/blob - lily/spacing-engraver.cc
(shift_region_to_valid): divide by zero fix. This
[lilypond.git] / lily / spacing-engraver.cc
1 /*
2   spacing-engraver.cc -- implement Spacing_engraver
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1999--2005 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8
9 #include "paper-column.hh"
10 #include "engraver.hh"
11 #include "pqueue.hh"
12 #include "note-spacing.hh"
13 #include "staff-spacing.hh"
14 #include "pointer-group-interface.hh"
15 #include "spanner.hh"
16
17 struct Rhythmic_tuple
18 {
19   Grob_info info_;
20   Moment end_;
21
22   Rhythmic_tuple ()
23   {
24   }
25   Rhythmic_tuple (Grob_info i, Moment m)
26   {
27     info_ = i;
28     end_ = m;
29   }
30   static int time_compare (Rhythmic_tuple const &, Rhythmic_tuple const &);
31 };
32
33 /**
34    Acknowledge rhythmic elements, for initializing spacing fields in
35    the columns.
36
37    should be the  last one of the toplevel context
38 */
39 class Spacing_engraver : public Engraver
40 {
41   PQueue<Rhythmic_tuple> playing_durations_;
42   Array<Rhythmic_tuple> now_durations_;
43   Array<Rhythmic_tuple> stopped_durations_;
44   Moment now_;
45   Spanner *spacing_;
46
47   TRANSLATOR_DECLARATIONS (Spacing_engraver);
48   
49 protected:
50   DECLARE_ACKNOWLEDGER (staff_spacing);
51   DECLARE_ACKNOWLEDGER (note_spacing);
52   DECLARE_ACKNOWLEDGER (rhythmic_head);
53   
54   void start_translation_timestep ();
55   void stop_translation_timestep ();
56   void process_music ();
57   virtual void finalize ();
58 };
59
60 inline int
61 compare (Rhythmic_tuple const &a, Rhythmic_tuple const &b)
62 {
63   return Rhythmic_tuple::time_compare (a, b);
64 }
65
66 int
67 Rhythmic_tuple::time_compare (Rhythmic_tuple const &h1,
68                               Rhythmic_tuple const &h2)
69 {
70   return (h1.end_ - h2.end_).main_part_.sign ();
71 }
72
73 Spacing_engraver::Spacing_engraver ()
74 {
75   spacing_ = 0;
76 }
77
78 void
79 Spacing_engraver::process_music ()
80 {
81   if (!spacing_)
82     {
83       spacing_ = make_spanner ("SpacingSpanner", SCM_EOL);
84       spacing_->set_bound (LEFT, unsmob_grob (get_property ("currentCommandColumn")));
85     }
86 }
87
88 void
89 Spacing_engraver::finalize ()
90 {
91   if (spacing_)
92     {
93       Grob *p = unsmob_grob (get_property ("currentCommandColumn"));
94
95       spacing_->set_bound (RIGHT, p);
96       spacing_ = 0;
97     }
98 }
99
100 void
101 Spacing_engraver::acknowledge_note_spacing (Grob_info i)
102 {
103   Pointer_group_interface::add_grob (spacing_, ly_symbol2scm ("wishes"), i.grob ());
104 }
105
106 void
107 Spacing_engraver::acknowledge_staff_spacing (Grob_info i)
108 {
109   Pointer_group_interface::add_grob (spacing_, ly_symbol2scm ("wishes"), i.grob ());
110 }
111   
112 void
113 Spacing_engraver::acknowledge_rhythmic_head (Grob_info i)
114 {
115   if (i.grob ()->internal_has_interface (ly_symbol2scm ("lyric-syllable-interface"))
116       || i.grob ()->internal_has_interface (ly_symbol2scm ("multi-measure-interface")))
117     return;
118
119   /*
120     only pay attention to durations that are not grace notes.
121   */
122   if (!now_.grace_part_)
123     {
124       Music *r = i.music_cause ();
125       if (r && r->is_mus_type ("rhythmic-event"))
126         {
127           Moment len = r->get_length ();
128           Rhythmic_tuple t (i, now_mom () + len);
129           now_durations_.push (t);
130         }
131     }
132 }
133
134 void
135 Spacing_engraver::stop_translation_timestep ()
136 {
137   Paper_column *musical_column
138     = dynamic_cast<Paper_column *> (unsmob_grob (get_property ("currentMusicalColumn")));
139   
140  SCM proportional = get_property ("proportionalNotationDuration");
141  if (unsmob_moment (proportional))
142    {
143      musical_column->set_property ("shortest-playing-duration", proportional);
144      musical_column->set_property ("shortest-starter-duration", proportional);
145      return; 
146    }
147   
148   Moment shortest_playing;
149   shortest_playing.set_infinite (1);
150   for (int i = 0; i < playing_durations_.size (); i++)
151     {
152       Music *mus = playing_durations_[i].info_.music_cause ();
153       if (mus)
154         {
155           Moment m = mus->get_length ();
156           shortest_playing = min (shortest_playing, m);
157         }
158     }
159   Moment starter;
160   starter.set_infinite (1);
161
162   for (int i = 0; i < now_durations_.size (); i++)
163     {
164       Moment m = now_durations_[i].info_.music_cause ()->get_length ();
165       if (m.to_bool ())
166         {
167           starter = min (starter, m);
168           playing_durations_.insert (now_durations_[i]);
169         }
170     }
171   now_durations_.clear ();
172
173   shortest_playing = min (shortest_playing, starter);
174
175  
176   assert (starter.to_bool ());
177   SCM sh = shortest_playing.smobbed_copy ();
178   SCM st = starter.smobbed_copy ();
179
180   musical_column->set_property ("shortest-playing-duration", sh);
181   musical_column->set_property ("shortest-starter-duration", st);
182 }
183
184 void
185 Spacing_engraver::start_translation_timestep ()
186 {
187   now_ = now_mom ();
188   stopped_durations_.clear ();
189   while (playing_durations_.size () && playing_durations_.front ().end_ < now_)
190     playing_durations_.delmin ();
191   while (playing_durations_.size () && playing_durations_.front ().end_ == now_)
192     stopped_durations_.push (playing_durations_.get ());
193 }
194
195 #include "translator.icc"
196
197 ADD_ACKNOWLEDGER (Spacing_engraver, staff_spacing);
198 ADD_ACKNOWLEDGER (Spacing_engraver, note_spacing);
199 ADD_ACKNOWLEDGER (Spacing_engraver, rhythmic_head);
200   
201 ADD_TRANSLATOR (Spacing_engraver,
202                 /* descr */ "make a SpacingSpanner and do bookkeeping of shortest starting and playing notes  ",
203                 /* creats*/ "SpacingSpanner",
204                 /* accepts */ "",
205                 /* reads */ "currentMusicalColumn currentCommandColumn proportionalNotationDuration",
206                 /* write */ "");