]> git.donarmstrong.com Git - lilypond.git/blob - lily/lyric-engraver.cc
Web-ja: update introduction
[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   void listen_lyric (Stream_event *);
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 (Context *c)
53   : Engraver (c)
54 {
55   text_ = 0;
56   last_text_ = 0;
57   event_ = 0;
58 }
59
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_ascii_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     {
100       if (!c->is_removable ())
101         return c;
102     }
103
104   SCM voice_name = lyrics->get_property ("associatedVoice");
105   string nm = lyrics->id_string ();
106
107   if (scm_is_string (voice_name))
108     nm = ly_scm2string (voice_name);
109   else if (nm == "" || !searchForVoice)
110     return 0;
111   else
112     {
113       ssize idx = nm.rfind ('-');
114       if (idx != NPOS)
115         nm = nm.substr (0, idx);
116     }
117
118   SCM voice_type = lyrics->get_property ("associatedVoiceType");
119   if (!scm_is_symbol (voice_type))
120     return 0;
121
122   Context *voice = find_context_near (lyrics, voice_type, nm);
123   if (voice)
124     return voice;
125
126   return find_context_near (lyrics, voice_type, "");
127 }
128
129 Grob *
130 get_current_note_head (Context *voice)
131 {
132   Moment now = voice->now_mom ();
133   for (SCM s = voice->get_property ("busyGrobs");
134        scm_is_pair (s); s = scm_cdr (s))
135     {
136       Grob *g = unsmob<Grob> (scm_cdar (s));;
137       Moment *end_mom = unsmob<Moment> (scm_caar (s));
138       if (!end_mom || !g)
139         {
140           programming_error ("busyGrobs invalid");
141           continue;
142         }
143
144       // It's a bit irritating that we just have the length and
145       // duration of the Grob.
146       Moment end_from_now =
147         get_event_length (unsmob<Stream_event> (g->get_property ("cause")), now)
148         + now;
149       // We cannot actually include more than a single grace note
150       // using busyGrobs on ungraced lyrics since a grob ending on
151       // grace time will just have disappeared from busyGrobs by the
152       // time our ungraced lyrics appear.  At best we may catch a
153       // single grace note.
154       //
155       // However, a single grace note ending on a non-grace time is
156       // indistinguishable from a proper note ending on a non-grace
157       // time.  So we really have no way to obey includeGraceNotes
158       // here.  Not with this mechanism.
159       if ((*end_mom == end_from_now)
160           && dynamic_cast<Item *> (g)
161           && has_interface<Note_head> (g))
162         {
163           return g;
164         }
165     }
166
167   return 0;
168 }
169
170 void
171 Lyric_engraver::stop_translation_timestep ()
172 {
173   if (text_)
174     {
175       Context *voice = get_voice_to_lyrics (context ());
176
177       if (voice)
178         {
179           Grob *head = get_current_note_head (voice);
180
181           if (head)
182             {
183               text_->set_parent (head->get_parent(X_AXIS), X_AXIS);
184               if (melisma_busy (voice)
185                   && !to_boolean (get_property ("ignoreMelismata")))
186                 text_->set_property ("self-alignment-X",
187                                      get_property ("lyricMelismaAlignment"));
188             }
189         }
190
191       last_text_ = text_;
192       text_ = 0;
193     }
194   event_ = 0;
195 }
196
197 void
198 Lyric_engraver::boot ()
199 {
200   ADD_LISTENER (Lyric_engraver, lyric);
201 }
202
203 ADD_TRANSLATOR (Lyric_engraver,
204                 /* doc */
205                 "Engrave text for lyrics.",
206
207                 /* create */
208                 "LyricText ",
209
210                 /* read */
211                 "ignoreMelismata "
212                 "lyricMelismaAlignment "
213                 "searchForVoice",
214
215                 /* write */
216                 ""
217                );