* lily/include/lily-guile.hh: many new ly_ functions. Thanks to
[lilypond.git] / lily / lyric-engraver.cc
blob361d73a9aecbc9c01298d699d65fffa82769219d
1 /*
2 lyric-engraver.cc -- implement Lyric_engraver
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2004 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 Jan Nieuwenhuizen <janneke@gnu.org>
8 */
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"
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.
21 class Lyric_engraver : public Engraver
23 protected:
24 virtual void stop_translation_timestep ();
25 virtual bool try_music (Music *);
26 virtual void process_music ();
28 public:
29 TRANSLATOR_DECLARATIONS (Lyric_engraver);
30 private:
31 Music * event_;
32 Item* text_;
34 Context* get_voice_context ();
37 Lyric_engraver::Lyric_engraver ()
39 text_ =0;
40 event_ =0;
43 bool
44 Lyric_engraver::try_music (Music*r)
46 if (r->is_mus_type ("lyric-event"))
48 if (event_)
49 return false;
50 event_ =r;
51 return true;
53 return false;
56 void
57 Lyric_engraver::process_music ()
59 if (event_)
61 text_= make_item ("LyricText");
63 text_->set_property ("text", event_->get_property ("text"));
64 announce_grob (text_, event_->self_scm ());
69 Context*
70 get_voice_to_lyrics (Context *lyrics)
72 SCM avc = lyrics->get_property ("associatedVoiceContext");
73 if (Context *c = unsmob_context (avc))
74 return c;
76 SCM voice = lyrics->get_property ("associatedVoice");
77 String nm = lyrics->id_string_;
79 if (ly_string_p (voice))
80 nm = ly_scm2string (voice);
81 else
83 int idx = nm.index_last ('-');
84 if (idx >= 0)
85 nm = nm.left_string (idx);
88 Context *c = lyrics->find_context_below (ly_symbol2scm ("Voice"), nm);
90 if (c)
91 return c;
93 return lyrics->find_context_below (ly_symbol2scm ("Voice"), "");
96 Grob *
97 get_current_note_head (Context * voice)
99 for (SCM s = voice->get_property ("busyGrobs");
100 ly_pair_p (s); s = ly_cdr (s))
102 Item*g = dynamic_cast<Item*> (unsmob_grob (ly_cdar (s)));
104 if (g && !g->get_column ()
105 && Note_head::has_interface (g))
106 return g;
109 return 0;
112 void
113 Lyric_engraver::stop_translation_timestep ()
115 if (text_)
117 Context * voice = get_voice_to_lyrics (daddy_context_);
119 if (voice)
121 Grob *head = get_current_note_head (voice);
123 if (head)
125 text_->set_parent (head, X_AXIS);
126 if (melisma_busy (voice))
127 text_->set_property ("self-alignment-X", scm_int2num (LEFT));
131 typeset_grob (text_);
132 text_ =0;
134 event_ =0;
138 ENTER_DESCRIPTION (Lyric_engraver,
139 /* descr */ "",
140 /* creats*/ "",
141 /* accepts */ "lyric-event",
142 /* acks */ "",
143 /* reads */ "",
144 /* write */ "");