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 Han-Wen Nienhuys <hanwen@xs4all.nl>
11 #include "staff-spacing.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 "break-align-interface.hh"
24 Return whether COL is fixed to its neighbors by some kind of spacing
28 If in doubt, then we're not loose; the spacing engine should space
29 for it, risking suboptimal spacing.
31 (Otherwise, we might risk core dumps, and other weird stuff.)
34 is_loose_column (Grob *l, Grob *c, Grob *r, Spacing_options const *options)
36 if (options->float_nonmusical_columns_
37 && Paper_column::when_mom (c).grace_part_)
40 if (Paper_column::is_musical (c)
41 || Item::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.is_empty () || rns.is_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) || Item::is_breakable (l_neighbor))
92 && (Paper_column::is_musical (r_neighbor) || Item::is_breakable (r_neighbor))))
98 A rather hairy check, but we really only want to move around
99 clefs. (anything else?)
101 in any case, we don't want to move bar lines.
103 extract_grob_set (c, "elements", elts);
104 for (int i = elts.size (); i--; )
107 if (g && Break_align_interface::has_interface (g))
109 extract_grob_set (g, "elements", gelts);
110 for (int j = gelts.size (); j--; )
115 ugh. -- fix staff-bar name?
117 if (h && h->get_property ("break-align-symbol") == ly_symbol2scm ("staff-bar"))
127 Remove columns that are not tightly fitting from COLS. In the
128 removed columns, set 'between-cols to the columns where it is in
132 Spacing_spanner::prune_loose_columns (Grob *me, Link_array<Grob> *cols,
133 Spacing_options const *options)
135 Link_array<Grob> newcols;
137 for (int i = 0; i < cols->size (); i++)
139 Grob *c = cols->elem (i);
141 bool loose = (i > 0 && i < cols->size()-1)
142 && is_loose_column (cols->elem (i - 1), c, cols->elem (i + 1), options);
147 extract_grob_set (c, "right-neighbors", rns_arr);
148 extract_grob_set (c, "left-neighbors", lns_arr);
150 SCM lns = lns_arr.size () ? lns_arr.top()->self_scm () : SCM_BOOL_F;
151 SCM rns = rns_arr.size () ? rns_arr.top()->self_scm () : SCM_BOOL_F;
154 Either object can be non existent, if the score ends
158 extract_grob_set (unsmob_grob (rns), "right-items", right_items);
159 c->set_object ("between-cols", scm_cons (lns,
160 right_items[0]->self_scm ()));
163 Set distance constraints for loose columns
165 Drul_array<Grob *> next_door;
166 next_door[LEFT] = cols->elem (i - 1);
167 next_door[RIGHT] = cols->elem (i + 1);
169 Drul_array<Real> dists (0, 0);
174 Item *lc = dynamic_cast<Item *> ((d == LEFT) ? next_door[LEFT] : c);
175 Item *rc = dynamic_cast<Item *> (d == LEFT ? c : next_door[RIGHT]);
177 extract_grob_set (lc, "spacing-wishes", wishes);
178 for (int k = wishes.size(); k--;)
180 Grob *sp = wishes[k];
181 if (Note_spacing::left_column (sp) != lc
182 || Note_spacing::right_column (sp) != rc)
189 if (Note_spacing::has_interface (sp))
192 The note spacing should be taken from the musical
196 Real base = note_spacing (me, lc, rc, options, &dummy);
197 Note_spacing::get_spacing (sp, rc, base, options->increment_, &space, &fixed);
199 space -= options->increment_;
201 dists[d] = max (dists[d], space);
203 else if (Staff_spacing::has_interface (sp))
205 Real space, fixed_space;
206 Staff_spacing::get_spacing_params (sp,
207 &space, &fixed_space);
209 dists[d] = max (dists[d], fixed_space);
213 programming_error ("Subversive spacing wish");
217 while (flip (&d) != LEFT);
220 r.distance_ = dists[LEFT] + dists[RIGHT];
221 r.item_drul_[LEFT] = dynamic_cast<Item *> (cols->elem (i - 1));
222 r.item_drul_[RIGHT] = dynamic_cast<Item *> (cols->elem (i + 1));
236 Set neighboring columns determined by the spacing-wishes grob property.
239 Spacing_spanner::set_explicit_neighbor_columns (Link_array<Grob> const &cols)
241 for (int i = 0; i < cols.size (); i++)
243 SCM right_neighbors = Grob_array::make_array ();
244 Grob_array *rn_arr = unsmob_grob_array (right_neighbors);
245 int min_rank = 100000; // inf.
247 extract_grob_set (cols[i], "spacing-wishes", wishes);
248 for (int k = wishes.size(); k--;)
250 Item *wish = dynamic_cast<Item *> ( wishes[k]);
252 Item *lc = wish->get_column ();
253 Grob *right = Note_spacing::right_column (wish);
258 Item *rc = dynamic_cast<Item *> (right);
260 int right_rank = Paper_column::get_rank (rc);
261 int left_rank = Paper_column::get_rank (lc);
264 update the left column.
266 if (right_rank <= min_rank)
268 if (right_rank < min_rank)
271 min_rank = right_rank;
276 update the right column of the wish.
280 extract_grob_set (rc, "left-neighbors", lns_arr);
283 Item *it = dynamic_cast<Item *> (lns_arr.top());
284 maxrank = Paper_column::get_rank (it->get_column ());
287 if (left_rank >= maxrank)
290 if (left_rank > maxrank)
292 Grob_array *ga = unsmob_grob_array (rc->get_object ("left-neighbors"));
297 Pointer_group_interface::add_grob (rc, ly_symbol2scm ("left-neighbors"), wish);
303 cols[i]->set_object ("right-neighbors", right_neighbors);
309 Set neighboring columns that have no left/right-neighbor set
310 yet. Only do breakable non-musical columns, and musical columns.
313 Spacing_spanner::set_implicit_neighbor_columns (Link_array<Grob> const &cols)
315 for (int i = 0; i < cols.size (); i++)
317 Item *it = dynamic_cast<Item *> (cols[i]);
318 if (!Item::is_breakable (it) && !Paper_column::is_musical (it))
321 // it->breakable || it->musical
324 sloppy with typing left/right-neighbors should take list, but paper-column found instead.
326 extract_grob_set (cols[i], "left-neighbors", lns);
327 if (lns.is_empty () && i )
329 SCM ga_scm = Grob_array::make_array();
330 Grob_array *ga = unsmob_grob_array (ga_scm);
332 cols[i]->set_object ("left-neighbors", ga_scm);
334 extract_grob_set (cols[i], "right-neighbors", rns);
335 if (rns.is_empty () && i < cols.size () - 1)
337 SCM ga_scm = Grob_array::make_array();
338 Grob_array *ga = unsmob_grob_array (ga_scm);
340 cols[i]->set_object ("right-neighbors", ga_scm);