1 /*
2 This file is part of LilyPond, the GNU music typesetter.
3
4 Copyright (C) 2005--2020 Han-Wen Nienhuys <hanwen@xs4all.nl>
5
6
7 LilyPond is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11
12 LilyPond is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with LilyPond. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "engraver.hh"
22
23 #include "context.hh"
24 #include "spanner.hh"
25 #include "item.hh"
26 #include "side-position-interface.hh"
27 #include "translator.icc"
28 #include "axis-group-interface.hh"
29
30 using std::vector;
31
32 class Figured_bass_position_engraver : public Engraver
33 {
34 TRANSLATOR_DECLARATIONS (Figured_bass_position_engraver);
35
36 Spanner *bass_figure_alignment_;
37 Spanner *positioner_;
38 vector<Grob *> support_;
39 vector<Grob *> span_support_;
40 protected:
41 void acknowledge_note_column (Grob_info);
42 void acknowledge_slur (Grob_info);
43 void acknowledge_stem (Grob_info);
44 void acknowledge_end_slur (Grob_info);
45 void acknowledge_end_tie (Grob_info);
46 void acknowledge_bass_figure_alignment (Grob_info);
47 void acknowledge_end_bass_figure_alignment (Grob_info);
48
49 void finalize () override;
50 void start_spanner ();
51 void stop_spanner ();
52 void stop_translation_timestep ();
53 };
54
Figured_bass_position_engraver(Context * c)55 Figured_bass_position_engraver::Figured_bass_position_engraver (Context *c)
56 : Engraver (c)
57 {
58 positioner_ = 0;
59 bass_figure_alignment_ = 0;
60 }
61
62 void
start_spanner()63 Figured_bass_position_engraver::start_spanner ()
64 {
65 assert (!positioner_);
66
67 positioner_ = make_spanner ("BassFigureAlignmentPositioning", bass_figure_alignment_->self_scm ());
68 positioner_->set_bound (LEFT, bass_figure_alignment_->get_bound (LEFT));
69 Axis_group_interface::add_element (positioner_, bass_figure_alignment_);
70 }
71
72 void
stop_spanner()73 Figured_bass_position_engraver::stop_spanner ()
74 {
75 if (positioner_ && !positioner_->get_bound (RIGHT))
76 {
77 positioner_->set_bound (RIGHT, bass_figure_alignment_->get_bound (RIGHT));
78 }
79
80 positioner_ = 0;
81 bass_figure_alignment_ = 0;
82 }
83
84 void
finalize()85 Figured_bass_position_engraver::finalize ()
86 {
87 stop_spanner ();
88 }
89
90 void
acknowledge_note_column(Grob_info info)91 Figured_bass_position_engraver::acknowledge_note_column (Grob_info info)
92 {
93 support_.push_back (info.grob ());
94 }
95
96 void
acknowledge_stem(Grob_info info)97 Figured_bass_position_engraver::acknowledge_stem (Grob_info info)
98 {
99 support_.push_back (info.grob ());
100 }
101
102 void
acknowledge_end_slur(Grob_info info)103 Figured_bass_position_engraver::acknowledge_end_slur (Grob_info info)
104 {
105 vector<Grob *>::iterator i = find (span_support_.begin (), span_support_.end (),
106 info.grob ());
107
108 if (i < span_support_.end ())
109 span_support_.erase (i);
110 }
111
112 void
acknowledge_slur(Grob_info info)113 Figured_bass_position_engraver::acknowledge_slur (Grob_info info)
114 {
115 span_support_.push_back (info.grob ());
116 }
117
118 void
acknowledge_end_tie(Grob_info info)119 Figured_bass_position_engraver::acknowledge_end_tie (Grob_info info)
120 {
121 support_.push_back (info.grob ());
122 }
123
124 void
stop_translation_timestep()125 Figured_bass_position_engraver::stop_translation_timestep ()
126 {
127 if (positioner_)
128 {
129 for (vsize i = 0; i < span_support_.size (); i++)
130 Side_position_interface::add_support (positioner_, span_support_[i]);
131 for (vsize i = 0; i < support_.size (); i++)
132 Side_position_interface::add_support (positioner_, support_[i]);
133 }
134
135 support_.clear ();
136 }
137
138 void
acknowledge_end_bass_figure_alignment(Grob_info)139 Figured_bass_position_engraver::acknowledge_end_bass_figure_alignment (Grob_info /* info */)
140 {
141 stop_spanner ();
142 }
143
144 void
acknowledge_bass_figure_alignment(Grob_info info)145 Figured_bass_position_engraver::acknowledge_bass_figure_alignment (Grob_info info)
146 {
147 bass_figure_alignment_ = dynamic_cast<Spanner *> (info.grob ());
148 start_spanner ();
149 }
150
151 void
boot()152 Figured_bass_position_engraver::boot ()
153 {
154 ADD_ACKNOWLEDGER (Figured_bass_position_engraver, note_column);
155 ADD_ACKNOWLEDGER (Figured_bass_position_engraver, slur);
156 ADD_ACKNOWLEDGER (Figured_bass_position_engraver, stem);
157 ADD_END_ACKNOWLEDGER (Figured_bass_position_engraver, slur);
158 ADD_END_ACKNOWLEDGER (Figured_bass_position_engraver, tie);
159 ADD_ACKNOWLEDGER (Figured_bass_position_engraver, bass_figure_alignment);
160 ADD_END_ACKNOWLEDGER (Figured_bass_position_engraver, bass_figure_alignment);
161 }
162
163 ADD_TRANSLATOR (Figured_bass_position_engraver,
164 /* doc */
165 "Position figured bass alignments over notes.",
166
167 /* create */
168 "BassFigureAlignmentPositioning ",
169
170 /* read */
171 "",
172
173 /* write */
174 ""
175 );
176