]> git.donarmstrong.com Git - lilypond.git/blob - lily/pitched-trill-engraver.cc
3555a99d45bd872e413a337f7856428691dbee85
[lilypond.git] / lily / pitched-trill-engraver.cc
1 /*
2   pitched-trill-engraver.cc -- implement Pitched_trill_engraver
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 2005--2007 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
8
9 #include "engraver.hh"
10
11 #include "axis-group-interface.hh"
12 #include "context.hh"
13 #include "dots.hh"
14 #include "item.hh"
15 #include "note-head.hh"
16 #include "pitch.hh"
17 #include "pointer-group-interface.hh"
18 #include "side-position-interface.hh"
19 #include "stream-event.hh"
20 #include "warn.hh"
21
22 class Pitched_trill_engraver : public Engraver
23 {
24 public:
25   TRANSLATOR_DECLARATIONS (Pitched_trill_engraver);
26
27 protected:
28   DECLARE_ACKNOWLEDGER (note_head);
29   DECLARE_ACKNOWLEDGER (dots);
30   DECLARE_ACKNOWLEDGER (trill_spanner);
31   void stop_translation_timestep ();
32
33 private:
34   Item *trill_head_;
35   Item *trill_group_;
36   Item *trill_accidental_;
37
38   vector<Grob*> heads_;
39
40   void make_trill (Stream_event *);
41 };
42
43 Pitched_trill_engraver::Pitched_trill_engraver ()
44 {
45   trill_head_ = 0;
46   trill_group_ = 0;
47   trill_accidental_ = 0;
48 }
49
50 void
51 Pitched_trill_engraver::acknowledge_dots (Grob_info info)
52 {
53   heads_.push_back (info.grob ());
54 }
55 void
56 Pitched_trill_engraver::acknowledge_note_head (Grob_info info)
57 {
58   heads_.push_back (info.grob ());
59 }
60
61 void
62 Pitched_trill_engraver::acknowledge_trill_spanner (Grob_info info)
63 {
64   Stream_event *ev = info.event_cause ();
65   if (ev
66       && ev->in_event_class ("trill-span-event")
67       && to_dir (ev->get_property ("span-direction")) == START
68       && unsmob_pitch (ev->get_property ("pitch")))
69     make_trill (ev);
70 }
71
72 void
73 Pitched_trill_engraver::make_trill (Stream_event *ev)
74 {
75   SCM scm_pitch = ev->get_property ("pitch");
76   Pitch *p = unsmob_pitch (scm_pitch);
77
78   SCM keysig = get_property ("localKeySignature");
79
80   SCM key = scm_cons (scm_from_int (p->get_octave ()),
81                       scm_from_int (p->get_notename ()));
82
83   int bn = measure_number (context());
84
85   SCM handle = scm_assoc (key, keysig);
86   if (handle != SCM_BOOL_F)
87     {
88       bool same_bar = (bn == robust_scm2int (scm_caddr (handle), 0));
89       bool same_alt
90         = (p->get_alteration () == robust_scm2rational (scm_cadr (handle), 0));
91
92       if (!same_bar || (same_bar && !same_alt))
93         handle = SCM_BOOL_F;
94     }
95
96   bool print_acc
97     = (handle == SCM_BOOL_F) || p->get_alteration () == Rational (0)
98     || (ev->get_property ("force-accidental") == SCM_BOOL_T);
99
100   if (trill_head_)
101     {
102       programming_error ("already have a trill head.");
103       trill_head_ = 0;
104     }
105
106   trill_head_ = make_item ("TrillPitchHead", ev->self_scm ());
107   SCM c0scm = get_property ("middleCPosition");
108
109   int c0 = scm_is_number (c0scm) ? scm_to_int (c0scm) : 0;
110
111   trill_head_->set_property ("staff-position",
112                              scm_from_int (unsmob_pitch (scm_pitch)->steps ()
113                                            + c0));
114
115   trill_group_ = make_item ("TrillPitchGroup", ev->self_scm ());
116   trill_group_->set_parent (trill_head_, Y_AXIS);
117
118   Axis_group_interface::add_element (trill_group_, trill_head_);
119
120   if (print_acc)
121     {
122       trill_accidental_ = make_item ("TrillPitchAccidental", ev->self_scm ());
123
124       // fixme: naming -> alterations
125       trill_accidental_->set_property ("alteration", ly_rational2scm (p->get_alteration ()));
126       Side_position_interface::add_support (trill_accidental_, trill_head_);
127       
128       trill_head_->set_object ("accidental-grob", trill_accidental_->self_scm ());
129       trill_accidental_->set_parent (trill_head_, Y_AXIS);
130       Axis_group_interface::add_element (trill_group_, trill_accidental_);
131     }
132 }
133
134 void
135 Pitched_trill_engraver::stop_translation_timestep ()
136 {
137   if (trill_group_)
138     for (vsize i = 0; i < heads_.size (); i++)
139       Side_position_interface::add_support (trill_group_, heads_[i]);
140
141   heads_.clear ();
142   trill_head_ = 0;
143   trill_group_ = 0;
144   trill_accidental_ = 0;
145 }
146
147
148 #include "translator.icc"
149
150 ADD_ACKNOWLEDGER (Pitched_trill_engraver, note_head);
151 ADD_ACKNOWLEDGER (Pitched_trill_engraver, dots);
152 ADD_ACKNOWLEDGER (Pitched_trill_engraver, trill_spanner);
153
154 ADD_TRANSLATOR (Pitched_trill_engraver,
155                 /* doc */
156                 "Print the bracketed note head after a note head with trill.",
157
158                 /* create */
159                 "TrillPitchHead "
160                 "TrillPitchAccidental "
161                 "TrillPitchGroup ",
162
163                 /* read */
164                 "",
165
166                 /* write */
167                 ""
168                 );