]> git.donarmstrong.com Git - lilypond.git/blob - lily/pitched-trill-engraver.cc
Issue 4550 (1/2) Avoid "using namespace std;" in included files
[lilypond.git] / lily / pitched-trill-engraver.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2005--2015 Han-Wen Nienhuys <hanwen@xs4all.nl>
5
6   LilyPond is free software: you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation, either version 3 of the License, or
9   (at your option) any later version.
10
11   LilyPond is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15
16   You should have received a copy of the GNU General Public License
17   along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "engraver.hh"
21
22 #include "axis-group-interface.hh"
23 #include "context.hh"
24 #include "dots.hh"
25 #include "item.hh"
26 #include "note-head.hh"
27 #include "pitch.hh"
28 #include "pointer-group-interface.hh"
29 #include "side-position-interface.hh"
30 #include "stream-event.hh"
31 #include "warn.hh"
32
33 #include "translator.icc"
34
35 using std::vector;
36
37 class Pitched_trill_engraver : public Engraver
38 {
39 public:
40   TRANSLATOR_DECLARATIONS (Pitched_trill_engraver);
41
42 protected:
43   DECLARE_ACKNOWLEDGER (note_head);
44   DECLARE_ACKNOWLEDGER (dots);
45   DECLARE_ACKNOWLEDGER (stem);
46   DECLARE_ACKNOWLEDGER (flag);
47   DECLARE_ACKNOWLEDGER (trill_spanner);
48   void stop_translation_timestep ();
49
50 private:
51   Item *trill_head_;
52   Item *trill_group_;
53   Item *trill_accidental_;
54
55   vector<Grob *> heads_;
56
57   void make_trill (Stream_event *);
58 };
59
60 Pitched_trill_engraver::Pitched_trill_engraver ()
61 {
62   trill_head_ = 0;
63   trill_group_ = 0;
64   trill_accidental_ = 0;
65 }
66
67 void
68 Pitched_trill_engraver::acknowledge_dots (Grob_info info)
69 {
70   heads_.push_back (info.grob ());
71 }
72 void
73 Pitched_trill_engraver::acknowledge_stem (Grob_info info)
74 {
75   heads_.push_back (info.grob ());
76 }
77 void
78 Pitched_trill_engraver::acknowledge_flag (Grob_info info)
79 {
80   heads_.push_back (info.grob ());
81 }
82 void
83 Pitched_trill_engraver::acknowledge_note_head (Grob_info info)
84 {
85   heads_.push_back (info.grob ());
86 }
87
88 void
89 Pitched_trill_engraver::acknowledge_trill_spanner (Grob_info info)
90 {
91   Stream_event *ev = info.event_cause ();
92   if (ev
93       && ev->in_event_class ("trill-span-event")
94       && to_dir (ev->get_property ("span-direction")) == START
95       && unsmob<Pitch> (ev->get_property ("pitch")))
96     make_trill (ev);
97 }
98
99 void
100 Pitched_trill_engraver::make_trill (Stream_event *ev)
101 {
102   SCM scm_pitch = ev->get_property ("pitch");
103   Pitch *p = unsmob<Pitch> (scm_pitch);
104
105   SCM keysig = get_property ("localAlterations");
106
107   SCM key = scm_cons (scm_from_int (p->get_octave ()),
108                       scm_from_int (p->get_notename ()));
109
110   int bn = measure_number (context ());
111
112   SCM handle = scm_assoc (key, keysig);
113   if (scm_is_true (handle))
114     {
115       bool same_bar = (bn == robust_scm2int (scm_caddr (handle), 0));
116       bool same_alt
117         = (p->get_alteration () == robust_scm2rational (scm_cadr (handle), 0));
118
119       if (!same_bar || (same_bar && !same_alt))
120         handle = SCM_BOOL_F;
121     }
122
123   bool print_acc = scm_is_false (handle)
124                    || p->get_alteration () == Rational (0)
125                    || to_boolean (ev->get_property ("force-accidental"));
126
127   if (trill_head_)
128     {
129       programming_error ("already have a trill head.");
130       trill_head_ = 0;
131     }
132
133   trill_head_ = make_item ("TrillPitchHead", ev->self_scm ());
134   SCM c0scm = get_property ("middleCPosition");
135
136   int c0 = scm_is_number (c0scm) ? scm_to_int (c0scm) : 0;
137
138   trill_head_->set_property ("staff-position",
139                              scm_from_int (unsmob<Pitch> (scm_pitch)->steps ()
140                                            + c0));
141
142   trill_group_ = make_item ("TrillPitchGroup", ev->self_scm ());
143   trill_group_->set_parent (trill_head_, Y_AXIS);
144
145   Axis_group_interface::add_element (trill_group_, trill_head_);
146
147   if (print_acc)
148     {
149       trill_accidental_ = make_item ("TrillPitchAccidental", ev->self_scm ());
150
151       // fixme: naming -> alterations
152       trill_accidental_->set_property ("alteration", ly_rational2scm (p->get_alteration ()));
153       Side_position_interface::add_support (trill_accidental_, trill_head_);
154
155       trill_head_->set_object ("accidental-grob", trill_accidental_->self_scm ());
156       trill_accidental_->set_parent (trill_head_, Y_AXIS);
157       Axis_group_interface::add_element (trill_group_, trill_accidental_);
158     }
159 }
160
161 void
162 Pitched_trill_engraver::stop_translation_timestep ()
163 {
164   if (trill_group_)
165     for (vsize i = 0; i < heads_.size (); i++)
166       Side_position_interface::add_support (trill_group_, heads_[i]);
167
168   heads_.clear ();
169   trill_head_ = 0;
170   trill_group_ = 0;
171   trill_accidental_ = 0;
172 }
173
174 ADD_ACKNOWLEDGER (Pitched_trill_engraver, note_head);
175 ADD_ACKNOWLEDGER (Pitched_trill_engraver, dots);
176 ADD_ACKNOWLEDGER (Pitched_trill_engraver, stem);
177 ADD_ACKNOWLEDGER (Pitched_trill_engraver, flag);
178 ADD_ACKNOWLEDGER (Pitched_trill_engraver, trill_spanner);
179
180 ADD_TRANSLATOR (Pitched_trill_engraver,
181                 /* doc */
182                 "Print the bracketed note head after a note head with trill.",
183
184                 /* create */
185                 "TrillPitchHead "
186                 "TrillPitchAccidental "
187                 "TrillPitchGroup ",
188
189                 /* read */
190                 "",
191
192                 /* write */
193                 ""
194                );