]> git.donarmstrong.com Git - lilypond.git/blob - lily/lyric-engraver.cc
0aa5fe3c97baa474ef7156c9e041fbe5bd57e169
[lilypond.git] / lily / lyric-engraver.cc
1 /*
2   lyric-engraver.cc -- implement Lyric_engraver
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1997--2007 Han-Wen Nienhuys <hanwen@xs4all.nl>
7   Jan Nieuwenhuizen <janneke@gnu.org>
8 */
9
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"
16
17 #include "translator.icc"
18
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.
22 */
23 class Lyric_engraver : public Engraver
24 {
25 protected:
26   void stop_translation_timestep ();
27   void process_music ();
28   DECLARE_TRANSLATOR_LISTENER (lyric);
29
30 public:
31   TRANSLATOR_DECLARATIONS (Lyric_engraver);
32
33 private:
34   Stream_event *event_;
35   Item *text_;
36   Item *last_text_;
37
38   Context *get_voice_context ();
39 };
40
41 Lyric_engraver::Lyric_engraver ()
42 {
43   text_ = 0;
44   last_text_ = 0;
45   event_ = 0;
46 }
47
48 IMPLEMENT_TRANSLATOR_LISTENER (Lyric_engraver, lyric);
49 void
50 Lyric_engraver::listen_lyric (Stream_event *ev)
51 {
52   ASSIGN_EVENT_ONCE (event_, ev);
53 }
54
55 void
56 Lyric_engraver::process_music ()
57 {
58   if (event_)
59     {
60       SCM text = event_->get_property ("text");
61
62       if (ly_is_equal (text, scm_from_locale_string (" ")))
63         {
64           if (last_text_)
65             last_text_->set_property ("self-alignment-X", scm_from_int (LEFT));
66         }
67       else
68         {
69           text_ = make_item ("LyricText", event_->self_scm ());
70         }
71     }
72 }
73
74 Context *
75 get_voice_to_lyrics (Context *lyrics)
76 {
77   SCM avc = lyrics->get_property ("associatedVoiceContext");
78   if (Context *c = unsmob_context (avc))
79     return c;
80
81   SCM voice_name = lyrics->get_property ("associatedVoice");
82   string nm = lyrics->id_string ();
83
84   if (scm_is_string (voice_name))
85     nm = ly_scm2string (voice_name);
86   else if (nm == "")
87     {
88       return 0;
89     }
90   else
91     {
92       ssize idx = nm.rfind ('-');
93       if (idx != NPOS)
94         nm = nm.substr (0, idx);
95     }
96
97   Context *parent = lyrics;
98   Context *voice = 0;
99   while (parent && !voice)
100     {
101       voice = find_context_below (parent, ly_symbol2scm ("Voice"), nm);
102       parent = parent->get_parent_context ();
103     }
104
105   if (voice)
106     return voice;
107
108   parent = lyrics;
109   voice = 0;
110   while (parent && !voice)
111     {
112       voice = find_context_below (parent, ly_symbol2scm ("Voice"), "");
113       parent = parent->get_parent_context ();
114     }
115
116   return voice;
117 }
118
119 Grob *
120 get_current_note_head (Context *voice)
121 {
122   Moment now = voice->now_mom ();
123   for (SCM s = voice->get_property ("busyGrobs");
124        scm_is_pair (s); s = scm_cdr (s))
125     {
126       Grob *g = unsmob_grob (scm_cdar (s));;
127       Moment *end_mom = unsmob_moment (scm_caar (s));
128       if (!end_mom || !g)
129         {
130           programming_error ("busyGrobs invalid");
131           continue;
132         }
133       
134       if (end_mom->main_part_ > now.main_part_
135           && dynamic_cast<Item *> (g)
136           && Note_head::has_interface (g))
137         return g;
138     }
139
140   return 0;
141 }
142
143 void
144 Lyric_engraver::stop_translation_timestep ()
145 {
146   if (text_)
147     {
148       Context *voice = get_voice_to_lyrics (context ());
149  
150       if (voice)
151         {
152           Grob *head = get_current_note_head (voice);
153
154           if (head)
155             {
156               text_->set_parent (head, X_AXIS);
157               if (melisma_busy (voice))
158                 text_->set_property ("self-alignment-X", get_property("lyricMelismaAlignment"));
159             }
160           else
161             {
162               text_->warning (_ ("Lyric syllable does not have note. Use \\lyricsto or associatedVoice."));
163               text_->set_property ("X-offset", scm_from_int (0));
164             }
165         }
166
167       last_text_ = text_;
168       text_ = 0;
169     }
170   event_ = 0;
171 }
172
173 ADD_TRANSLATOR (Lyric_engraver,
174                 /* doc */
175                 "Engrave text for lyrics.",
176
177                 /* create */
178                 "LyricText ",
179                 /* read */
180                 "lyricMelismaAlignment ",
181
182                 /* write */
183                 ""
184                 );