]> git.donarmstrong.com Git - lilypond.git/blob - lily/lyric-engraver.cc
Run grand replace for 2015.
[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 /**
31    Generate texts for lyric syllables.  We only do one lyric at a time.
32    Multiple copies of this engraver should be used to do multiple voices.
33 */
34 class Lyric_engraver : public Engraver
35 {
36 protected:
37   void stop_translation_timestep ();
38   void process_music ();
39   DECLARE_TRANSLATOR_LISTENER (lyric);
40
41 public:
42   TRANSLATOR_DECLARATIONS (Lyric_engraver);
43
44 private:
45   Stream_event *event_;
46   Item *text_;
47   Item *last_text_;
48
49   Context *get_voice_context ();
50 };
51
52 Lyric_engraver::Lyric_engraver ()
53 {
54   text_ = 0;
55   last_text_ = 0;
56   event_ = 0;
57 }
58
59 IMPLEMENT_TRANSLATOR_LISTENER (Lyric_engraver, lyric);
60 void
61 Lyric_engraver::listen_lyric (Stream_event *ev)
62 {
63   ASSIGN_EVENT_ONCE (event_, ev);
64 }
65
66 void
67 Lyric_engraver::process_music ()
68 {
69   if (event_)
70     {
71       SCM text = event_->get_property ("text");
72
73       if (ly_is_equal (text, scm_from_locale_string (" ")))
74         {
75           if (last_text_)
76             last_text_->set_property ("self-alignment-X",
77                                       get_property ("lyricMelismaAlignment"));
78         }
79       else
80         text_ = make_item ("LyricText", event_->self_scm ());
81     }
82
83   Context *voice = get_voice_to_lyrics (context ());
84   if (last_text_
85       && voice
86       && to_boolean (voice->get_property ("melismaBusy"))
87       && !to_boolean (context ()->get_property ("ignoreMelismata")))
88     last_text_->set_property ("self-alignment-X",
89                               get_property ("lyricMelismaAlignment"));
90 }
91
92 Context *
93 get_voice_to_lyrics (Context *lyrics)
94 {
95   bool searchForVoice = to_boolean (lyrics->get_property ("searchForVoice"));
96
97   SCM avc = lyrics->get_property ("associatedVoiceContext");
98   if (Context *c = Context::unsmob (avc))
99     return c;
100
101   SCM voice_name = lyrics->get_property ("associatedVoice");
102   string nm = lyrics->id_string ();
103
104   if (scm_is_string (voice_name))
105     nm = ly_scm2string (voice_name);
106   else if (nm == "" || !searchForVoice)
107     return 0;
108   else
109     {
110       ssize idx = nm.rfind ('-');
111       if (idx != NPOS)
112         nm = nm.substr (0, idx);
113     }
114
115   SCM voice_type = lyrics->get_property ("associatedVoiceType");
116   if (!scm_is_symbol (voice_type))
117     return 0;
118
119   Context *parent = lyrics;
120   Context *voice = 0;
121   while (parent && !voice)
122     {
123       voice = find_context_below (parent, voice_type, nm);
124       parent = parent->get_parent_context ();
125     }
126
127   if (voice)
128     return voice;
129
130   parent = lyrics;
131   voice = 0;
132   while (parent && !voice)
133     {
134       voice = find_context_below (parent, voice_type, "");
135       parent = parent->get_parent_context ();
136     }
137
138   return voice;
139 }
140
141 Grob *
142 get_current_note_head (Context *voice)
143 {
144   Moment now = voice->now_mom ();
145   for (SCM s = voice->get_property ("busyGrobs");
146        scm_is_pair (s); s = scm_cdr (s))
147     {
148       Grob *g = Grob::unsmob (scm_cdar (s));;
149       Moment *end_mom = Moment::unsmob (scm_caar (s));
150       if (!end_mom || !g)
151         {
152           programming_error ("busyGrobs invalid");
153           continue;
154         }
155
156       // It's a bit irritating that we just have the length and
157       // duration of the Grob.
158       Moment end_from_now =
159         get_event_length (Stream_event::unsmob (g->get_property ("cause")), now)
160         + now;
161       // We cannot actually include more than a single grace note
162       // using busyGrobs on ungraced lyrics since a grob ending on
163       // grace time will just have disappeared from busyGrobs by the
164       // time our ungraced lyrics appear.  At best we may catch a
165       // single grace note.
166       //
167       // However, a single grace note ending on a non-grace time is
168       // indistinguishable from a proper note ending on a non-grace
169       // time.  So we really have no way to obey includeGraceNotes
170       // here.  Not with this mechanism.
171       if ((*end_mom == end_from_now)
172           && dynamic_cast<Item *> (g)
173           && Note_head::has_interface (g))
174         {
175           return g;
176         }
177     }
178
179   return 0;
180 }
181
182 void
183 Lyric_engraver::stop_translation_timestep ()
184 {
185   if (text_)
186     {
187       Context *voice = get_voice_to_lyrics (context ());
188
189       if (voice)
190         {
191           Grob *head = get_current_note_head (voice);
192
193           if (head)
194             {
195               text_->set_parent (head->get_parent(X_AXIS), X_AXIS);
196               if (melisma_busy (voice)
197                   && !to_boolean (get_property ("ignoreMelismata")))
198                 text_->set_property ("self-alignment-X",
199                                      get_property ("lyricMelismaAlignment"));
200             }
201         }
202
203       last_text_ = text_;
204       text_ = 0;
205     }
206   event_ = 0;
207 }
208
209 ADD_TRANSLATOR (Lyric_engraver,
210                 /* doc */
211                 "Engrave text for lyrics.",
212
213                 /* create */
214                 "LyricText ",
215
216                 /* read */
217                 "ignoreMelismata "
218                 "lyricMelismaAlignment "
219                 "searchForVoice",
220
221                 /* write */
222                 ""
223                );