]> git.donarmstrong.com Git - lilypond.git/blob - lily/hyphen-engraver.cc
* lily/include/translator.icc: new file.
[lilypond.git] / lily / hyphen-engraver.cc
1 /*
2   hyphen-engraver.cc -- implement Hyphen_engraver
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1999--2005 Glen Prideaux <glenprideaux@iname.com>,
7   Han-Wen Nienhuys <hanwen@cs.uu.nl>,
8   Jan Nieuwenhuizen <janneke@gnu.org>
9 */
10
11 #include "engraver.hh"
12
13 #include "warn.hh"
14 #include "item.hh"
15 #include "spanner.hh"
16 #include "pointer-group-interface.hh"
17
18 class Hyphen_engraver : public Engraver
19 {
20   Music *ev_;
21   Spanner *hyphen_;
22   Spanner *finished_hyphen_;
23 public:
24   TRANSLATOR_DECLARATIONS (Hyphen_engraver);
25
26 protected:
27   virtual void acknowledge_grob (Grob_info);
28   virtual void finalize ();
29   virtual bool try_music (Music *);
30   PRECOMPUTED_VIRTUAL void stop_translation_timestep ();
31   PRECOMPUTED_VIRTUAL void process_music ();
32 private:
33 };
34
35 Hyphen_engraver::Hyphen_engraver ()
36 {
37   hyphen_ = 0;
38   finished_hyphen_ = 0;
39   ev_ = 0;
40 }
41
42 void
43 Hyphen_engraver::acknowledge_grob (Grob_info i)
44 {
45   Item *item = dynamic_cast<Item *> (i.grob ());
46   // -> Text_item
47   if (item && item->internal_has_interface (ly_symbol2scm ("lyric-syllable-interface")))
48     {
49       if (hyphen_)
50         hyphen_->set_bound (LEFT, item);
51
52       if (finished_hyphen_)
53         finished_hyphen_->set_bound (RIGHT, item);
54     }
55 }
56
57 bool
58 Hyphen_engraver::try_music (Music *r)
59 {
60   if (ev_)
61     return false;
62
63   ev_ = r;
64   return true;
65 }
66
67 void
68 completize_hyphen (Spanner *sp)
69 {
70   if (!sp->get_bound (RIGHT))
71     {
72       extract_item_set (sp, "heads", heads);
73       if (heads.size ())
74         {
75           sp->set_bound (RIGHT, heads.top ());
76         }
77     }
78 }
79
80 void
81 Hyphen_engraver::finalize ()
82 {
83   if (hyphen_)
84     {
85       completize_hyphen (hyphen_);
86
87       if (!hyphen_->get_bound (RIGHT))
88         {
89           hyphen_->warning (_ ("removing unterminated hyphen"));
90           hyphen_->suicide ();
91         }
92
93       hyphen_ = 0;
94     }
95
96   if (finished_hyphen_)
97     {
98       completize_hyphen (finished_hyphen_);
99
100       if (!finished_hyphen_->get_bound (RIGHT))
101         {
102           finished_hyphen_->warning (_ ("unterminated hyphen; removing"));
103           finished_hyphen_->suicide ();
104         }
105       finished_hyphen_ = 0;
106     }
107 }
108
109 void
110 Hyphen_engraver::process_music ()
111 {
112   if (ev_)
113     {
114       hyphen_ = make_spanner ("LyricHyphen", ev_->self_scm ());
115     }
116 }
117
118 void
119 Hyphen_engraver::stop_translation_timestep ()
120 {
121   if (finished_hyphen_ && finished_hyphen_->get_bound (RIGHT))
122     {
123       finished_hyphen_ = 0;
124     }
125
126   if (finished_hyphen_ && hyphen_)
127     {
128       programming_error ("hyphen not finished yet");
129       finished_hyphen_ = 0;
130     }
131
132   if (hyphen_)
133     finished_hyphen_ = hyphen_;
134   hyphen_ = 0;
135
136   ev_ = 0;
137 }
138
139 #include "translator.icc"
140
141 ADD_TRANSLATOR (Hyphen_engraver,
142                 /* descr */ "Create lyric hyphens",
143                 /* creats*/ "LyricHyphen",
144                 /* accepts */ "hyphen-event",
145                 /* acks  */ "lyric-syllable-interface",
146                 /* reads */ "",
147                 /* write */ "");