2 note-spacing.cc -- implement Note_spacing
4 source file of the GNU LilyPond music typesetter
6 (c) 2001--2005 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 Link_array<Grob> const &items (ly_scm2link_array (props [d]));
43 for (int 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));
69 SCM r = it->get_object ("rest");
70 Grob *g = unsmob_grob (r);
72 g = Note_column::first_head (it);
75 Ugh. If Stem is switched off, we don't know what the
76 first note head will be.
79 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
109 eg. a quarter rest gets almost 0.5 ss less horizontal space than a note.
111 What is sticking out of the note head (eg. a flag), doesn't get
112 the full amount of space.
114 FIXED also includes the left part of the right object.
117 = (left_head_wid.is_empty () ? increment
121 (left_head_wid[RIGHT]+
124 What's sticking out of the head, eg. a flag:
126 (extents[LEFT][RIGHT] - left_head_wid[RIGHT]) / 2))
129 What is sticking out on the left side of the right note:
131 + (extents[RIGHT].is_empty ()
133 : ((- extents[RIGHT][LEFT] / 2)
136 Add that which sticks out a lot.
138 + max (0.0, -extents[RIGHT][LEFT] - (base_space - increment))));
141 We don't do complicated stuff: (base_space - increment) is the
142 normal amount of white, which also determines the amount of
143 stretch. Upon (extreme) stretching, notes with accidentals should
144 stretch as much as notes without accidentals.
146 *space = (base_space - increment) + *fixed;
150 The below situation is now handled by the "sticks out a lot" case
151 above. However we keep around the code for a few releases before
154 if (!extents[RIGHT].is_empty ()
155 && (Item::is_breakable (right_col)
156 || right_col->original ()))
159 This is for the situation
163 Since we only take half of the right-object space above, the
164 barline will bump into the notes preceding it, if the right
165 thing is big. We add the rest of the extents here:
168 *space += -extents[RIGHT][LEFT] / 2;
169 *fixed += -extents[RIGHT][LEFT] / 2;
173 stem_dir_correction (me, right_col, increment, space, fixed);
177 Note_spacing::left_column (Grob *me)
182 return dynamic_cast<Item *> (me)->get_column ();
186 Compute the column of the right-items. This is a big function,
187 since RIGHT-ITEMS may span more columns (eg. if a clef if inserted,
188 this will add a new columns to RIGHT-ITEMS. Here we look at the
189 columns, and return the left-most. If there are multiple columns, we
193 Note_spacing::right_column (Grob *me)
198 Grob_array *a = unsmob_grob_array (me->get_object ("right-items"));
200 int min_rank = INT_MAX;
202 for (int i = 0; a && i < a->size (); i++)
204 Item *ri = a->item (i);
205 Item *col = ri->get_column ();
207 int rank = Paper_column::get_rank (col);
221 Link_array<Grob> &right = a->array_reference ();
222 for (int i = right.size (); i--;)
224 if (dynamic_cast<Item *> (right[i])->get_column () != mincol)
236 Correct for optical illusions. See [Wanske] p. 138. The combination
237 up-stem + down-stem should get extra space, the combination
238 down-stem + up-stem less.
240 TODO: have to check wether the stems are in the same staff.
243 Note_spacing::stem_dir_correction (Grob *me, Item *rcolumn,
245 Real *space, Real *fixed)
247 Drul_array<Direction> stem_dirs (CENTER, CENTER);
248 Drul_array<Interval> stem_posns;
249 Drul_array<Interval> head_posns;
250 Drul_array<SCM> props (me->get_object ("left-items"),
251 me->get_object ("right-items"));
253 Drul_array<Spanner *> beams_drul (0, 0);
254 Drul_array<Grob *> stems_drul (0, 0);
256 stem_dirs[LEFT] = stem_dirs[RIGHT] = CENTER;
258 Interval bar_xextent;
259 Interval bar_yextent;
261 bool correct_stem_dirs = true;
263 bool acc_right = false;
267 Link_array<Grob> const &items (ly_scm2link_array (props [d]));
268 for (int i = 0; i < items.size (); i++)
270 Item *it = dynamic_cast<Item *> (items[i]);
273 acc_right = acc_right || Note_column::accidentals (it);
275 Grob *stem = Note_column::get_stem (it);
277 if (!stem || !stem->is_live ())
279 if (d == RIGHT && Separation_item::has_interface (it))
281 if (it->get_column () != rcolumn)
282 it = it->find_prebroken_piece (rcolumn->break_status_dir ());
284 Grob *last = Separation_item::extremal_break_aligned_grob (it, LEFT, &bar_xextent);
287 bar_yextent = Staff_spacing::bar_y_positions (last);
295 if (Stem::is_invisible (stem))
297 correct_stem_dirs = false;
301 stems_drul[d] = stem;
302 beams_drul[d] = Stem::get_beam (stem);
304 Direction sd = get_grob_direction (stem);
305 if (stem_dirs[d] && stem_dirs[d] != sd)
307 correct_stem_dirs = false;
313 Correction doesn't seem appropriate when there is a large flag
314 hanging from the note.
317 && Stem::duration_log (stem) > 2 && !Stem::get_beam (stem))
318 correct_stem_dirs = false;
320 Interval hp = Stem::head_positions (stem);
323 Real chord_start = hp[sd];
324 Real stem_end = Stem::stem_end_position (stem);
326 stem_posns[d] = Interval (min (chord_start, stem_end), max (chord_start, stem_end));
327 head_posns[d].unite (hp);
331 while (flip (&d) != LEFT);
334 don't correct if accidentals are sticking out of the right side.
339 Real correction = 0.0;
341 if (!bar_yextent.is_empty ())
343 stem_dirs[RIGHT] = -stem_dirs[LEFT];
344 stem_posns[RIGHT] = bar_yextent;
347 if (correct_stem_dirs && stem_dirs[LEFT] * stem_dirs[RIGHT] == -1)
349 if (beams_drul[LEFT] && beams_drul[LEFT] == beams_drul[RIGHT])
353 this is a knee: maximal correction.
355 Real note_head_width = increment;
356 Grob *st = stems_drul[RIGHT];
357 Grob *head = st ? Stem::support_head (st) : 0;
359 Interval head_extent;
362 head_extent = head->extent (rcolumn, X_AXIS);
364 if (!head_extent.is_empty ())
365 note_head_width = head_extent[RIGHT];
369 Real thick = Stem::thickness (st);
371 note_head_width -= thick;
375 correction = note_head_width * stem_dirs[LEFT];
376 correction *= robust_scm2double (me->get_property ("knee-spacing-correction"), 0);
377 *fixed += correction;
381 intersect = stem_posns[LEFT];
382 intersect.intersect (stem_posns[RIGHT]);
383 correct_stem_dirs = correct_stem_dirs && !intersect.is_empty ();
385 if (correct_stem_dirs)
387 correction = abs (intersect.length ());
392 correction = min (correction / 7, 1.0);
393 correction *= stem_dirs[LEFT];
395 *= robust_scm2double (me->get_property ("stem-spacing-correction"), 0);
398 if (!bar_yextent.is_empty ())
402 else if (correct_stem_dirs && stem_dirs[LEFT] * stem_dirs[RIGHT] == UP)
405 Correct for the following situation:
414 ^ move the center one to the left.
417 this effect seems to be much more subtle than the
418 stem-direction stuff (why?), and also does not scale with the
419 difference in stem length.
423 Interval hp = head_posns[LEFT];
424 hp.intersect (head_posns[RIGHT]);
429 = (head_posns[LEFT][DOWN] > head_posns[RIGHT][UP]) ? RIGHT : LEFT;
431 Real delta = head_posns[-lowest][DOWN] - head_posns[lowest][UP];
432 Real corr = robust_scm2double (me->get_property ("same-direction-correction"), 0);
435 correction = -lowest * corr;
438 *space += correction;
440 /* there used to be a correction for bar_xextent () here, but
441 it's unclear what that was good for ?
445 ADD_INTERFACE (Note_spacing, "note-spacing-interface",
446 "This object calculates spacing wishes for individual voices.",
447 "left-items right-items stem-spacing-correction same-direction-correction knee-spacing-correction");