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")))
53 max_corr = max (max_corr, - a->extent (col, X_AXIS)[LEFT]);
57 Let's decrease the space a little if the problem is not located
60 if (bar_size.is_empty ())
63 if (!bar_size.is_empty ())
64 if (Grob *stem = Note_column::get_stem (g))
66 Direction d = Stem::get_direction (stem);
69 Real stem_start = Stem::head_positions (stem) [DOWN];
70 Real stem_end = Stem::stem_end_position (stem);
71 Interval stem_posns (min (stem_start, stem_end),
72 max (stem_end, stem_start));
74 stem_posns.intersect (bar_size);
76 Real corr = min (abs (stem_posns.length () / 7.0), 1.0);
78 *= robust_scm2double (me->get_property ("stem-spacing-correction"), 1);
82 max_corr = max (max_corr, corr);
89 Y-positions that are covered by BAR_GROB, in the case that it is a
92 Staff_spacing::bar_y_positions (Grob *bar_grob)
95 bar_size.set_empty ();
96 if (Bar_line::has_interface (bar_grob))
98 SCM glyph = bar_grob->get_property ("glyph");
100 String glyph_string = scm_is_string (glyph) ? ly_scm2string (glyph) : "";
101 if (glyph_string.left_string (1) == "|" || glyph_string.left_string (1) == ".")
103 SCM sz = Bar_line::get_staff_bar_size (bar_grob->self_scm ());
104 bar_size = Interval (-1, 1);
105 bar_size *= robust_scm2double (sz, 1)
106 / Staff_symbol_referencer::staff_space (bar_grob);
113 Do corrections for the following notes.
115 This is slightly convoluted, since the staffspacing grob gets
116 pointers to the separation-items, not the note-columns or
120 Staff_spacing::next_notes_correction (Grob *me, Grob *last_grob)
122 Interval bar_size = bar_y_positions (last_grob);
125 extract_grob_set (me, "right-items", right_items);
126 for (int i = right_items.size (); i--;)
128 Grob *g = right_items[i];
130 max_corr = max (max_corr, next_note_correction (me, g, bar_size));
131 extract_grob_set (g, "elements", elts);
132 for (int j = elts.size (); j--;)
133 max_corr = max (max_corr, next_note_correction (me, elts[j], bar_size));
140 Staff_spacing::get_spacing_params (Grob *me, Real *space, Real *fixed)
145 Grob *separation_item = 0;
146 Item *me_item = dynamic_cast<Item *> (me);
148 extract_grob_set (me, "left-items", items);
149 for (int i = items.size (); i--;)
151 Grob *cand = items[i];
152 if (cand && Separation_item::has_interface (cand))
153 separation_item = cand;
156 // printf ("doing col %d\n" , Paper_column::get_rank (left_col));
158 if (!separation_item)
160 programming_error ("no sep item");
165 Grob *last_grob = Separation_item::extremal_break_aligned_grob (separation_item, RIGHT,
172 Should insert a adjustable space here? For excercises, you might want to
173 use a staff without a clef in the beginning.
177 we used to have a warning here, but it generates a lot of
178 spurious error messages.
183 *fixed = last_ext[RIGHT];
184 *space = *fixed + 1.0;
186 SCM alist = last_grob->get_property ("space-alist");
187 if (!scm_list_p (alist))
190 SCM space_def = scm_sloppy_assq (ly_symbol2scm ("first-note"), alist);
191 if (me_item->break_status_dir () == CENTER)
193 SCM nndef = scm_sloppy_assq (ly_symbol2scm ("next-note"), alist);
194 if (scm_is_pair (nndef))
198 if (!scm_is_pair (space_def))
200 programming_error ("unknown prefatory spacing");
204 space_def = scm_cdr (space_def);
205 Real distance = scm_to_double (scm_cdr (space_def));
206 SCM type = scm_car (space_def);
208 *fixed = last_ext[RIGHT];
209 if (type == ly_symbol2scm ("fixed-space"))
214 else if (type == ly_symbol2scm ("extra-space"))
216 *space = *fixed + distance;
218 else if (type == ly_symbol2scm ("semi-fixed-space"))
220 *fixed += distance / 2;
221 *space = *fixed + distance / 2;
223 else if (type == ly_symbol2scm ("minimum-space"))
225 *space = last_ext[LEFT] + max (last_ext.length (), distance);
227 else if (type == ly_symbol2scm ("minimum-fixed-space"))
229 *space = last_ext[LEFT] + max (last_ext.length (), distance);
233 *space += next_notes_correction (me, last_grob);
236 ADD_INTERFACE (Staff_spacing, "staff-spacing-interface",
237 "This object calculates spacing details from a "
238 " breakable symbol (left) to another object. For example, it takes care "
239 " of optical spacing from a bar lines to a note.",
240 "stem-spacing-correction left-items right-items");