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