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"
24 TODO: detect hshifts due to collisions, and account for them in
29 Note_spacing::get_spacing (Grob *me, Item *right_col,
30 Real base_space, Real increment, Real *space, Real *fixed)
32 Drul_array<SCM> props (me->get_object ("left-items"),
33 me->get_object ("right-items"));
35 Direction col_dir = right_col->break_status_dir ();
36 Drul_array<Interval> extents;
38 Interval left_head_wid;
41 Link_array<Grob> const &items (ly_scm2link_array (props [d]));
42 for (int i = items.size (); i--;)
44 Item *it = dynamic_cast<Item *> (items[i]);
46 if (d == RIGHT && it->break_status_dir () != col_dir)
47 it = it->find_prebroken_piece (col_dir);
50 some kind of mismatch, eg. a note column, that is behind a
56 Item *it_col = it->get_column ();
57 if (d == RIGHT && right_col != it_col)
60 if (Separation_item::has_interface (it))
62 extents[d].unite (Separation_item::width (it));
68 SCM r = it->get_object ("rest");
69 Grob *g = unsmob_grob (r);
71 g = Note_column::first_head (it);
74 Ugh. If Stem is switched off, we don't know what the
75 first note head will be.
78 left_head_wid = g->extent (it_col, X_AXIS);
81 extents[d].unite (it->extent (it_col, X_AXIS));
84 Grob *accs = Note_column::accidentals (it);
86 accs = Note_column::accidentals (it->get_parent (X_AXIS));
91 = Accidental_placement::get_relevant_accidental_extent (accs, it_col, me);
96 if (Grob *arpeggio = Note_column::arpeggio (it))
97 extents[d].unite (arpeggio->extent (it_col, X_AXIS));
101 if (extents[d].is_empty ())
102 extents[d] = Interval (0, 0);
104 while (flip (&d) != LEFT);
107 We look at the width of the note head, since smaller heads get less space
108 eg. a quarter rest gets almost 0.5 ss less horizontal space than a note.
110 What is sticking out of the note head (eg. a flag), doesn't get
111 the full amount of space.
113 FIXED also includes the left part of the right object.
116 = (left_head_wid.is_empty () ? increment
120 (left_head_wid[RIGHT]+
123 What's sticking out of the head, eg. a flag:
125 (extents[LEFT][RIGHT] - left_head_wid[RIGHT]) / 2))
128 What is sticking out on the left side of the right note:
130 + (extents[RIGHT].is_empty ()
132 : ((- extents[RIGHT][LEFT] / 2)
135 Add that which sticks out a lot.
137 + max (0.0, -extents[RIGHT][LEFT] - (base_space - increment))));
140 We don't do complicated stuff: (base_space - increment) is the
141 normal amount of white, which also determines the amount of
142 stretch. Upon (extreme) stretching, notes with accidentals should
143 stretch as much as notes without accidentals.
145 *space = (base_space - increment) + *fixed;
147 if (!extents[RIGHT].is_empty ()
148 && (Item::is_breakable (right_col)
149 || right_col->original_))
152 This is for the situation
156 Since we only take half of the right-object space above, the
157 barline will bump into the notes preceding it, if the right
158 thing is big. We add the rest of the extents here:
161 *space += -extents[RIGHT][LEFT] / 2;
162 *fixed += -extents[RIGHT][LEFT] / 2;
165 stem_dir_correction (me, right_col, increment, space, fixed);
169 Note_spacing::left_column (Grob *me)
174 return dynamic_cast<Item *> (me)->get_column ();
178 Compute the column of the right-items. This is a big function,
179 since RIGHT-ITEMS may span more columns (eg. if a clef if inserted,
180 this will add a new columns to RIGHT-ITEMS. Here we look at the
181 columns, and return the left-most. If there are multiple columns, we
185 Note_spacing::right_column (Grob *me)
190 Grob_array *a = unsmob_grob_array (me->get_object ("right-items"));
192 int min_rank = INT_MAX;
194 for (int i = 0; a && i < a->size (); i++)
196 Item *ri = a->item (i);
197 Item *col = ri->get_column ();
199 int rank = Paper_column::get_rank (col);
213 Link_array<Grob> &right = a->array_reference ();
214 for (int i = right.size (); i--;)
216 if (dynamic_cast<Item *> (right[i])->get_column () != mincol)
228 Correct for optical illusions. See [Wanske] p. 138. The combination
229 up-stem + down-stem should get extra space, the combination
230 down-stem + up-stem less.
232 TODO: have to check wether the stems are in the same staff.
235 Note_spacing::stem_dir_correction (Grob *me, Item *rcolumn,
237 Real *space, Real *fixed)
239 Drul_array<Direction> stem_dirs (CENTER, CENTER);
240 Drul_array<Interval> stem_posns;
241 Drul_array<Interval> head_posns;
242 Drul_array<SCM> props (me->get_object ("left-items"),
243 me->get_object ("right-items"));
245 Drul_array<Spanner *> beams_drul (0, 0);
246 Drul_array<Grob *> stems_drul (0, 0);
248 stem_dirs[LEFT] = stem_dirs[RIGHT] = CENTER;
250 Interval bar_xextent;
251 Interval bar_yextent;
253 bool correct_stem_dirs = true;
255 bool acc_right = false;
259 Link_array<Grob> const &items (ly_scm2link_array (props [d]));
260 for (int i = 0; i < items.size (); i++)
262 Item *it = dynamic_cast<Item *> (items[i]);
265 acc_right = acc_right || Note_column::accidentals (it);
267 Grob *stem = Note_column::get_stem (it);
269 if (!stem || !stem->is_live ())
271 if (d == RIGHT && Separation_item::has_interface (it))
273 if (it->get_column () != rcolumn)
274 it = it->find_prebroken_piece (rcolumn->break_status_dir ());
276 Grob *last = Separation_item::extremal_break_aligned_grob (it, LEFT, &bar_xextent);
279 bar_yextent = Staff_spacing::bar_y_positions (last);
287 if (Stem::is_invisible (stem))
289 correct_stem_dirs = false;
293 stems_drul[d] = stem;
294 beams_drul[d] = Stem::get_beam (stem);
296 Direction sd = Stem::get_direction (stem);
297 if (stem_dirs[d] && stem_dirs[d] != sd)
299 correct_stem_dirs = false;
305 Correction doesn't seem appropriate when there is a large flag
306 hanging from the note.
309 && Stem::duration_log (stem) > 2 && !Stem::get_beam (stem))
310 correct_stem_dirs = false;
312 Interval hp = Stem::head_positions (stem);
315 Real chord_start = hp[sd];
316 Real stem_end = Stem::stem_end_position (stem);
318 stem_posns[d] = Interval (min (chord_start, stem_end), max (chord_start, stem_end));
319 head_posns[d].unite (hp);
323 while (flip (&d) != LEFT);
326 don't correct if accidentals are sticking out of the right side.
331 Real correction = 0.0;
333 if (!bar_yextent.is_empty ())
335 stem_dirs[RIGHT] = -stem_dirs[LEFT];
336 stem_posns[RIGHT] = bar_yextent;
339 if (correct_stem_dirs && stem_dirs[LEFT] * stem_dirs[RIGHT] == -1)
341 if (beams_drul[LEFT] && beams_drul[LEFT] == beams_drul[RIGHT])
345 this is a knee: maximal correction.
347 Real note_head_width = increment;
348 Grob *st = stems_drul[RIGHT];
349 Grob *head = st ? Stem::support_head (st) : 0;
351 Interval head_extent;
354 head_extent = head->extent (rcolumn, X_AXIS);
356 if (!head_extent.is_empty ())
357 note_head_width = head_extent[RIGHT];
361 Real thick = Stem::thickness (st);
363 note_head_width -= thick;
367 correction = note_head_width * stem_dirs[LEFT];
368 correction *= robust_scm2double (me->get_property ("knee-spacing-correction"), 0);
369 *fixed += correction;
373 intersect = stem_posns[LEFT];
374 intersect.intersect (stem_posns[RIGHT]);
375 correct_stem_dirs = correct_stem_dirs && !intersect.is_empty ();
377 if (correct_stem_dirs)
379 correction = abs (intersect.length ());
384 correction = min (correction / 7, 1.0);
385 correction *= stem_dirs[LEFT];
387 *= robust_scm2double (me->get_property ("stem-spacing-correction"), 0);
390 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");