Improve handling of nested overrides using list syntax.
[lilypond/mpolesky.git] / lily / lyric-engraver.cc
blob999efaaa75f18ce7fac617fc1f649f0d4ade4a14
1 /*
2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 1997--2010 Han-Wen Nienhuys <hanwen@xs4all.nl>
5 Jan Nieuwenhuizen <janneke@gnu.org>
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.
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.
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/>.
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"
28 #include "translator.icc"
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.
34 class Lyric_engraver : public Engraver
36 protected:
37 void stop_translation_timestep ();
38 void process_music ();
39 DECLARE_TRANSLATOR_LISTENER (lyric);
41 public:
42 TRANSLATOR_DECLARATIONS (Lyric_engraver);
44 private:
45 Stream_event *event_;
46 Item *text_;
47 Item *last_text_;
49 Context *get_voice_context ();
52 Lyric_engraver::Lyric_engraver ()
54 text_ = 0;
55 last_text_ = 0;
56 event_ = 0;
59 IMPLEMENT_TRANSLATOR_LISTENER (Lyric_engraver, lyric);
60 void
61 Lyric_engraver::listen_lyric (Stream_event *ev)
63 ASSIGN_EVENT_ONCE (event_, ev);
66 void
67 Lyric_engraver::process_music ()
69 if (event_)
71 SCM text = event_->get_property ("text");
73 if (ly_is_equal (text, scm_from_locale_string (" ")))
75 if (last_text_)
76 last_text_->set_property ("self-alignment-X", scm_from_int (LEFT));
79 "Empty" LyricText objects are needed to allow the Extender_engraver to
80 distinguish between the end of a lyrics block and manual melismata.
82 text_ = make_item ("LyricText", event_->self_scm ());
86 Context *
87 get_voice_to_lyrics (Context *lyrics)
89 SCM avc = lyrics->get_property ("associatedVoiceContext");
90 if (Context *c = unsmob_context (avc))
91 return c;
93 SCM voice_name = lyrics->get_property ("associatedVoice");
94 string nm = lyrics->id_string ();
96 if (scm_is_string (voice_name))
97 nm = ly_scm2string (voice_name);
98 else if (nm == "")
99 return 0;
100 else
102 ssize idx = nm.rfind ('-');
103 if (idx != NPOS)
104 nm = nm.substr (0, idx);
107 Context *parent = lyrics;
108 Context *voice = 0;
109 while (parent && !voice)
111 voice = find_context_below (parent, ly_symbol2scm ("Voice"), nm);
112 parent = parent->get_parent_context ();
115 if (voice)
116 return voice;
118 parent = lyrics;
119 voice = 0;
120 while (parent && !voice)
122 voice = find_context_below (parent, ly_symbol2scm ("Voice"), "");
123 parent = parent->get_parent_context ();
126 return voice;
129 Grob *
130 get_current_note_head (Context *voice)
132 Moment now = voice->now_mom ();
133 for (SCM s = voice->get_property ("busyGrobs");
134 scm_is_pair (s); s = scm_cdr (s))
136 Grob *g = unsmob_grob (scm_cdar (s));;
137 Moment *end_mom = unsmob_moment (scm_caar (s));
138 if (!end_mom || !g)
140 programming_error ("busyGrobs invalid");
141 continue;
144 if (end_mom->main_part_ > now.main_part_
145 && dynamic_cast<Item *> (g)
146 && Note_head::has_interface (g))
147 return g;
150 return 0;
153 void
154 Lyric_engraver::stop_translation_timestep ()
156 if (text_)
158 Context *voice = get_voice_to_lyrics (context ());
160 if (voice)
162 Grob *head = get_current_note_head (voice);
164 if (head)
166 text_->set_parent (head, X_AXIS);
167 if (melisma_busy (voice)
168 && !to_boolean (get_property ("ignoreMelismata")))
169 text_->set_property ("self-alignment-X",
170 get_property("lyricMelismaAlignment"));
172 else
174 text_->warning (_ ("Lyric syllable does not have note. Use \\lyricsto or associatedVoice."));
175 text_->set_property ("X-offset", scm_from_int (0));
179 last_text_ = text_;
180 text_ = 0;
182 event_ = 0;
185 ADD_TRANSLATOR (Lyric_engraver,
186 /* doc */
187 "Engrave text for lyrics.",
189 /* create */
190 "LyricText ",
192 /* read */
193 "ignoreMelismata "
194 "lyricMelismaAlignment ",
196 /* write */