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)
61 if (Separation_item::has_interface (it))
63 extents[d].unite (Separation_item::width (it));
68 && Note_column::has_interface (it))
70 SCM r = it->get_object ("rest");
71 Grob *g = unsmob_grob (r);
73 g = Note_column::first_head (it);
76 Ugh. If Stem is switched off, we don't know what the
77 first note head will be.
81 if (g->common_refpoint (it_col, X_AXIS) != it_col)
82 programming_error ("Note_spacing::get_spacing (): Common refpoint incorrect");
84 left_head_wid = g->extent (it_col, X_AXIS);
88 extents[d].unite (it->extent (it_col, X_AXIS));
91 Grob *accs = Note_column::accidentals (it);
93 accs = Note_column::accidentals (it->get_parent (X_AXIS));
98 = Accidental_placement::get_relevant_accidental_extent (accs, it_col, me);
100 extents[d].unite (v);
103 if (Grob *arpeggio = Note_column::arpeggio (it))
104 extents[d].unite (arpeggio->extent (it_col, X_AXIS));
108 if (extents[d].is_empty ())
109 extents[d] = Interval (0, 0);
111 while (flip (&d) != LEFT);
114 We look at the width of the note head, since smaller heads get less space
116 eg. a quarter rest gets almost 0.5 ss less horizontal space than a note.
118 What is sticking out of the note head (eg. a flag), doesn't get
119 the full amount of space.
121 FIXED also includes the left part of the right object.
124 = (left_head_wid.is_empty () ? increment
128 (left_head_wid[RIGHT]+
131 What's sticking out of the head, eg. a flag:
133 (extents[LEFT][RIGHT] - left_head_wid[RIGHT]) / 2))
136 What is sticking out on the left side of the right note:
138 + (extents[RIGHT].is_empty ()
140 : ((- extents[RIGHT][LEFT] / 2)
143 Add that which sticks out a lot.
145 + max (0.0, -extents[RIGHT][LEFT] - (base_space - 0.5 * increment))));
148 We don't do complicated stuff: (base_space - increment) is the
149 normal amount of white, which also determines the amount of
150 stretch. Upon (extreme) stretching, notes with accidentals should
151 stretch as much as notes without accidentals.
153 *space = (base_space - increment) + *fixed;
155 stem_dir_correction (me, right_col, increment, space, fixed);
159 Note_spacing::left_column (Grob *me)
164 return dynamic_cast<Item *> (me)->get_column ();
168 Compute the column of the right-items. This is a big function,
169 since RIGHT-ITEMS may span more columns (eg. if a clef is inserted,
170 this will add a new column to RIGHT-ITEMS. Here we look at the
171 columns, and return the left-most. If there are multiple columns, we
175 Note_spacing::right_column (Grob *me)
180 Grob_array *a = unsmob_grob_array (me->get_object ("right-items"));
182 int min_rank = INT_MAX;
184 for (vsize i = 0; a && i < a->size (); i++)
186 Item *ri = a->item (i);
187 Item *col = ri->get_column ();
189 int rank = Paper_column::get_rank (col);
203 vector<Grob*> &right = a->array_reference ();
204 for (vsize i = right.size (); i--;)
206 if (dynamic_cast<Item *> (right[i])->get_column () != mincol)
207 right.erase (right.begin () + i);
215 Correct for optical illusions. See [Wanske] p. 138. The combination
216 up-stem + down-stem should get extra space, the combination
217 down-stem + up-stem less.
219 TODO: have to check wether the stems are in the same staff.
222 Note_spacing::stem_dir_correction (Grob *me, Item *rcolumn,
224 Real *space, Real *fixed)
226 Drul_array<Direction> stem_dirs (CENTER, CENTER);
227 Drul_array<Interval> stem_posns;
228 Drul_array<Interval> head_posns;
229 Drul_array<SCM> props (me->get_object ("left-items"),
230 me->get_object ("right-items"));
232 Drul_array<Spanner *> beams_drul (0, 0);
233 Drul_array<Grob *> stems_drul (0, 0);
235 stem_dirs[LEFT] = stem_dirs[RIGHT] = CENTER;
237 Interval bar_xextent;
238 Interval bar_yextent;
240 bool correct_stem_dirs = true;
242 bool acc_right = false;
246 vector<Grob*> const &items (ly_scm2link_array (props [d]));
247 for (vsize i = 0; i < items.size (); i++)
249 Item *it = dynamic_cast<Item *> (items[i]);
252 acc_right = acc_right || Note_column::accidentals (it);
254 Grob *stem = Note_column::get_stem (it);
256 if (!stem || !stem->is_live ())
258 if (d == RIGHT && Separation_item::has_interface (it))
260 if (it->get_column () != rcolumn)
261 it = it->find_prebroken_piece (rcolumn->break_status_dir ());
263 Grob *last = Separation_item::extremal_break_aligned_grob (it, LEFT, &bar_xextent);
266 bar_yextent = Staff_spacing::bar_y_positions (last);
274 if (Stem::is_invisible (stem))
276 correct_stem_dirs = false;
280 stems_drul[d] = stem;
281 beams_drul[d] = Stem::get_beam (stem);
283 Direction stem_dir = get_grob_direction (stem);
284 if (stem_dirs[d] && stem_dirs[d] != stem_dir)
286 correct_stem_dirs = false;
289 stem_dirs[d] = stem_dir;
292 Correction doesn't seem appropriate when there is a large flag
293 hanging from the note.
296 && Stem::duration_log (stem) > 2 && !Stem::get_beam (stem))
297 correct_stem_dirs = false;
299 Interval hp = Stem::head_positions (stem);
300 if (correct_stem_dirs
303 Real chord_start = hp[stem_dir];
306 can't look at stem-end-position, since that triggers
307 beam slope computations.
309 Real stem_end = hp[stem_dir] +
310 stem_dir * robust_scm2double (stem->get_property ("length"), 7);
312 stem_posns[d] = Interval (min (chord_start, stem_end),
313 max (chord_start, stem_end));
314 head_posns[d].unite (hp);
318 while (flip (&d) != LEFT);
321 don't correct if accidentals are sticking out of the right side.
326 Real correction = 0.0;
328 if (!bar_yextent.is_empty ())
330 stem_dirs[RIGHT] = -stem_dirs[LEFT];
331 stem_posns[RIGHT] = bar_yextent;
332 stem_posns[RIGHT] *= 2;
335 if (correct_stem_dirs && stem_dirs[LEFT] * stem_dirs[RIGHT] == -1)
337 if (beams_drul[LEFT] && beams_drul[LEFT] == beams_drul[RIGHT])
341 this is a knee: maximal correction.
343 Real note_head_width = increment;
344 Grob *st = stems_drul[RIGHT];
345 Grob *head = st ? Stem::support_head (st) : 0;
347 Interval head_extent;
350 head_extent = head->extent (rcolumn, X_AXIS);
352 if (!head_extent.is_empty ())
353 note_head_width = head_extent[RIGHT];
357 Real thick = Stem::thickness (st);
359 note_head_width -= thick;
363 correction = note_head_width * stem_dirs[LEFT];
364 correction *= robust_scm2double (me->get_property ("knee-spacing-correction"), 0);
365 *fixed += correction;
369 intersect = stem_posns[LEFT];
370 intersect.intersect (stem_posns[RIGHT]);
371 correct_stem_dirs = correct_stem_dirs && !intersect.is_empty ();
373 if (correct_stem_dirs)
375 correction = abs (intersect.length ());
380 correction = min (correction / 7, 1.0);
381 correction *= stem_dirs[LEFT];
383 *= robust_scm2double (me->get_property ("stem-spacing-correction"), 0);
386 if (!bar_yextent.is_empty ())
390 else if (correct_stem_dirs && stem_dirs[LEFT] * stem_dirs[RIGHT] == UP)
393 Correct for the following situation:
402 ^ move the center one to the left.
405 this effect seems to be much more subtle than the
406 stem-direction stuff (why?), and also does not scale with the
407 difference in stem length.
411 Interval hp = head_posns[LEFT];
412 hp.intersect (head_posns[RIGHT]);
417 = (head_posns[LEFT][DOWN] > head_posns[RIGHT][UP]) ? RIGHT : LEFT;
419 Real delta = head_posns[-lowest][DOWN] - head_posns[lowest][UP];
420 Real corr = robust_scm2double (me->get_property ("same-direction-correction"), 0);
423 correction = -lowest * corr;
426 *space += correction;
428 /* there used to be a correction for bar_xextent () here, but
429 it's unclear what that was good for ?
433 ADD_INTERFACE (Note_spacing,
434 "This object calculates spacing wishes for individual voices.",
437 "knee-spacing-correction "
440 "same-direction-correction "
441 "stem-spacing-correction "