]> git.donarmstrong.com Git - lilypond.git/blob - lily/key-engraver.cc
* lily/include/translator.icc: new file.
[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 start_translation_timestep ();
42   PRECOMPUTED_VIRTUAL void process_music ();
43   virtual void acknowledge_grob (Grob_info);
44 };
45
46 void
47 Key_engraver::finalize ()
48 {
49 }
50
51 Key_engraver::Key_engraver ()
52 {
53   key_ev_ = 0;
54   item_ = 0;
55   cancellation_ = 0;
56 }
57
58 void
59 Key_engraver::create_key (bool def)
60 {
61   if (!item_)
62     {
63       item_ = make_item ("KeySignature", key_ev_ ? key_ev_->self_scm () : SCM_EOL);
64
65       item_->set_property ("c0-position",
66                            get_property ("middleCPosition"));
67
68       SCM last = get_property ("lastKeySignature");
69       SCM key = get_property ("keySignature");
70       if (to_boolean (get_property ("printKeyCancellation"))
71           && !scm_is_eq (last, key))
72         {
73           cancellation_ = make_item ("KeyCancellation", key_ev_ ? key_ev_->self_scm () : SCM_EOL);
74           cancellation_->set_property ("old-accidentals", last);
75           cancellation_->set_property ("c0-position",
76                                        get_property ("middleCPosition"));
77         }
78       item_->set_property ("new-accidentals", key);
79     }
80
81   if (!def)
82     {
83       SCM vis = get_property ("explicitKeySignatureVisibility");
84       if (ly_is_procedure (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 #include "translator.icc"
184
185 ADD_TRANSLATOR (Key_engraver,
186                 /* descr */ "",
187                 /* creats*/ "KeySignature",
188                 /* accepts */ "key-change-event",
189                 /* acks  */ "bar-line-interface clef-interface",
190                 /* reads */ "keySignature printKeyCancellation lastKeySignature explicitKeySignatureVisibility createKeyOnClefChange keyAccidentalOrder keySignature",
191                 /* write */ "lastKeySignature tonic keySignature");