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