Fixes Issue 1504, allowing feather beam line breaking.
[lilypond/patrick.git] / lily / lyric-engraver.cc
blobffae2783ba2d409ea3b6ccf445ef6a3476feff72
1 /*
2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 1997--2011 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",
77 get_property ("lyricMelismaAlignment"));
79 else
80 text_ = make_item ("LyricText", event_->self_scm ());
84 Context *
85 get_voice_to_lyrics (Context *lyrics)
87 SCM avc = lyrics->get_property ("associatedVoiceContext");
88 if (Context *c = unsmob_context (avc))
89 return c;
91 SCM voice_name = lyrics->get_property ("associatedVoice");
92 string nm = lyrics->id_string ();
94 if (scm_is_string (voice_name))
95 nm = ly_scm2string (voice_name);
96 else if (nm == "")
97 return 0;
98 else
100 ssize idx = nm.rfind ('-');
101 if (idx != NPOS)
102 nm = nm.substr (0, idx);
105 Context *parent = lyrics;
106 Context *voice = 0;
107 while (parent && !voice)
109 voice = find_context_below (parent, ly_symbol2scm ("Voice"), nm);
110 parent = parent->get_parent_context ();
113 if (voice)
114 return voice;
116 parent = lyrics;
117 voice = 0;
118 while (parent && !voice)
120 voice = find_context_below (parent, ly_symbol2scm ("Voice"), "");
121 parent = parent->get_parent_context ();
124 return voice;
127 Grob *
128 get_current_note_head (Context *voice, bool include_grace_notes)
130 Moment now = voice->now_mom ();
131 for (SCM s = voice->get_property ("busyGrobs");
132 scm_is_pair (s); s = scm_cdr (s))
134 Grob *g = unsmob_grob (scm_cdar (s));;
135 Moment *end_mom = unsmob_moment (scm_caar (s));
136 if (!end_mom || !g)
138 programming_error ("busyGrobs invalid");
139 continue;
142 if (((end_mom->main_part_ > now.main_part_) ||
143 (include_grace_notes && end_mom->grace_part_ > now.grace_part_))
144 && dynamic_cast<Item *> (g)
145 && Note_head::has_interface (g))
147 return g;
151 return 0;
154 void
155 Lyric_engraver::stop_translation_timestep ()
157 if (text_)
159 Context *voice = get_voice_to_lyrics (context ());
161 if (voice)
163 bool include_grace_notes = to_boolean (get_property ("includeGraceNotes"));
164 Grob *head = get_current_note_head (voice, include_grace_notes);
166 if (head)
168 text_->set_parent (head, X_AXIS);
169 if (melisma_busy (voice)
170 && !to_boolean (get_property ("ignoreMelismata")))
171 text_->set_property ("self-alignment-X",
172 get_property ("lyricMelismaAlignment"));
174 else
176 text_->warning (_ ("Lyric syllable does not have note. Use \\lyricsto or associatedVoice."));
177 text_->set_property ("X-offset", scm_from_int (0));
181 last_text_ = text_;
182 text_ = 0;
184 event_ = 0;
187 ADD_TRANSLATOR (Lyric_engraver,
188 /* doc */
189 "Engrave text for lyrics.",
191 /* create */
192 "LyricText ",
194 /* read */
195 "ignoreMelismata "
196 "includeGraceNotes "
197 "lyricMelismaAlignment ",
199 /* write */