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--2009 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-interface.hh"
18 #include "spacing-spanner.hh"
19 #include "note-spacing.hh"
21 #include "grob-array.hh"
22 #include "break-align-interface.hh"
26 Return whether COL is fixed to its neighbors by some kind of spacing
30 If in doubt, then we're not loose; the spacing engine should space
31 for it, risking suboptimal spacing.
33 (Otherwise, we might risk core dumps, and other weird stuff.)
36 is_loose_column (Grob
*l
, Grob
*col
, Grob
*r
, Spacing_options
const *options
)
38 if (!to_boolean (col
->get_property ("allow-loose-spacing")))
42 if ((options
->float_nonmusical_columns_
43 || options
->float_grace_columns_
)
44 && Paper_column::when_mom (col
).grace_part_
)
50 if (Paper_column::is_musical (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 Item
*r_neighbor
= unsmob_item (col
->get_object ("right-neighbor"));
76 Item
*l_neighbor
= unsmob_item (col
->get_object ("left-neighbor"));
78 if (!l_neighbor
|| !r_neighbor
)
81 /* If a non-empty column (ie. not \bar "") is placed nicely in series with
82 its neighbor (ie. no funny polyphonic stuff), don't make it loose.
84 if (l
== l_neighbor
&& r
== r_neighbor
&& col
->extent (col
, X_AXIS
).length () > 0)
88 Only declare loose if the bounds make a little sense. This means
89 some cases (two isolated, consecutive clef changes) won't be
90 nicely folded, but hey, then don't do that.
92 if (! ((Paper_column::is_musical (l_neighbor
) || Paper_column::is_breakable (l_neighbor
))
93 && (Paper_column::is_musical (r_neighbor
) || Paper_column::is_breakable (r_neighbor
))))
97 in any case, we don't want to move bar lines.
99 extract_grob_set (col
, "elements", elts
);
100 for (vsize i
= elts
.size (); i
--;)
103 if (g
&& Break_alignment_interface::has_interface (g
))
105 extract_grob_set (g
, "elements", gelts
);
106 for (vsize j
= gelts
.size (); j
--;)
110 if (h
&& h
->get_property ("break-align-symbol") == ly_symbol2scm ("staff-bar"))
112 extract_grob_set (h
, "elements", helts
);
113 for (vsize k
= helts
.size (); k
--;)
114 if ("" != robust_scm2string (helts
[k
]->get_property ("glyph-name"), ""))
125 Spacing_spanner::set_distances_for_loose_col (Grob
*me
, Grob
*c
,
126 Drul_array
<Item
*> next_door
,
127 Spacing_options
const *options
)
130 Drul_array
<Real
> dists (0, 0);
134 Item
*lc
= dynamic_cast<Item
*> ((d
== LEFT
) ? next_door
[LEFT
] : c
);
135 Item
*rc
= dynamic_cast<Item
*> (d
== LEFT
? c
: next_door
[RIGHT
]);
137 extract_grob_set (lc
, "spacing-wishes", wishes
);
138 for (vsize k
= wishes
.size (); k
--;)
140 Grob
*sp
= wishes
[k
];
141 if (Spacing_interface::left_column (sp
) != lc
142 || Spacing_interface::right_column (sp
) != rc
)
145 if (Note_spacing::has_interface (sp
))
148 The note spacing should be taken from the musical
151 Real base
= note_spacing (me
, lc
, rc
, options
);
152 Spring spring
= Note_spacing::get_spacing (sp
, rc
, base
, options
->increment_
);
154 dists
[d
] = max (dists
[d
], spring
.min_distance ());
156 else if (Staff_spacing::has_interface (sp
))
158 Spring spring
= Staff_spacing::get_spacing (sp
, rc
);
160 dists
[d
] = max (dists
[d
], spring
.min_distance ());
163 programming_error ("Subversive spacing wish");
166 while (flip (&d
) != LEFT
);
169 r
.distance_
= dists
[LEFT
] + dists
[RIGHT
];
170 r
.item_drul_
= next_door
;
177 Remove columns that are not tightly fitting from COLS. In the
178 removed columns, set 'between-cols to the columns where it is in
182 Spacing_spanner::prune_loose_columns (Grob
*me
,
184 Spacing_options
*options
)
186 vector
<Grob
*> newcols
;
188 for (vsize i
= 0; i
< cols
->size (); i
++)
190 Grob
*c
= cols
->at (i
);
192 bool loose
= (i
> 0 && i
+ 1 < cols
->size ())
193 && is_loose_column (cols
->at (i
- 1), c
, cols
->at (i
+ 1), options
);
195 /* Breakable columns never get pruned; even if they are loose,
196 their broken pieces are not. However, we mark them so that
197 the spacing can take their mid-line looseness into account. */
198 if (loose
&& Paper_column::is_breakable (c
))
201 c
->set_property ("maybe-loose", SCM_BOOL_T
);
206 Grob
*right_neighbor
= unsmob_grob (c
->get_object ("right-neighbor"));
207 Grob
*left_neighbor
= unsmob_grob (c
->get_object ("left-neighbor"));
210 Either object can be non existent, if the score ends
213 if (!right_neighbor
|| !left_neighbor
)
215 c
->programming_error ("Cannot determine neighbors for floating column. ");
216 c
->set_object ("between-cols", scm_cons (cols
->at (i
-1)->self_scm (),
217 cols
->at (i
+1)->self_scm ()));
221 c
->set_object ("between-cols", scm_cons (left_neighbor
->self_scm (),
222 right_neighbor
->self_scm ()));
226 Set distance constraints for loose columns
228 Drul_array
<Item
*> next_door (dynamic_cast<Item
*> (cols
->at (i
- 1)),
229 dynamic_cast<Item
*> (cols
->at (i
+ 1)));
231 set_distances_for_loose_col (me
, c
, next_door
, options
);
236 newcols
.push_back (c
);
243 Set neighboring columns determined by the spacing-wishes grob property.
246 Spacing_spanner::set_explicit_neighbor_columns (vector
<Grob
*> const &cols
)
248 for (vsize i
= 0; i
< cols
.size (); i
++)
250 extract_grob_set (cols
[i
], "spacing-wishes", wishes
);
251 for (vsize j
= wishes
.size (); j
--;)
253 Item
*wish
= dynamic_cast<Item
*> (wishes
[j
]);
254 Item
*left_col
= wish
->get_column ();
255 int left_rank
= Paper_column::get_rank (left_col
);
256 int min_right_rank
= INT_MAX
;
258 extract_grob_set (wish
, "right-items", right_items
);
259 for (vsize k
= right_items
.size (); k
--;)
261 Item
*right_col
= dynamic_cast<Item
*> (right_items
[k
])->get_column ();
262 int right_rank
= Paper_column::get_rank (right_col
);
264 if (right_rank
< min_right_rank
)
266 left_col
->set_object ("right-neighbor", right_col
->self_scm ());
267 min_right_rank
= right_rank
;
270 Grob
*old_left_neighbor
= unsmob_grob (right_col
->get_object ("left-neighbor"));
271 if (!old_left_neighbor
|| left_rank
> Paper_column::get_rank (old_left_neighbor
))
272 right_col
->set_object ("left-neighbor", left_col
->self_scm ());
279 Set neighboring columns that have no left/right-neighbor set
280 yet. Only do breakable non-musical columns, and musical columns.
281 Why only these? --jneem
284 Spacing_spanner::set_implicit_neighbor_columns (vector
<Grob
*> const &cols
)
286 for (vsize i
= 0; i
< cols
.size (); i
++)
288 Item
*it
= dynamic_cast<Item
*> (cols
[i
]);
289 if (!Paper_column::is_breakable (it
) && !Paper_column::is_musical (it
))
292 if (i
&& !unsmob_grob (cols
[i
]->get_object ("left-neighbor")))
293 cols
[i
]->set_object ("left-neighbor", cols
[i
-1]->self_scm ());
294 if (i
+ 1 < cols
.size () && !unsmob_grob (cols
[i
]->get_object ("right-neighbor")))
295 cols
[i
]->set_object ("right-neighbor", cols
[i
+1]->self_scm ());