]> git.donarmstrong.com Git - lilypond.git/blob - lily/lyric-engraver.cc
Run grand-replace (issue 3765)
[lilypond.git] / lily / lyric-engraver.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1997--2014 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 = unsmob_context (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   Context *parent = lyrics;
116   Context *voice = 0;
117   while (parent && !voice)
118     {
119       voice = find_context_below (parent, ly_symbol2scm ("Voice"), nm);
120       parent = parent->get_parent_context ();
121     }
122
123   if (voice)
124     return voice;
125
126   parent = lyrics;
127   voice = 0;
128   while (parent && !voice)
129     {
130       voice = find_context_below (parent, ly_symbol2scm ("Voice"), "");
131       parent = parent->get_parent_context ();
132     }
133
134   return voice;
135 }
136
137 Grob *
138 get_current_note_head (Context *voice, bool include_grace_notes)
139 {
140   Moment now = voice->now_mom ();
141   for (SCM s = voice->get_property ("busyGrobs");
142        scm_is_pair (s); s = scm_cdr (s))
143     {
144       Grob *g = unsmob_grob (scm_cdar (s));;
145       Moment *end_mom = unsmob_moment (scm_caar (s));
146       if (!end_mom || !g)
147         {
148           programming_error ("busyGrobs invalid");
149           continue;
150         }
151
152       if (((end_mom->main_part_ > now.main_part_)
153            || (include_grace_notes && end_mom->grace_part_ > now.grace_part_))
154           && dynamic_cast<Item *> (g)
155           && Note_head::has_interface (g))
156         {
157           return g;
158         }
159     }
160
161   return 0;
162 }
163
164 void
165 Lyric_engraver::stop_translation_timestep ()
166 {
167   if (text_)
168     {
169       Context *voice = get_voice_to_lyrics (context ());
170
171       if (voice)
172         {
173           bool include_grace_notes = to_boolean (get_property ("includeGraceNotes"));
174           Grob *head = get_current_note_head (voice, include_grace_notes);
175
176           if (head)
177             {
178               text_->set_parent (head, X_AXIS);
179               if (melisma_busy (voice)
180                   && !to_boolean (get_property ("ignoreMelismata")))
181                 text_->set_property ("self-alignment-X",
182                                      get_property ("lyricMelismaAlignment"));
183             }
184           else
185             {
186               text_->warning (_ ("Lyric syllable does not have note."
187                                  "  Use \\lyricsto or associatedVoice."));
188               text_->set_property ("X-offset", scm_from_int (0));
189             }
190         }
191
192       last_text_ = text_;
193       text_ = 0;
194     }
195   event_ = 0;
196 }
197
198 ADD_TRANSLATOR (Lyric_engraver,
199                 /* doc */
200                 "Engrave text for lyrics.",
201
202                 /* create */
203                 "LyricText ",
204
205                 /* read */
206                 "ignoreMelismata "
207                 "includeGraceNotes "
208                 "lyricMelismaAlignment "
209                 "searchForVoice",
210
211                 /* write */
212                 ""
213                );