2 staff-spacing.cc -- implement Staff_spacing
4 source file of the GNU LilyPond music typesetter
6 (c) 2001--2006 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]));
46 Duh. If this gets out of hand, we should invent something more generic.
48 Grob *accs = Note_column::accidentals (g);
52 if (Accidental_placement::has_interface (accs))
53 v = Accidental_placement::get_relevant_accidental_extent (accs, col, me);
55 v = accs->extent (col, X_AXIS);
57 left_stickout_correction = max (left_stickout_correction, (- v[LEFT]));
59 Grob *arpeggio = unsmob_grob (g->get_object ("arpeggio"));
61 left_stickout_correction = max (left_stickout_correction, - arpeggio->extent (col, X_AXIS)[LEFT]);
65 Let's decrease the space a little if the problem is not located
68 if (bar_size.is_empty ())
69 left_stickout_correction *= 0.75;
72 We want 0.3 ss before the sticking-out object.
74 current_fixed/2 is our guess at (right side of left object + 0.3)
76 left_stickout_correction += current_fixed/2 - current_space;
77 left_stickout_correction = max (left_stickout_correction, 0.0);
80 Real optical_corr = 0.0;
81 Grob *stem = Note_column::get_stem (g);
82 if (!bar_size.is_empty ()
87 Direction d = get_grob_direction (stem);
88 if (Stem::is_normal_stem (stem) && d == DOWN)
92 can't look at stem-end-position, since that triggers
93 beam slope computations.
95 Real stem_start = Stem::head_positions (stem) [d];
96 Real stem_end = stem_start +
97 d * robust_scm2double (stem->get_property ("length"), 7);
99 Interval stem_posns (min (stem_start, stem_end),
100 max (stem_end, stem_start));
102 stem_posns.intersect (bar_size);
104 optical_corr = min (abs (stem_posns.length () / 7.0), 1.0);
105 optical_corr *= robust_scm2double (me->get_property ("stem-spacing-correction"), 1);
110 Real correction = optical_corr + left_stickout_correction;
116 This minute adjustments don't make sense for widely spaced scores.
117 Hence, we need to keep the stretchable (that is, space - fix)
120 *space += correction;
125 Y-positions that are covered by BAR_GROB, in the case that it is a
128 Staff_spacing::bar_y_positions (Grob *bar_grob)
131 bar_size.set_empty ();
132 if (Bar_line::has_interface (bar_grob))
134 SCM glyph = bar_grob->get_property ("glyph-name");
135 Grob *staff_sym = Staff_symbol_referencer::get_staff_symbol (bar_grob);
137 string glyph_string = scm_is_string (glyph) ? ly_scm2string (glyph) : "";
138 if (glyph_string.substr (0, 1) == "|"
139 || glyph_string.substr (0, 1) == ".")
141 Grob *common = bar_grob->common_refpoint (staff_sym, Y_AXIS);
142 bar_size = bar_grob->extent (common, Y_AXIS);
143 bar_size *= 1.0 / Staff_symbol_referencer::staff_space (bar_grob);
150 Do corrections for the following notes.
152 This is slightly convoluted, since the staffspacing grob gets
153 pointers to the separation-items, not the note-columns or
158 Staff_spacing::next_notes_correction (Grob *me, Grob *last_grob,
159 Real current_space, Real current_fixed,
160 Real *compound_space, Real *compound_fixed
163 Interval bar_size = bar_y_positions (last_grob);
165 extract_grob_set (me, "right-items", right_items);
167 *compound_fixed = 0.0;
168 *compound_space = 0.0;
171 for (vsize i = right_items.size (); i--;)
173 Grob *g = right_items[i];
174 if (Note_column::has_interface (right_items[i]))
176 Grob *g = right_items[i];
181 next_note_correction (me, g, bar_size,
182 current_space, current_fixed,
183 &space, &fixed, &wish_count);
185 *compound_space += space;
186 *compound_fixed += fixed;
190 extract_grob_set (g, "elements", elts);
191 for (vsize j = elts.size (); j--;)
195 next_note_correction (me, elts[j], bar_size,
196 current_space, current_fixed,
199 *compound_fixed += fixed;
200 *compound_space += space;
207 *compound_space /= wish_count;
208 *compound_fixed /= wish_count;
213 Staff_spacing::get_spacing_params (Grob *me, Real *space, Real *fixed)
218 Grob *separation_item = 0;
219 Item *me_item = dynamic_cast<Item *> (me);
221 extract_grob_set (me, "left-items", items);
222 for (vsize i = items.size (); i--;)
224 Grob *cand = items[i];
225 if (cand && Separation_item::has_interface (cand))
226 separation_item = cand;
229 // printf ("doing col %d\n" , Paper_column::get_rank (left_col));
230 if (!separation_item)
232 programming_error ("no sep item");
237 Grob *last_grob = Separation_item::extremal_break_aligned_grob (separation_item, RIGHT,
244 Should insert a adjustable space here? For excercises, you might want to
245 use a staff without a clef in the beginning.
249 we used to have a warning here, but it generates a lot of
250 spurious error messages.
255 *fixed = last_ext[RIGHT];
256 *space = *fixed + 1.0;
258 SCM alist = last_grob->get_property ("space-alist");
259 if (!scm_list_p (alist))
262 SCM space_def = scm_sloppy_assq (ly_symbol2scm ("first-note"), alist);
263 if (me_item->break_status_dir () == CENTER)
265 SCM nndef = scm_sloppy_assq (ly_symbol2scm ("next-note"), alist);
266 if (scm_is_pair (nndef))
270 if (!scm_is_pair (space_def))
272 programming_error ("unknown prefatory spacing");
276 space_def = scm_cdr (space_def);
277 Real distance = scm_to_double (scm_cdr (space_def));
278 SCM type = scm_car (space_def);
280 *fixed = last_ext[RIGHT];
281 if (type == ly_symbol2scm ("fixed-space"))
286 else if (type == ly_symbol2scm ("extra-space"))
287 *space = *fixed + distance;
288 else if (type == ly_symbol2scm ("semi-fixed-space"))
290 *fixed += distance / 2;
291 *space = *fixed + distance / 2;
293 else if (type == ly_symbol2scm ("minimum-space"))
294 *space = last_ext[LEFT] + max (last_ext.length (), distance);
295 else if (type == ly_symbol2scm ("minimum-fixed-space"))
297 *space = last_ext[LEFT] + max (last_ext.length (), distance);
301 Real correction_fixed = 0.0;
302 Real correction_space = 0.0;
303 next_notes_correction (me, last_grob,
305 &correction_space, &correction_fixed );
306 *space += correction_space;
307 *fixed += correction_fixed;
310 ADD_INTERFACE (Staff_spacing,
311 "This object calculates spacing details from a "
312 " breakable symbol (left) to another object. For example, it takes care "
313 " of optical spacing from a bar lines to a note.",
316 "stem-spacing-correction "