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--2007 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-spanner.hh"
18 #include "note-spacing.hh"
20 #include "grob-array.hh"
21 #include "break-align-interface.hh"
25 Return whether COL is fixed to its neighbors by some kind of spacing
29 If in doubt, then we're not loose; the spacing engine should space
30 for it, risking suboptimal spacing.
32 (Otherwise, we might risk core dumps, and other weird stuff.)
35 is_loose_column (Grob *l, Grob *col, Grob *r, Spacing_options const *options)
37 if (!to_boolean (col->get_property ("allow-loose-spacing")))
41 if ((options->float_nonmusical_columns_
42 || options->float_grace_columns_)
43 && Paper_column::when_mom (col).grace_part_)
49 if (Paper_column::is_musical (col)
50 || Paper_column::is_breakable (col))
54 If this column doesn't have a proper neighbor, we should really
55 make it loose, but spacing it correctly is more than we can
58 (this happens in the following situation:
69 the column containing the clef is really loose, and should be
70 attached right to the first column, but that is a lot of work for
71 such a borderline case.)
75 extract_grob_set (col, "right-neighbors", rns);
76 extract_grob_set (col, "left-neighbors", lns);
78 if (lns.empty () || rns.empty ())
82 Item *l_neighbor = dynamic_cast<Item *> (lns[0]);
83 Item *r_neighbor = dynamic_cast<Item *> (rns[0]);
85 if (!l_neighbor || !r_neighbor)
88 l_neighbor = l_neighbor->get_column ();
89 r_neighbor = dynamic_cast<Item *> (Note_spacing::right_column (r_neighbor));
91 if (l == l_neighbor && r == r_neighbor)
94 if (!l_neighbor || !r_neighbor)
98 Only declare loose if the bounds make a little sense. This means
99 some cases (two isolated, consecutive clef changes) won't be
100 nicely folded, but hey, then don't do that.
102 if (! ((Paper_column::is_musical (l_neighbor) || Paper_column::is_breakable (l_neighbor))
103 && (Paper_column::is_musical (r_neighbor) || Paper_column::is_breakable (r_neighbor))))
107 in any case, we don't want to move bar lines.
109 extract_grob_set (col, "elements", elts);
110 for (vsize i = elts.size (); i--;)
113 if (g && Break_alignment_interface::has_interface (g))
115 extract_grob_set (g, "elements", gelts);
116 for (vsize j = gelts.size (); j--;)
120 if (h && h->get_property ("break-align-symbol") == ly_symbol2scm ("staff-bar"))
130 Spacing_spanner::set_distances_for_loose_col (Grob *me, Grob *c,
131 Drul_array<Item *> next_door,
132 Spacing_options const *options)
135 Drul_array<Real> dists (0, 0);
139 Item *lc = dynamic_cast<Item *> ((d == LEFT) ? next_door[LEFT] : c);
140 Item *rc = dynamic_cast<Item *> (d == LEFT ? c : next_door[RIGHT]);
142 extract_grob_set (lc, "spacing-wishes", wishes);
143 for (vsize k = wishes.size (); k--;)
145 Grob *sp = wishes[k];
146 if (Note_spacing::left_column (sp) != lc
147 || Note_spacing::right_column (sp) != rc)
150 if (Note_spacing::has_interface (sp))
153 The note spacing should be taken from the musical
159 Real base = note_spacing (me, lc, rc, options);
160 Note_spacing::get_spacing (sp, rc, base, options->increment_,
163 space -= options->increment_;
165 dists[d] = max (dists[d], space);
167 else if (Staff_spacing::has_interface (sp))
169 Spring spring = Staff_spacing::get_spacing_params (sp);
170 Real fixed = spring.distance_ - spring.inverse_compress_strength_;
172 dists[d] = max (dists[d], fixed);
175 programming_error ("Subversive spacing wish");
178 while (flip (&d) != LEFT);
181 r.distance_ = dists[LEFT] + dists[RIGHT];
182 r.item_drul_ = next_door;
189 Remove columns that are not tightly fitting from COLS. In the
190 removed columns, set 'between-cols to the columns where it is in
194 Spacing_spanner::prune_loose_columns (Grob *me,
196 Spacing_options *options)
198 vector<Grob*> newcols;
200 for (vsize i = 0; i < cols->size (); i++)
202 Grob *c = cols->at (i);
204 bool loose = (i > 0 && i + 1 < cols->size ())
205 && is_loose_column (cols->at (i - 1), c, cols->at (i + 1), options);
209 extract_grob_set (c, "right-neighbors", rns_arr);
210 extract_grob_set (c, "left-neighbors", lns_arr);
212 SCM lns = lns_arr.size () ? lns_arr.back ()->self_scm () : SCM_BOOL_F;
213 SCM rns = rns_arr.size () ? rns_arr.back ()->self_scm () : SCM_BOOL_F;
216 Either object can be non existent, if the score ends
220 extract_grob_set (unsmob_grob (rns), "right-items", right_items);
221 if (right_items.size () == 0 || !unsmob_grob (lns))
223 c->programming_error ("Cannot determine neighbors for floating column. ");
224 c->set_object ("between-cols", scm_cons (cols->at (i-1)->self_scm (),
225 cols->at (i+1)->self_scm ()));
230 int min_rank = INT_MAX;
231 for (vsize j = 0; j < right_items.size (); j ++)
233 int rank = dynamic_cast<Item*> (right_items[j])->get_column ()->get_rank ();
236 min_item = right_items[j];
241 c->set_object ("between-cols", scm_cons (lns,
242 min_item->self_scm ()));
245 Set distance constraints for loose columns
247 Drul_array<Item *> next_door (dynamic_cast<Item*> (cols->at (i - 1)),
248 dynamic_cast<Item*> (cols->at (i + 1)));
250 set_distances_for_loose_col (me, c, next_door, options);
255 newcols.push_back (c);
262 Set neighboring columns determined by the spacing-wishes grob property.
265 Spacing_spanner::set_explicit_neighbor_columns (vector<Grob*> const &cols)
267 for (vsize i = 0; i < cols.size (); i++)
269 SCM right_neighbors = Grob_array::make_array ();
270 Grob_array *rn_arr = unsmob_grob_array (right_neighbors);
271 int min_rank = INT_MAX;
273 extract_grob_set (cols[i], "spacing-wishes", wishes);
274 for (vsize k = wishes.size (); k--;)
276 Item *wish = dynamic_cast<Item *> (wishes[k]);
278 Item *lc = wish->get_column ();
279 Grob *right = Note_spacing::right_column (wish);
284 Item *rc = dynamic_cast<Item *> (right);
286 int right_rank = Paper_column::get_rank (rc);
287 int left_rank = Paper_column::get_rank (lc);
290 update the left column.
292 if (right_rank <= min_rank)
294 if (right_rank < min_rank)
297 min_rank = right_rank;
302 update the right column of the wish.
306 extract_grob_set (rc, "left-neighbors", lns_arr);
309 Item *it = dynamic_cast<Item *> (lns_arr.back ());
310 maxrank = Paper_column::get_rank (it->get_column ());
313 if (left_rank >= maxrank)
316 if (left_rank > maxrank)
318 Grob_array *ga = unsmob_grob_array (rc->get_object ("left-neighbors"));
323 Pointer_group_interface::add_grob (rc, ly_symbol2scm ("left-neighbors"), wish);
328 cols[i]->set_object ("right-neighbors", right_neighbors);
333 Set neighboring columns that have no left/right-neighbor set
334 yet. Only do breakable non-musical columns, and musical columns.
337 Spacing_spanner::set_implicit_neighbor_columns (vector<Grob*> const &cols)
339 for (vsize i = 0; i < cols.size (); i++)
341 Item *it = dynamic_cast<Item *> (cols[i]);
342 if (!Paper_column::is_breakable (it) && !Paper_column::is_musical (it))
346 sloppy with typing left/right-neighbors should take list, but paper-column found instead.
348 extract_grob_set (cols[i], "left-neighbors", lns);
349 if (lns.empty () && i)
351 SCM ga_scm = Grob_array::make_array ();
352 Grob_array *ga = unsmob_grob_array (ga_scm);
353 ga->add (cols[i - 1]);
354 cols[i]->set_object ("left-neighbors", ga_scm);
356 extract_grob_set (cols[i], "right-neighbors", rns);
357 if (rns.empty () && i + 1 < cols.size ())
359 SCM ga_scm = Grob_array::make_array ();
360 Grob_array *ga = unsmob_grob_array (ga_scm);
361 ga->add (cols[i + 1]);
362 cols[i]->set_object ("right-neighbors", ga_scm);