]> git.donarmstrong.com Git - lilypond.git/blob - lily/lyric-engraver.cc
Merge branch 'lilypond/translation' of ssh://git.sv.gnu.org/srv/git/lilypond into...
[lilypond.git] / lily / lyric-engraver.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1997--2011 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_ && voice &&
85         to_boolean (voice->get_property ("melismaBusy")))
86       last_text_->set_property ("self-alignment-X",
87                                 get_property ("lyricMelismaAlignment"));
88 }
89
90 Context *
91 get_voice_to_lyrics (Context *lyrics)
92 {
93   SCM avc = lyrics->get_property ("associatedVoiceContext");
94   if (Context *c = unsmob_context (avc))
95     return c;
96
97   SCM voice_name = lyrics->get_property ("associatedVoice");
98   string nm = lyrics->id_string ();
99
100   if (scm_is_string (voice_name))
101     nm = ly_scm2string (voice_name);
102   else if (nm == "")
103     return 0;
104   else
105     {
106       ssize idx = nm.rfind ('-');
107       if (idx != NPOS)
108         nm = nm.substr (0, idx);
109     }
110
111   Context *parent = lyrics;
112   Context *voice = 0;
113   while (parent && !voice)
114     {
115       voice = find_context_below (parent, ly_symbol2scm ("Voice"), nm);
116       parent = parent->get_parent_context ();
117     }
118
119   if (voice)
120     return voice;
121
122   parent = lyrics;
123   voice = 0;
124   while (parent && !voice)
125     {
126       voice = find_context_below (parent, ly_symbol2scm ("Voice"), "");
127       parent = parent->get_parent_context ();
128     }
129
130   return voice;
131 }
132
133 Grob *
134 get_current_note_head (Context *voice, bool include_grace_notes)
135 {
136   Moment now = voice->now_mom ();
137   for (SCM s = voice->get_property ("busyGrobs");
138        scm_is_pair (s); s = scm_cdr (s))
139     {
140       Grob *g = unsmob_grob (scm_cdar (s));;
141       Moment *end_mom = unsmob_moment (scm_caar (s));
142       if (!end_mom || !g)
143         {
144           programming_error ("busyGrobs invalid");
145           continue;
146         }
147
148       if (((end_mom->main_part_ > now.main_part_) ||
149            (include_grace_notes && end_mom->grace_part_ > now.grace_part_))
150           && dynamic_cast<Item *> (g)
151           && Note_head::has_interface (g))
152         {
153           return g;
154         }
155     }
156
157   return 0;
158 }
159
160 void
161 Lyric_engraver::stop_translation_timestep ()
162 {
163   if (text_)
164     {
165       Context *voice = get_voice_to_lyrics (context ());
166
167       if (voice)
168         {
169           bool include_grace_notes = to_boolean (get_property ("includeGraceNotes"));
170           Grob *head = get_current_note_head (voice, include_grace_notes);
171
172           if (head)
173             {
174               text_->set_parent (head, X_AXIS);
175               if (melisma_busy (voice)
176                   && !to_boolean (get_property ("ignoreMelismata")))
177                 text_->set_property ("self-alignment-X",
178                                      get_property ("lyricMelismaAlignment"));
179             }
180           else
181             {
182               text_->warning (_ ("Lyric syllable does not have note. Use \\lyricsto or associatedVoice."));
183               text_->set_property ("X-offset", scm_from_int (0));
184             }
185         }
186
187       last_text_ = text_;
188       text_ = 0;
189     }
190   event_ = 0;
191 }
192
193 ADD_TRANSLATOR (Lyric_engraver,
194                 /* doc */
195                 "Engrave text for lyrics.",
196
197                 /* create */
198                 "LyricText ",
199
200                 /* read */
201                 "ignoreMelismata "
202                 "includeGraceNotes "
203                 "lyricMelismaAlignment ",
204
205                 /* write */
206                 ""
207                 );