]> git.donarmstrong.com Git - lilypond.git/blob - lily/accidental-engraver.cc
Fix #733.
[lilypond.git] / lily / accidental-engraver.cc
1 /*
2   accidental-engraver.cc -- implement Accidental_engraver
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1997--2009 Han-Wen Nienhuys <hanwen@xs4all.nl>
7   Modified 2001--2002 by Rune Zedeler <rz@daimi.au.dk>
8 */
9
10 #include "accidental-placement.hh"
11 #include "arpeggio.hh"
12 #include "context.hh"
13 #include "duration.hh"
14 #include "engraver.hh"
15 #include "international.hh"
16 #include "item.hh"
17 #include "pitch.hh"
18 #include "protected-scm.hh"
19 #include "rhythmic-head.hh"
20 #include "separation-item.hh"
21 #include "side-position-interface.hh"
22 #include "spanner.hh"
23 #include "stream-event.hh"
24 #include "tie.hh"
25 #include "warn.hh"
26
27 #include "translator.icc"
28
29 class Accidental_entry
30 {
31 public:
32   bool done_;
33   Stream_event *melodic_;
34   Grob *accidental_;
35   Context *origin_;
36   Engraver *origin_engraver_;
37   Grob *head_;
38   bool tied_;
39
40   Accidental_entry ();
41 };
42
43 Accidental_entry::Accidental_entry ()
44 {
45   tied_ = false;
46   done_ = false;
47   melodic_ = 0;
48   accidental_ = 0;
49   origin_ = 0;
50   head_ = 0;
51 }
52
53 class Accidental_engraver : public Engraver
54 {
55   void update_local_key_signature (SCM new_signature);
56   void create_accidental (Accidental_entry *entry, bool, bool);
57   Grob *make_standard_accidental (Stream_event *note, Grob *note_head, Engraver *trans, bool);
58   Grob *make_suggested_accidental (Stream_event *note, Grob *note_head, Engraver *trans);
59
60 protected:
61   TRANSLATOR_DECLARATIONS (Accidental_engraver);
62   void process_music ();
63
64   void acknowledge_tie (Grob_info);
65   void acknowledge_arpeggio (Grob_info);
66   void acknowledge_rhythmic_head (Grob_info);
67   void acknowledge_finger (Grob_info);
68   void acknowledge_note_column (Grob_info);
69
70   void stop_translation_timestep ();
71   void process_acknowledged ();
72   
73   virtual void finalize ();
74   virtual void derived_mark () const;
75
76 public:
77   SCM last_keysig_;
78
79   vector<Grob*> left_objects_;
80   vector<Grob*> right_objects_;
81
82   Grob *accidental_placement_;
83
84   vector<Accidental_entry> accidentals_;
85   vector<Spanner*> ties_;
86   vector<Grob*> note_columns_;
87 };
88
89 /*
90   localKeySignature is changed at runtime, which means that references
91   in grobs should always store ly_deep_copy ()s of those.
92 */
93
94
95 Accidental_engraver::Accidental_engraver ()
96 {
97   accidental_placement_ = 0;
98   last_keysig_ = SCM_EOL;
99 }
100
101 void
102 Accidental_engraver::derived_mark () const
103 {
104   scm_gc_mark (last_keysig_);
105 }
106
107 void
108 Accidental_engraver::update_local_key_signature (SCM new_sig)
109 {
110   last_keysig_ = new_sig;
111   set_context_property_on_children (context (),
112                                     ly_symbol2scm ("localKeySignature"),
113                                     new_sig);
114
115   Context *trans = context ()->get_parent_context ();
116
117   /*
118     Reset parent contexts so that e.g. piano-accidentals won't remember old
119     cross-staff accidentals after key-sig-changes.
120   */
121
122   SCM val;
123   while (trans && trans->where_defined (ly_symbol2scm ("localKeySignature"), &val) == trans)
124     {
125       trans->set_property ("localKeySignature", ly_deep_copy (last_keysig_));
126       trans = trans->get_parent_context ();
127     }
128 }
129
130 struct Accidental_result
131 {
132   bool need_acc;
133   bool need_restore;
134
135   Accidental_result ()
136   {
137     need_restore = need_acc = false;
138   }
139
140   Accidental_result (bool restore, bool acc)
141   {
142     need_restore = restore;
143     need_acc = acc;
144   }
145
146   Accidental_result (SCM scm)
147   {
148     need_restore = to_boolean (scm_car (scm));
149     need_acc = to_boolean (scm_cdr (scm));
150   }
151
152   int score () const
153   {
154     return need_acc ? 1 : 0
155       + need_restore ? 1 : 0;
156   }
157 };
158
159 static
160 Accidental_result
161 check_pitch_against_rules (Pitch const &pitch, Context *origin,
162                            SCM rules, int bar_number, SCM measurepos)
163 {
164   Accidental_result result;
165   SCM pitch_scm = pitch.smobbed_copy ();
166   SCM barnum_scm = scm_from_int (bar_number);
167
168   if (scm_is_pair (rules) && !scm_is_symbol (scm_car (rules)))
169     warning (_f ("accidental typesetting list must begin with context-name: %s",
170                  ly_scm2string (scm_car (rules)).c_str ()));
171
172   for (; scm_is_pair (rules) && origin; rules = scm_cdr (rules))
173     {
174       SCM rule = scm_car (rules);
175       if (ly_is_procedure (rule))
176         {
177           SCM rule_result_scm = scm_call_4 (rule, origin->self_scm (),
178                                             pitch_scm, barnum_scm, measurepos);
179           Accidental_result rule_result (rule_result_scm);
180
181           result.need_acc |= rule_result.need_acc;
182           result.need_restore |= rule_result.need_restore;
183         }
184
185       /*
186         If symbol then it is a context name.  Scan parent contexts to
187         find it.
188       */
189       else if (scm_is_symbol (rule))
190         {
191           Context *dad = origin;
192           while (dad && !dad->is_alias (rule))
193             dad = dad->get_parent_context ();
194
195           if (dad)
196             origin = dad;
197         }
198       else
199         warning (_f ("procedure or context-name expected for accidental rule, found %s",
200                      print_scm_val (rule).c_str ()));
201     }
202
203   return result;
204 }
205
206 void
207 Accidental_engraver::process_acknowledged ()
208 {
209   if (accidentals_.size () && !accidentals_.back ().done_)
210     {
211       SCM accidental_rules = get_property ("autoAccidentals");
212       SCM cautionary_rules = get_property ("autoCautionaries");
213       SCM measure_position = get_property ("measurePosition");
214       int barnum = measure_number (context());
215
216       for (vsize i = 0; i < accidentals_.size (); i++)
217         {
218           if (accidentals_[i].done_)
219             continue;
220           accidentals_[i].done_ = true;
221
222           Stream_event *note = accidentals_[i].melodic_;
223           Context *origin = accidentals_[i].origin_;
224
225           Pitch *pitch = unsmob_pitch (note->get_property ("pitch"));
226           if (!pitch)
227             continue;
228
229           Accidental_result acc = check_pitch_against_rules (*pitch, origin, accidental_rules,
230                                                              barnum, measure_position);
231           Accidental_result caut = check_pitch_against_rules (*pitch, origin, cautionary_rules,
232                                                               barnum, measure_position);
233
234           bool cautionary = to_boolean (note->get_property ("cautionary"));
235           if (caut.score () > acc.score ())
236             {
237               acc.need_acc |= caut.need_acc; 
238               acc.need_restore |= caut.need_restore; 
239
240               cautionary = true;
241             }
242
243           bool forced = to_boolean (note->get_property ("force-accidental"));
244           if (!acc.need_acc && forced)
245             acc.need_acc = true;
246
247           /*
248             Cannot look for ties: it's not guaranteed that they reach
249             us before the notes.
250           */
251           if (!note->in_event_class ("trill-span-event"))
252             {
253               if (acc.need_acc)       
254                 create_accidental (&accidentals_[i], acc.need_restore, cautionary);
255
256               if (forced || cautionary)
257                 accidentals_[i].accidental_->set_property ("forced", SCM_BOOL_T);
258             }
259         }
260     }
261 }
262
263 void
264 Accidental_engraver::create_accidental (Accidental_entry *entry,
265                                         bool restore_natural,
266                                         bool cautionary)
267 {
268   Stream_event *note = entry->melodic_;
269   Grob *support = entry->head_;
270   bool as_suggestion = to_boolean (entry->origin_->get_property ("suggestAccidentals"));
271   Grob *a = 0;
272   if (as_suggestion)
273     a = make_suggested_accidental (note, support, entry->origin_engraver_);
274   else
275     a = make_standard_accidental (note, support, entry->origin_engraver_, cautionary);
276
277   if (restore_natural)
278     {
279       if (to_boolean (get_property ("extraNatural")))
280         a->set_property ("restore-first", SCM_BOOL_T);
281     }
282
283   entry->accidental_ = a;
284 }
285
286 Grob *
287 Accidental_engraver::make_standard_accidental (Stream_event * /* note */,
288                                                Grob *note_head,
289                                                Engraver *trans,
290                                                bool cautionary)
291 {
292   /*
293     We construct the accidentals at the originating Voice
294     level, so that we get the property settings for
295     Accidental from the respective Voice.
296   */
297   Grob *a = 0;
298   if (cautionary)
299     a = trans->make_item ("AccidentalCautionary", note_head->self_scm ());
300   else
301     a = trans->make_item ("Accidental", note_head->self_scm ());
302
303   /*
304     We add the accidentals to the support of the arpeggio,
305     so it is put left of the accidentals.
306   */
307   for (vsize i = 0; i < left_objects_.size (); i++)
308     {
309       if (left_objects_[i]->get_property ("side-axis") == scm_from_int (X_AXIS))
310         Side_position_interface::add_support (left_objects_[i], a);
311     }
312
313   for (vsize i = 0; i < right_objects_.size (); i++)
314     Side_position_interface::add_support (a, right_objects_[i]);
315
316   a->set_parent (note_head, Y_AXIS);
317
318   if (!accidental_placement_)
319     accidental_placement_ = make_item ("AccidentalPlacement",
320                                        a->self_scm ());
321   Accidental_placement::add_accidental (accidental_placement_, a);
322
323   note_head->set_object ("accidental-grob", a->self_scm ());
324   
325   return a;
326 }
327
328 Grob *
329 Accidental_engraver::make_suggested_accidental (Stream_event * /* note */,
330                                                 Grob *note_head,
331                                                 Engraver *trans)
332 {
333   Grob *a = trans->make_item ("AccidentalSuggestion", note_head->self_scm ());
334
335   Side_position_interface::add_support (a, note_head);
336   if (Grob *stem = unsmob_grob (a->get_object ("stem")))
337     Side_position_interface::add_support (a, stem);
338
339   a->set_parent (note_head, X_AXIS);
340   return a;
341 }
342
343 void
344 Accidental_engraver::finalize ()
345 {
346   last_keysig_ = SCM_EOL;
347 }
348
349 void
350 Accidental_engraver::stop_translation_timestep ()
351 {
352   for (vsize j = ties_.size (); j--;)
353     {
354       Grob *r = Tie::head (ties_[j], RIGHT);
355       for (vsize i = accidentals_.size (); i--;)
356         if (accidentals_[i].head_ == r)
357           {
358             if (Grob *g = accidentals_[i].accidental_)
359               {
360                 g->set_object ("tie", ties_[j]->self_scm ());
361                 accidentals_[i].tied_ = true;
362               }
363             ties_.erase (ties_.begin () + j);
364             break;
365           }
366     }
367
368   for (vsize i = accidentals_.size (); i--;)
369     {      
370       Stream_event *note = accidentals_[i].melodic_;
371       Context *origin = accidentals_[i].origin_;
372
373       int barnum = measure_number (origin);
374
375       Pitch *pitch = unsmob_pitch (note->get_property ("pitch"));
376       if (!pitch)
377         continue;
378
379       int n = pitch->get_notename ();
380       int o = pitch->get_octave ();
381       Rational a = pitch->get_alteration ();
382       SCM key = scm_cons (scm_from_int (o), scm_from_int (n));
383
384       Moment end_mp = measure_position (context (),
385                                         unsmob_duration (note->get_property ("duration")));
386       SCM position = scm_cons (scm_from_int (barnum), end_mp.smobbed_copy ());
387
388       SCM localsig = SCM_EOL;
389       while (origin
390              && origin->where_defined (ly_symbol2scm ("localKeySignature"), &localsig))
391         {
392           bool change = false;
393           if (accidentals_[i].tied_
394               && !(to_boolean (accidentals_[i].accidental_->get_property ("forced"))))
395             {
396               /*
397                 Remember an alteration that is different both from
398                 that of the tied note and of the key signature.
399               */
400               localsig = ly_assoc_prepend_x (localsig, key,scm_cons (ly_symbol2scm ("tied"),
401                                                                      position));
402               change = true;
403             }
404           else
405             {
406               /*
407                 not really really correct if there is more than one
408                 note head with the same notename.
409               */
410               localsig = ly_assoc_prepend_x (localsig, key,
411                                              scm_cons (ly_rational2scm (a),
412                                                        position));
413               change = true;
414             }
415
416           if (change)
417             origin->set_property ("localKeySignature", localsig);
418
419           origin = origin->get_parent_context ();
420         }
421     }
422
423   if (accidental_placement_)
424     for (vsize i = 0; i < note_columns_.size (); i++)
425       Separation_item::add_conditional_item (note_columns_[i], accidental_placement_);
426
427   accidental_placement_ = 0;
428   accidentals_.clear ();
429   left_objects_.clear ();
430   right_objects_.clear ();
431 }
432
433 void
434 Accidental_engraver::acknowledge_rhythmic_head (Grob_info info)
435 {
436   Stream_event *note = info.event_cause ();
437   if (note
438       && (note->in_event_class ("note-event")
439           || note->in_event_class ("trill-span-event")))
440     {
441       /*
442         string harmonics usually don't have accidentals.
443       */
444       if (info.grob ()->get_property ("style") != ly_symbol2scm ("harmonic")
445           || to_boolean (get_property ("harmonicAccidentals")))
446         {
447           Accidental_entry entry;
448           entry.head_ = info.grob ();
449           entry.origin_engraver_ = dynamic_cast<Engraver *> (info.origin_translator ());
450           entry.origin_ = entry.origin_engraver_->context ();
451           entry.melodic_ = note;
452
453           accidentals_.push_back (entry);
454         }
455     }
456 }
457
458 void
459 Accidental_engraver::acknowledge_tie (Grob_info info)
460 {
461   ties_.push_back (dynamic_cast<Spanner *> (info.grob ()));
462 }
463
464 void
465 Accidental_engraver::acknowledge_note_column (Grob_info info)
466 {
467   note_columns_.push_back (info.grob ());
468 }
469
470 void
471 Accidental_engraver::acknowledge_arpeggio (Grob_info info)
472 {
473   left_objects_.push_back (info.grob ());
474 }
475
476 void
477 Accidental_engraver::acknowledge_finger (Grob_info info)
478 {
479   left_objects_.push_back (info.grob ());
480 }
481
482 void
483 Accidental_engraver::process_music ()
484 {
485   SCM sig = get_property ("keySignature");
486   if (last_keysig_ != sig)
487     update_local_key_signature (sig);
488 }
489
490 ADD_ACKNOWLEDGER (Accidental_engraver, arpeggio);
491 ADD_ACKNOWLEDGER (Accidental_engraver, finger);
492 ADD_ACKNOWLEDGER (Accidental_engraver, rhythmic_head);
493 ADD_ACKNOWLEDGER (Accidental_engraver, tie);
494 ADD_ACKNOWLEDGER (Accidental_engraver, note_column);
495
496 ADD_TRANSLATOR (Accidental_engraver,
497                 /* doc */
498                 "Make accidentals."
499                 "  Catch note heads, ties and notices key-change events."
500                 "  This engraver usually lives at Staff level, but"
501                 " reads the settings for Accidental at @code{Voice} level,"
502                 " so you can @code{\\override} them at @code{Voice}.",
503
504                 /* create */
505                 "Accidental "
506                 "AccidentalCautionary "
507                 "AccidentalPlacement "
508                 "AccidentalSuggestion ",
509
510                 /* read */
511                 "autoAccidentals "
512                 "autoCautionaries "
513                 "internalBarNumber "
514                 "extraNatural "
515                 "harmonicAccidentals "
516                 "keySignature "
517                 "localKeySignature ",
518
519                 /* write */
520                 "localKeySignature "
521                 );