]> git.donarmstrong.com Git - lilypond.git/blob - lily/lyric-engraver.cc
* input/regression/lyric-extender.ly: simplify.
[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--2004 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7   Jan Nieuwenhuizen <janneke@gnu.org>
8 */
9
10 #include "context.hh"
11 #include "engraver.hh"
12 #include "event.hh"
13 #include "font-metric.hh"
14 #include "item.hh"
15 #include "multi-measure-rest.hh"
16 #include "note-head.hh"
17 #include "rest.hh"
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   virtual void stop_translation_timestep ();
27   virtual bool try_music (Music *);
28   virtual void process_music ();
29
30 public:
31   TRANSLATOR_DECLARATIONS (Lyric_engraver);
32
33 private:
34   Music *event_;
35   Item *text_;
36
37   Context *get_voice_context ();
38 };
39
40 Lyric_engraver::Lyric_engraver ()
41 {
42   text_ = 0;
43   event_ = 0;
44 }
45
46 bool
47 Lyric_engraver::try_music (Music*r)
48 {
49   if (!event_)
50     {
51       event_ = r;
52       return true;
53     }
54   return false;
55 }
56
57 void
58 Lyric_engraver::process_music ()
59 {
60   if (event_)
61     {
62       text_ = make_item ("LyricText", event_->self_scm ());
63       text_->set_property ("text", event_->get_property ("text"));
64     }
65 }
66
67
68 Context*
69 get_voice_to_lyrics (Context *lyrics)
70 {
71   SCM avc = lyrics->get_property ("associatedVoiceContext");
72   if  (Context *c = unsmob_context (avc))
73     return c;
74
75   SCM voice_name = lyrics->get_property ("associatedVoice");
76   String nm = lyrics->id_string ();
77
78   if (scm_is_string (voice_name))
79     nm = ly_scm2string (voice_name);
80   else
81     {
82       int idx = nm.index_last ('-');
83       if (idx >= 0)
84         nm = nm.left_string (idx);
85     }
86
87   Context *parent = lyrics;
88   Context *voice = 0;
89   while (parent && !voice)
90     {
91       voice = find_context_below (parent, ly_symbol2scm ("Voice"), nm);
92       parent = parent->get_parent_context ();
93     }
94
95   if (voice)
96     return voice;
97
98   parent = lyrics;
99   voice = 0;
100   while (parent && !voice)
101     {
102       voice = find_context_below (parent, ly_symbol2scm ("Voice"), "");
103       parent = parent->get_parent_context ();
104     }
105
106   return voice;
107 }
108
109 Grob *
110 get_current_note_head (Context *voice)
111 {
112   for (SCM s = voice->get_property ("busyGrobs");
113        ly_c_pair_p (s); s = ly_cdr (s))
114     {
115       Item *g = dynamic_cast<Item*> (unsmob_grob (ly_cdar (s)));
116         
117       if (g && !g->get_column ()
118           && Note_head::has_interface (g))
119         return g;
120     }
121         
122   return 0;
123 }
124
125 void
126 Lyric_engraver::stop_translation_timestep ()
127 {
128   if (text_)
129     {
130       Context *voice = get_voice_to_lyrics (context ());
131
132       if (voice)
133         {
134           Grob *head = get_current_note_head (voice);
135
136           if (head)
137             {
138               text_->set_parent (head, X_AXIS);
139               if (melisma_busy (voice))
140                 text_->set_property ("self-alignment-X", scm_int2num (LEFT));
141             }
142         }
143
144       text_ = 0;
145     }
146   event_ = 0;
147 }
148
149
150 ENTER_DESCRIPTION (Lyric_engraver,
151 /* descr */       "",
152 /* creats*/       "LyricText",
153 /* accepts */     "lyric-event",
154 /* acks  */      "",
155 /* reads */       "",
156 /* write */       "");