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