2 note-spacing.cc -- implement Note_spacing
4 source file of the GNU LilyPond music typesetter
6 (c) 2001--2007 Han-Wen Nienhuys <hanwen@xs4all.nl>
9 #include "note-spacing.hh"
11 #include "directional-element-interface.hh"
12 #include "grob-array.hh"
13 #include "paper-column.hh"
15 #include "note-column.hh"
18 #include "separation-item.hh"
19 #include "staff-spacing.hh"
20 #include "accidental-placement.hh"
21 #include "output-def.hh"
22 #include "pointer-group-interface.hh"
25 TODO: detect hshifts due to collisions, and account for them in
30 Note_spacing::get_spacing (Grob *me, Item *right_col,
31 Real base_space, Real increment, Real *space, Real *fixed)
33 Drul_array<SCM> props (me->get_object ("left-items"),
34 me->get_object ("right-items"));
36 Direction col_dir = right_col->break_status_dir ();
37 Drul_array<Interval> extents;
39 Interval left_head_wid;
42 vector<Grob*> const &items (ly_scm2link_array (props [d]));
43 for (vsize i = items.size (); i--;)
45 Item *it = dynamic_cast<Item *> (items[i]);
47 if (d == RIGHT && it->break_status_dir () != col_dir)
48 it = it->find_prebroken_piece (col_dir);
51 some kind of mismatch, eg. a note column, that is behind a
57 Item *it_col = it->get_column ();
58 if (d == RIGHT && right_col != it_col)
62 && Note_column::has_interface (it))
64 SCM r = it->get_object ("rest");
65 Grob *g = unsmob_grob (r);
67 g = Note_column::first_head (it);
70 Ugh. If Stem is switched off, we don't know what the
71 first note head will be.
75 if (g->common_refpoint (it_col, X_AXIS) != it_col)
76 programming_error ("Note_spacing::get_spacing (): Common refpoint incorrect");
78 left_head_wid = g->extent (it_col, X_AXIS);
82 extents[d].unite (it->extent (it_col, X_AXIS));
85 Grob *accs = Note_column::accidentals (it);
87 accs = Note_column::accidentals (it->get_parent (X_AXIS));
92 = Accidental_placement::get_relevant_accidental_extent (accs, it_col, me);
97 if (Grob *arpeggio = Note_column::arpeggio (it))
98 extents[d].unite (arpeggio->extent (it_col, X_AXIS));
102 if (extents[d].is_empty ())
103 extents[d] = Interval (0, 0);
105 while (flip (&d) != LEFT);
108 We look at the width of the note head, since smaller heads get less space
110 eg. a quarter rest gets almost 0.5 ss less horizontal space than a note.
112 What is sticking out of the note head (eg. a flag), doesn't get
113 the full amount of space.
115 FIXED also includes the left part of the right object.
118 = (left_head_wid.is_empty () ? increment
122 (left_head_wid[RIGHT]+
125 What's sticking out of the head, eg. a flag:
127 (extents[LEFT][RIGHT] - left_head_wid[RIGHT]) / 2))
130 What is sticking out on the left side of the right note:
132 + (extents[RIGHT].is_empty ()
134 : ((- extents[RIGHT][LEFT] / 2)
137 Add that which sticks out a lot.
139 + max (0.0, -extents[RIGHT][LEFT] - (base_space - 0.5 * increment))));
142 We don't do complicated stuff: (base_space - increment) is the
143 normal amount of white, which also determines the amount of
144 stretch. Upon (extreme) stretching, notes with accidentals should
145 stretch as much as notes without accidentals.
147 *space = (base_space - increment) + *fixed;
149 stem_dir_correction (me, right_col, increment, space, fixed);
154 Correct for optical illusions. See [Wanske] p. 138. The combination
155 up-stem + down-stem should get extra space, the combination
156 down-stem + up-stem less.
158 TODO: have to check wether the stems are in the same staff.
161 Note_spacing::stem_dir_correction (Grob *me, Item *rcolumn,
163 Real *space, Real *fixed)
165 Drul_array<Direction> stem_dirs (CENTER, CENTER);
166 Drul_array<Interval> stem_posns;
167 Drul_array<Interval> head_posns;
168 Drul_array<SCM> props (me->get_object ("left-items"),
169 me->get_object ("right-items"));
171 Drul_array<Spanner *> beams_drul (0, 0);
172 Drul_array<Grob *> stems_drul (0, 0);
174 stem_dirs[LEFT] = stem_dirs[RIGHT] = CENTER;
176 Interval bar_xextent;
177 Interval bar_yextent;
179 bool correct_stem_dirs = true;
181 bool acc_right = false;
185 vector<Grob*> const &items (ly_scm2link_array (props [d]));
186 for (vsize i = 0; i < items.size (); i++)
188 Item *it = dynamic_cast<Item *> (items[i]);
191 acc_right = acc_right || Note_column::accidentals (it);
193 Grob *stem = Note_column::get_stem (it);
195 if (!stem || !stem->is_live ())
197 if (d == RIGHT && Separation_item::has_interface (it))
199 if (it->get_column () != rcolumn)
200 it = it->find_prebroken_piece (rcolumn->break_status_dir ());
202 Grob *last = Separation_item::extremal_break_aligned_grob (it, LEFT, &bar_xextent);
205 bar_yextent = Staff_spacing::bar_y_positions (last);
213 if (Stem::is_invisible (stem))
215 correct_stem_dirs = false;
219 stems_drul[d] = stem;
220 beams_drul[d] = Stem::get_beam (stem);
222 Direction stem_dir = get_grob_direction (stem);
223 if (stem_dirs[d] && stem_dirs[d] != stem_dir)
225 correct_stem_dirs = false;
228 stem_dirs[d] = stem_dir;
231 Correction doesn't seem appropriate when there is a large flag
232 hanging from the note.
235 && Stem::duration_log (stem) > 2 && !Stem::get_beam (stem))
236 correct_stem_dirs = false;
238 Interval hp = Stem::head_positions (stem);
239 if (correct_stem_dirs
242 Real chord_start = hp[stem_dir];
245 can't look at stem-end-position, since that triggers
246 beam slope computations.
248 Real stem_end = hp[stem_dir] +
249 stem_dir * robust_scm2double (stem->get_property ("length"), 7);
251 stem_posns[d] = Interval (min (chord_start, stem_end),
252 max (chord_start, stem_end));
253 head_posns[d].unite (hp);
257 while (flip (&d) != LEFT);
260 don't correct if accidentals are sticking out of the right side.
265 Real correction = 0.0;
267 if (!bar_yextent.is_empty ())
269 stem_dirs[RIGHT] = -stem_dirs[LEFT];
270 stem_posns[RIGHT] = bar_yextent;
271 stem_posns[RIGHT] *= 2;
274 if (correct_stem_dirs && stem_dirs[LEFT] * stem_dirs[RIGHT] == -1)
276 if (beams_drul[LEFT] && beams_drul[LEFT] == beams_drul[RIGHT])
280 this is a knee: maximal correction.
282 Real note_head_width = increment;
283 Grob *st = stems_drul[RIGHT];
284 Grob *head = st ? Stem::support_head (st) : 0;
286 Interval head_extent;
289 head_extent = head->extent (rcolumn, X_AXIS);
291 if (!head_extent.is_empty ())
292 note_head_width = head_extent[RIGHT];
296 Real thick = Stem::thickness (st);
298 note_head_width -= thick;
302 correction = note_head_width * stem_dirs[LEFT];
303 correction *= robust_scm2double (me->get_property ("knee-spacing-correction"), 0);
304 *fixed += correction;
308 intersect = stem_posns[LEFT];
309 intersect.intersect (stem_posns[RIGHT]);
310 correct_stem_dirs = correct_stem_dirs && !intersect.is_empty ();
312 if (correct_stem_dirs)
314 correction = abs (intersect.length ());
319 correction = min (correction / 7, 1.0);
320 correction *= stem_dirs[LEFT];
322 *= robust_scm2double (me->get_property ("stem-spacing-correction"), 0);
325 if (!bar_yextent.is_empty ())
329 else if (correct_stem_dirs && stem_dirs[LEFT] * stem_dirs[RIGHT] == UP)
332 Correct for the following situation:
341 ^ move the center one to the left.
344 this effect seems to be much more subtle than the
345 stem-direction stuff (why?), and also does not scale with the
346 difference in stem length.
350 Interval hp = head_posns[LEFT];
351 hp.intersect (head_posns[RIGHT]);
356 = (head_posns[LEFT][DOWN] > head_posns[RIGHT][UP]) ? RIGHT : LEFT;
358 Real delta = head_posns[-lowest][DOWN] - head_posns[lowest][UP];
359 Real corr = robust_scm2double (me->get_property ("same-direction-correction"), 0);
362 correction = -lowest * corr;
365 *space += correction;
367 /* there used to be a correction for bar_xextent () here, but
368 it's unclear what that was good for ?
372 ADD_INTERFACE (Note_spacing,
373 "This object calculates spacing wishes for individual voices.",
376 "knee-spacing-correction "
379 "same-direction-correction "
380 "stem-spacing-correction "