2 staff-spacing.cc -- implement Staff_spacing
4 source file of the GNU LilyPond music typesetter
6 (c) 2001--2007 Han-Wen Nienhuys <hanwen@xs4all.nl>
9 #include "staff-spacing.hh"
14 #include "paper-column.hh"
15 #include "separation-item.hh"
17 #include "bar-line.hh"
18 #include "staff-symbol-referencer.hh"
19 #include "note-column.hh"
21 #include "accidental-placement.hh"
22 #include "pointer-group-interface.hh"
23 #include "directional-element-interface.hh"
26 Insert some more space for the next note, in case it has a stem in
30 Staff_spacing::next_note_correction (Grob *me,
33 Real current_space, Real current_fixed,
39 if (!g || !Note_column::has_interface (g))
42 Item *col = dynamic_cast<Item *> (g)->get_column ();
43 Real left_stickout_correction = max (0., (- g->extent (col, X_AXIS)[LEFT]));
45 /* staff space -> positions */
49 Duh. If this gets out of hand, we should invent something more generic.
51 Grob *accs = Note_column::accidentals (g);
55 if (Accidental_placement::has_interface (accs))
56 v = Accidental_placement::get_relevant_accidental_extent (accs, col, me);
58 v = accs->extent (col, X_AXIS);
60 left_stickout_correction = max (left_stickout_correction, (- v[LEFT]));
62 Grob *arpeggio = unsmob_grob (g->get_object ("arpeggio"));
64 left_stickout_correction = max (left_stickout_correction, - arpeggio->extent (col, X_AXIS)[LEFT]);
68 Let's decrease the space a little if the problem is not located
71 if (bar_size.is_empty ())
72 left_stickout_correction *= 0.75;
75 We want 0.3 ss before the sticking-out object.
77 current_fixed/2 is our guess at (right side of left object + 0.3)
79 left_stickout_correction += current_fixed/2 - current_space;
80 left_stickout_correction = max (left_stickout_correction, 0.0);
83 Real optical_corr = 0.0;
84 Grob *stem = Note_column::get_stem (g);
85 if (!bar_size.is_empty ()
90 Direction d = get_grob_direction (stem);
91 if (Stem::is_normal_stem (stem) && d == DOWN)
95 can't look at stem-end-position, since that triggers
96 beam slope computations.
98 Real stem_start = Stem::head_positions (stem) [d];
99 Real stem_end = stem_start +
100 d * robust_scm2double (stem->get_property ("length"), 7);
102 Interval stem_posns (min (stem_start, stem_end),
103 max (stem_end, stem_start));
105 stem_posns.intersect (bar_size);
107 optical_corr = min (abs (stem_posns.length () / 7.0), 1.0);
108 optical_corr *= robust_scm2double (me->get_property ("stem-spacing-correction"), 1);
113 Real correction = optical_corr + left_stickout_correction;
119 This minute adjustments don't make sense for widely spaced scores.
120 Hence, we need to keep the stretchable (that is, space - fix)
123 *space += correction;
128 Y-positions that are covered by BAR_GROB, in the case that it is a
131 Staff_spacing::bar_y_positions (Grob *bar_grob)
134 bar_size.set_empty ();
135 if (Bar_line::has_interface (bar_grob))
137 SCM glyph = bar_grob->get_property ("glyph-name");
138 Grob *staff_sym = Staff_symbol_referencer::get_staff_symbol (bar_grob);
140 string glyph_string = scm_is_string (glyph) ? ly_scm2string (glyph) : "";
141 if (glyph_string.substr (0, 1) == "|"
142 || glyph_string.substr (0, 1) == ".")
144 Grob *common = bar_grob->common_refpoint (staff_sym, Y_AXIS);
145 bar_size = bar_grob->extent (common, Y_AXIS);
146 bar_size *= 1.0 / Staff_symbol_referencer::staff_space (bar_grob);
153 Do corrections for the following notes.
155 This is slightly convoluted, since the staffspacing grob gets
156 pointers to the separation-items, not the note-columns or
161 Staff_spacing::next_notes_correction (Grob *me, Grob *last_grob,
162 Real current_space, Real current_fixed,
163 Real *compound_space, Real *compound_fixed
166 Interval bar_size = bar_y_positions (last_grob);
168 extract_grob_set (me, "right-items", right_items);
170 *compound_fixed = 0.0;
171 *compound_space = 0.0;
174 for (vsize i = right_items.size (); i--;)
176 Grob *g = right_items[i];
177 if (Note_column::has_interface (right_items[i]))
179 Grob *g = right_items[i];
184 next_note_correction (me, g, bar_size,
185 current_space, current_fixed,
186 &space, &fixed, &wish_count);
188 *compound_space += space;
189 *compound_fixed += fixed;
193 extract_grob_set (g, "elements", elts);
194 for (vsize j = elts.size (); j--;)
198 next_note_correction (me, elts[j], bar_size,
199 current_space, current_fixed,
202 *compound_fixed += fixed;
203 *compound_space += space;
210 *compound_space /= wish_count;
211 *compound_fixed /= wish_count;
216 Staff_spacing::get_spacing_params (Grob *me, Real *space, Real *fixed)
221 Grob *separation_item = 0;
222 Item *me_item = dynamic_cast<Item *> (me);
224 extract_grob_set (me, "left-items", items);
225 for (vsize i = items.size (); i--;)
227 Grob *cand = items[i];
228 if (cand && Separation_item::has_interface (cand))
229 separation_item = cand;
232 // printf ("doing col %d\n" , Paper_column::get_rank (left_col));
233 if (!separation_item)
235 programming_error ("no sep item");
240 Grob *last_grob = Separation_item::extremal_break_aligned_grob (separation_item, RIGHT,
247 Should insert a adjustable space here? For excercises, you might want to
248 use a staff without a clef in the beginning.
252 we used to have a warning here, but it generates a lot of
253 spurious error messages.
258 *fixed = last_ext[RIGHT];
259 *space = *fixed + 1.0;
261 SCM alist = last_grob->get_property ("space-alist");
262 if (!scm_list_p (alist))
265 SCM space_def = scm_sloppy_assq (ly_symbol2scm ("first-note"), alist);
266 if (me_item->break_status_dir () == CENTER)
268 SCM nndef = scm_sloppy_assq (ly_symbol2scm ("next-note"), alist);
269 if (scm_is_pair (nndef))
273 if (!scm_is_pair (space_def))
275 programming_error ("unknown prefatory spacing");
279 space_def = scm_cdr (space_def);
280 Real distance = scm_to_double (scm_cdr (space_def));
281 SCM type = scm_car (space_def);
283 *fixed = last_ext[RIGHT];
284 if (type == ly_symbol2scm ("fixed-space"))
289 else if (type == ly_symbol2scm ("extra-space"))
290 *space = *fixed + distance;
291 else if (type == ly_symbol2scm ("semi-fixed-space"))
293 *fixed += distance / 2;
294 *space = *fixed + distance / 2;
296 else if (type == ly_symbol2scm ("minimum-space"))
297 *space = last_ext[LEFT] + max (last_ext.length (), distance);
298 else if (type == ly_symbol2scm ("minimum-fixed-space"))
300 *space = last_ext[LEFT] + max (last_ext.length (), distance);
304 Real correction_fixed = 0.0;
305 Real correction_space = 0.0;
306 next_notes_correction (me, last_grob,
308 &correction_space, &correction_fixed );
309 *space += correction_space;
310 *fixed += correction_fixed;
313 ADD_INTERFACE (Staff_spacing,
314 "This object calculates spacing details from a "
315 " breakable symbol (left) to another object. For example, it takes care "
316 " of optical spacing from a bar lines to a note.",
319 "stem-spacing-correction "