1 /*
2 This file is part of LilyPond, the GNU music typesetter.
3
4 Copyright (C) 1997--2020 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 "context.hh"
21 #include "paper-column.hh"
22 #include "align-interface.hh"
23 #include "axis-group-interface.hh"
24 #include "engraver.hh"
25 #include "international.hh"
26 #include "spanner.hh"
27 #include "pointer-group-interface.hh"
28 #include "grob-array.hh"
29
30 #include "translator.icc"
31
32 using std::string;
33 using std::vector;
34
35 class Vertical_align_engraver : public Engraver
36 {
37 Spanner *valign_;
38 bool qualifies (Grob_info) const;
39 SCM id_to_group_hashtab_;
40
41 public:
42 TRANSLATOR_DECLARATIONS (Vertical_align_engraver);
43 void acknowledge_axis_group (Grob_info);
44 void acknowledge_outside_staff (Grob_info);
45
46 protected:
47 void derived_mark () const override;
48 void process_music ();
49 void finalize () override;
50 void initialize () override;
51
52 bool top_level_;
53 };
54
55 void
boot()56 Vertical_align_engraver::boot ()
57 {
58 ADD_ACKNOWLEDGER (Vertical_align_engraver, axis_group);
59 ADD_ACKNOWLEDGER (Vertical_align_engraver, outside_staff);
60 }
61
62 ADD_TRANSLATOR (Vertical_align_engraver,
63 /* doc */
64 "Catch groups (staves, lyrics lines, etc.) and stack them"
65 " vertically.",
66
67 /* create */
68 "VerticalAlignment ",
69
70 /* read */
71 "alignAboveContext "
72 "alignBelowContext "
73 "hasAxisGroup ",
74
75 /* write */
76 ""
77 );
78
Vertical_align_engraver(Context * c)79 Vertical_align_engraver::Vertical_align_engraver (Context *c)
80 : Engraver (c)
81 {
82 valign_ = 0;
83 id_to_group_hashtab_ = SCM_EOL;
84 top_level_ = false;
85 }
86
87 void
derived_mark() const88 Vertical_align_engraver::derived_mark () const
89 {
90 scm_gc_mark (id_to_group_hashtab_);
91 }
92
93 void
initialize()94 Vertical_align_engraver::initialize ()
95 {
96 id_to_group_hashtab_ = scm_c_make_hash_table (11);
97 }
98
99 void
process_music()100 Vertical_align_engraver::process_music ()
101 {
102 if (!valign_ && !scm_is_null (id_to_group_hashtab_))
103 {
104 if (from_scm<bool> (get_property (this, "hasAxisGroup")))
105 {
106 warning (_ ("Ignoring Vertical_align_engraver in VerticalAxisGroup"));
107 id_to_group_hashtab_ = SCM_EOL;
108 return;
109 }
110
111 top_level_ = from_scm<bool> (get_property (this, "topLevelAlignment"));
112
113 valign_ = make_spanner (top_level_ ? "VerticalAlignment" : "StaffGrouper", SCM_EOL);
114 valign_->set_bound (LEFT, unsmob<Grob> (get_property (this, "currentCommandColumn")));
115 Align_interface::set_ordered (valign_);
116 }
117 }
118
119 void
finalize()120 Vertical_align_engraver::finalize ()
121 {
122 if (valign_)
123 {
124 valign_->set_bound (RIGHT, unsmob<Grob> (get_property (this, "currentCommandColumn")));
125 valign_ = 0;
126 }
127 }
128
129 bool
qualifies(Grob_info i) const130 Vertical_align_engraver::qualifies (Grob_info i) const
131 {
132 return has_interface<Axis_group_interface> (i.grob ())
133 && dynamic_cast<Spanner *> (i.grob ())
134 && !i.grob ()->get_y_parent ()
135 && !from_scm<bool> (get_property (i.grob (), "no-alignment"))
136 && Axis_group_interface::has_axis (i.grob (), Y_AXIS);
137 }
138
139 void
acknowledge_axis_group(Grob_info i)140 Vertical_align_engraver::acknowledge_axis_group (Grob_info i)
141 {
142 if (scm_is_null (id_to_group_hashtab_))
143 return;
144
145 if (top_level_ && qualifies (i))
146 {
147 string id = i.context ()->id_string ();
148
149 scm_hash_set_x (id_to_group_hashtab_, ly_string2scm (id),
150 i.grob ()->self_scm ());
151
152 SCM before_id = get_property (i.context (), "alignAboveContext");
153 SCM after_id = get_property (i.context (), "alignBelowContext");
154
155 SCM before = scm_hash_ref (id_to_group_hashtab_, before_id, SCM_BOOL_F);
156 SCM after = scm_hash_ref (id_to_group_hashtab_, after_id, SCM_BOOL_F);
157
158 Grob *before_grob = unsmob<Grob> (before);
159 Grob *after_grob = unsmob<Grob> (after);
160
161 Align_interface::add_element (valign_, i.grob ());
162
163 if (before_grob || after_grob)
164 {
165 Grob_array *ga = unsmob<Grob_array> (get_object (valign_, "elements"));
166 vector<Grob *> &arr = ga->array_reference ();
167
168 Grob *added = arr.back ();
169 arr.pop_back ();
170 for (vsize i = 0; i < arr.size (); i++)
171 {
172 if (arr[i] == before_grob)
173 {
174 arr.insert (arr.begin () + i, added);
175
176 /* Only set staff affinity if it already has one. That way we won't
177 set staff-affinity on things that don't want it (like staves). */
178 if (scm_is_number (get_property (added, "staff-affinity")))
179 set_property (added, "staff-affinity", to_scm (DOWN));
180 break;
181 }
182 else if (arr[i] == after_grob)
183 {
184 arr.insert (arr.begin () + i + 1, added);
185 if (scm_is_number (get_property (added, "staff-affinity")))
186 set_property (added, "staff-affinity", to_scm (UP));
187 break;
188 }
189 }
190 }
191 }
192 else if (qualifies (i))
193 {
194 Pointer_group_interface::add_grob (valign_, ly_symbol2scm ("elements"), i.grob ());
195 if (!unsmob<Grob> (get_object (i.grob (), "staff-grouper")))
196 set_object (i.grob (), "staff-grouper", valign_->self_scm ());
197 }
198 }
199
200 void
acknowledge_outside_staff(Grob_info i)201 Vertical_align_engraver::acknowledge_outside_staff (Grob_info i)
202 {
203 if (!top_level_) // valign_ is a staff grouper
204 {
205 if (valign_)
206 {
207 // Claim outside-staff grobs created by engravers in this immediate
208 // context.
209 if (i.context () == context ())
210 i.grob ()->set_y_parent (valign_);
211 }
212 else
213 {
214 programming_error ("cannot claim outside-staff grob before creating staff grouper");
215 }
216 }
217 }
218