]> git.donarmstrong.com Git - lilypond.git/blob - lily/lyric-engraver.cc
(conv): add 2.2.0 rule.
[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 = lyrics->get_property ("associatedVoice");
77   String nm = lyrics->id_string_;
78
79   if (gh_string_p (voice))
80     nm = ly_scm2string (voice);
81   else
82     {
83       int idx = nm.index_last ('-');
84       if (idx >= 0)
85         nm = nm.left_string (idx);
86     }
87
88   Context *c =  lyrics->find_context_below (ly_symbol2scm ("Voice"), nm);
89
90   if (c)
91     return c;
92
93   return lyrics->find_context_below (ly_symbol2scm ("Voice"), "");
94 }
95
96 Grob *
97 get_current_note_head (Context * voice)
98 {
99   for (SCM s = voice->get_property ("busyGrobs");
100        gh_pair_p (s); s = gh_cdr (s))
101     {
102       Item*g = dynamic_cast<Item*> (unsmob_grob (gh_cdar (s)));
103           
104       if (g && !g->get_column ()
105           && Note_head::has_interface (g))
106         return g;
107     }
108           
109   return 0;  
110 }
111
112 void
113 Lyric_engraver::stop_translation_timestep ()
114 {
115   if (text_)
116     {
117       Context * voice = get_voice_to_lyrics (daddy_context_);
118
119       if (voice)
120         {
121           Grob *head = get_current_note_head (voice);
122
123           if (head)
124             {
125               text_->set_parent (head, X_AXIS);
126               if (melisma_busy (voice))
127                 text_->set_property ("self-alignment-X", gh_int2scm (LEFT)); 
128             }
129         }
130       
131       typeset_grob (text_);
132       text_ =0;
133     }
134   event_ =0;
135 }
136
137
138 ENTER_DESCRIPTION (Lyric_engraver,
139 /* descr */       "",
140 /* creats*/       "",
141 /* accepts */     "lyric-event",
142 /* acks  */      "",
143 /* reads */       "",
144 /* write */       "");