]> git.donarmstrong.com Git - lilypond.git/blob - lily/key-engraver.cc
Update.
[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       item_->set_property ("new-accidentals", key);
78     }
79
80   if (!def)
81     {
82       SCM vis = get_property ("explicitKeySignatureVisibility");
83       if (ly_c_procedure_p (vis))
84         item_->set_property ("break-visibility", vis);
85     }
86 }
87
88 bool
89 Key_engraver::try_music (Music *ev)
90 {
91   if (ev->is_mus_type ("key-change-event"))
92     {
93       /* do this only once, just to be on the safe side.  */
94       if (!key_ev_)
95         {
96           key_ev_ = ev;
97           read_ev (key_ev_);
98         }
99       return true;
100     }
101   return false;
102 }
103
104 void
105 Key_engraver::acknowledge_grob (Grob_info info)
106 {
107   if (Clef::has_interface (info.grob_))
108     {
109       SCM c = get_property ("createKeyOnClefChange");
110       if (to_boolean (c))
111         {
112           create_key (false);
113         }
114     }
115   else if (Bar_line::has_interface (info.grob_)
116            && scm_is_pair (get_property ("keySignature")))
117     {
118       create_key (true);
119     }
120 }
121
122 void
123 Key_engraver::process_music ()
124 {
125   if (key_ev_
126       || get_property ("lastKeySignature") != get_property ("keySignature"))
127     create_key (false);
128 }
129
130 void
131 Key_engraver::stop_translation_timestep ()
132 {
133   item_ = 0;
134   context ()->set_property ("lastKeySignature", get_property ("keySignature"));
135   cancellation_ = 0;
136 }
137
138 void
139 Key_engraver::read_ev (Music const *r)
140 {
141   SCM p = r->get_property ("pitch-alist");
142   if (!scm_is_pair (p))
143     return;
144
145   SCM n = scm_list_copy (p);
146   SCM accs = SCM_EOL;
147   for (SCM s = get_property ("keyAccidentalOrder");
148        scm_is_pair (s); s = scm_cdr (s))
149     {
150       if (scm_is_pair (scm_member (scm_car (s), n)))
151         {
152           accs = scm_cons (scm_car (s), accs);
153           n = scm_delete_x (scm_car (s), n);
154         }
155     }
156
157   for (SCM s = n; scm_is_pair (s); s = scm_cdr (s))
158     if (scm_to_int (scm_cdar (s)))
159       accs = scm_cons (scm_car (s), accs);
160
161   context ()->set_property ("keySignature", accs);
162   context ()->set_property ("tonic",
163                             r->get_property ("tonic"));
164 }
165
166 void
167 Key_engraver::start_translation_timestep ()
168 {
169   key_ev_ = 0;
170 }
171
172 void
173 Key_engraver::initialize ()
174 {
175   context ()->set_property ("keySignature", SCM_EOL);
176   context ()->set_property ("lastKeySignature", SCM_EOL);
177
178   Pitch p (0, 0, 0);
179   context ()->set_property ("tonic", p.smobbed_copy ());
180 }
181
182 ADD_TRANSLATOR (Key_engraver,
183                 /* descr */ "",
184                 /* creats*/ "KeySignature",
185                 /* accepts */ "key-change-event",
186                 /* acks  */ "bar-line-interface clef-interface",
187                 /* reads */ "keySignature printKeyCancellation lastKeySignature explicitKeySignatureVisibility createKeyOnClefChange keyAccidentalOrder keySignature",
188                 /* write */ "lastKeySignature tonic keySignature");