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;
74 Real optical_corr = 0.0;
75 Grob *stem = Note_column::get_stem (g);
76 if (!bar_size.is_empty ()
81 Direction d = get_grob_direction (stem);
82 if (Stem::is_normal_stem (stem) && d == DOWN)
86 can't look at stem-end-position, since that triggers
87 beam slope computations.
89 Real stem_start = Stem::head_positions (stem) [d];
90 Real stem_end = stem_start +
91 d * robust_scm2double (stem->get_property ("length"), 7);
93 Interval stem_posns (min (stem_start, stem_end),
94 max (stem_end, stem_start));
96 stem_posns.intersect (bar_size);
98 optical_corr = min (abs (stem_posns.length () / 7.0), 1.0);
99 optical_corr *= robust_scm2double (me->get_property ("stem-spacing-correction"), 1);
104 Real correction = optical_corr + left_stickout_correction;
110 This minute adjustments don't make sense for widely spaced scores.
111 Hence, we need to keep the stretchable (that is, space - fix)
114 *space += correction;
119 Y-positions that are covered by BAR_GROB, in the case that it is a
122 Staff_spacing::bar_y_positions (Grob *bar_grob)
125 bar_size.set_empty ();
126 if (Bar_line::has_interface (bar_grob))
128 SCM glyph = bar_grob->get_property ("glyph-name");
129 Grob *staff_sym = Staff_symbol_referencer::get_staff_symbol (bar_grob);
131 string glyph_string = scm_is_string (glyph) ? ly_scm2string (glyph) : "";
132 if (glyph_string.substr (0, 1) == "|"
133 || glyph_string.substr (0, 1) == ".")
135 Grob *common = bar_grob->common_refpoint (staff_sym, Y_AXIS);
136 bar_size = bar_grob->extent (common, Y_AXIS);
137 bar_size *= 1.0 / Staff_symbol_referencer::staff_space (bar_grob);
144 Do corrections for the following notes.
146 This is slightly convoluted, since the staffspacing grob gets
147 pointers to the separation-items, not the note-columns or
152 Staff_spacing::next_notes_correction (Grob *me, Grob *last_grob,
153 Real current_space, Real current_fixed,
154 Real *compound_space, Real *compound_fixed
157 Interval bar_size = bar_y_positions (last_grob);
158 Grob *orig = me->original () ? me->original () : me;
160 extract_grob_set (orig, "right-items", right_items);
162 *compound_fixed = 0.0;
163 *compound_space = 0.0;
166 for (vsize i = right_items.size (); i--;)
168 Grob *g = right_items[i];
169 if (Note_column::has_interface (right_items[i]))
171 Grob *g = right_items[i];
176 next_note_correction (me, g, bar_size,
177 current_space, current_fixed,
178 &space, &fixed, &wish_count);
180 *compound_space += space;
181 *compound_fixed += fixed;
185 extract_grob_set (g, "elements", elts);
186 for (vsize j = elts.size (); j--;)
190 next_note_correction (me, elts[j], bar_size,
191 current_space, current_fixed,
194 *compound_fixed += fixed;
195 *compound_space += space;
202 *compound_space /= wish_count;
203 *compound_fixed /= wish_count;
208 Staff_spacing::get_spacing_params (Grob *me, Real *space, Real *fixed)
213 Grob *separation_item = 0;
214 Item *me_item = dynamic_cast<Item *> (me);
216 extract_grob_set (me, "left-items", items);
217 for (vsize i = items.size (); i--;)
219 Grob *cand = items[i];
220 if (cand && Separation_item::has_interface (cand))
221 separation_item = cand;
224 // printf ("doing col %d\n" , Paper_column::get_rank (left_col));
225 if (!separation_item)
227 programming_error ("no sep item");
232 Grob *last_grob = Separation_item::extremal_break_aligned_grob (separation_item, RIGHT,
239 Should insert a adjustable space here? For excercises, you might want to
240 use a staff without a clef in the beginning.
244 we used to have a warning here, but it generates a lot of
245 spurious error messages.
250 *fixed = last_ext[RIGHT];
251 *space = *fixed + 1.0;
253 SCM alist = last_grob->get_property ("space-alist");
254 if (!scm_list_p (alist))
257 SCM space_def = scm_sloppy_assq (ly_symbol2scm ("first-note"), alist);
258 if (me_item->break_status_dir () == CENTER)
260 SCM nndef = scm_sloppy_assq (ly_symbol2scm ("next-note"), alist);
261 if (scm_is_pair (nndef))
265 if (!scm_is_pair (space_def))
267 programming_error ("unknown prefatory spacing");
271 space_def = scm_cdr (space_def);
272 Real distance = scm_to_double (scm_cdr (space_def));
273 SCM type = scm_car (space_def);
275 *fixed = last_ext[RIGHT];
276 if (type == ly_symbol2scm ("fixed-space"))
281 else if (type == ly_symbol2scm ("extra-space"))
282 *space = *fixed + distance;
283 else if (type == ly_symbol2scm ("semi-fixed-space"))
285 *fixed += distance / 2;
286 *space = *fixed + distance / 2;
288 else if (type == ly_symbol2scm ("minimum-space"))
289 *space = last_ext[LEFT] + max (last_ext.length (), distance);
290 else if (type == ly_symbol2scm ("minimum-fixed-space"))
292 *space = last_ext[LEFT] + max (last_ext.length (), distance);
296 Real correction_fixed = 0.0;
297 Real correction_space = 0.0;
298 next_notes_correction (me, last_grob,
300 &correction_space, &correction_fixed );
301 *space += correction_space;
302 *fixed += correction_fixed;
305 ADD_INTERFACE (Staff_spacing,
306 "This object calculates spacing details from a "
307 " breakable symbol (left) to another object. For example, it takes care "
308 " of optical spacing from a bar lines to a note.",
311 "stem-spacing-correction "