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))
46 v = Accidental_placement::get_relevant_accidental_extent (a, col, me);
49 v = a->extent (col, X_AXIS);
51 max_corr = max (max_corr, (- v[LEFT]));
53 if (Grob *a = unsmob_grob (g->get_object ("arpeggio")))
55 max_corr = max (max_corr, - a->extent (col, X_AXIS)[LEFT]);
59 Let's decrease the space a little if the problem is not located
62 if (bar_size.is_empty ())
65 if (!bar_size.is_empty ())
66 if (Grob *stem = Note_column::get_stem (g))
68 Direction d = Stem::get_direction (stem);
71 Real stem_start = Stem::head_positions (stem) [DOWN];
72 Real stem_end = Stem::stem_end_position (stem);
73 Interval stem_posns (min (stem_start, stem_end),
74 max (stem_end, stem_start));
76 stem_posns.intersect (bar_size);
78 Real corr = min (abs (stem_posns.length () / 7.0), 1.0);
80 *= robust_scm2double (me->get_property ("stem-spacing-correction"), 1);
84 max_corr = max (max_corr, corr);
91 Y-positions that are covered by BAR_GROB, in the case that it is a
94 Staff_spacing::bar_y_positions (Grob *bar_grob)
97 bar_size.set_empty ();
98 if (Bar_line::has_interface (bar_grob))
100 SCM glyph = bar_grob->get_property ("glyph");
102 String glyph_string = scm_is_string (glyph) ? ly_scm2string (glyph) : "";
103 if (glyph_string.left_string (1) == "|" || glyph_string.left_string (1) == ".")
105 SCM sz = Bar_line::get_staff_bar_size (bar_grob->self_scm ());
106 bar_size = Interval (-1, 1);
107 bar_size *= robust_scm2double (sz, 1)
108 / Staff_symbol_referencer::staff_space (bar_grob);
115 Do corrections for the following notes.
117 This is slightly convoluted, since the staffspacing grob gets
118 pointers to the separation-items, not the note-columns or
122 Staff_spacing::next_notes_correction (Grob *me, Grob *last_grob)
124 Interval bar_size = bar_y_positions (last_grob);
128 extract_grob_set (me, "right-items", right_items);
129 for (int i = right_items.size (); i--;)
131 Grob *g = right_items[i];
133 max_corr = max (max_corr, next_note_correction (me, g, bar_size));
134 extract_grob_set (g, "elements", elts);
135 for (int j = elts.size(); j--;)
136 max_corr = max (max_corr, next_note_correction (me, elts[j], bar_size));
143 Staff_spacing::get_spacing_params (Grob *me, Real *space, Real *fixed)
148 Grob *separation_item = 0;
149 Item *me_item = dynamic_cast<Item *> (me);
151 extract_grob_set (me, "left-items", items);
152 for (int i = items.size(); i--;)
154 Grob *cand = items[i];
155 if (cand && Separation_item::has_interface (cand))
156 separation_item = cand;
159 // printf ("doing col %d\n" , Paper_column::get_rank (left_col));
161 if (!separation_item)
163 programming_error ("no sep item");
168 Grob *last_grob = Separation_item::extremal_break_aligned_grob (separation_item, RIGHT,
175 Should insert a adjustable space here? For excercises, you might want to
176 use a staff without a clef in the beginning.
180 we used to have a warning here, but it generates a lot of
181 spurious error messages.
186 *fixed = last_ext[RIGHT];
187 *space = *fixed + 1.0;
189 SCM alist = last_grob->get_property ("space-alist");
190 if (!scm_list_p (alist))
193 SCM space_def = scm_sloppy_assq (ly_symbol2scm ("first-note"), alist);
194 if (me_item->break_status_dir () == CENTER)
196 SCM nndef = scm_sloppy_assq (ly_symbol2scm ("next-note"), alist);
197 if (scm_is_pair (nndef))
201 if (!scm_is_pair (space_def))
203 programming_error ("unknown prefatory spacing");
207 space_def = scm_cdr (space_def);
208 Real distance = scm_to_double (scm_cdr (space_def));
209 SCM type = scm_car (space_def);
211 *fixed = last_ext[RIGHT];
212 if (type == ly_symbol2scm ("fixed-space"))
217 else if (type == ly_symbol2scm ("extra-space"))
219 *space = *fixed + distance;
221 else if (type == ly_symbol2scm ("semi-fixed-space"))
223 *fixed += distance / 2;
224 *space = *fixed + distance / 2;
226 else if (type == ly_symbol2scm ("minimum-space"))
228 *space = last_ext[LEFT] + max (last_ext.length (), distance);
230 else if (type == ly_symbol2scm ("minimum-fixed-space"))
232 *space = last_ext[LEFT] + max (last_ext.length (), distance);
236 *space += next_notes_correction (me, last_grob);
239 ADD_INTERFACE (Staff_spacing, "staff-spacing-interface",
240 "This object calculates spacing details from a "
241 " breakable symbol (left) to another object. For example, it takes care "
242 " of optical spacing from a bar lines to a note.",
243 "stem-spacing-correction left-items right-items");