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--2006 Han-Wen Nienhuys <hanwen@xs4all.nl>
10 #include "staff-spacing.hh"
13 #include "paper-column.hh"
14 #include "column-x-positions.hh"
15 #include "pointer-group-interface.hh"
16 #include "spacing-spanner.hh"
17 #include "note-spacing.hh"
19 #include "break-align-interface.hh"
23 Return whether COL is fixed to its neighbors by some kind of spacing
27 If in doubt, then we're not loose; the spacing engine should space
28 for it, risking suboptimal spacing.
30 (Otherwise, we might risk core dumps, and other weird stuff.)
33 is_loose_column (Grob *l, Grob *c, Grob *r, Spacing_options const *options)
35 if ((options->float_nonmusical_columns_
36 ||options->float_grace_columns_)
37 && Paper_column::when_mom (c).grace_part_)
40 if (Paper_column::is_musical (c)
41 || Paper_column::is_breakable (c))
44 extract_grob_set (c, "right-neighbors", rns);
45 extract_grob_set (c, "left-neighbors", lns);
48 If this column doesn't have a proper neighbor, we should really
49 make it loose, but spacing it correctly is more than we can
52 (this happens in the following situation:
63 the column containing the clef is really loose, and should be
64 attached right to the first column, but that is a lot of work for
65 such a borderline case.)
68 if (lns.empty () || rns.empty ())
71 Item *l_neighbor = dynamic_cast<Item *> (lns[0]);
72 Item *r_neighbor = dynamic_cast<Item *> (rns[0]);
74 if (!l_neighbor || !r_neighbor)
77 l_neighbor = l_neighbor->get_column ();
78 r_neighbor = dynamic_cast<Item *> (Note_spacing::right_column (r_neighbor));
80 if (l == l_neighbor && r == r_neighbor)
83 if (!l_neighbor || !r_neighbor)
87 Only declare loose if the bounds make a little sense. This means
88 some cases (two isolated, consecutive clef changes) won't be
89 nicely folded, but hey, then don't do that.
91 if (! ((Paper_column::is_musical (l_neighbor) || Paper_column::is_breakable (l_neighbor))
92 && (Paper_column::is_musical (r_neighbor) || Paper_column::is_breakable (r_neighbor))))
96 A rather hairy check, but we really only want to move around
97 clefs. (anything else?)
99 in any case, we don't want to move bar lines.
101 extract_grob_set (c, "elements", elts);
102 for (vsize i = elts.size (); i--;)
105 if (g && Break_align_interface::has_interface (g))
107 extract_grob_set (g, "elements", gelts);
108 for (vsize j = gelts.size (); j--;)
113 ugh. -- fix staff-bar name?
115 if (h && h->get_property ("break-align-symbol") == ly_symbol2scm ("staff-bar"))
125 Remove columns that are not tightly fitting from COLS. In the
126 removed columns, set 'between-cols to the columns where it is in
130 Spacing_spanner::prune_loose_columns (Grob *me, vector<Grob*> *cols,
131 Spacing_options const *options)
133 vector<Grob*> newcols;
135 for (vsize i = 0; i < cols->size (); i++)
137 Grob *c = cols->at (i);
139 bool loose = (i > 0 && i < cols->size () - 1)
140 && is_loose_column (cols->at (i - 1), c, cols->at (i + 1), options);
144 extract_grob_set (c, "right-neighbors", rns_arr);
145 extract_grob_set (c, "left-neighbors", lns_arr);
147 SCM lns = lns_arr.size () ? lns_arr.back ()->self_scm () : SCM_BOOL_F;
148 SCM rns = rns_arr.size () ? rns_arr.back ()->self_scm () : SCM_BOOL_F;
151 Either object can be non existent, if the score ends
155 extract_grob_set (unsmob_grob (rns), "right-items", right_items);
156 c->set_object ("between-cols", scm_cons (lns,
157 right_items[0]->self_scm ()));
160 Set distance constraints for loose columns
162 Drul_array<Grob *> next_door;
163 next_door[LEFT] = cols->at (i - 1);
164 next_door[RIGHT] = cols->at (i + 1);
166 Drul_array<Real> dists (0, 0);
171 Item *lc = dynamic_cast<Item *> ((d == LEFT) ? next_door[LEFT] : c);
172 Item *rc = dynamic_cast<Item *> (d == LEFT ? c : next_door[RIGHT]);
174 extract_grob_set (lc, "spacing-wishes", wishes);
175 for (vsize k = wishes.size (); k--;)
177 Grob *sp = wishes[k];
178 if (Note_spacing::left_column (sp) != lc
179 || Note_spacing::right_column (sp) != rc)
186 if (Note_spacing::has_interface (sp))
189 The note spacing should be taken from the musical
193 Real base = note_spacing (me, lc, rc, options, &dummy);
194 Note_spacing::get_spacing (sp, rc, base, options->increment_, &space, &fixed);
196 space -= options->increment_;
198 dists[d] = max (dists[d], space);
200 else if (Staff_spacing::has_interface (sp))
202 Real space, fixed_space;
203 Staff_spacing::get_spacing_params (sp,
204 &space, &fixed_space);
206 dists[d] = max (dists[d], fixed_space);
209 programming_error ("Subversive spacing wish");
212 while (flip (&d) != LEFT);
215 r.distance_ = dists[LEFT] + dists[RIGHT];
216 r.item_drul_[LEFT] = dynamic_cast<Item *> (cols->at (i - 1));
217 r.item_drul_[RIGHT] = dynamic_cast<Item *> (cols->at (i + 1));
222 newcols.push_back (c);
229 Set neighboring columns determined by the spacing-wishes grob property.
232 Spacing_spanner::set_explicit_neighbor_columns (vector<Grob*> const &cols)
234 for (vsize i = 0; i < cols.size (); i++)
236 SCM right_neighbors = Grob_array::make_array ();
237 Grob_array *rn_arr = unsmob_grob_array (right_neighbors);
238 int min_rank = 100000; // inf.
240 extract_grob_set (cols[i], "spacing-wishes", wishes);
241 for (vsize k = wishes.size (); k--;)
243 Item *wish = dynamic_cast<Item *> (wishes[k]);
245 Item *lc = wish->get_column ();
246 Grob *right = Note_spacing::right_column (wish);
251 Item *rc = dynamic_cast<Item *> (right);
253 int right_rank = Paper_column::get_rank (rc);
254 int left_rank = Paper_column::get_rank (lc);
257 update the left column.
259 if (right_rank <= min_rank)
261 if (right_rank < min_rank)
264 min_rank = right_rank;
269 update the right column of the wish.
273 extract_grob_set (rc, "left-neighbors", lns_arr);
276 Item *it = dynamic_cast<Item *> (lns_arr.back ());
277 maxrank = Paper_column::get_rank (it->get_column ());
280 if (left_rank >= maxrank)
283 if (left_rank > maxrank)
285 Grob_array *ga = unsmob_grob_array (rc->get_object ("left-neighbors"));
290 Pointer_group_interface::add_grob (rc, ly_symbol2scm ("left-neighbors"), wish);
295 cols[i]->set_object ("right-neighbors", right_neighbors);
300 Set neighboring columns that have no left/right-neighbor set
301 yet. Only do breakable non-musical columns, and musical columns.
304 Spacing_spanner::set_implicit_neighbor_columns (vector<Grob*> const &cols)
306 for (vsize i = 0; i < cols.size (); i++)
308 Item *it = dynamic_cast<Item *> (cols[i]);
309 if (!Paper_column::is_breakable (it) && !Paper_column::is_musical (it))
312 // it->breakable || it->musical
315 sloppy with typing left/right-neighbors should take list, but paper-column found instead.
317 extract_grob_set (cols[i], "left-neighbors", lns);
318 if (lns.empty () && i)
320 SCM ga_scm = Grob_array::make_array ();
321 Grob_array *ga = unsmob_grob_array (ga_scm);
322 ga->add (cols[i - 1]);
323 cols[i]->set_object ("left-neighbors", ga_scm);
325 extract_grob_set (cols[i], "right-neighbors", rns);
326 if (rns.empty () && i < cols.size () - 1)
328 SCM ga_scm = Grob_array::make_array ();
329 Grob_array *ga = unsmob_grob_array (ga_scm);
330 ga->add (cols[i + 1]);
331 cols[i]->set_object ("right-neighbors", ga_scm);