2 spacing-loose-columns.cc -- implement loose column spacing.
4 source file of the GNU LilyPond music typesetter
6 (c) 2005 Han-Wen Nienhuys <hanwen@xs4all.nl>
11 #include "paper-column.hh"
12 #include "column-x-positions.hh"
13 #include "staff-spacing.hh"
14 #include "pointer-group-interface.hh"
15 #include "spacing-spanner.hh"
16 #include "note-spacing.hh"
18 #include "break-align-interface.hh"
20 /* Find the loose columns in POSNS, and drape them around the columns
21 specified in BETWEEN-COLS. */
23 set_loose_columns (System *which, Column_x_positions const *posns)
25 int loose_col_count = posns->loose_cols_.size ();
26 for (int i = 0; i < loose_col_count; i++)
29 Item *loose = dynamic_cast<Item *> (posns->loose_cols_[i]);
30 Paper_column *col = dynamic_cast<Paper_column *> (loose);
32 if (col->get_system ())
39 SCM between = loose->get_object ("between-cols");
40 if (!scm_is_pair (between))
43 Item *le = dynamic_cast<Item *> (unsmob_grob (scm_car (between)));
44 Item *re = dynamic_cast<Item *> (unsmob_grob (scm_cdr (between)));
51 left = le->get_column ();
52 if (!left->get_system ())
53 left = left->find_prebroken_piece (RIGHT);
57 loose = right = re->get_column ();
60 if (!right->get_system ())
61 right = right->find_prebroken_piece (LEFT);
63 Grob *common = right->common_refpoint (left, X_AXIS);
66 Real total_space = 0.0;
67 Real total_fixed = 0.0;
69 extract_grob_set (col, "spacing-wishes", wishes);
70 for (int i = 0; i < wishes.size (); i++)
72 Grob *spacing = wishes[i];
73 if (Staff_spacing::has_interface (spacing))
77 Staff_spacing::get_spacing_params (spacing, &space, &fixed);
85 Real right_point = 0.0;
86 Real distance_to_next = 0.0;
92 distance_to_next = total_space;
93 right_point = right->relative_coordinate (common, X_AXIS);
97 Interval my_extent = col->extent (col, X_AXIS);
98 distance_to_next = my_extent[RIGHT] + 1.0;
99 right_point = right->extent (common, X_AXIS)[LEFT];
102 Real my_offset = right_point - distance_to_next;
104 col->system_ = which;
105 col->translate_axis (my_offset - col->relative_coordinate (common, X_AXIS), X_AXIS);
112 Return whether COL is fixed to its neighbors by some kind of spacing
116 If in doubt, then we're not loose; the spacing engine should space
117 for it, risking suboptimal spacing.
119 (Otherwise, we might risk core dumps, and other weird stuff.)
122 loose_column (Grob *l, Grob *c, Grob *r)
124 extract_grob_set (c, "right-neighbors", rns);
125 extract_grob_set (c, "left-neighbors", lns);
128 If this column doesn't have a proper neighbor, we should really
129 make it loose, but spacing it correctly is more than we can
130 currently can handle.
132 (this happens in the following situation:
143 the column containing the clef is really loose, and should be
144 attached right to the first column, but that is a lot of work for
145 such a borderline case.)
148 if (lns.is_empty () || rns.is_empty ())
151 Item *l_neighbor = dynamic_cast<Item *> (lns[0]);
152 Item *r_neighbor = dynamic_cast<Item *> (rns[0]);
154 if (!l_neighbor || !r_neighbor)
157 l_neighbor = l_neighbor->get_column ();
158 r_neighbor = dynamic_cast<Item *> (Note_spacing::right_column (r_neighbor));
160 if (l == l_neighbor && r == r_neighbor)
163 if (!l_neighbor || !r_neighbor)
167 Only declare loose if the bounds make a little sense. This means
168 some cases (two isolated, consecutive clef changes) won't be
169 nicely folded, but hey, then don't do that.
171 if (! ((Paper_column::is_musical (l_neighbor) || Item::is_breakable (l_neighbor))
172 && (Paper_column::is_musical (r_neighbor) || Item::is_breakable (r_neighbor))))
178 A rather hairy check, but we really only want to move around
179 clefs. (anything else?)
181 in any case, we don't want to move bar lines.
183 extract_grob_set (c, "elements", elts);
184 for (int i = elts.size (); i--; )
187 if (g && Break_align_interface::has_interface (g))
189 extract_grob_set (g, "elements", gelts);
190 for (int j = gelts.size (); j--; )
195 ugh. -- fix staff-bar name?
197 if (h && h->get_property ("break-align-symbol") == ly_symbol2scm ("staff-bar"))
207 Remove columns that are not tightly fitting from COLS. In the
208 removed columns, set 'between-cols to the columns where it is in
212 Spacing_spanner::prune_loose_columns (Grob *me, Link_array<Grob> *cols,
213 Spacing_options const *options)
215 Link_array<Grob> newcols;
216 Real increment = robust_scm2double (me->get_property ("spacing-increment"), 1.2);
217 for (int i = 0; i < cols->size (); i++)
219 if (Item::is_breakable (cols->elem (i))
220 || Paper_column::is_musical (cols->elem (i)))
222 newcols.push (cols->elem (i));
226 Grob *c = cols->elem (i);
227 if (loose_column (cols->elem (i - 1), c, cols->elem (i + 1)))
229 extract_grob_set (c, "right-neighbors", rns_arr);
230 extract_grob_set (c, "left-neighbors", lns_arr);
232 SCM lns = lns_arr.size () ? lns_arr.top()->self_scm () : SCM_BOOL_F;
233 SCM rns = rns_arr.size () ? rns_arr.top()->self_scm () : SCM_BOOL_F;
236 Either object can be non existent, if the score ends
240 extract_grob_set (unsmob_grob (rns), "right-items", right_items);
241 c->set_object ("between-cols", scm_cons (lns,
242 right_items[0]->self_scm ()));
245 Set distance constraints for loose columns
247 Drul_array<Grob *> next_door;
248 next_door[LEFT] = cols->elem (i - 1);
249 next_door[RIGHT] = cols->elem (i + 1);
251 Drul_array<Real> dists (0, 0);
256 Item *lc = dynamic_cast<Item *> ((d == LEFT) ? next_door[LEFT] : c);
257 Item *rc = dynamic_cast<Item *> (d == LEFT ? c : next_door[RIGHT]);
260 extract_grob_set (lc, "spacing-wishes", wishes);
261 for (int k = wishes.size(); k--;)
263 Grob *sp = wishes[k];
264 if (Note_spacing::left_column (sp) != lc
265 || Note_spacing::right_column (sp) != rc)
275 The note spacing should be taken from the musical
279 Real base = note_spacing (me, lc, rc, options, &dummy);
280 Note_spacing::get_spacing (sp, rc, base, increment, &space, &fixed);
284 dists[d] = max (dists[d], space);
288 Real space, fixed_space;
289 Staff_spacing::get_spacing_params (sp,
290 &space, &fixed_space);
292 dists[d] = max (dists[d], fixed_space);
296 while (flip (&d) != LEFT);
299 r.distance_ = dists[LEFT] + dists[RIGHT];
300 r.item_drul_[LEFT] = dynamic_cast<Item *> (cols->elem (i - 1));
301 r.item_drul_[RIGHT] = dynamic_cast<Item *> (cols->elem (i + 1));
315 Set neighboring columns determined by the spacing-wishes grob property.
318 Spacing_spanner::set_explicit_neighbor_columns (Link_array<Grob> const &cols)
320 for (int i = 0; i < cols.size (); i++)
322 SCM right_neighbors = Grob_array::make_array ();
323 Grob_array *rn_arr = unsmob_grob_array (right_neighbors);
324 int min_rank = 100000; // inf.
326 extract_grob_set (cols[i], "spacing-wishes", wishes);
327 for (int k = wishes.size(); k--;)
329 Item *wish = dynamic_cast<Item *> ( wishes[k]);
331 Item *lc = wish->get_column ();
332 Grob *right = Note_spacing::right_column (wish);
337 Item *rc = dynamic_cast<Item *> (right);
339 int right_rank = Paper_column::get_rank (rc);
340 int left_rank = Paper_column::get_rank (lc);
343 update the left column.
345 if (right_rank <= min_rank)
347 if (right_rank < min_rank)
350 min_rank = right_rank;
355 update the right column of the wish.
359 extract_grob_set (rc, "left-neighbors", lns_arr);
362 Item *it = dynamic_cast<Item *> (lns_arr.top());
363 maxrank = Paper_column::get_rank (it->get_column ());
366 if (left_rank >= maxrank)
369 if (left_rank > maxrank)
371 Grob_array *ga = unsmob_grob_array (rc->get_object ("left-neighbors"));
376 Pointer_group_interface::add_grob (rc, ly_symbol2scm ("left-neighbors"), wish);
382 cols[i]->set_object ("right-neighbors", right_neighbors);
388 Set neighboring columns that have no left/right-neighbor set
389 yet. Only do breakable non-musical columns, and musical columns.
392 Spacing_spanner::set_implicit_neighbor_columns (Link_array<Grob> const &cols)
394 for (int i = 0; i < cols.size (); i++)
396 Item *it = dynamic_cast<Item *> (cols[i]);
397 if (!Item::is_breakable (it) && !Paper_column::is_musical (it))
400 // it->breakable || it->musical
403 sloppy with typing left/right-neighbors should take list, but paper-column found instead.
405 extract_grob_set (cols[i], "left-neighbors", lns);
406 if (lns.is_empty () && i )
408 SCM ga_scm = Grob_array::make_array();
409 Grob_array *ga = unsmob_grob_array (ga_scm);
411 cols[i]->set_object ("left-neighbors", ga_scm);
413 extract_grob_set (cols[i], "right-neighbors", rns);
414 if (rns.is_empty () && i < cols.size () - 1)
416 SCM ga_scm = Grob_array::make_array();
417 Grob_array *ga = unsmob_grob_array (ga_scm);
419 cols[i]->set_object ("right-neighbors", ga_scm);