(readPostTable): ugh. Kludge: nglyphs in maxp
[lilypond.git] / lily / lyric-engraver.cc
blob340a5d914cdff0e1cab9149d9b53ccc57e2d908a
1 /*
2 lyric-engraver.cc -- implement Lyric_engraver
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2005 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 Jan Nieuwenhuizen <janneke@gnu.org>
8 */
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"
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.
22 class Lyric_engraver : public Engraver
24 protected:
25 virtual void stop_translation_timestep ();
26 virtual bool try_music (Music *);
27 virtual void process_music ();
29 public:
30 TRANSLATOR_DECLARATIONS (Lyric_engraver);
32 private:
33 Music *event_;
34 Item *text_;
36 Context *get_voice_context ();
39 Lyric_engraver::Lyric_engraver ()
41 text_ = 0;
42 event_ = 0;
45 bool
46 Lyric_engraver::try_music (Music *r)
48 if (!event_)
50 event_ = r;
51 return true;
53 return false;
56 void
57 Lyric_engraver::process_music ()
59 if (event_)
61 text_ = make_item ("LyricText", event_->self_scm ());
62 text_->set_property ("text", event_->get_property ("text"));
66 Context *
67 get_voice_to_lyrics (Context *lyrics)
69 SCM avc = lyrics->get_property ("associatedVoiceContext");
70 if (Context *c = unsmob_context (avc))
71 return c;
73 SCM voice_name = lyrics->get_property ("associatedVoice");
74 String nm = lyrics->id_string ();
76 if (scm_is_string (voice_name))
77 nm = ly_scm2string (voice_name);
78 else
80 int idx = nm.index_last ('-');
81 if (idx >= 0)
82 nm = nm.left_string (idx);
85 Context *parent = lyrics;
86 Context *voice = 0;
87 while (parent && !voice)
89 voice = find_context_below (parent, ly_symbol2scm ("Voice"), nm);
90 parent = parent->get_parent_context ();
93 if (voice)
94 return voice;
96 parent = lyrics;
97 voice = 0;
98 while (parent && !voice)
100 voice = find_context_below (parent, ly_symbol2scm ("Voice"), "");
101 parent = parent->get_parent_context ();
104 return voice;
107 Grob *
108 get_current_note_head (Context *voice)
110 for (SCM s = voice->get_property ("busyGrobs");
111 scm_is_pair (s); s = scm_cdr (s))
113 Item *g = dynamic_cast<Item *> (unsmob_grob (scm_cdar (s)));
115 if (g && !g->get_column ()
116 && Note_head::has_interface (g))
117 return g;
120 return 0;
123 void
124 Lyric_engraver::stop_translation_timestep ()
126 if (text_)
128 Context *voice = get_voice_to_lyrics (context ());
130 if (voice)
132 Grob *head = get_current_note_head (voice);
134 if (head)
136 text_->set_parent (head, X_AXIS);
137 if (melisma_busy (voice))
138 text_->set_property ("self-alignment-X", scm_int2num (LEFT));
142 text_ = 0;
144 event_ = 0;
147 ADD_TRANSLATOR (Lyric_engraver,
148 /* descr */ "",
149 /* creats*/ "LyricText",
150 /* accepts */ "lyric-event",
151 /* acks */ "",
152 /* reads */ "",
153 /* write */ "");