Move ambitus print callback to scheme.
[lilypond/mpolesky.git] / lily / lyric-engraver.cc
blob74d15e4df1faedef65fddc857260b3fe33c46c5e
1 /*
2 lyric-engraver.cc -- implement Lyric_engraver
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2009 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 Jan Nieuwenhuizen <janneke@gnu.org>
8 */
10 #include "context.hh"
11 #include "engraver.hh"
12 #include "item.hh"
13 #include "note-head.hh"
14 #include "stream-event.hh"
15 #include "international.hh"
17 #include "translator.icc"
19 /**
20 Generate texts for lyric syllables. We only do one lyric at a time.
21 Multiple copies of this engraver should be used to do multiple voices.
23 class Lyric_engraver : public Engraver
25 protected:
26 void stop_translation_timestep ();
27 void process_music ();
28 DECLARE_TRANSLATOR_LISTENER (lyric);
30 public:
31 TRANSLATOR_DECLARATIONS (Lyric_engraver);
33 private:
34 Stream_event *event_;
35 Item *text_;
36 Item *last_text_;
38 Context *get_voice_context ();
41 Lyric_engraver::Lyric_engraver ()
43 text_ = 0;
44 last_text_ = 0;
45 event_ = 0;
48 IMPLEMENT_TRANSLATOR_LISTENER (Lyric_engraver, lyric);
49 void
50 Lyric_engraver::listen_lyric (Stream_event *ev)
52 ASSIGN_EVENT_ONCE (event_, ev);
55 void
56 Lyric_engraver::process_music ()
58 if (event_)
60 SCM text = event_->get_property ("text");
62 if (ly_is_equal (text, scm_from_locale_string (" ")))
64 if (last_text_)
65 last_text_->set_property ("self-alignment-X", scm_from_int (LEFT));
67 else
68 text_ = make_item ("LyricText", event_->self_scm ());
72 Context *
73 get_voice_to_lyrics (Context *lyrics)
75 SCM avc = lyrics->get_property ("associatedVoiceContext");
76 if (Context *c = unsmob_context (avc))
77 return c;
79 SCM voice_name = lyrics->get_property ("associatedVoice");
80 string nm = lyrics->id_string ();
82 if (scm_is_string (voice_name))
83 nm = ly_scm2string (voice_name);
84 else if (nm == "")
85 return 0;
86 else
88 ssize idx = nm.rfind ('-');
89 if (idx != NPOS)
90 nm = nm.substr (0, idx);
93 Context *parent = lyrics;
94 Context *voice = 0;
95 while (parent && !voice)
97 voice = find_context_below (parent, ly_symbol2scm ("Voice"), nm);
98 parent = parent->get_parent_context ();
101 if (voice)
102 return voice;
104 parent = lyrics;
105 voice = 0;
106 while (parent && !voice)
108 voice = find_context_below (parent, ly_symbol2scm ("Voice"), "");
109 parent = parent->get_parent_context ();
112 return voice;
115 Grob *
116 get_current_note_head (Context *voice)
118 Moment now = voice->now_mom ();
119 for (SCM s = voice->get_property ("busyGrobs");
120 scm_is_pair (s); s = scm_cdr (s))
122 Grob *g = unsmob_grob (scm_cdar (s));;
123 Moment *end_mom = unsmob_moment (scm_caar (s));
124 if (!end_mom || !g)
126 programming_error ("busyGrobs invalid");
127 continue;
130 if (end_mom->main_part_ > now.main_part_
131 && dynamic_cast<Item *> (g)
132 && Note_head::has_interface (g))
133 return g;
136 return 0;
139 void
140 Lyric_engraver::stop_translation_timestep ()
142 if (text_)
144 Context *voice = get_voice_to_lyrics (context ());
146 if (voice)
148 Grob *head = get_current_note_head (voice);
150 if (head)
152 text_->set_parent (head, X_AXIS);
153 if (melisma_busy (voice)
154 && !to_boolean (get_property ("ignoreMelismata")))
155 text_->set_property ("self-alignment-X",
156 get_property("lyricMelismaAlignment"));
158 else
160 text_->warning (_ ("Lyric syllable does not have note. Use \\lyricsto or associatedVoice."));
161 text_->set_property ("X-offset", scm_from_int (0));
165 last_text_ = text_;
166 text_ = 0;
168 event_ = 0;
171 ADD_TRANSLATOR (Lyric_engraver,
172 /* doc */
173 "Engrave text for lyrics.",
175 /* create */
176 "LyricText ",
178 /* read */
179 "ignoreMelismata "
180 "lyricMelismaAlignment ",
182 /* write */