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>
14 #include "paper-column.hh"
15 #include "column-x-positions.hh"
16 #include "staff-spacing.hh"
17 #include "pointer-group-interface.hh"
18 #include "spacing-spanner.hh"
19 #include "note-spacing.hh"
21 #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)
36 extract_grob_set (c, "right-neighbors", rns);
37 extract_grob_set (c, "left-neighbors", lns);
40 If this column doesn't have a proper neighbor, we should really
41 make it loose, but spacing it correctly is more than we can
44 (this happens in the following situation:
55 the column containing the clef is really loose, and should be
56 attached right to the first column, but that is a lot of work for
57 such a borderline case.)
60 if (lns.is_empty () || rns.is_empty ())
63 Item *l_neighbor = dynamic_cast<Item *> (lns[0]);
64 Item *r_neighbor = dynamic_cast<Item *> (rns[0]);
66 if (!l_neighbor || !r_neighbor)
69 l_neighbor = l_neighbor->get_column ();
70 r_neighbor = dynamic_cast<Item *> (Note_spacing::right_column (r_neighbor));
72 if (l == l_neighbor && r == r_neighbor)
75 if (!l_neighbor || !r_neighbor)
79 Only declare loose if the bounds make a little sense. This means
80 some cases (two isolated, consecutive clef changes) won't be
81 nicely folded, but hey, then don't do that.
83 if (! ((Paper_column::is_musical (l_neighbor) || Item::is_breakable (l_neighbor))
84 && (Paper_column::is_musical (r_neighbor) || Item::is_breakable (r_neighbor))))
90 A rather hairy check, but we really only want to move around
91 clefs. (anything else?)
93 in any case, we don't want to move bar lines.
95 extract_grob_set (c, "elements", elts);
96 for (int i = elts.size (); i--; )
99 if (g && Break_align_interface::has_interface (g))
101 extract_grob_set (g, "elements", gelts);
102 for (int j = gelts.size (); j--; )
107 ugh. -- fix staff-bar name?
109 if (h && h->get_property ("break-align-symbol") == ly_symbol2scm ("staff-bar"))
119 Remove columns that are not tightly fitting from COLS. In the
120 removed columns, set 'between-cols to the columns where it is in
124 Spacing_spanner::prune_loose_columns (Grob *me, Link_array<Grob> *cols,
125 Spacing_options const *options)
127 Link_array<Grob> newcols;
128 Real increment = robust_scm2double (me->get_property ("spacing-increment"), 1.2);
129 for (int i = 0; i < cols->size (); i++)
131 if (Item::is_breakable (cols->elem (i))
132 || Paper_column::is_musical (cols->elem (i)))
134 newcols.push (cols->elem (i));
138 Grob *c = cols->elem (i);
139 if (is_loose_column (cols->elem (i - 1), c, cols->elem (i + 1)))
141 extract_grob_set (c, "right-neighbors", rns_arr);
142 extract_grob_set (c, "left-neighbors", lns_arr);
144 SCM lns = lns_arr.size () ? lns_arr.top()->self_scm () : SCM_BOOL_F;
145 SCM rns = rns_arr.size () ? rns_arr.top()->self_scm () : SCM_BOOL_F;
148 Either object can be non existent, if the score ends
152 extract_grob_set (unsmob_grob (rns), "right-items", right_items);
153 c->set_object ("between-cols", scm_cons (lns,
154 right_items[0]->self_scm ()));
157 Set distance constraints for loose columns
159 Drul_array<Grob *> next_door;
160 next_door[LEFT] = cols->elem (i - 1);
161 next_door[RIGHT] = cols->elem (i + 1);
163 Drul_array<Real> dists (0, 0);
168 Item *lc = dynamic_cast<Item *> ((d == LEFT) ? next_door[LEFT] : c);
169 Item *rc = dynamic_cast<Item *> (d == LEFT ? c : next_door[RIGHT]);
172 extract_grob_set (lc, "spacing-wishes", wishes);
173 for (int k = wishes.size(); k--;)
175 Grob *sp = wishes[k];
176 if (Note_spacing::left_column (sp) != lc
177 || Note_spacing::right_column (sp) != rc)
187 The note spacing should be taken from the musical
191 Real base = note_spacing (me, lc, rc, options, &dummy);
192 Note_spacing::get_spacing (sp, rc, base, increment, &space, &fixed);
196 dists[d] = max (dists[d], space);
200 Real space, fixed_space;
201 Staff_spacing::get_spacing_params (sp,
202 &space, &fixed_space);
204 dists[d] = max (dists[d], fixed_space);
208 while (flip (&d) != LEFT);
211 r.distance_ = dists[LEFT] + dists[RIGHT];
212 r.item_drul_[LEFT] = dynamic_cast<Item *> (cols->elem (i - 1));
213 r.item_drul_[RIGHT] = dynamic_cast<Item *> (cols->elem (i + 1));
227 Set neighboring columns determined by the spacing-wishes grob property.
230 Spacing_spanner::set_explicit_neighbor_columns (Link_array<Grob> const &cols)
232 for (int i = 0; i < cols.size (); i++)
234 SCM right_neighbors = Grob_array::make_array ();
235 Grob_array *rn_arr = unsmob_grob_array (right_neighbors);
236 int min_rank = 100000; // inf.
238 extract_grob_set (cols[i], "spacing-wishes", wishes);
239 for (int k = wishes.size(); k--;)
241 Item *wish = dynamic_cast<Item *> ( wishes[k]);
243 Item *lc = wish->get_column ();
244 Grob *right = Note_spacing::right_column (wish);
249 Item *rc = dynamic_cast<Item *> (right);
251 int right_rank = Paper_column::get_rank (rc);
252 int left_rank = Paper_column::get_rank (lc);
255 update the left column.
257 if (right_rank <= min_rank)
259 if (right_rank < min_rank)
262 min_rank = right_rank;
267 update the right column of the wish.
271 extract_grob_set (rc, "left-neighbors", lns_arr);
274 Item *it = dynamic_cast<Item *> (lns_arr.top());
275 maxrank = Paper_column::get_rank (it->get_column ());
278 if (left_rank >= maxrank)
281 if (left_rank > maxrank)
283 Grob_array *ga = unsmob_grob_array (rc->get_object ("left-neighbors"));
288 Pointer_group_interface::add_grob (rc, ly_symbol2scm ("left-neighbors"), wish);
294 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 (Link_array<Grob> const &cols)
306 for (int i = 0; i < cols.size (); i++)
308 Item *it = dynamic_cast<Item *> (cols[i]);
309 if (!Item::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.is_empty () && i )
320 SCM ga_scm = Grob_array::make_array();
321 Grob_array *ga = unsmob_grob_array (ga_scm);
323 cols[i]->set_object ("left-neighbors", ga_scm);
325 extract_grob_set (cols[i], "right-neighbors", rns);
326 if (rns.is_empty () && i < cols.size () - 1)
328 SCM ga_scm = Grob_array::make_array();
329 Grob_array *ga = unsmob_grob_array (ga_scm);
331 cols[i]->set_object ("right-neighbors", ga_scm);