]> git.donarmstrong.com Git - lilypond.git/blob - lily/lyric-engraver.cc
* lily/include/lily-guile.hh: compatibility glue for 1.6
[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 (event_)
47     return false;
48   event_ =r;
49   return true;
50 }
51
52 void
53 Lyric_engraver::process_music ()
54 {
55   if (event_)
56     {
57       text_=  make_item ("LyricText",event_->self_scm ());
58       
59       text_->set_property ("text", event_->get_property ("text"));
60     }
61 }
62
63
64 Context*
65 get_voice_to_lyrics (Context *lyrics)
66 {
67   SCM avc = lyrics->get_property ("associatedVoiceContext");
68   if  (Context *c = unsmob_context (avc))
69     return c;
70
71   SCM voice_name = lyrics->get_property ("associatedVoice");
72   String nm = lyrics->id_string ();
73
74   if (scm_is_string (voice_name))
75     nm = ly_scm2string (voice_name);
76   else
77     {
78       int idx = nm.index_last ('-');
79       if (idx >= 0)
80         nm = nm.left_string (idx);
81     }
82
83   Context *parent = lyrics;
84   Context *voice = 0; 
85   while (parent && !voice)
86     {
87       voice = find_context_below (parent, ly_symbol2scm ("Voice"), nm);
88       parent = parent->get_parent_context ();
89     }
90
91   if (voice)
92     return voice;
93
94   parent = lyrics;
95   voice = 0; 
96   while (parent && !voice)
97     {
98       voice = find_context_below (parent, ly_symbol2scm ("Voice"), "");
99       parent = parent->get_parent_context ();
100     }
101
102   return voice;
103 }
104
105 Grob *
106 get_current_note_head (Context * voice)
107 {
108   for (SCM s = voice->get_property ("busyGrobs");
109        ly_c_pair_p (s); s = ly_cdr (s))
110     {
111       Item*g = dynamic_cast<Item*> (unsmob_grob (ly_cdar (s)));
112           
113       if (g && !g->get_column ()
114           && Note_head::has_interface (g))
115         return g;
116     }
117           
118   return 0;  
119 }
120
121 void
122 Lyric_engraver::stop_translation_timestep ()
123 {
124   if (text_)
125     {
126       Context * voice = get_voice_to_lyrics (context ());
127
128       if (voice)
129         {
130           Grob *head = get_current_note_head (voice);
131
132           if (head)
133             {
134               text_->set_parent (head, X_AXIS);
135               if (melisma_busy (voice))
136                 text_->set_property ("self-alignment-X", scm_int2num (LEFT)); 
137             }
138         }
139       
140       text_ =0;
141     }
142   event_ =0;
143 }
144
145
146 ENTER_DESCRIPTION (Lyric_engraver,
147 /* descr */       "",
148 /* creats*/       "LyricText",
149 /* accepts */     "lyric-event",
150 /* acks  */      "",
151 /* reads */       "",
152 /* write */       "");