]> git.donarmstrong.com Git - lilypond.git/blob - lily/hyphen-engraver.cc
* flower
[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 "warn.hh"
12 #include "item.hh"
13 #include "engraver.hh"
14 #include "spanner.hh"
15
16 class Hyphen_engraver : public Engraver
17 {
18   Music *ev_;
19   Spanner *hyphen_;
20   Spanner *finished_hyphen_;
21 public:
22   TRANSLATOR_DECLARATIONS (Hyphen_engraver);
23
24 protected:
25   virtual void acknowledge_grob (Grob_info);
26   virtual void finalize ();
27   virtual bool try_music (Music *);
28   virtual void stop_translation_timestep ();
29   virtual void process_music ();
30 private:
31
32 };
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       SCM heads = sp->get_property ("heads");
73       if (scm_is_pair (heads))
74         {
75           Item *it = dynamic_cast<Item *> (unsmob_grob (scm_car (heads)));
76           if (it)
77             sp->set_bound (RIGHT, it);
78         }
79     }
80 }
81
82 void
83 Hyphen_engraver::finalize ()
84 {
85   if (hyphen_)
86     {
87       completize_hyphen (hyphen_);
88
89       if (!hyphen_->get_bound (RIGHT))
90         {
91           hyphen_->warning (_ ("removing unterminated hyphen"));
92           hyphen_->suicide ();
93         }
94
95       hyphen_ = 0;
96     }
97
98   if (finished_hyphen_)
99     {
100       completize_hyphen (finished_hyphen_);
101
102       if (!finished_hyphen_->get_bound (RIGHT))
103         {
104           finished_hyphen_->warning (_ ("unterminated hyphen; removing"));
105           finished_hyphen_->suicide ();
106         }
107       finished_hyphen_ = 0;
108     }
109 }
110
111 void
112 Hyphen_engraver::process_music ()
113 {
114   if (ev_)
115     {
116       hyphen_ = make_spanner ("LyricHyphen", ev_->self_scm ());
117     }
118 }
119
120 void
121 Hyphen_engraver::stop_translation_timestep ()
122 {
123   if (finished_hyphen_ && finished_hyphen_->get_bound (RIGHT))
124     {
125       finished_hyphen_ = 0;
126     }
127
128   if (finished_hyphen_ && hyphen_)
129     {
130       programming_error ("Haven't finished hyphen yet.");
131       finished_hyphen_ = 0;
132     }
133
134   if (hyphen_)
135     finished_hyphen_ = hyphen_;
136   hyphen_ = 0;
137
138   ev_ = 0;
139 }
140
141
142 ADD_TRANSLATOR (Hyphen_engraver,
143                 /* descr */ "Create lyric hyphens",
144                 /* creats*/ "LyricHyphen",
145                 /* accepts */ "hyphen-event",
146                 /* acks  */ "lyric-syllable-interface",
147                 /* reads */ "",
148                 /* write */ "");