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