]> git.donarmstrong.com Git - lilypond.git/blob - lily/ambitus-engraver.cc
Issue 4954/1: Change spanner-id to be a key instead of a string
[lilypond.git] / lily / ambitus-engraver.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2002--2015 Juergen Reuter <reuter@ipd.uka.de>
5
6   Han-Wen Nienhuys <hanwen@xs4all.nl
7
8   LilyPond is free software: you can redistribute it and/or modify
9   it under the terms of the GNU General Public License as published by
10   the Free Software Foundation, either version 3 of the License, or
11   (at your option) any later version.
12
13   LilyPond is distributed in the hope that it will be useful,
14   but WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16   GNU General Public License for more details.
17
18   You should have received a copy of the GNU General Public License
19   along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "engraver.hh"
23
24 #include "accidental-placement.hh"
25 #include "axis-group-interface.hh"
26 #include "item.hh"
27 #include "note-head.hh"
28 #include "pitch-interval.hh"
29 #include "pointer-group-interface.hh"
30 #include "protected-scm.hh"
31 #include "side-position-interface.hh"
32 #include "separation-item.hh"
33 #include "staff-symbol-referencer.hh"
34 #include "stream-event.hh"
35
36 #include "translator.icc"
37
38 class Ambitus_engraver : public Engraver
39 {
40 public:
41   TRANSLATOR_DECLARATIONS (Ambitus_engraver);
42 protected:
43   void acknowledge_note_head (Grob_info);
44
45   void process_music ();
46   void stop_translation_timestep ();
47   virtual void finalize ();
48   virtual void derived_mark () const;
49
50 private:
51   void create_ambitus ();
52   Item *ambitus_;
53   Item *group_;
54   Drul_array<Item *> heads_;
55   Drul_array<Item *> accidentals_;
56   Drul_array<Stream_event *> causes_;
57   Pitch_interval pitch_interval_;
58   bool is_typeset_;
59   int start_c0_;
60   SCM start_key_sig_;
61 };
62
63 void
64 Ambitus_engraver::derived_mark () const
65 {
66   scm_gc_mark (start_key_sig_);
67 }
68
69 void
70 Ambitus_engraver::create_ambitus ()
71 {
72   ambitus_ = make_item ("AmbitusLine", SCM_EOL);
73   group_ = make_item ("Ambitus", SCM_EOL);
74   for (DOWN_and_UP (d))
75     {
76       heads_[d] = make_item ("AmbitusNoteHead", SCM_EOL);
77       accidentals_[d] = make_item ("AmbitusAccidental", SCM_EOL);
78       accidentals_[d]->set_parent (heads_[d], Y_AXIS);
79       heads_[d]->set_object ("accidental-grob",
80                              accidentals_[d]->self_scm ());
81       Axis_group_interface::add_element (group_, heads_[d]);
82       Axis_group_interface::add_element (group_, accidentals_[d]);
83     }
84
85   ambitus_->set_parent (heads_[DOWN], X_AXIS);
86   Axis_group_interface::add_element (group_, ambitus_);
87
88   is_typeset_ = false;
89 }
90
91 Ambitus_engraver::Ambitus_engraver ()
92 {
93   ambitus_ = 0;
94   heads_.set (0, 0);
95   accidentals_.set (0, 0);
96   group_ = 0;
97   is_typeset_ = false;
98   start_key_sig_ = SCM_EOL;
99 }
100
101 void
102 Ambitus_engraver::process_music ()
103 {
104   /*
105    * Ensure that ambitus is created in the very first timestep
106    */
107   if (!ambitus_)
108     create_ambitus ();
109 }
110
111 void
112 Ambitus_engraver::stop_translation_timestep ()
113 {
114   if (ambitus_ && !is_typeset_)
115     {
116       /*
117        * Evaluate middleCPosition not until now, since otherwise we
118        * may then oversee a clef that is defined in a staff context if
119        * we are in a voice context; middleCPosition would then be
120        * assumed to be 0.
121
122        * Don't use middleCPosition as this may be thwarted by a cue
123        * starting here.  middleCOffset is not affected by cue clefs.
124        */
125       int clef_pos = robust_scm2int (get_property ("middleCClefPosition"), 0);
126       int offset = robust_scm2int (get_property ("middleCOffset"), 0);
127
128       start_c0_ = clef_pos + offset;
129       start_key_sig_ = get_property ("keyAlterations");
130
131       is_typeset_ = true;
132     }
133 }
134
135 void
136 Ambitus_engraver::acknowledge_note_head (Grob_info info)
137 {
138   Stream_event *nr = info.event_cause ();
139   if (nr && nr->in_event_class ("note-event")
140       && !to_boolean (info.grob ()->get_property ("ignore-ambitus")))
141     {
142       SCM p = nr->get_property ("pitch");
143       /*
144         If the engraver is added to a percussion context,
145         filter out unpitched note heads.
146       */
147       if (!unsmob<Pitch> (p))
148         return;
149       Pitch pitch = *unsmob<Pitch> (p);
150       Drul_array<bool> expands = pitch_interval_.add_point (pitch);
151       if (expands[UP])
152         causes_[UP] = nr;
153       if (expands[DOWN])
154         causes_[DOWN] = nr;
155     }
156 }
157
158 void
159 Ambitus_engraver::finalize ()
160 {
161   if (ambitus_ && !pitch_interval_.is_empty ())
162     {
163       Grob *accidental_placement
164         = make_item ("AccidentalPlacement", accidentals_[DOWN]->self_scm ());
165
166       for (DOWN_and_UP (d))
167         {
168           Pitch p = pitch_interval_[d];
169           heads_[d]->set_property ("cause", causes_[d]->self_scm ());
170           heads_[d]->set_property ("staff-position",
171                                    scm_from_int (start_c0_ + p.steps ()));
172
173           SCM handle = scm_assoc (scm_cons (scm_from_int (p.get_octave ()),
174                                             scm_from_int (p.get_notename ())),
175                                   start_key_sig_);
176
177           if (scm_is_false (handle))
178             handle = scm_assoc (scm_from_int (p.get_notename ()),
179                                 start_key_sig_);
180
181           Rational sig_alter = (scm_is_true (handle))
182                                ? robust_scm2rational (scm_cdr (handle), Rational (0))
183                                : Rational (0);
184
185           const Pitch other = pitch_interval_[-d];
186
187           if (sig_alter == p.get_alteration ()
188               && !((p.steps () == other.steps ())
189                    && (p.get_alteration () != other.get_alteration ())))
190             {
191               accidentals_[d]->suicide ();
192               heads_[d]->set_object ("accidental-grob", SCM_EOL);
193             }
194           else
195             accidentals_[d]->
196             set_property ("alteration",
197                           ly_rational2scm (p.get_alteration ()));
198           Separation_item::add_conditional_item (heads_[d],
199                                                  accidental_placement);
200           Accidental_placement::add_accidental (accidental_placement,
201                                                 accidentals_[d], false, 0);
202           Pointer_group_interface::add_grob (ambitus_,
203                                              ly_symbol2scm ("note-heads"),
204                                              heads_[d]);
205         }
206
207       Axis_group_interface::add_element (group_, accidental_placement);
208     }
209   else
210     {
211       for (DOWN_and_UP (d))
212         {
213           accidentals_[d]->suicide ();
214           heads_[d]->suicide ();
215         }
216
217       ambitus_->suicide ();
218     }
219 }
220
221 void
222 Ambitus_engraver::boot ()
223 {
224   ADD_ACKNOWLEDGER (Ambitus_engraver, note_head);
225 }
226
227 ADD_TRANSLATOR (Ambitus_engraver,
228                 /* doc */
229                 "Create an ambitus.",
230
231                 /* create */
232                 "AccidentalPlacement "
233                 "Ambitus "
234                 "AmbitusAccidental "
235                 "AmbitusLine "
236                 "AmbitusNoteHead ",
237
238                 /* read */
239                 "keyAlterations "
240                 "middleCClefPosition "
241                 "middleCOffset ",
242
243                 /* write */
244                 ""
245                );