2 staff-spacing.cc -- implement Staff_spacing
4 source file of the GNU LilyPond music typesetter
6 (c) 2001--2007 Han-Wen Nienhuys <hanwen@xs4all.nl>
9 #include "staff-spacing.hh"
14 #include "international.hh"
15 #include "paper-column.hh"
16 #include "separation-item.hh"
18 #include "bar-line.hh"
19 #include "staff-symbol-referencer.hh"
20 #include "note-column.hh"
22 #include "accidental-placement.hh"
23 #include "pointer-group-interface.hh"
24 #include "directional-element-interface.hh"
27 Staff_spacing::left_stickout (Grob *me, Grob *g)
29 if (!g || !Note_column::has_interface (g))
32 Item *col = dynamic_cast<Item *> (g)->get_column ();
33 Real ret = max (0., (- g->extent (col, X_AXIS)[LEFT]));
36 Duh. If this gets out of hand, we should invent something more generic.
38 Grob *accs = Note_column::accidentals (g);
42 if (Accidental_placement::has_interface (accs))
43 v = Accidental_placement::get_relevant_accidental_extent (accs, col, me);
45 v = accs->extent (col, X_AXIS);
47 ret = max (ret, (- v[LEFT]));
49 Grob *arpeggio = unsmob_grob (g->get_object ("arpeggio"));
51 ret = max (ret, - arpeggio->extent (col, X_AXIS)[LEFT]);
57 Staff_spacing::optical_correction (Grob *me, Grob *g, Interval bar_height)
59 if (!g || !Note_column::has_interface (g))
62 Grob *stem = Note_column::get_stem (g);
65 if (!bar_height.is_empty () && stem)
67 Direction d = get_grob_direction (stem);
68 if (Stem::is_normal_stem (stem) && d == DOWN)
72 can't look at stem-end-position, since that triggers
73 beam slope computations.
75 Real stem_start = Stem::head_positions (stem) [d];
76 Real stem_end = stem_start +
77 d * robust_scm2double (stem->get_property ("length"), 7);
79 Interval stem_posns (min (stem_start, stem_end),
80 max (stem_end, stem_start));
82 stem_posns.intersect (bar_height);
84 ret = min (abs (stem_posns.length () / 7.0), 1.0);
85 ret *= robust_scm2double (me->get_property ("stem-spacing-correction"), 1);
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.substr (0, 1) == "|"
106 || glyph_string.substr (0, 1) == ".")
108 Grob *common = bar_grob->common_refpoint (staff_sym, Y_AXIS);
109 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
125 Staff_spacing::next_notes_correction (Grob *me,
130 Interval bar_size = bar_y_positions (last_grob);
131 Grob *orig = me->original () ? me->original () : me;
133 extract_grob_set (orig, "right-items", right_items);
135 Real max_left_stickout = 0.0;
136 Real max_optical = 0.0;
138 for (vsize i = right_items.size (); i--;)
140 Grob *g = right_items[i];
141 if (Note_column::has_interface (right_items[i]))
143 max_left_stickout = max (max_left_stickout, left_stickout (me, g));
144 max_optical = max (max_optical, optical_correction (me, g, bar_size));
148 extract_grob_set (g, "elements", elts);
149 for (vsize j = elts.size (); j--;)
151 max_left_stickout = max (max_left_stickout, left_stickout (me, elts[j]));
152 max_optical = max (max_optical, optical_correction (me, g, bar_size));
157 /* we put a minimum distance of 0.3 between the right edge of the left grob and
158 the left edge of the right grob */
159 max_left_stickout = max (0.0, max_left_stickout + right_stickout + 0.3 - current_fixed);
160 return max (max_left_stickout, max_optical);
163 /* This routine does not impose any minimum distances between columns; it only
164 affects springs. As such, the FIXED variable does not refer to a minimum
165 distance between columns, but instead to a minimum desired distance between
166 columns -- this ends up affecting the stiffness of a spring. In fact, FIXED
167 will be the distance between columns if there is a compression force of 1.0
168 applied to the line. */
170 Staff_spacing::get_spacing_params (Grob *me)
172 Grob *separation_item = 0;
173 Item *me_item = dynamic_cast<Item *> (me);
175 extract_grob_set (me, "left-items", items);
176 for (vsize i = items.size (); i--;)
178 Grob *cand = items[i];
179 if (cand && Separation_item::has_interface (cand))
180 separation_item = cand;
183 // printf ("doing col %d\n" , Paper_column::get_rank (left_col));
184 if (!separation_item)
186 programming_error ("no sep item");
191 Grob *last_grob = Separation_item::extremal_break_aligned_grob (separation_item, RIGHT,
198 Should insert a adjustable space here? For excercises, you might want to
199 use a staff without a clef in the beginning.
203 we used to have a warning here, but it generates a lot of
204 spurious error messages.
209 SCM alist = last_grob->get_property ("space-alist");
210 if (!scm_list_p (alist))
213 SCM space_def = scm_sloppy_assq (ly_symbol2scm ("first-note"), alist);
214 if (me_item->break_status_dir () == CENTER)
216 SCM nndef = scm_sloppy_assq (ly_symbol2scm ("next-note"), alist);
217 if (scm_is_pair (nndef))
221 if (!scm_is_pair (space_def))
223 programming_error ("unknown prefatory spacing");
227 space_def = scm_cdr (space_def);
228 Real distance = scm_to_double (scm_cdr (space_def));
229 SCM type = scm_car (space_def);
231 Real fixed = last_ext[RIGHT];
232 Real ideal = fixed + 1.0;
234 if (type == ly_symbol2scm ("fixed-space"))
239 else if (type == ly_symbol2scm ("extra-space"))
240 ideal = fixed + distance;
241 else if (type == ly_symbol2scm ("semi-fixed-space"))
243 fixed += distance / 2;
244 ideal = fixed + distance / 2;
246 else if (type == ly_symbol2scm ("minimum-space"))
247 ideal = last_ext[LEFT] + max (last_ext.length (), distance);
248 else if (type == ly_symbol2scm ("minimum-fixed-space"))
250 fixed = last_ext[LEFT] + max (last_ext.length (), distance);
254 Real correction = next_notes_correction (me, last_grob, fixed, last_ext[RIGHT]);
259 ret.min_distance_ = 0.0;
260 ret.distance_ = ideal;
261 ret.inverse_stretch_strength_ = ret.inverse_compress_strength_ = ideal - fixed;
265 ADD_INTERFACE (Staff_spacing,
266 "This object calculates spacing details from a "
267 " breakable symbol (left) to another object. For example, it takes care "
268 " of optical spacing from a bar lines to a note.",
271 "stem-spacing-correction "