2 spacing-determine-loose-columns.cc -- implement Spacing_spanner
3 methods that decide which columns to turn loose.
5 source file of the GNU LilyPond music typesetter
7 (c) 2005--2009 Han-Wen Nienhuys <hanwen@xs4all.nl>
10 #include "staff-spacing.hh"
12 #include "spacing-options.hh"
14 #include "paper-column.hh"
15 #include "column-x-positions.hh"
16 #include "pointer-group-interface.hh"
17 #include "spacing-interface.hh"
18 #include "spacing-spanner.hh"
19 #include "note-spacing.hh"
21 #include "grob-array.hh"
22 #include "break-align-interface.hh"
26 Return whether COL is fixed to its neighbors by some kind of spacing
30 If in doubt, then we're not loose; the spacing engine should space
31 for it, risking suboptimal spacing.
33 (Otherwise, we might risk core dumps, and other weird stuff.)
36 is_loose_column (Grob *l, Grob *col, Grob *r, Spacing_options const *options)
38 if (!to_boolean (col->get_property ("allow-loose-spacing")))
42 if ((options->float_nonmusical_columns_
43 || options->float_grace_columns_)
44 && Paper_column::when_mom (col).grace_part_)
50 if (Paper_column::is_musical (col)
51 || Paper_column::is_breakable (col))
55 If this column doesn't have a proper neighbor, we should really
56 make it loose, but spacing it correctly is more than we can
59 (this happens in the following situation:
70 the column containing the clef is really loose, and should be
71 attached right to the first column, but that is a lot of work for
72 such a borderline case.)
76 Item *r_neighbor = unsmob_item (col->get_object ("right-neighbor"));
77 Item *l_neighbor = unsmob_item (col->get_object ("left-neighbor"));
79 if (!l_neighbor || !r_neighbor)
82 if (l == l_neighbor && r == r_neighbor)
86 Only declare loose if the bounds make a little sense. This means
87 some cases (two isolated, consecutive clef changes) won't be
88 nicely folded, but hey, then don't do that.
90 if (! ((Paper_column::is_musical (l_neighbor) || Paper_column::is_breakable (l_neighbor))
91 && (Paper_column::is_musical (r_neighbor) || Paper_column::is_breakable (r_neighbor))))
95 in any case, we don't want to move bar lines.
97 extract_grob_set (col, "elements", elts);
98 for (vsize i = elts.size (); i--;)
101 if (g && Break_alignment_interface::has_interface (g))
103 extract_grob_set (g, "elements", gelts);
104 for (vsize j = gelts.size (); j--;)
108 if (h && h->get_property ("break-align-symbol") == ly_symbol2scm ("staff-bar"))
118 Spacing_spanner::set_distances_for_loose_col (Grob *me, Grob *c,
119 Drul_array<Item *> next_door,
120 Spacing_options const *options)
123 Drul_array<Real> dists (0, 0);
127 Item *lc = dynamic_cast<Item *> ((d == LEFT) ? next_door[LEFT] : c);
128 Item *rc = dynamic_cast<Item *> (d == LEFT ? c : next_door[RIGHT]);
130 extract_grob_set (lc, "spacing-wishes", wishes);
131 for (vsize k = wishes.size (); k--;)
133 Grob *sp = wishes[k];
134 if (Spacing_interface::left_column (sp) != lc
135 || Spacing_interface::right_column (sp) != rc)
138 if (Note_spacing::has_interface (sp))
141 The note spacing should be taken from the musical
144 Real base = note_spacing (me, lc, rc, options);
145 Spring spring = Note_spacing::get_spacing (sp, rc, base, options->increment_);
147 dists[d] = max (dists[d], spring.min_distance ());
149 else if (Staff_spacing::has_interface (sp))
151 Spring spring = Staff_spacing::get_spacing (sp, rc);
153 dists[d] = max (dists[d], spring.min_distance ());
156 programming_error ("Subversive spacing wish");
159 while (flip (&d) != LEFT);
162 r.distance_ = dists[LEFT] + dists[RIGHT];
163 r.item_drul_ = next_door;
170 Remove columns that are not tightly fitting from COLS. In the
171 removed columns, set 'between-cols to the columns where it is in
175 Spacing_spanner::prune_loose_columns (Grob *me,
177 Spacing_options *options)
179 vector<Grob*> newcols;
181 for (vsize i = 0; i < cols->size (); i++)
183 Grob *c = cols->at (i);
185 bool loose = (i > 0 && i + 1 < cols->size ())
186 && is_loose_column (cols->at (i - 1), c, cols->at (i + 1), options);
190 Grob *right_neighbor = unsmob_grob (c->get_object ("right-neighbor"));
191 Grob *left_neighbor = unsmob_grob (c->get_object ("left-neighbor"));
194 Either object can be non existent, if the score ends
197 if (!right_neighbor || !left_neighbor)
199 c->programming_error ("Cannot determine neighbors for floating column. ");
200 c->set_object ("between-cols", scm_cons (cols->at (i-1)->self_scm (),
201 cols->at (i+1)->self_scm ()));
205 c->set_object ("between-cols", scm_cons (left_neighbor->self_scm (),
206 right_neighbor->self_scm ()));
210 Set distance constraints for loose columns
212 Drul_array<Item *> next_door (dynamic_cast<Item*> (cols->at (i - 1)),
213 dynamic_cast<Item*> (cols->at (i + 1)));
215 set_distances_for_loose_col (me, c, next_door, options);
220 newcols.push_back (c);
227 Set neighboring columns determined by the spacing-wishes grob property.
230 Spacing_spanner::set_explicit_neighbor_columns (vector<Grob*> const &cols)
232 for (vsize i = 0; i < cols.size (); i++)
234 extract_grob_set (cols[i], "spacing-wishes", wishes);
235 for (vsize j = wishes.size (); j--;)
237 Item *wish = dynamic_cast<Item*> (wishes[j]);
238 Item *left_col = wish->get_column ();
239 int left_rank = Paper_column::get_rank (left_col);
240 int min_right_rank = INT_MAX;
242 extract_grob_set (wish, "right-items", right_items);
243 for (vsize k = right_items.size (); k--;)
245 Item *right_col = dynamic_cast<Item*> (right_items[k])->get_column ();
246 int right_rank = Paper_column::get_rank (right_col);
248 if (right_rank < min_right_rank)
250 left_col->set_object ("right-neighbor", right_col->self_scm ());
251 min_right_rank = right_rank;
254 Grob *old_left_neighbor = unsmob_grob (right_col->get_object ("left-neighbor"));
255 if (!old_left_neighbor || left_rank > Paper_column::get_rank (old_left_neighbor))
256 right_col->set_object ("left-neighbor", left_col->self_scm ());
263 Set neighboring columns that have no left/right-neighbor set
264 yet. Only do breakable non-musical columns, and musical columns.
265 Why only these? --jneem
268 Spacing_spanner::set_implicit_neighbor_columns (vector<Grob*> const &cols)
270 for (vsize i = 0; i < cols.size (); i++)
272 Item *it = dynamic_cast<Item *> (cols[i]);
273 if (!Paper_column::is_breakable (it) && !Paper_column::is_musical (it))
276 if (i && !unsmob_grob (cols[i]->get_object ("left-neighbor")))
277 cols[i]->set_object ("left-neighbor", cols[i-1]->self_scm ());
278 if (i + 1 < cols.size () && !unsmob_grob (cols[i]->get_object ("right-neighbor")))
279 cols[i]->set_object ("right-neighbor", cols[i+1]->self_scm ());