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);
53 some kind of mismatch, eg. a note column, that is behind a
59 Item *it_col = it->get_column ();
60 if (d == RIGHT && right_col != it_col)
63 if (Separation_item::has_interface (it))
65 extents[d].unite (Separation_item::width (it));
71 SCM r = it->get_object ("rest");
72 Grob *g = unsmob_grob (r);
74 g = Note_column::first_head (it);
77 Ugh. If Stem is switched off, we don't know what the
78 first note head will be.
81 left_head_wid = g->extent (it_col, X_AXIS);
84 extents[d].unite (it->extent (it_col, X_AXIS));
87 Grob *accs = Note_column::accidentals (it);
89 accs = Note_column::accidentals (it->get_parent (X_AXIS));
94 = Accidental_placement::get_relevant_accidental_extent (accs, it_col, me);
100 if (Grob *arpeggio = Note_column::arpeggio (it))
102 extents[d].unite (arpeggio->extent (it_col, X_AXIS));
107 if (extents[d].is_empty ())
108 extents[d] = Interval (0, 0);
110 while (flip (&d) != LEFT);
113 We look at the width of the note head, since smaller heads get less space
114 eg. a quarter rest gets almost 0.5 ss less horizontal space than a note.
116 What is sticking out of the note head (eg. a flag), doesn't get
117 the full amount of space.
119 FIXED also includes the left part of the right object.
122 = (left_head_wid.is_empty () ? increment
126 (left_head_wid[RIGHT]+
129 What's sticking out of the head, eg. a flag:
131 (extents[LEFT][RIGHT] - left_head_wid[RIGHT]) / 2))
134 What is sticking out on the left side of the right note:
136 + (extents[RIGHT].is_empty ()
138 : ((- extents[RIGHT][LEFT] / 2)
141 Add that which sticks out a lot.
143 + max (0.0, -extents[RIGHT][LEFT] - (base_space - 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 if (!extents[RIGHT].is_empty ()
156 && (Item::is_breakable (right_col)
157 || right_col->original_))
160 This is for the situation
164 Since we only take half of the right-object space above, the
165 barline will bump into the notes preceding it, if the right
166 thing is big. We add the rest of the extents here:
169 *space += -extents[RIGHT][LEFT] / 2;
170 *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)
238 Correct for optical illusions. See [Wanske] p. 138. The combination
239 up-stem + down-stem should get extra space, the combination
240 down-stem + up-stem less.
242 TODO: have to check wether the stems are in the same staff.
245 Note_spacing::stem_dir_correction (Grob *me, Item *rcolumn,
247 Real *space, Real *fixed)
249 Drul_array<Direction> stem_dirs (CENTER, CENTER);
250 Drul_array<Interval> stem_posns;
251 Drul_array<Interval> head_posns;
252 Drul_array<SCM> props (me->get_object ("left-items"),
253 me->get_object ("right-items"));
255 Drul_array<Spanner *> beams_drul (0, 0);
256 Drul_array<Grob *> stems_drul (0, 0);
258 stem_dirs[LEFT] = stem_dirs[RIGHT] = CENTER;
260 Interval bar_xextent;
261 Interval bar_yextent;
263 bool correct_stem_dirs = true;
265 bool acc_right = false;
269 Link_array<Grob> const &items (ly_scm2link_array (props [d]));
270 for (int i = 0; i < items.size(); i++)
272 Item *it = dynamic_cast<Item *> (items[i]);
275 acc_right = acc_right || Note_column::accidentals (it);
277 Grob *stem = Note_column::get_stem (it);
279 if (!stem || !stem->is_live ())
281 if (d == RIGHT && Separation_item::has_interface (it))
283 if (it->get_column () != rcolumn)
285 it = it->find_prebroken_piece (rcolumn->break_status_dir ());
288 Grob *last = Separation_item::extremal_break_aligned_grob (it, LEFT, &bar_xextent);
291 bar_yextent = Staff_spacing::bar_y_positions (last);
299 if (Stem::is_invisible (stem))
301 correct_stem_dirs = false;
305 stems_drul[d] = stem;
306 beams_drul[d] = Stem::get_beam (stem);
308 Direction sd = Stem::get_direction (stem);
309 if (stem_dirs[d] && stem_dirs[d] != sd)
311 correct_stem_dirs = false;
317 Correction doesn't seem appropriate when there is a large flag
318 hanging from the note.
321 && Stem::duration_log (stem) > 2 && !Stem::get_beam (stem))
323 correct_stem_dirs = false;
326 Interval hp = Stem::head_positions (stem);
329 Real chord_start = hp[sd];
330 Real stem_end = Stem::stem_end_position (stem);
332 stem_posns[d] = Interval (min (chord_start, stem_end), max (chord_start, stem_end));
333 head_posns[d].unite (hp);
337 while (flip (&d) != LEFT);
340 don't correct if accidentals are sticking out of the right side.
345 Real correction = 0.0;
347 if (!bar_yextent.is_empty ())
349 stem_dirs[RIGHT] = -stem_dirs[LEFT];
350 stem_posns[RIGHT] = bar_yextent;
353 if (correct_stem_dirs && stem_dirs[LEFT] * stem_dirs[RIGHT] == -1)
355 if (beams_drul[LEFT] && beams_drul[LEFT] == beams_drul[RIGHT])
359 this is a knee: maximal correction.
361 Real note_head_width = increment;
362 Grob *st = stems_drul[RIGHT];
363 Grob *head = st ? Stem::support_head (st) : 0;
365 Interval head_extent;
368 head_extent = head->extent (rcolumn, X_AXIS);
370 if (!head_extent.is_empty ())
371 note_head_width = head_extent[RIGHT];
375 Real thick = Stem::thickness (st);
377 note_head_width -= thick;
381 correction = note_head_width * stem_dirs[LEFT];
382 correction *= robust_scm2double (me->get_property ("knee-spacing-correction"), 0);
383 *fixed += correction;
387 intersect = stem_posns[LEFT];
388 intersect.intersect (stem_posns[RIGHT]);
389 correct_stem_dirs = correct_stem_dirs && !intersect.is_empty ();
391 if (correct_stem_dirs)
393 correction = abs (intersect.length ());
398 correction = min (correction / 7, 1.0);
399 correction *= stem_dirs[LEFT];
401 *= robust_scm2double (me->get_property ("stem-spacing-correction"), 0);
404 if (!bar_yextent.is_empty ())
410 else if (correct_stem_dirs && stem_dirs[LEFT] * stem_dirs[RIGHT] == UP)
413 Correct for the following situation:
422 ^ move the center one to the left.
425 this effect seems to be much more subtle than the
426 stem-direction stuff (why?), and also does not scale with the
427 difference in stem length.
431 Interval hp = head_posns[LEFT];
432 hp.intersect (head_posns[RIGHT]);
437 = (head_posns[LEFT][DOWN] > head_posns[RIGHT][UP]) ? RIGHT : LEFT;
439 Real delta = head_posns[-lowest][DOWN] - head_posns[lowest][UP];
440 Real corr = robust_scm2double (me->get_property ("same-direction-correction"), 0);
443 correction = -lowest * corr;
446 *space += correction;
448 /* there used to be a correction for bar_xextent () here, but
449 it's unclear what that was good for ?
453 ADD_INTERFACE (Note_spacing, "note-spacing-interface",
454 "This object calculates spacing wishes for individual voices.",
455 "left-items right-items stem-spacing-correction same-direction-correction knee-spacing-correction");