]> git.donarmstrong.com Git - lilypond.git/blob - lily/grob-pq-engraver.cc
Fix some bugs in the dynamic engraver and PostScript backend
[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--2006  Han-Wen Nienhuys <hanwen@xs4all.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   vector<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_back (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     busy = scm_cdr (busy);
97
98   vector_sort (started_now_, Grob_pq_entry::compare);
99   SCM lst = SCM_EOL;
100   SCM *tail = &lst;
101   for (vsize i = 0; i < started_now_.size (); i++)
102     {
103       *tail = scm_acons (started_now_[i].end_.smobbed_copy (),
104                          started_now_[i].grob_->self_scm (),
105                          SCM_EOL);
106       tail = SCM_CDRLOC (*tail);
107     }
108
109   busy = scm_merge_x (lst, busy, ly_grob_pq_less_p_proc);
110   context ()->set_property ("busyGrobs", busy);
111
112   started_now_.clear ();
113 }
114
115 void
116 Grob_pq_engraver::start_translation_timestep ()
117 {
118   Moment now = now_mom ();
119
120   SCM start_busy = get_property ("busyGrobs");
121   SCM busy = start_busy;
122   while (scm_is_pair (busy) && *unsmob_moment (scm_caar (busy)) < now)
123     {
124       /*
125         The grob-pq-engraver is not water tight, and stuff like
126         tupletSpannerDuration confuses it.
127       */
128       busy = scm_cdr (busy);
129     }
130
131   if (start_busy != busy)
132     context ()->set_property ("busyGrobs", busy);
133 }
134
135 #include "translator.icc"
136 ADD_ACKNOWLEDGER (Grob_pq_engraver, grob);
137 ADD_TRANSLATOR (Grob_pq_engraver,
138
139                 /* doc */ "Administrate when certain grobs (eg. note heads) stop playing",
140                 /* create */ "",
141                 /* accept */ "",
142                 /* read */ "busyGrobs",
143                 /* write */ "busyGrobs");