]> git.donarmstrong.com Git - lilypond.git/blob - lily/text-spanner-engraver.cc
52ad7201b1199d123b9ea274785c835a4bc61c20
[lilypond.git] / lily / text-spanner-engraver.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2000--2014 Jan Nieuwenhuizen <janneke@gnu.org>
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 "international.hh"
23 #include "note-column.hh"
24 #include "pointer-group-interface.hh"
25 #include "side-position-interface.hh"
26 #include "spanner.hh"
27 #include "stream-event.hh"
28
29 #include "translator.icc"
30
31 class Text_spanner_engraver : public Engraver
32 {
33 public:
34   TRANSLATOR_DECLARATIONS (Text_spanner_engraver);
35 protected:
36   virtual void finalize ();
37   DECLARE_TRANSLATOR_LISTENER (text_span);
38   DECLARE_ACKNOWLEDGER (note_column);
39   void stop_translation_timestep ();
40   void process_music ();
41
42 private:
43   Spanner *span_;
44   Spanner *finished_;
45   Stream_event *current_event_;
46   Drul_array<Stream_event *> event_drul_;
47   void typeset_all ();
48 };
49
50 Text_spanner_engraver::Text_spanner_engraver ()
51 {
52   finished_ = 0;
53   current_event_ = 0;
54   span_ = 0;
55   event_drul_.set (0, 0);
56 }
57
58 IMPLEMENT_TRANSLATOR_LISTENER (Text_spanner_engraver, text_span);
59 void
60 Text_spanner_engraver::listen_text_span (Stream_event *ev)
61 {
62   Direction d = to_dir (ev->get_property ("span-direction"));
63   ASSIGN_EVENT_ONCE (event_drul_[d], ev);
64 }
65
66 void
67 Text_spanner_engraver::process_music ()
68 {
69   if (event_drul_[STOP])
70     {
71       if (!span_)
72         event_drul_[STOP]->origin ()->warning (_ ("cannot find start of text spanner"));
73       else
74         {
75           finished_ = span_;
76           announce_end_grob (finished_, SCM_EOL);
77           span_ = 0;
78           current_event_ = 0;
79         }
80     }
81
82   if (event_drul_[START])
83     {
84       if (current_event_)
85         event_drul_[START]->origin ()->warning (_ ("already have a text spanner"));
86       else
87         {
88           current_event_ = event_drul_[START];
89           span_ = make_spanner ("TextSpanner", event_drul_[START]->self_scm ());
90
91           Side_position_interface::set_axis (span_, Y_AXIS);
92           event_drul_[START] = 0;
93         }
94     }
95 }
96
97 void
98 Text_spanner_engraver::typeset_all ()
99 {
100   if (finished_)
101     {
102       if (!finished_->get_bound (RIGHT))
103         {
104           Grob *e = unsmob_grob (get_property ("currentMusicalColumn"));
105           finished_->set_bound (RIGHT, e);
106         }
107       finished_ = 0;
108     }
109 }
110
111 void
112 Text_spanner_engraver::stop_translation_timestep ()
113 {
114   if (span_ && !span_->get_bound (LEFT))
115     {
116       Grob *e = unsmob_grob (get_property ("currentMusicalColumn"));
117       span_->set_bound (LEFT, e);
118     }
119
120   typeset_all ();
121   event_drul_.set (0, 0);
122 }
123
124 void
125 Text_spanner_engraver::finalize ()
126 {
127   typeset_all ();
128   if (span_)
129     {
130       current_event_->origin ()->warning (_ ("unterminated text spanner"));
131       span_->suicide ();
132       span_ = 0;
133     }
134 }
135
136 void
137 Text_spanner_engraver::acknowledge_note_column (Grob_info info)
138 {
139   if (span_)
140     {
141       Pointer_group_interface::add_grob (span_,
142                                          ly_symbol2scm ("note-columns"),
143                                          info.grob ());
144       if (!span_->get_bound (LEFT))
145         add_bound_item (span_, info.grob ());
146     }
147   else if (finished_)
148     {
149       Pointer_group_interface::add_grob (finished_,
150                                          ly_symbol2scm ("note-columns"),
151                                          info.grob ());
152       if (!finished_->get_bound (RIGHT))
153         add_bound_item (finished_, info.grob ());
154     }
155 }
156
157 ADD_ACKNOWLEDGER (Text_spanner_engraver, note_column);
158
159 ADD_TRANSLATOR (Text_spanner_engraver,
160                 /* doc */
161                 "Create text spanner from an event.",
162
163                 /* create */
164                 "TextSpanner ",
165
166                 /* read */
167                 "currentMusicalColumn ",
168
169                 /* write */
170                 ""
171                );