]> git.donarmstrong.com Git - lilypond.git/blob - lily/key-engraver.cc
* flower
[lilypond.git] / lily / key-engraver.cc
1 /*
2   key-engraver.cc -- implement Key_engraver
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1997--2005 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8
9 #include "item.hh"
10 #include "bar-line.hh"
11 #include "staff-symbol-referencer.hh"
12 #include "context.hh"
13 #include "engraver.hh"
14 #include "protected-scm.hh"
15 #include "clef.hh"
16
17 /*
18   TODO: The representation  of key sigs is all fucked.
19 */
20
21 /**
22    Make the key signature.
23 */
24 class Key_engraver : public Engraver
25 {
26   void create_key (bool);
27   void read_ev (Music const *r);
28
29   Music *key_ev_;
30   Item *item_;
31   Item *cancellation_;
32 public:
33   TRANSLATOR_DECLARATIONS (Key_engraver);
34
35 protected:
36   virtual void initialize ();
37   virtual void finalize ();
38   virtual bool try_music (Music *ev);
39   virtual void stop_translation_timestep ();
40   virtual void start_translation_timestep ();
41   virtual void process_music ();
42   virtual void acknowledge_grob (Grob_info);
43 };
44
45 void
46 Key_engraver::finalize ()
47 {
48 }
49
50 Key_engraver::Key_engraver ()
51 {
52   key_ev_ = 0;
53   item_ = 0;
54   cancellation_ = 0;
55 }
56
57 void
58 Key_engraver::create_key (bool def)
59 {
60   if (!item_)
61     {
62       item_ = make_item ("KeySignature", key_ev_ ? key_ev_->self_scm () : SCM_EOL);
63
64       item_->set_property ("c0-position",
65                            get_property ("middleCPosition"));
66
67       SCM last = get_property ("lastKeySignature");
68       SCM key = get_property ("keySignature");
69       if (to_boolean (get_property ("printKeyCancellation"))
70           && !scm_is_eq (last, key))
71         {
72           cancellation_ = make_item ("KeyCancellation", key_ev_ ? key_ev_->self_scm () : SCM_EOL);
73           cancellation_->set_property ("old-accidentals", last);
74           cancellation_->set_property ("c0-position",
75                                        get_property ("middleCPosition"));
76
77         }
78       item_->set_property ("new-accidentals", key);
79     }
80
81   if (!def)
82     {
83       SCM vis = get_property ("explicitKeySignatureVisibility");
84       if (ly_c_procedure_p (vis))
85         item_->set_property ("break-visibility", vis);
86     }
87 }
88
89 bool
90 Key_engraver::try_music (Music *ev)
91 {
92   if (ev->is_mus_type ("key-change-event"))
93     {
94       /* do this only once, just to be on the safe side.  */
95       if (!key_ev_)
96         {
97           key_ev_ = ev;
98           read_ev (key_ev_);
99         }
100       return true;
101     }
102   return false;
103 }
104
105 void
106 Key_engraver::acknowledge_grob (Grob_info info)
107 {
108   if (Clef::has_interface (info.grob_))
109     {
110       SCM c = get_property ("createKeyOnClefChange");
111       if (to_boolean (c))
112         {
113           create_key (false);
114         }
115     }
116   else if (Bar_line::has_interface (info.grob_)
117            && scm_is_pair (get_property ("keySignature")))
118     {
119       create_key (true);
120     }
121 }
122
123 void
124 Key_engraver::process_music ()
125 {
126   if (key_ev_
127       || get_property ("lastKeySignature") != get_property ("keySignature"))
128     create_key (false);
129 }
130
131 void
132 Key_engraver::stop_translation_timestep ()
133 {
134   item_ = 0;
135   context ()->set_property ("lastKeySignature", get_property ("keySignature"));
136   cancellation_ = 0;
137 }
138
139 void
140 Key_engraver::read_ev (Music const *r)
141 {
142   SCM p = r->get_property ("pitch-alist");
143   if (!scm_is_pair (p))
144     return;
145
146   SCM n = scm_list_copy (p);
147   SCM accs = SCM_EOL;
148   for (SCM s = get_property ("keyAccidentalOrder");
149        scm_is_pair (s); s = scm_cdr (s))
150     {
151       if (scm_is_pair (scm_member (scm_car (s), n)))
152         {
153           accs = scm_cons (scm_car (s), accs);
154           n = scm_delete_x (scm_car (s), n);
155         }
156     }
157
158   for (SCM s = n; scm_is_pair (s); s = scm_cdr (s))
159     if (scm_to_int (scm_cdar (s)))
160       accs = scm_cons (scm_car (s), accs);
161
162   context ()->set_property ("keySignature", accs);
163   context ()->set_property ("tonic",
164                             r->get_property ("tonic"));
165 }
166
167 void
168 Key_engraver::start_translation_timestep ()
169 {
170   key_ev_ = 0;
171 }
172
173 void
174 Key_engraver::initialize ()
175 {
176   context ()->set_property ("keySignature", SCM_EOL);
177   context ()->set_property ("lastKeySignature", SCM_EOL);
178
179   Pitch p (0, 0, 0);
180   context ()->set_property ("tonic", p.smobbed_copy ());
181
182 }
183
184 ADD_TRANSLATOR (Key_engraver,
185                 /* descr */ "",
186                 /* creats*/ "KeySignature",
187                 /* accepts */ "key-change-event",
188                 /* acks  */ "bar-line-interface clef-interface",
189                 /* reads */ "keySignature printKeyCancellation lastKeySignature explicitKeySignatureVisibility createKeyOnClefChange keyAccidentalOrder keySignature",
190                 /* write */ "lastKeySignature tonic keySignature");