Fixes Issue 1504, allowing feather beam line breaking.
[lilypond/patrick.git] / lily / ambitus-engraver.cc
blob221c1e8881ee950c70da5af60b29f202236d30ef
1 /*
2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 2002--2011 Juergen Reuter <reuter@ipd.uka.de>
6 Han-Wen Nienhuys <hanwen@xs4all.nl
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.
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.
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/>.
22 #include "engraver.hh"
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"
36 #include "translator.icc"
38 class Ambitus_engraver : public Engraver
40 public:
41 TRANSLATOR_DECLARATIONS (Ambitus_engraver);
42 protected:
43 DECLARE_ACKNOWLEDGER (note_head);
45 void process_music ();
46 void stop_translation_timestep ();
47 virtual void finalize ();
48 virtual void derived_mark () const;
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_;
63 void
64 Ambitus_engraver::derived_mark () const
66 scm_gc_mark (start_key_sig_);
69 void
70 Ambitus_engraver::create_ambitus ()
72 ambitus_ = make_item ("AmbitusLine", SCM_EOL);
73 group_ = make_item ("Ambitus", SCM_EOL);
74 Direction d = DOWN;
77 heads_[d] = make_item ("AmbitusNoteHead", SCM_EOL);
78 accidentals_[d] = make_item ("AmbitusAccidental", SCM_EOL);
79 accidentals_[d]->set_parent (heads_[d], Y_AXIS);
80 heads_[d]->set_object ("accidental-grob",
81 accidentals_[d]->self_scm ());
82 Axis_group_interface::add_element (group_, heads_[d]);
83 Axis_group_interface::add_element (group_, accidentals_[d]);
85 while (flip (&d) != DOWN);
87 ambitus_->set_parent (heads_[DOWN], X_AXIS);
88 Axis_group_interface::add_element (group_, ambitus_);
90 is_typeset_ = false;
93 Ambitus_engraver::Ambitus_engraver ()
95 ambitus_ = 0;
96 heads_.set (0, 0);
97 accidentals_.set (0, 0);
98 group_ = 0;
99 is_typeset_ = false;
100 start_key_sig_ = SCM_EOL;
103 void
104 Ambitus_engraver::process_music ()
107 * Ensure that ambitus is created in the very first timestep
109 if (!ambitus_)
110 create_ambitus ();
113 void
114 Ambitus_engraver::stop_translation_timestep ()
116 if (ambitus_ && !is_typeset_)
119 * Evaluate middleCPosition not until now, since otherwise we
120 * may then oversee a clef that is defined in a staff context if
121 * we are in a voice context; middleCPosition would then be
122 * assumed to be 0.
124 start_c0_ = robust_scm2int (get_property ("middleCPosition"), 0);
125 start_key_sig_ = get_property ("keySignature");
127 is_typeset_ = true;
131 void
132 Ambitus_engraver::acknowledge_note_head (Grob_info info)
134 Stream_event *nr = info.event_cause ();
135 if (nr && nr->in_event_class ("note-event"))
137 SCM p = nr->get_property ("pitch");
139 If the engraver is added to a percussion context,
140 filter out unpitched note heads.
142 if (!unsmob_pitch (p))
143 return;
144 Pitch pitch = *unsmob_pitch (p);
145 Drul_array<bool> expands = pitch_interval_.add_point (pitch);
146 if (expands[UP])
147 causes_[UP] = nr;
148 if (expands[DOWN])
149 causes_[DOWN] = nr;
153 void
154 Ambitus_engraver::finalize ()
156 if (ambitus_ && !pitch_interval_.is_empty ())
158 Grob *accidental_placement =
159 make_item ("AccidentalPlacement", accidentals_[DOWN]->self_scm ());
161 Direction d = DOWN;
164 Pitch p = pitch_interval_[d];
165 heads_[d]->set_property ("cause", causes_[d]->self_scm());
166 heads_[d]->set_property ("staff-position",
167 scm_from_int (start_c0_ + p.steps ()));
169 SCM handle = scm_assoc (scm_cons (scm_from_int (p.get_octave ()),
170 scm_from_int (p.get_notename ())),
171 start_key_sig_);
173 if (handle == SCM_BOOL_F)
174 handle = scm_assoc (scm_from_int (p.get_notename ()),
175 start_key_sig_);
177 Rational sig_alter = (handle != SCM_BOOL_F)
178 ? robust_scm2rational (scm_cdr (handle), Rational (0))
179 : Rational (0);
181 const Pitch other = pitch_interval_[-d];
183 if (sig_alter == p.get_alteration ()
184 && !((p.steps () == other.steps ())
185 && (p.get_alteration () != other.get_alteration ())))
187 accidentals_[d]->suicide ();
188 heads_[d]->set_object ("accidental-grob", SCM_EOL);
190 else
191 accidentals_[d]->
192 set_property ("alteration",
193 ly_rational2scm (p.get_alteration ()));
194 Separation_item::add_conditional_item (heads_[d],
195 accidental_placement);
196 Accidental_placement::add_accidental (accidental_placement,
197 accidentals_[d]);
198 Pointer_group_interface::add_grob (ambitus_,
199 ly_symbol2scm ("note-heads"),
200 heads_[d]);
202 while (flip (&d) != DOWN);
204 Axis_group_interface::add_element (group_, accidental_placement);
206 else
208 Direction d = DOWN;
211 accidentals_[d]->suicide ();
212 heads_[d]->suicide ();
214 while (flip (&d) != DOWN);
216 ambitus_->suicide ();
220 ADD_ACKNOWLEDGER (Ambitus_engraver, note_head);
221 ADD_TRANSLATOR (Ambitus_engraver,
222 /* doc */
223 "Create an ambitus.",
225 /* create */
226 "AccidentalPlacement "
227 "Ambitus "
228 "AmbitusAccidental "
229 "AmbitusLine "
230 "AmbitusNoteHead ",
232 /* read */
233 "keySignature "
234 "middleCPosition ",
236 /* write */