]> git.donarmstrong.com Git - lilypond.git/blob - lily/lyric-engraver.cc
7c88ccc8c74b4d93fbcdbeeb6f1058721d9b8237
[lilypond.git] / lily / lyric-engraver.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1997--2015 Han-Wen Nienhuys <hanwen@xs4all.nl>
5   Jan Nieuwenhuizen <janneke@gnu.org>
6
7   LilyPond is free software: you can redistribute it and/or modify
8   it under the terms of the GNU General Public License as published by
9   the Free Software Foundation, either version 3 of the License, or
10   (at your option) any later version.
11
12   LilyPond is distributed in the hope that it will be useful,
13   but WITHOUT ANY WARRANTY; without even the implied warranty of
14   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   GNU General Public License for more details.
16
17   You should have received a copy of the GNU General Public License
18   along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "context.hh"
22 #include "engraver.hh"
23 #include "item.hh"
24 #include "note-head.hh"
25 #include "stream-event.hh"
26 #include "international.hh"
27
28 #include "translator.icc"
29
30 using std::string;
31
32 /**
33    Generate texts for lyric syllables.  We only do one lyric at a time.
34    Multiple copies of this engraver should be used to do multiple voices.
35 */
36 class Lyric_engraver : public Engraver
37 {
38 protected:
39   void stop_translation_timestep ();
40   void process_music ();
41   DECLARE_TRANSLATOR_LISTENER (lyric);
42
43 public:
44   TRANSLATOR_DECLARATIONS (Lyric_engraver);
45
46 private:
47   Stream_event *event_;
48   Item *text_;
49   Item *last_text_;
50
51   Context *get_voice_context ();
52 };
53
54 Lyric_engraver::Lyric_engraver ()
55 {
56   text_ = 0;
57   last_text_ = 0;
58   event_ = 0;
59 }
60
61 IMPLEMENT_TRANSLATOR_LISTENER (Lyric_engraver, lyric);
62 void
63 Lyric_engraver::listen_lyric (Stream_event *ev)
64 {
65   ASSIGN_EVENT_ONCE (event_, ev);
66 }
67
68 void
69 Lyric_engraver::process_music ()
70 {
71   if (event_)
72     {
73       SCM text = event_->get_property ("text");
74
75       if (ly_is_equal (text, scm_from_ascii_string (" ")))
76         {
77           if (last_text_)
78             last_text_->set_property ("self-alignment-X",
79                                       get_property ("lyricMelismaAlignment"));
80         }
81       else
82         text_ = make_item ("LyricText", event_->self_scm ());
83     }
84
85   Context *voice = get_voice_to_lyrics (context ());
86   if (last_text_
87       && voice
88       && to_boolean (voice->get_property ("melismaBusy"))
89       && !to_boolean (context ()->get_property ("ignoreMelismata")))
90     last_text_->set_property ("self-alignment-X",
91                               get_property ("lyricMelismaAlignment"));
92 }
93
94 Context *
95 get_voice_to_lyrics (Context *lyrics)
96 {
97   bool searchForVoice = to_boolean (lyrics->get_property ("searchForVoice"));
98
99   SCM avc = lyrics->get_property ("associatedVoiceContext");
100   if (Context *c = unsmob<Context> (avc))
101     {
102       if (!c->is_removable ())
103         return c;
104     }
105
106   SCM voice_name = lyrics->get_property ("associatedVoice");
107   string nm = lyrics->id_string ();
108
109   if (scm_is_string (voice_name))
110     nm = ly_scm2string (voice_name);
111   else if (nm == "" || !searchForVoice)
112     return 0;
113   else
114     {
115       ssize idx = nm.rfind ('-');
116       if (idx != NPOS)
117         nm = nm.substr (0, idx);
118     }
119
120   SCM voice_type = lyrics->get_property ("associatedVoiceType");
121   if (!scm_is_symbol (voice_type))
122     return 0;
123
124   Context *voice = find_context_near (lyrics, voice_type, nm);
125   if (voice)
126     return voice;
127
128   return find_context_near (lyrics, voice_type, "");
129 }
130
131 Grob *
132 get_current_note_head (Context *voice)
133 {
134   Moment now = voice->now_mom ();
135   for (SCM s = voice->get_property ("busyGrobs");
136        scm_is_pair (s); s = scm_cdr (s))
137     {
138       Grob *g = unsmob<Grob> (scm_cdar (s));;
139       Moment *end_mom = unsmob<Moment> (scm_caar (s));
140       if (!end_mom || !g)
141         {
142           programming_error ("busyGrobs invalid");
143           continue;
144         }
145
146       // It's a bit irritating that we just have the length and
147       // duration of the Grob.
148       Moment end_from_now =
149         get_event_length (unsmob<Stream_event> (g->get_property ("cause")), now)
150         + now;
151       // We cannot actually include more than a single grace note
152       // using busyGrobs on ungraced lyrics since a grob ending on
153       // grace time will just have disappeared from busyGrobs by the
154       // time our ungraced lyrics appear.  At best we may catch a
155       // single grace note.
156       //
157       // However, a single grace note ending on a non-grace time is
158       // indistinguishable from a proper note ending on a non-grace
159       // time.  So we really have no way to obey includeGraceNotes
160       // here.  Not with this mechanism.
161       if ((*end_mom == end_from_now)
162           && dynamic_cast<Item *> (g)
163           && has_interface<Note_head> (g))
164         {
165           return g;
166         }
167     }
168
169   return 0;
170 }
171
172 void
173 Lyric_engraver::stop_translation_timestep ()
174 {
175   if (text_)
176     {
177       Context *voice = get_voice_to_lyrics (context ());
178
179       if (voice)
180         {
181           Grob *head = get_current_note_head (voice);
182
183           if (head)
184             {
185               text_->set_parent (head->get_parent(X_AXIS), X_AXIS);
186               if (melisma_busy (voice)
187                   && !to_boolean (get_property ("ignoreMelismata")))
188                 text_->set_property ("self-alignment-X",
189                                      get_property ("lyricMelismaAlignment"));
190             }
191         }
192
193       last_text_ = text_;
194       text_ = 0;
195     }
196   event_ = 0;
197 }
198
199 ADD_TRANSLATOR (Lyric_engraver,
200                 /* doc */
201                 "Engrave text for lyrics.",
202
203                 /* create */
204                 "LyricText ",
205
206                 /* read */
207                 "ignoreMelismata "
208                 "lyricMelismaAlignment "
209                 "searchForVoice",
210
211                 /* write */
212                 ""
213                );