]> git.donarmstrong.com Git - lilypond.git/blob - lily/key-engraver.cc
(internal_print): only call
[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   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_is_procedure (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   key_ev_ = 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::initialize ()
169 {
170   context ()->set_property ("keySignature", SCM_EOL);
171   context ()->set_property ("lastKeySignature", SCM_EOL);
172
173   Pitch p (0, 0, 0);
174   context ()->set_property ("tonic", p.smobbed_copy ());
175 }
176
177 #include "translator.icc"
178
179 ADD_TRANSLATOR (Key_engraver,
180                 /* descr */ "",
181                 /* creats*/ "KeySignature",
182                 /* accepts */ "key-change-event",
183                 /* acks  */ "bar-line-interface clef-interface",
184                 /* reads */ "keySignature printKeyCancellation lastKeySignature explicitKeySignatureVisibility createKeyOnClefChange keyAccidentalOrder keySignature",
185                 /* write */ "lastKeySignature tonic keySignature");