]> git.donarmstrong.com Git - lilypond.git/blob - lily/pitched-trill-engraver.cc
Run grand-replace (issue 3765)
[lilypond.git] / lily / pitched-trill-engraver.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2005--2014 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 class Pitched_trill_engraver : public Engraver
34 {
35 public:
36   TRANSLATOR_DECLARATIONS (Pitched_trill_engraver);
37
38 protected:
39   DECLARE_ACKNOWLEDGER (note_head);
40   DECLARE_ACKNOWLEDGER (dots);
41   DECLARE_ACKNOWLEDGER (stem);
42   DECLARE_ACKNOWLEDGER (flag);
43   DECLARE_ACKNOWLEDGER (trill_spanner);
44   void stop_translation_timestep ();
45
46 private:
47   Item *trill_head_;
48   Item *trill_group_;
49   Item *trill_accidental_;
50
51   vector<Grob *> heads_;
52
53   void make_trill (Stream_event *);
54 };
55
56 Pitched_trill_engraver::Pitched_trill_engraver ()
57 {
58   trill_head_ = 0;
59   trill_group_ = 0;
60   trill_accidental_ = 0;
61 }
62
63 void
64 Pitched_trill_engraver::acknowledge_dots (Grob_info info)
65 {
66   heads_.push_back (info.grob ());
67 }
68 void
69 Pitched_trill_engraver::acknowledge_stem (Grob_info info)
70 {
71   heads_.push_back (info.grob ());
72 }
73 void
74 Pitched_trill_engraver::acknowledge_flag (Grob_info info)
75 {
76   heads_.push_back (info.grob ());
77 }
78 void
79 Pitched_trill_engraver::acknowledge_note_head (Grob_info info)
80 {
81   heads_.push_back (info.grob ());
82 }
83
84 void
85 Pitched_trill_engraver::acknowledge_trill_spanner (Grob_info info)
86 {
87   Stream_event *ev = info.event_cause ();
88   if (ev
89       && ev->in_event_class ("trill-span-event")
90       && to_dir (ev->get_property ("span-direction")) == START
91       && unsmob_pitch (ev->get_property ("pitch")))
92     make_trill (ev);
93 }
94
95 void
96 Pitched_trill_engraver::make_trill (Stream_event *ev)
97 {
98   SCM scm_pitch = ev->get_property ("pitch");
99   Pitch *p = unsmob_pitch (scm_pitch);
100
101   SCM keysig = get_property ("localKeySignature");
102
103   SCM key = scm_cons (scm_from_int (p->get_octave ()),
104                       scm_from_int (p->get_notename ()));
105
106   int bn = measure_number (context ());
107
108   SCM handle = scm_assoc (key, keysig);
109   if (handle != SCM_BOOL_F)
110     {
111       bool same_bar = (bn == robust_scm2int (scm_caddr (handle), 0));
112       bool same_alt
113         = (p->get_alteration () == robust_scm2rational (scm_cadr (handle), 0));
114
115       if (!same_bar || (same_bar && !same_alt))
116         handle = SCM_BOOL_F;
117     }
118
119   bool print_acc
120     = (handle == SCM_BOOL_F) || p->get_alteration () == Rational (0)
121       || (ev->get_property ("force-accidental") == SCM_BOOL_T);
122
123   if (trill_head_)
124     {
125       programming_error ("already have a trill head.");
126       trill_head_ = 0;
127     }
128
129   trill_head_ = make_item ("TrillPitchHead", ev->self_scm ());
130   SCM c0scm = get_property ("middleCPosition");
131
132   int c0 = scm_is_number (c0scm) ? scm_to_int (c0scm) : 0;
133
134   trill_head_->set_property ("staff-position",
135                              scm_from_int (unsmob_pitch (scm_pitch)->steps ()
136                                            + c0));
137
138   trill_group_ = make_item ("TrillPitchGroup", ev->self_scm ());
139   trill_group_->set_parent (trill_head_, Y_AXIS);
140
141   Axis_group_interface::add_element (trill_group_, trill_head_);
142
143   if (print_acc)
144     {
145       trill_accidental_ = make_item ("TrillPitchAccidental", ev->self_scm ());
146
147       // fixme: naming -> alterations
148       trill_accidental_->set_property ("alteration", ly_rational2scm (p->get_alteration ()));
149       Side_position_interface::add_support (trill_accidental_, trill_head_);
150
151       trill_head_->set_object ("accidental-grob", trill_accidental_->self_scm ());
152       trill_accidental_->set_parent (trill_head_, Y_AXIS);
153       Axis_group_interface::add_element (trill_group_, trill_accidental_);
154     }
155 }
156
157 void
158 Pitched_trill_engraver::stop_translation_timestep ()
159 {
160   if (trill_group_)
161     for (vsize i = 0; i < heads_.size (); i++)
162       Side_position_interface::add_support (trill_group_, heads_[i]);
163
164   heads_.clear ();
165   trill_head_ = 0;
166   trill_group_ = 0;
167   trill_accidental_ = 0;
168 }
169
170 #include "translator.icc"
171
172 ADD_ACKNOWLEDGER (Pitched_trill_engraver, note_head);
173 ADD_ACKNOWLEDGER (Pitched_trill_engraver, dots);
174 ADD_ACKNOWLEDGER (Pitched_trill_engraver, stem);
175 ADD_ACKNOWLEDGER (Pitched_trill_engraver, flag);
176 ADD_ACKNOWLEDGER (Pitched_trill_engraver, trill_spanner);
177
178 ADD_TRANSLATOR (Pitched_trill_engraver,
179                 /* doc */
180                 "Print the bracketed note head after a note head with trill.",
181
182                 /* create */
183                 "TrillPitchHead "
184                 "TrillPitchAccidental "
185                 "TrillPitchGroup ",
186
187                 /* read */
188                 "",
189
190                 /* write */
191                 ""
192                );