]> git.donarmstrong.com Git - lilypond.git/blob - lily/key-engraver.cc
* lily/lexer.ll: change is_string -> ly_c_string_p
[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");
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       announce_grob (item_, key_ev_ ? key_ev_->self_scm () : SCM_EOL);
77     }
78
79   if (!def)
80     {
81       SCM vis = get_property ("explicitKeySignatureVisibility"); 
82       if (is_procedure (vis))
83         item_->set_property ("break-visibility",vis);
84     }
85 }      
86
87
88 bool
89 Key_engraver::try_music (Music * ev)
90 {
91   if (ev->is_mus_type ("key-change-event"))
92     {
93       if (!key_ev_)
94         {
95           /*
96             do this only once, just to be on the safe side.
97             */      
98           key_ev_ = ev;
99           read_ev (key_ev_);
100         }
101       
102       return true;
103     }   
104   return  false;
105 }
106
107
108 void
109 Key_engraver::acknowledge_grob (Grob_info info)
110 {
111   if (Clef::has_interface (info.grob_))
112     {
113       SCM c =  get_property ("createKeyOnClefChange");
114       if (to_boolean (c))
115         {
116           create_key (false);
117         }
118     }
119   else if (Bar_line::has_interface (info.grob_)
120            && ly_c_pair_p (get_property ("keySignature")))
121     {
122       create_key (true);
123     }
124 }
125
126
127 void
128 Key_engraver::process_music ()
129 {
130   if (key_ev_ ||
131       get_property ("lastKeySignature") != get_property ("keySignature"))
132     create_key (false);
133 }
134
135
136 void
137 Key_engraver::stop_translation_timestep ()
138 {
139   if (item_) 
140     {
141       typeset_grob (item_);
142       item_ = 0;
143     }
144 }
145
146
147 void
148 Key_engraver::read_ev (Music const * r)
149 {
150   SCM p = r->get_property ("pitch-alist");
151   if (!ly_c_pair_p (p))
152     return;
153
154   SCM n = scm_list_copy (p);
155   SCM accs = SCM_EOL;
156   for (SCM s = get_property ("keyAccidentalOrder");
157        ly_c_pair_p (s); s = ly_cdr (s))
158     {
159       if (ly_c_pair_p (scm_member (ly_car (s), n)))
160         {
161           accs = scm_cons (ly_car (s), accs);
162           n = scm_delete_x (ly_car (s), n);
163         }
164     }
165   
166   for (SCM s = n ; ly_c_pair_p (s); s = ly_cdr (s))
167     if (ly_scm2int (ly_cdar (s)))
168       accs = scm_cons (ly_car (s), accs);
169
170   get_parent_context ()->set_property ("keySignature", accs);
171   get_parent_context ()->set_property ("tonic" ,
172                               r->get_property ("tonic"));
173 }
174
175
176 void
177 Key_engraver::start_translation_timestep ()
178 {
179   key_ev_ = 0;
180   get_parent_context ()->set_property ("lastKeySignature", get_property ("keySignature"));
181 }
182
183
184 void
185 Key_engraver::initialize ()
186 {
187   get_parent_context ()->set_property ("keySignature", SCM_EOL);
188   get_parent_context ()->set_property ("lastKeySignature", SCM_EOL);
189
190   Pitch p (0,0,0);
191   get_parent_context ()->set_property ("tonic", p.smobbed_copy ());
192
193 }
194
195
196 ENTER_DESCRIPTION (Key_engraver,
197 /* descr */       "",
198 /* creats*/       "KeySignature",
199 /* accepts */     "key-change-event",
200 /* acks  */      "bar-line-interface clef-interface",
201 /* reads */       "keySignature printKeyCancellation lastKeySignature explicitKeySignatureVisibility createKeyOnClefChange keyAccidentalOrder keySignature",
202 /* write */       "lastKeySignature tonic keySignature");