2 staff-spacing.cc -- implement Staff_spacing
4 source file of the GNU LilyPond music typesetter
6 (c) 2001--2005 Han-Wen Nienhuys <hanwen@cs.uu.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"
25 Insert some more space for the next note, in case it has a stem in
29 Staff_spacing::next_note_correction (Grob *me,
37 if (!g || !Note_column::has_interface (g))
40 Item *col = dynamic_cast<Item *> (g)->get_column ();
41 Real max_corr = max (0., (- g->extent (col, X_AXIS)[LEFT]));
44 Duh. If this gets out of hand, we should invent something more generic.
46 if (Grob *a = Note_column::accidentals (g))
49 if (Accidental_placement::has_interface (a))
50 v = Accidental_placement::get_relevant_accidental_extent (a, col, me);
52 v = a->extent (col, X_AXIS);
54 max_corr = max (max_corr, (- v[LEFT]));
56 if (Grob *a = unsmob_grob (g->get_object ("arpeggio")))
57 max_corr = max (max_corr, - a->extent (col, X_AXIS)[LEFT]);
60 Let's decrease the space a little if the problem is not located
63 if (bar_size.is_empty ())
66 if (!bar_size.is_empty ())
67 if (Grob *stem = Note_column::get_stem (g))
69 Direction d = Stem::get_direction (stem);
72 Real stem_start = Stem::head_positions (stem) [DOWN];
73 Real stem_end = Stem::stem_end_position (stem);
74 Interval stem_posns (min (stem_start, stem_end),
75 max (stem_end, stem_start));
77 stem_posns.intersect (bar_size);
79 Real corr = min (abs (stem_posns.length () / 7.0), 1.0);
80 corr *= robust_scm2double (me->get_property ("stem-spacing-correction"), 1);
84 max_corr = max (max_corr, corr);
92 Y-positions that are covered by BAR_GROB, in the case that it is a
95 Staff_spacing::bar_y_positions (Grob *bar_grob)
98 bar_size.set_empty ();
99 if (Bar_line::has_interface (bar_grob))
101 SCM glyph = bar_grob->get_property ("glyph-name");
102 Grob *staff_sym = Staff_symbol_referencer::get_staff_symbol (bar_grob);
104 String glyph_string = scm_is_string (glyph) ? ly_scm2string (glyph) : "";
105 if (glyph_string.left_string (1) == "|" || glyph_string.left_string (1) == ".")
107 Grob *common = bar_grob->common_refpoint (staff_sym, Y_AXIS);
108 Interval bar_size = bar_grob->extent (common, Y_AXIS);
109 bar_size *= 1.0 / Staff_symbol_referencer::staff_space (bar_grob);
116 Do corrections for the following notes.
118 This is slightly convoluted, since the staffspacing grob gets
119 pointers to the separation-items, not the note-columns or
123 Staff_spacing::next_notes_correction (Grob *me, Grob *last_grob,
124 Real *compound_space, Real *compound_fixed
127 Interval bar_size = bar_y_positions (last_grob);
129 extract_grob_set (me, "right-items", right_items);
131 *compound_fixed = 0.0;
132 *compound_space = 0.0;
135 for (int i = right_items.size (); i--;)
137 Grob *g = right_items[i];
142 next_note_correction (me, g, bar_size, &space, &fixed);
144 *compound_space += space;
145 *compound_fixed += fixed;
148 extract_grob_set (g, "elements", elts);
149 for (int j = elts.size (); j--;)
153 next_note_correction (me, elts[j], bar_size, &space, &fixed);
154 *compound_fixed += fixed;
155 *compound_space += space;
162 *compound_space /= wishes;
163 *compound_fixed /= wishes;
168 Staff_spacing::get_spacing_params (Grob *me, Real *space, Real *fixed)
173 Grob *separation_item = 0;
174 Item *me_item = dynamic_cast<Item *> (me);
176 extract_grob_set (me, "left-items", items);
177 for (int i = items.size (); i--;)
179 Grob *cand = items[i];
180 if (cand && Separation_item::has_interface (cand))
181 separation_item = cand;
184 // printf ("doing col %d\n" , Paper_column::get_rank (left_col));
185 if (!separation_item)
187 programming_error ("no sep item");
192 Grob *last_grob = Separation_item::extremal_break_aligned_grob (separation_item, RIGHT,
199 Should insert a adjustable space here? For excercises, you might want to
200 use a staff without a clef in the beginning.
204 we used to have a warning here, but it generates a lot of
205 spurious error messages.
210 *fixed = last_ext[RIGHT];
211 *space = *fixed + 1.0;
213 SCM alist = last_grob->get_property ("space-alist");
214 if (!scm_list_p (alist))
217 SCM space_def = scm_sloppy_assq (ly_symbol2scm ("first-note"), alist);
218 if (me_item->break_status_dir () == CENTER)
220 SCM nndef = scm_sloppy_assq (ly_symbol2scm ("next-note"), alist);
221 if (scm_is_pair (nndef))
225 if (!scm_is_pair (space_def))
227 programming_error ("unknown prefatory spacing");
231 space_def = scm_cdr (space_def);
232 Real distance = scm_to_double (scm_cdr (space_def));
233 SCM type = scm_car (space_def);
235 *fixed = last_ext[RIGHT];
236 if (type == ly_symbol2scm ("fixed-space"))
241 else if (type == ly_symbol2scm ("extra-space"))
242 *space = *fixed + distance;
243 else if (type == ly_symbol2scm ("semi-fixed-space"))
245 *fixed += distance / 2;
246 *space = *fixed + distance / 2;
248 else if (type == ly_symbol2scm ("minimum-space"))
249 *space = last_ext[LEFT] + max (last_ext.length (), distance);
250 else if (type == ly_symbol2scm ("minimum-fixed-space"))
252 *space = last_ext[LEFT] + max (last_ext.length (), distance);
256 Real correction_fixed, correction_space;
257 next_notes_correction (me, last_grob, &correction_space, &correction_fixed );
258 *space += correction_space;
259 *fixed += correction_fixed;
262 ADD_INTERFACE (Staff_spacing, "staff-spacing-interface",
263 "This object calculates spacing details from a "
264 " breakable symbol (left) to another object. For example, it takes care "
265 " of optical spacing from a bar lines to a note.",
266 "stem-spacing-correction left-items right-items");