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"
13 #include "paper-column.hh"
14 #include "separation-item.hh"
16 #include "bar-line.hh"
17 #include "staff-symbol-referencer.hh"
18 #include "note-column.hh"
20 #include "accidental-placement.hh"
21 #include "pointer-group-interface.hh"
24 Insert some more space for the next note, in case it has a stem in
28 Staff_spacing::next_note_correction (Grob *me,
32 if (!g || !Note_column::has_interface (g))
35 Item *col = dynamic_cast<Item *> (g)->get_column ();
36 Real max_corr = max (0., (- g->extent (col, X_AXIS)[LEFT]));
39 Duh. If this gets out of hand, we should invent something more generic.
41 if (Grob *a = Note_column::accidentals (g))
44 if (Accidental_placement::has_interface (a))
45 v = Accidental_placement::get_relevant_accidental_extent (a, col, me);
47 v = a->extent (col, X_AXIS);
49 max_corr = max (max_corr, (- v[LEFT]));
51 if (Grob *a = unsmob_grob (g->get_object ("arpeggio")))
52 max_corr = max (max_corr, - a->extent (col, X_AXIS)[LEFT]);
55 Let's decrease the space a little if the problem is not located
58 if (bar_size.is_empty ())
61 if (!bar_size.is_empty ())
62 if (Grob *stem = Note_column::get_stem (g))
64 Direction d = Stem::get_direction (stem);
67 Real stem_start = Stem::head_positions (stem) [DOWN];
68 Real stem_end = Stem::stem_end_position (stem);
69 Interval stem_posns (min (stem_start, stem_end),
70 max (stem_end, stem_start));
72 stem_posns.intersect (bar_size);
74 Real corr = min (abs (stem_posns.length () / 7.0), 1.0);
76 *= robust_scm2double (me->get_property ("stem-spacing-correction"), 1);
80 max_corr = max (max_corr, corr);
87 Y-positions that are covered by BAR_GROB, in the case that it is a
90 Staff_spacing::bar_y_positions (Grob *bar_grob)
93 bar_size.set_empty ();
94 if (Bar_line::has_interface (bar_grob))
96 SCM glyph = bar_grob->get_property ("glyph");
98 String glyph_string = scm_is_string (glyph) ? ly_scm2string (glyph) : "";
99 if (glyph_string.left_string (1) == "|" || glyph_string.left_string (1) == ".")
101 SCM sz = Bar_line::get_staff_bar_size (bar_grob->self_scm ());
102 bar_size = Interval (-1, 1);
103 bar_size *= robust_scm2double (sz, 1)
104 / Staff_symbol_referencer::staff_space (bar_grob);
111 Do corrections for the following notes.
113 This is slightly convoluted, since the staffspacing grob gets
114 pointers to the separation-items, not the note-columns or
118 Staff_spacing::next_notes_correction (Grob *me, Grob *last_grob)
120 Interval bar_size = bar_y_positions (last_grob);
123 extract_grob_set (me, "right-items", right_items);
124 for (int i = right_items.size (); i--;)
126 Grob *g = right_items[i];
128 max_corr = max (max_corr, next_note_correction (me, g, bar_size));
129 extract_grob_set (g, "elements", elts);
130 for (int j = elts.size (); j--;)
131 max_corr = max (max_corr, next_note_correction (me, elts[j], bar_size));
138 Staff_spacing::get_spacing_params (Grob *me, Real *space, Real *fixed)
143 Grob *separation_item = 0;
144 Item *me_item = dynamic_cast<Item *> (me);
146 extract_grob_set (me, "left-items", items);
147 for (int i = items.size (); i--;)
149 Grob *cand = items[i];
150 if (cand && Separation_item::has_interface (cand))
151 separation_item = cand;
154 // printf ("doing col %d\n" , Paper_column::get_rank (left_col));
156 if (!separation_item)
158 programming_error ("no sep item");
163 Grob *last_grob = Separation_item::extremal_break_aligned_grob (separation_item, RIGHT,
170 Should insert a adjustable space here? For excercises, you might want to
171 use a staff without a clef in the beginning.
175 we used to have a warning here, but it generates a lot of
176 spurious error messages.
181 *fixed = last_ext[RIGHT];
182 *space = *fixed + 1.0;
184 SCM alist = last_grob->get_property ("space-alist");
185 if (!scm_list_p (alist))
188 SCM space_def = scm_sloppy_assq (ly_symbol2scm ("first-note"), alist);
189 if (me_item->break_status_dir () == CENTER)
191 SCM nndef = scm_sloppy_assq (ly_symbol2scm ("next-note"), alist);
192 if (scm_is_pair (nndef))
196 if (!scm_is_pair (space_def))
198 programming_error ("unknown prefatory spacing");
202 space_def = scm_cdr (space_def);
203 Real distance = scm_to_double (scm_cdr (space_def));
204 SCM type = scm_car (space_def);
206 *fixed = last_ext[RIGHT];
207 if (type == ly_symbol2scm ("fixed-space"))
212 else if (type == ly_symbol2scm ("extra-space"))
213 *space = *fixed + distance;
214 else if (type == ly_symbol2scm ("semi-fixed-space"))
216 *fixed += distance / 2;
217 *space = *fixed + distance / 2;
219 else if (type == ly_symbol2scm ("minimum-space"))
220 *space = last_ext[LEFT] + max (last_ext.length (), distance);
221 else if (type == ly_symbol2scm ("minimum-fixed-space"))
223 *space = last_ext[LEFT] + max (last_ext.length (), distance);
227 *space += next_notes_correction (me, last_grob);
230 ADD_INTERFACE (Staff_spacing, "staff-spacing-interface",
231 "This object calculates spacing details from a "
232 " breakable symbol (left) to another object. For example, it takes care "
233 " of optical spacing from a bar lines to a note.",
234 "stem-spacing-correction left-items right-items");