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