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;
157 The below situation is now handled by the "sticks out a lot" case
158 above. However we keep around the code for a few releases before
161 if (!extents[RIGHT].is_empty ()
162 && (Paper_column::is_breakable (right_col)))
165 This is for the situation
169 Since we only take half of the right-object space above, the
170 barline will bump into the notes preceding it, if the right
171 thing is big. We add the rest of the extents here:
174 *space += -extents[RIGHT][LEFT] / 2;
175 *fixed += -extents[RIGHT][LEFT] / 2;
179 stem_dir_correction (me, right_col, increment, space, fixed);
183 Note_spacing::left_column (Grob *me)
188 return dynamic_cast<Item *> (me)->get_column ();
192 Compute the column of the right-items. This is a big function,
193 since RIGHT-ITEMS may span more columns (eg. if a clef if inserted,
194 this will add a new columns to RIGHT-ITEMS. Here we look at the
195 columns, and return the left-most. If there are multiple columns, we
199 Note_spacing::right_column (Grob *me)
204 Grob_array *a = unsmob_grob_array (me->get_object ("right-items"));
206 int min_rank = INT_MAX;
208 for (vsize i = 0; a && i < a->size (); i++)
210 Item *ri = a->item (i);
211 Item *col = ri->get_column ();
213 int rank = Paper_column::get_rank (col);
227 vector<Grob*> &right = a->array_reference ();
228 for (vsize i = right.size (); i--;)
230 if (dynamic_cast<Item *> (right[i])->get_column () != mincol)
231 right.erase (right.begin () + i);
242 Correct for optical illusions. See [Wanske] p. 138. The combination
243 up-stem + down-stem should get extra space, the combination
244 down-stem + up-stem less.
246 TODO: have to check wether the stems are in the same staff.
249 Note_spacing::stem_dir_correction (Grob *me, Item *rcolumn,
251 Real *space, Real *fixed)
253 Drul_array<Direction> stem_dirs (CENTER, CENTER);
254 Drul_array<Interval> stem_posns;
255 Drul_array<Interval> head_posns;
256 Drul_array<SCM> props (me->get_object ("left-items"),
257 me->get_object ("right-items"));
259 Drul_array<Spanner *> beams_drul (0, 0);
260 Drul_array<Grob *> stems_drul (0, 0);
262 stem_dirs[LEFT] = stem_dirs[RIGHT] = CENTER;
264 Interval bar_xextent;
265 Interval bar_yextent;
267 bool correct_stem_dirs = true;
269 bool acc_right = false;
273 vector<Grob*> const &items (ly_scm2link_array (props [d]));
274 for (vsize i = 0; i < items.size (); i++)
276 Item *it = dynamic_cast<Item *> (items[i]);
279 acc_right = acc_right || Note_column::accidentals (it);
281 Grob *stem = Note_column::get_stem (it);
283 if (!stem || !stem->is_live ())
285 if (d == RIGHT && Separation_item::has_interface (it))
287 if (it->get_column () != rcolumn)
288 it = it->find_prebroken_piece (rcolumn->break_status_dir ());
290 Grob *last = Separation_item::extremal_break_aligned_grob (it, LEFT, &bar_xextent);
293 bar_yextent = Staff_spacing::bar_y_positions (last);
301 if (Stem::is_invisible (stem))
303 correct_stem_dirs = false;
307 stems_drul[d] = stem;
308 beams_drul[d] = Stem::get_beam (stem);
310 Direction stem_dir = get_grob_direction (stem);
311 if (stem_dirs[d] && stem_dirs[d] != stem_dir)
313 correct_stem_dirs = false;
316 stem_dirs[d] = stem_dir;
319 Correction doesn't seem appropriate when there is a large flag
320 hanging from the note.
323 && Stem::duration_log (stem) > 2 && !Stem::get_beam (stem))
324 correct_stem_dirs = false;
326 Interval hp = Stem::head_positions (stem);
327 if (correct_stem_dirs
330 Real chord_start = hp[stem_dir];
333 can't look at stem-end-position, since that triggers
334 beam slope computations.
336 Real stem_end = hp[stem_dir] +
337 stem_dir * robust_scm2double (stem->get_property ("length"), 7);
339 stem_posns[d] = Interval (min (chord_start, stem_end),
340 max (chord_start, stem_end));
341 head_posns[d].unite (hp);
345 while (flip (&d) != LEFT);
348 don't correct if accidentals are sticking out of the right side.
353 Real correction = 0.0;
355 if (!bar_yextent.is_empty ())
357 stem_dirs[RIGHT] = -stem_dirs[LEFT];
358 stem_posns[RIGHT] = bar_yextent;
359 stem_posns[RIGHT] *= 2;
362 if (correct_stem_dirs && stem_dirs[LEFT] * stem_dirs[RIGHT] == -1)
364 if (beams_drul[LEFT] && beams_drul[LEFT] == beams_drul[RIGHT])
368 this is a knee: maximal correction.
370 Real note_head_width = increment;
371 Grob *st = stems_drul[RIGHT];
372 Grob *head = st ? Stem::support_head (st) : 0;
374 Interval head_extent;
377 head_extent = head->extent (rcolumn, X_AXIS);
379 if (!head_extent.is_empty ())
380 note_head_width = head_extent[RIGHT];
384 Real thick = Stem::thickness (st);
386 note_head_width -= thick;
390 correction = note_head_width * stem_dirs[LEFT];
391 correction *= robust_scm2double (me->get_property ("knee-spacing-correction"), 0);
392 *fixed += correction;
396 intersect = stem_posns[LEFT];
397 intersect.intersect (stem_posns[RIGHT]);
398 correct_stem_dirs = correct_stem_dirs && !intersect.is_empty ();
400 if (correct_stem_dirs)
402 correction = abs (intersect.length ());
407 correction = min (correction / 7, 1.0);
408 correction *= stem_dirs[LEFT];
410 *= robust_scm2double (me->get_property ("stem-spacing-correction"), 0);
413 if (!bar_yextent.is_empty ())
417 else if (correct_stem_dirs && stem_dirs[LEFT] * stem_dirs[RIGHT] == UP)
420 Correct for the following situation:
429 ^ move the center one to the left.
432 this effect seems to be much more subtle than the
433 stem-direction stuff (why?), and also does not scale with the
434 difference in stem length.
438 Interval hp = head_posns[LEFT];
439 hp.intersect (head_posns[RIGHT]);
444 = (head_posns[LEFT][DOWN] > head_posns[RIGHT][UP]) ? RIGHT : LEFT;
446 Real delta = head_posns[-lowest][DOWN] - head_posns[lowest][UP];
447 Real corr = robust_scm2double (me->get_property ("same-direction-correction"), 0);
450 correction = -lowest * corr;
453 *space += correction;
455 /* there used to be a correction for bar_xextent () here, but
456 it's unclear what that was good for ?
460 ADD_INTERFACE (Note_spacing,
461 "This object calculates spacing wishes for individual voices.",
464 "knee-spacing-correction "
467 "same-direction-correction "
468 "stem-spacing-correction "