]> git.donarmstrong.com Git - lilypond.git/blob - lily/footnote-engraver.cc
Issue 2559: For time-based footnotes without grob spec, attach to all grobs
[lilypond.git] / lily / footnote-engraver.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2011--2012 Mike Solomon <mike@apollinemike.com>
5
6   LilyPond is free software: you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation, either version 3 of the License, or
9   (at your option) any later version.
10
11   LilyPond is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15
16   You should have received a copy of the GNU General Public License
17   along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "engraver.hh"
21
22 #include "music.hh"
23 #include "stream-event.hh"
24 #include "international.hh"
25 #include "item.hh"
26 #include "pointer-group-interface.hh"
27 #include "spanner.hh"
28 #include "system.hh"
29
30 #include "translator.icc"
31
32 class Footnote_engraver : public Engraver
33 {
34   TRANSLATOR_DECLARATIONS (Footnote_engraver);
35
36   DECLARE_TRANSLATOR_LISTENER (footnote);
37   DECLARE_ACKNOWLEDGER (grob);
38   DECLARE_END_ACKNOWLEDGER (grob);
39   vector<Stream_event *> events_;
40   vector<Drul_array<Spanner *> > annotated_spanners_;
41
42   void stop_translation_timestep ();
43   void finalize ();
44   virtual void derived_mark () const;
45
46   void footnotify (Grob *, Stream_event *);
47 };
48
49 IMPLEMENT_TRANSLATOR_LISTENER (Footnote_engraver, footnote);
50 void
51 Footnote_engraver::listen_footnote (Stream_event *ev)
52 {
53   events_.push_back (ev);
54 }
55
56 void
57 Footnote_engraver::stop_translation_timestep ()
58 {
59   events_.clear ();
60 }
61
62 void
63 Footnote_engraver::finalize ()
64 {
65   annotated_spanners_.clear ();
66 }
67
68 void
69 Footnote_engraver::derived_mark () const
70 {
71   for (vsize i = 0; i < events_.size (); ++i)
72     scm_gc_mark (events_[i]->self_scm ());
73 }
74
75 Footnote_engraver::Footnote_engraver ()
76 {
77 }
78
79 void
80 Footnote_engraver::footnotify (Grob *g, Stream_event *event)
81 {
82   Spanner *s = dynamic_cast<Spanner *>(g);
83
84   if (s)
85     {
86       Spanner *b = make_spanner ("FootnoteSpanner", event->self_scm ());
87       b->set_parent (s, Y_AXIS);
88       b->set_parent (s, X_AXIS);
89       Grob *bound = unsmob_grob (get_property ("currentMusicalColumn"));
90       b->set_bound (LEFT, bound);
91       annotated_spanners_.push_back (Drul_array<Spanner *> (s, b));
92     }
93   else
94     {
95       Grob *b = make_item ("FootnoteItem", event->self_scm ());
96       b->set_parent (g, Y_AXIS);
97       b->set_parent (g, X_AXIS);
98     }
99 }
100
101 void
102 Footnote_engraver::acknowledge_grob (Grob_info info)
103 {
104   Music *mus = unsmob_music (info.grob ()->get_property ("footnote-music"));
105
106   if (mus)
107     {
108       if (!mus->is_mus_type ("footnote-event")) {
109         mus->origin ()->programming_error (_ ("Must be footnote-event."));
110         return;
111       }
112       Stream_event *ev = mus->to_event (context ());
113       footnotify (info.grob (), ev);
114       ev->unprotect ();
115       return;
116     }
117
118   // The following performance hog should eventually be removed:
119   // instead of adding a -\footnote ... \default articulation at the
120   // end of a note, you can perfectly well use \footnote ... before
121   // the note.  This is just for the sake of automatic convert-ly
122   // rules.
123
124   Stream_event *cause = info.event_cause ();
125
126   SCM arts = cause ? cause->get_property ("articulations") : SCM_EOL;
127   for (SCM s = arts; scm_is_pair (s); s = scm_cdr (s))
128     {
129       Stream_event *e = unsmob_stream_event (scm_car (s));
130       if (e->in_event_class ("footnote-event"))
131         footnotify (info.grob (), e);
132     }
133
134   // In contrast, the following code is only called when actual
135   // footnote events have been listened to.  It should not affect
136   // performance.
137
138   for (vsize i = 0; i < events_.size (); i++)
139     {
140       SCM name = events_[i]->get_property ("symbol");
141       if (!scm_is_symbol (name)
142           || info.grob ()->name () == ly_symbol2string (name))
143         footnotify (info.grob (), events_[i]);
144     }
145 }
146
147 void
148 Footnote_engraver::acknowledge_end_grob (Grob_info info)
149 {
150   Spanner *s = dynamic_cast<Spanner *>(info.grob ());
151
152   if (s)
153     for (vsize i = 0; i < annotated_spanners_.size (); i++)
154       {
155         if (annotated_spanners_[i][LEFT] == s)
156           {
157             Grob *bound = unsmob_grob (get_property ("currentMusicalColumn"));
158             annotated_spanners_[i][RIGHT]->set_bound (RIGHT, bound);
159             break;
160           }
161       }
162 }
163
164 ADD_ACKNOWLEDGER (Footnote_engraver, grob);
165 ADD_END_ACKNOWLEDGER (Footnote_engraver, grob);
166
167 ADD_TRANSLATOR (Footnote_engraver,
168                 /* doc */
169                 "Create footnote texts.",
170
171                 /* create */
172                 "FootnoteItem "
173                 "FootnoteSpanner ",
174
175                 /*read*/
176                 "currentMusicalColumn ",
177
178                 /*write*/
179                 ""
180                );