]> 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       /* do this only once, just to be on the safe side.  */
92       if (!key_ev_)
93         {
94           key_ev_ = ev;
95           read_ev (key_ev_);
96         }
97       return true;
98     }   
99   return  false;
100 }
101
102
103 void
104 Key_engraver::acknowledge_grob (Grob_info info)
105 {
106   if (Clef::has_interface (info.grob_))
107     {
108       SCM c =  get_property ("createKeyOnClefChange");
109       if (to_boolean (c))
110         {
111           create_key (false);
112         }
113     }
114   else if (Bar_line::has_interface (info.grob_)
115            && ly_c_pair_p (get_property ("keySignature")))
116     {
117       create_key (true);
118     }
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
131 void
132 Key_engraver::stop_translation_timestep ()
133 {
134   item_ = 0;
135   context ()->set_property ("lastKeySignature", get_property ("keySignature"));
136 }
137
138
139 void
140 Key_engraver::read_ev (Music const * r)
141 {
142   SCM p = r->get_property ("pitch-alist");
143   if (!ly_c_pair_p (p))
144     return;
145
146   SCM n = scm_list_copy (p);
147   SCM accs = SCM_EOL;
148   for (SCM s = get_property ("keyAccidentalOrder");
149        ly_c_pair_p (s); s = ly_cdr (s))
150     {
151       if (ly_c_pair_p (scm_member (ly_car (s), n)))
152         {
153           accs = scm_cons (ly_car (s), accs);
154           n = scm_delete_x (ly_car (s), n);
155         }
156     }
157   
158   for (SCM s = n ; ly_c_pair_p (s); s = ly_cdr (s))
159     if (scm_to_int (ly_cdar (s)))
160       accs = scm_cons (ly_car (s), accs);
161
162   context ()->set_property ("keySignature", accs);
163   context ()->set_property ("tonic" ,
164                             r->get_property ("tonic"));
165 }
166
167
168 void
169 Key_engraver::start_translation_timestep ()
170 {
171   key_ev_ = 0;
172 }
173
174
175 void
176 Key_engraver::initialize ()
177 {
178   context ()->set_property ("keySignature", SCM_EOL);
179   context ()->set_property ("lastKeySignature", SCM_EOL);
180
181   Pitch p (0,0,0);
182   context ()->set_property ("tonic", p.smobbed_copy ());
183
184 }
185
186
187 ENTER_DESCRIPTION (Key_engraver,
188 /* descr */       "",
189 /* creats*/       "KeySignature",
190 /* accepts */     "key-change-event",
191 /* acks  */      "bar-line-interface clef-interface",
192 /* reads */       "keySignature printKeyCancellation lastKeySignature explicitKeySignatureVisibility createKeyOnClefChange keyAccidentalOrder keySignature",
193 /* write */       "lastKeySignature tonic keySignature");