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 "spacing-interface.hh"
23 #include "accidental-placement.hh"
24 #include "pointer-group-interface.hh"
25 #include "directional-element-interface.hh"
28 Staff_spacing::optical_correction (Grob *me, Grob *g, Interval bar_height)
30 if (!g || !Note_column::has_interface (g))
33 Grob *stem = Note_column::get_stem (g);
36 if (!bar_height.is_empty () && stem)
38 Direction d = get_grob_direction (stem);
39 if (Stem::is_normal_stem (stem) && d == DOWN)
43 can't look at stem-end-position, since that triggers
44 beam slope computations.
46 Real stem_start = Stem::head_positions (stem) [d];
47 Real stem_end = stem_start +
48 d * robust_scm2double (stem->get_property ("length"), 7);
50 Interval stem_posns (min (stem_start, stem_end),
51 max (stem_end, stem_start));
53 stem_posns.intersect (bar_height);
55 ret = min (abs (stem_posns.length () / 7.0), 1.0);
56 ret *= robust_scm2double (me->get_property ("stem-spacing-correction"), 1);
63 Y-positions that are covered by BAR_GROB, in the case that it is a
66 Staff_spacing::bar_y_positions (Grob *bar_grob)
69 bar_size.set_empty ();
70 if (Bar_line::has_interface (bar_grob))
72 SCM glyph = bar_grob->get_property ("glyph-name");
73 Grob *staff_sym = Staff_symbol_referencer::get_staff_symbol (bar_grob);
75 string glyph_string = scm_is_string (glyph) ? ly_scm2string (glyph) : "";
76 if (glyph_string.substr (0, 1) == "|"
77 || glyph_string.substr (0, 1) == ".")
79 Grob *common = bar_grob->common_refpoint (staff_sym, Y_AXIS);
80 bar_size = bar_grob->extent (common, Y_AXIS);
81 bar_size *= 1.0 / Staff_symbol_referencer::staff_space (bar_grob);
88 Staff_spacing::next_notes_correction (Grob *me,
91 Interval bar_size = bar_y_positions (last_grob);
92 Grob *orig = me->original () ? me->original () : me;
93 vector<Item*> note_columns = Spacing_interface::right_note_columns (orig);
95 Real max_optical = 0.0;
97 for (vsize i = 0; i < note_columns.size (); i++)
98 max_optical = max (max_optical, optical_correction (me, note_columns[i], bar_size));
103 /* This routine does not impose any minimum distances between columns; it only
104 affects springs. As such, the FIXED variable does not refer to a minimum
105 distance between columns, but instead to a minimum desired distance between
106 columns -- this ends up affecting the stiffness of a spring. In fact, FIXED
107 will be the distance between columns if there is a compression force of 1.0
108 applied to the line. */
110 Staff_spacing::get_spacing (Grob *me)
112 Grob *separation_item = 0;
113 Item *me_item = dynamic_cast<Item *> (me);
115 extract_grob_set (me, "left-items", items);
116 for (vsize i = items.size (); i--;)
118 Grob *cand = items[i];
119 if (cand && Separation_item::has_interface (cand))
120 separation_item = cand;
123 // printf ("doing col %d\n" , Paper_column::get_rank (left_col));
124 if (!separation_item)
126 programming_error ("no sep item");
131 Grob *last_grob = Separation_item::extremal_break_aligned_grob (separation_item, RIGHT,
138 Should insert a adjustable space here? For excercises, you might want to
139 use a staff without a clef in the beginning.
143 we used to have a warning here, but it generates a lot of
144 spurious error messages.
149 SCM alist = last_grob->get_property ("space-alist");
150 if (!scm_list_p (alist))
153 SCM space_def = scm_sloppy_assq (ly_symbol2scm ("first-note"), alist);
154 if (me_item->break_status_dir () == CENTER)
156 SCM nndef = scm_sloppy_assq (ly_symbol2scm ("next-note"), alist);
157 if (scm_is_pair (nndef))
161 if (!scm_is_pair (space_def))
163 programming_error ("unknown prefatory spacing");
167 space_def = scm_cdr (space_def);
168 Real distance = scm_to_double (scm_cdr (space_def));
169 SCM type = scm_car (space_def);
171 Real fixed = last_ext[RIGHT];
172 Real ideal = fixed + 1.0;
174 if (type == ly_symbol2scm ("fixed-space"))
179 else if (type == ly_symbol2scm ("extra-space"))
180 ideal = fixed + distance;
181 else if (type == ly_symbol2scm ("semi-fixed-space"))
183 fixed += distance / 2;
184 ideal = fixed + distance / 2;
186 else if (type == ly_symbol2scm ("minimum-space"))
187 ideal = last_ext[LEFT] + max (last_ext.length (), distance);
188 else if (type == ly_symbol2scm ("minimum-fixed-space"))
190 fixed = last_ext[LEFT] + max (last_ext.length (), distance);
194 Real optical_correction = next_notes_correction (me, last_grob);
195 Real min_dist = Spacing_interface::minimum_distance (me);
196 Real min_dist_correction = max (0.0, 0.3 + min_dist - fixed);
197 Real correction = max (optical_correction, min_dist_correction);
202 Spring ret (ideal, min_dist);
203 ret.set_inverse_stretch_strength (ideal - fixed);
207 ADD_INTERFACE (Staff_spacing,
208 "This object calculates spacing details from a "
209 " breakable symbol (left) to another object. For example, it takes care "
210 " of optical spacing from a bar lines to a note.",
213 "stem-spacing-correction "