]> git.donarmstrong.com Git - lilypond.git/blob - lily/grob-pq-engraver.cc
(class Phrasing_slur_engraver):
[lilypond.git] / lily / grob-pq-engraver.cc
1 /*
2   grob-pq-engraver.cc -- implement Grob_pq_engraver
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 2001--2005  Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8
9 #include "context.hh"
10 #include "engraver.hh"
11 #include "grob.hh"
12 #include "warn.hh"
13
14 struct Grob_pq_entry
15 {
16   Grob *grob_;
17   Moment end_;
18   static int compare (Grob_pq_entry const &a,
19                       Grob_pq_entry const &b)
20   {
21     return Moment::compare (a.end_, b.end_);
22   }
23 };
24
25 class Grob_pq_engraver : public Engraver
26 {
27 public:
28   TRANSLATOR_DECLARATIONS (Grob_pq_engraver);
29 protected:
30   virtual void initialize ();
31   DECLARE_ACKNOWLEDGER (grob);
32   void start_translation_timestep ();
33   void stop_translation_timestep ();
34
35   Array<Grob_pq_entry> started_now_;
36 };
37
38 Grob_pq_engraver::Grob_pq_engraver ()
39 {
40 }
41
42 void
43 Grob_pq_engraver::initialize ()
44 {
45   context ()->set_property ("busyGrobs", SCM_EOL);
46 }
47
48 LY_DEFINE (ly_grob_pq_less_p, "ly:grob-pq-less?",
49            2, 0, 0, (SCM a, SCM b),
50            "Compare 2 grob priority queue entries. Internal")
51 {
52   if (Moment::compare (*unsmob_moment (scm_car (a)),
53                        *unsmob_moment (scm_car (b))) < 0)
54     return SCM_BOOL_T;
55   else
56     return SCM_BOOL_F;
57 }
58
59 void
60 Grob_pq_engraver::acknowledge_grob (Grob_info gi)
61 {
62   Music *m = gi.music_cause ();
63
64   if (m
65       && !gi.grob ()->internal_has_interface (ly_symbol2scm ("multi-measure-interface")))
66     {
67       Moment n = now_mom ();
68       Moment l = m->get_length ();
69
70       if (!l.to_bool ())
71         return;
72
73       if (n.grace_part_)
74         {
75           l.grace_part_ = l.main_part_;
76           l.main_part_ = 0;
77         }
78
79       Moment end = n + l;
80
81       Grob_pq_entry e;
82       e.grob_ = gi.grob ();
83       e.end_ = end;
84       
85       started_now_.push (e);
86     }
87 }
88
89 void
90 Grob_pq_engraver::stop_translation_timestep ()
91 {
92   Moment now = now_mom ();
93   SCM start_busy = get_property ("busyGrobs");
94   SCM busy = start_busy;
95   while (scm_is_pair (busy) && *unsmob_moment (scm_caar (busy)) == now)
96     {
97       busy = scm_cdr (busy);
98     }
99
100   started_now_.sort (Grob_pq_entry::compare);
101   SCM lst = SCM_EOL;
102   SCM *tail = &lst;
103   for (int i = 0; i < started_now_.size (); i++)
104     {
105       *tail = scm_acons (started_now_[i].end_.smobbed_copy (),
106                          started_now_[i].grob_->self_scm (),
107                          SCM_EOL);
108       tail = SCM_CDRLOC(*tail);
109     }
110   
111   busy = scm_merge_x (lst, busy, ly_grob_pq_less_p_proc);
112   context ()->set_property ("busyGrobs", busy);
113
114   started_now_.clear ();
115 }
116
117 void
118 Grob_pq_engraver::start_translation_timestep ()
119 {
120   Moment now = now_mom ();
121
122   SCM start_busy = get_property ("busyGrobs");
123   SCM busy = start_busy;
124   while (scm_is_pair (busy) && *unsmob_moment (scm_caar (busy)) < now)
125     {
126       /*
127         The grob-pq-engraver is not water tight, and stuff like
128         tupletSpannerDuration confuses it.
129       */
130       busy = scm_cdr (busy);
131     }
132
133   if (start_busy != busy)
134     context ()->set_property ("busyGrobs", busy);
135 }
136
137 #include "translator.icc"
138 ADD_ACKNOWLEDGER (Grob_pq_engraver, grob);
139 ADD_TRANSLATOR (Grob_pq_engraver,
140
141                 /* doc */ "Administrate when certain grobs (eg. note heads) stop playing",
142                 /* create */ "",
143                 /* accept */ "",
144                 /* read */ "busyGrobs",
145                 /* write */ "busyGrobs");