2 note-spacing.cc -- implement Note_spacing
4 source file of the GNU LilyPond music typesetter
6 (c) 2001--2005 Han-Wen Nienhuys <hanwen@cs.uu.nl>
9 #include "note-spacing.hh"
11 #include "grob-array.hh"
12 #include "paper-column.hh"
14 #include "note-column.hh"
17 #include "separation-item.hh"
18 #include "staff-spacing.hh"
19 #include "accidental-placement.hh"
20 #include "output-def.hh"
21 #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)
49 it = it->find_prebroken_piece (col_dir);
52 some kind of mismatch, eg. a note column, that is behind a
58 Item *it_col = it->get_column ();
59 if (d == RIGHT && right_col != it_col)
62 if (Separation_item::has_interface (it))
64 extents[d].unite (Separation_item::width (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.
80 left_head_wid = g->extent (it_col, X_AXIS);
83 extents[d].unite (it->extent (it_col, X_AXIS));
86 Grob *accs = Note_column::accidentals (it);
88 accs = Note_column::accidentals (it->get_parent (X_AXIS));
93 = Accidental_placement::get_relevant_accidental_extent (accs, it_col, me);
100 if (extents[d].is_empty ())
101 extents[d] = Interval (0, 0);
103 while (flip (&d) != LEFT);
106 We look at the width of the note head, since smaller heads get less space
107 eg. a quarter rest gets almost 0.5 ss less horizontal space than a note.
109 What is sticking out of the note head (eg. a flag), doesn't get
110 the full amount of space.
112 FIXED also includes the left part of the right object.
115 = (left_head_wid.is_empty () ? increment
119 (left_head_wid[RIGHT]+
122 What's sticking out of the head, eg. a flag:
124 (extents[LEFT][RIGHT] - left_head_wid[RIGHT]) / 2))
127 What is sticking out of the right note:
129 + (extents[RIGHT].is_empty () ? 0.0 : - extents[RIGHT][LEFT] / 2);
132 We don't do complicated stuff: (base_space - increment) is the
133 normal amount of white, which also determines the amount of
134 stretch. Upon (extreme) stretching, notes with accidentals should
135 stretch as much as notes without accidentals.
137 *space = (base_space - increment) + *fixed;
139 if (!extents[RIGHT].is_empty ()
140 && (Item::is_breakable (right_col)
141 || right_col->original_))
144 This is for the situation
148 Since we only take half of the right-object space above, the
149 barline will bump into the notes preceding it, if the right
150 thing is big. We add the rest of the extents here:
153 *space += -extents[RIGHT][LEFT] / 2;
154 *fixed += -extents[RIGHT][LEFT] / 2;
157 stem_dir_correction (me, right_col, increment, space, fixed);
161 Note_spacing::left_column (Grob *me)
166 return dynamic_cast<Item *> (me)->get_column ();
170 Compute the column of the right-items. This is a big function,
171 since RIGHT-ITEMS may span more columns (eg. if a clef if inserted,
172 this will add a new columns to RIGHT-ITEMS. Here we look at the
173 columns, and return the left-most. If there are multiple columns, we
177 Note_spacing::right_column (Grob *me)
182 Grob_array * a = unsmob_grob_array (me->get_object ("right-items"));
184 int min_rank = INT_MAX;
186 for (int i = 0; a && i < a->size (); i++)
188 Item *ri = a->item (i);
189 Item *col = ri->get_column ();
191 int rank = Paper_column::get_rank (col);
205 Link_array<Grob> & right = a->array_reference ();
206 for (int i = right.size(); i--;)
208 if (dynamic_cast<Item*> (right[i])->get_column () != mincol)
222 Correct for optical illusions. See [Wanske] p. 138. The combination
223 up-stem + down-stem should get extra space, the combination
224 down-stem + up-stem less.
226 TODO: have to check wether the stems are in the same staff.
229 Note_spacing::stem_dir_correction (Grob *me, Item *rcolumn,
231 Real *space, Real *fixed)
233 Drul_array<Direction> stem_dirs (CENTER, CENTER);
234 Drul_array<Interval> stem_posns;
235 Drul_array<Interval> head_posns;
236 Drul_array<SCM> props (me->get_object ("left-items"),
237 me->get_object ("right-items"));
239 Drul_array<Spanner *> beams_drul (0, 0);
240 Drul_array<Grob *> stems_drul (0, 0);
242 stem_dirs[LEFT] = stem_dirs[RIGHT] = CENTER;
244 Interval bar_xextent;
245 Interval bar_yextent;
247 bool correct_stem_dirs = true;
249 bool acc_right = false;
253 Link_array<Grob> const &items (ly_scm2link_array (props [d]));
254 for (int i = 0; i < items.size(); i++)
256 Item *it = dynamic_cast<Item *> (items[i]);
259 acc_right = acc_right || Note_column::accidentals (it);
261 Grob *stem = Note_column::get_stem (it);
263 if (!stem || !stem->is_live ())
265 if (d == RIGHT && Separation_item::has_interface (it))
267 if (it->get_column () != rcolumn)
269 it = it->find_prebroken_piece (rcolumn->break_status_dir ());
272 Grob *last = Separation_item::extremal_break_aligned_grob (it, LEFT, &bar_xextent);
275 bar_yextent = Staff_spacing::bar_y_positions (last);
283 if (Stem::is_invisible (stem))
285 correct_stem_dirs = false;
289 stems_drul[d] = stem;
290 beams_drul[d] = Stem::get_beam (stem);
292 Direction sd = Stem::get_direction (stem);
293 if (stem_dirs[d] && stem_dirs[d] != sd)
295 correct_stem_dirs = false;
301 Correction doesn't seem appropriate when there is a large flag
302 hanging from the note.
305 && Stem::duration_log (stem) > 2 && !Stem::get_beam (stem))
307 correct_stem_dirs = false;
310 Interval hp = Stem::head_positions (stem);
313 Real chord_start = hp[sd];
314 Real stem_end = Stem::stem_end_position (stem);
316 stem_posns[d] = Interval (min (chord_start, stem_end), max (chord_start, stem_end));
317 head_posns[d].unite (hp);
321 while (flip (&d) != LEFT);
324 don't correct if accidentals are sticking out of the right side.
329 Real correction = 0.0;
331 if (!bar_yextent.is_empty ())
333 stem_dirs[RIGHT] = -stem_dirs[LEFT];
334 stem_posns[RIGHT] = bar_yextent;
337 if (correct_stem_dirs && stem_dirs[LEFT] * stem_dirs[RIGHT] == -1)
339 if (beams_drul[LEFT] && beams_drul[LEFT] == beams_drul[RIGHT])
343 this is a knee: maximal correction.
345 Real note_head_width = increment;
346 Grob *st = stems_drul[RIGHT];
347 Grob *head = st ? Stem::support_head (st) : 0;
349 Interval head_extent;
352 head_extent = head->extent (rcolumn, X_AXIS);
354 if (!head_extent.is_empty ())
355 note_head_width = head_extent[RIGHT];
359 Real thick = Stem::thickness (st);
361 note_head_width -= thick;
365 correction = note_head_width * stem_dirs[LEFT];
366 correction *= robust_scm2double (me->get_property ("knee-spacing-correction"), 0);
367 *fixed += correction;
371 intersect = stem_posns[LEFT];
372 intersect.intersect (stem_posns[RIGHT]);
373 correct_stem_dirs = correct_stem_dirs && !intersect.is_empty ();
375 if (correct_stem_dirs)
377 correction = abs (intersect.length ());
382 correction = min (correction / 7, 1.0);
383 correction *= stem_dirs[LEFT];
385 *= robust_scm2double (me->get_property ("stem-spacing-correction"), 0);
388 if (!bar_yextent.is_empty ())
394 else if (correct_stem_dirs && stem_dirs[LEFT] * stem_dirs[RIGHT] == UP)
397 Correct for the following situation:
406 ^ move the center one to the left.
409 this effect seems to be much more subtle than the
410 stem-direction stuff (why?), and also does not scale with the
411 difference in stem length.
415 Interval hp = head_posns[LEFT];
416 hp.intersect (head_posns[RIGHT]);
421 = (head_posns[LEFT][DOWN] > head_posns[RIGHT][UP]) ? RIGHT : LEFT;
423 Real delta = head_posns[-lowest][DOWN] - head_posns[lowest][UP];
424 Real corr = robust_scm2double (me->get_property ("same-direction-correction"), 0);
427 correction = -lowest * corr;
430 *space += correction;
432 /* there used to be a correction for bar_xextent () here, but
433 it's unclear what that was good for ?
437 ADD_INTERFACE (Note_spacing, "note-spacing-interface",
438 "This object calculates spacing wishes for individual voices.",
439 "left-items right-items stem-spacing-correction same-direction-correction knee-spacing-correction");