2 staff-spacing.cc -- implement Staff_spacing
4 source file of the GNU LilyPond music typesetter
6 (c) 2001--2005 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,
38 if (!g || !Note_column::has_interface (g))
41 Item *col = dynamic_cast<Item *> (g)->get_column ();
42 Real max_corr = max (0., (- g->extent (col, X_AXIS)[LEFT]));
45 Duh. If this gets out of hand, we should invent something more generic.
47 if (Grob *a = Note_column::accidentals (g))
50 if (Accidental_placement::has_interface (a))
51 v = Accidental_placement::get_relevant_accidental_extent (a, col, me);
53 v = a->extent (col, X_AXIS);
55 max_corr = max (max_corr, (- v[LEFT]));
57 if (Grob *a = unsmob_grob (g->get_object ("arpeggio")))
58 max_corr = max (max_corr, - a->extent (col, X_AXIS)[LEFT]);
61 Let's decrease the space a little if the problem is not located
64 if (bar_size.is_empty ())
67 if (!bar_size.is_empty ())
68 if (Grob *stem = Note_column::get_stem (g))
70 Direction d = get_grob_direction (stem);
73 Real stem_start = Stem::head_positions (stem) [DOWN];
74 Real stem_end = Stem::stem_end_position (stem);
75 Interval stem_posns (min (stem_start, stem_end),
76 max (stem_end, stem_start));
78 stem_posns.intersect (bar_size);
80 Real corr = min (abs (stem_posns.length () / 7.0), 1.0);
81 corr *= robust_scm2double (me->get_property ("stem-spacing-correction"), 1);
85 max_corr = max (max_corr, corr);
93 Y-positions that are covered by BAR_GROB, in the case that it is a
96 Staff_spacing::bar_y_positions (Grob *bar_grob)
99 bar_size.set_empty ();
100 if (Bar_line::has_interface (bar_grob))
102 SCM glyph = bar_grob->get_property ("glyph-name");
103 Grob *staff_sym = Staff_symbol_referencer::get_staff_symbol (bar_grob);
105 String glyph_string = scm_is_string (glyph) ? ly_scm2string (glyph) : "";
106 if (glyph_string.left_string (1) == "|" || glyph_string.left_string (1) == ".")
108 Grob *common = bar_grob->common_refpoint (staff_sym, Y_AXIS);
109 Interval bar_size = bar_grob->extent (common, Y_AXIS);
110 bar_size *= 1.0 / Staff_symbol_referencer::staff_space (bar_grob);
117 Do corrections for the following notes.
119 This is slightly convoluted, since the staffspacing grob gets
120 pointers to the separation-items, not the note-columns or
124 Staff_spacing::next_notes_correction (Grob *me, Grob *last_grob,
125 Real *compound_space, Real *compound_fixed
128 Interval bar_size = bar_y_positions (last_grob);
130 extract_grob_set (me, "right-items", right_items);
132 *compound_fixed = 0.0;
133 *compound_space = 0.0;
136 for (int i = right_items.size (); i--;)
138 Grob *g = right_items[i];
143 next_note_correction (me, g, bar_size, &space, &fixed);
145 *compound_space += space;
146 *compound_fixed += fixed;
149 extract_grob_set (g, "elements", elts);
150 for (int j = elts.size (); j--;)
154 next_note_correction (me, elts[j], bar_size, &space, &fixed);
155 *compound_fixed += fixed;
156 *compound_space += space;
163 *compound_space /= wish_count;
164 *compound_fixed /= wish_count;
169 Staff_spacing::get_spacing_params (Grob *me, Real *space, Real *fixed)
174 Grob *separation_item = 0;
175 Item *me_item = dynamic_cast<Item *> (me);
177 extract_grob_set (me, "left-items", items);
178 for (int i = items.size (); i--;)
180 Grob *cand = items[i];
181 if (cand && Separation_item::has_interface (cand))
182 separation_item = cand;
185 // printf ("doing col %d\n" , Paper_column::get_rank (left_col));
186 if (!separation_item)
188 programming_error ("no sep item");
193 Grob *last_grob = Separation_item::extremal_break_aligned_grob (separation_item, RIGHT,
200 Should insert a adjustable space here? For excercises, you might want to
201 use a staff without a clef in the beginning.
205 we used to have a warning here, but it generates a lot of
206 spurious error messages.
211 *fixed = last_ext[RIGHT];
212 *space = *fixed + 1.0;
214 SCM alist = last_grob->get_property ("space-alist");
215 if (!scm_list_p (alist))
218 SCM space_def = scm_sloppy_assq (ly_symbol2scm ("first-note"), alist);
219 if (me_item->break_status_dir () == CENTER)
221 SCM nndef = scm_sloppy_assq (ly_symbol2scm ("next-note"), alist);
222 if (scm_is_pair (nndef))
226 if (!scm_is_pair (space_def))
228 programming_error ("unknown prefatory spacing");
232 space_def = scm_cdr (space_def);
233 Real distance = scm_to_double (scm_cdr (space_def));
234 SCM type = scm_car (space_def);
236 *fixed = last_ext[RIGHT];
237 if (type == ly_symbol2scm ("fixed-space"))
242 else if (type == ly_symbol2scm ("extra-space"))
243 *space = *fixed + distance;
244 else if (type == ly_symbol2scm ("semi-fixed-space"))
246 *fixed += distance / 2;
247 *space = *fixed + distance / 2;
249 else if (type == ly_symbol2scm ("minimum-space"))
250 *space = last_ext[LEFT] + max (last_ext.length (), distance);
251 else if (type == ly_symbol2scm ("minimum-fixed-space"))
253 *space = last_ext[LEFT] + max (last_ext.length (), distance);
257 Real correction_fixed, correction_space;
258 next_notes_correction (me, last_grob, &correction_space, &correction_fixed );
259 *space += correction_space;
260 *fixed += correction_fixed;
263 ADD_INTERFACE (Staff_spacing, "staff-spacing-interface",
264 "This object calculates spacing details from a "
265 " breakable symbol (left) to another object. For example, it takes care "
266 " of optical spacing from a bar lines to a note.",
269 "stem-spacing-correction "