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