]> git.donarmstrong.com Git - lilypond.git/blob - lily/hyphen-engraver.cc
*** empty log message ***
[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 Hyphen_engraver::Hyphen_engraver ()
34 {
35   hyphen_ = 0;
36   finished_hyphen_ = 0;
37   ev_ = 0;
38 }
39
40 void
41 Hyphen_engraver::acknowledge_grob (Grob_info i)
42 {
43   Item *item = dynamic_cast<Item *> (i.grob_);
44   // -> Text_item
45   if (item && item->internal_has_interface (ly_symbol2scm ("lyric-syllable-interface")))
46     {
47       if (hyphen_)
48         hyphen_->set_bound (LEFT, item);
49
50       if (finished_hyphen_)
51         finished_hyphen_->set_bound (RIGHT, item);
52     }
53 }
54
55 bool
56 Hyphen_engraver::try_music (Music *r)
57 {
58   if (ev_)
59     return false;
60
61   ev_ = r;
62   return true;
63 }
64
65 void
66 completize_hyphen (Spanner *sp)
67 {
68   if (!sp->get_bound (RIGHT))
69     {
70       SCM heads = sp->get_property ("heads");
71       if (scm_is_pair (heads))
72         {
73           Item *it = dynamic_cast<Item *> (unsmob_grob (scm_car (heads)));
74           if (it)
75             sp->set_bound (RIGHT, it);
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 ADD_TRANSLATOR (Hyphen_engraver,
140                 /* descr */ "Create lyric hyphens",
141                 /* creats*/ "LyricHyphen",
142                 /* accepts */ "hyphen-event",
143                 /* acks  */ "lyric-syllable-interface",
144                 /* reads */ "",
145                 /* write */ "");