2 note-spacing.cc -- implement Note_spacing
4 source file of the GNU LilyPond music typesetter
6 (c) 2001--2006 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));
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.
80 if (g->common_refpoint (it_col, X_AXIS) != it_col)
81 programming_error ("Note_spacing::get_spacing(): Common refpoint incorrect");
83 left_head_wid = g->extent (it_col, X_AXIS);
87 extents[d].unite (it->extent (it_col, X_AXIS));
90 Grob *accs = Note_column::accidentals (it);
92 accs = Note_column::accidentals (it->get_parent (X_AXIS));
97 = Accidental_placement::get_relevant_accidental_extent (accs, it_col, me);
102 if (Grob *arpeggio = Note_column::arpeggio (it))
103 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))));
146 We don't do complicated stuff: (base_space - increment) is the
147 normal amount of white, which also determines the amount of
148 stretch. Upon (extreme) stretching, notes with accidentals should
149 stretch as much as notes without accidentals.
151 *space = (base_space - increment) + *fixed;
155 The below situation is now handled by the "sticks out a lot" case
156 above. However we keep around the code for a few releases before
159 if (!extents[RIGHT].is_empty ()
160 && (Item::is_breakable (right_col)
161 || right_col->original ()))
164 This is for the situation
168 Since we only take half of the right-object space above, the
169 barline will bump into the notes preceding it, if the right
170 thing is big. We add the rest of the extents here:
173 *space += -extents[RIGHT][LEFT] / 2;
174 *fixed += -extents[RIGHT][LEFT] / 2;
178 stem_dir_correction (me, right_col, increment, space, fixed);
182 Note_spacing::left_column (Grob *me)
187 return dynamic_cast<Item *> (me)->get_column ();
191 Compute the column of the right-items. This is a big function,
192 since RIGHT-ITEMS may span more columns (eg. if a clef if inserted,
193 this will add a new columns to RIGHT-ITEMS. Here we look at the
194 columns, and return the left-most. If there are multiple columns, we
198 Note_spacing::right_column (Grob *me)
203 Grob_array *a = unsmob_grob_array (me->get_object ("right-items"));
205 int min_rank = INT_MAX;
207 for (vsize i = 0; a && i < a->size (); i++)
209 Item *ri = a->item (i);
210 Item *col = ri->get_column ();
212 int rank = Paper_column::get_rank (col);
226 vector<Grob*> &right = a->array_reference ();
227 for (vsize i = right.size (); i--;)
229 if (dynamic_cast<Item *> (right[i])->get_column () != mincol)
230 right.erase (right.begin () + i);
241 Correct for optical illusions. See [Wanske] p. 138. The combination
242 up-stem + down-stem should get extra space, the combination
243 down-stem + up-stem less.
245 TODO: have to check wether the stems are in the same staff.
248 Note_spacing::stem_dir_correction (Grob *me, Item *rcolumn,
250 Real *space, Real *fixed)
252 Drul_array<Direction> stem_dirs (CENTER, CENTER);
253 Drul_array<Interval> stem_posns;
254 Drul_array<Interval> head_posns;
255 Drul_array<SCM> props (me->get_object ("left-items"),
256 me->get_object ("right-items"));
258 Drul_array<Spanner *> beams_drul (0, 0);
259 Drul_array<Grob *> stems_drul (0, 0);
261 stem_dirs[LEFT] = stem_dirs[RIGHT] = CENTER;
263 Interval bar_xextent;
264 Interval bar_yextent;
266 bool correct_stem_dirs = true;
268 bool acc_right = false;
272 vector<Grob*> const &items (ly_scm2link_array (props [d]));
273 for (vsize i = 0; i < items.size (); i++)
275 Item *it = dynamic_cast<Item *> (items[i]);
278 acc_right = acc_right || Note_column::accidentals (it);
280 Grob *stem = Note_column::get_stem (it);
282 if (!stem || !stem->is_live ())
284 if (d == RIGHT && Separation_item::has_interface (it))
286 if (it->get_column () != rcolumn)
287 it = it->find_prebroken_piece (rcolumn->break_status_dir ());
289 Grob *last = Separation_item::extremal_break_aligned_grob (it, LEFT, &bar_xextent);
292 bar_yextent = Staff_spacing::bar_y_positions (last);
300 if (Stem::is_invisible (stem))
302 correct_stem_dirs = false;
306 stems_drul[d] = stem;
307 beams_drul[d] = Stem::get_beam (stem);
309 Direction sd = get_grob_direction (stem);
310 if (stem_dirs[d] && stem_dirs[d] != sd)
312 correct_stem_dirs = false;
318 Correction doesn't seem appropriate when there is a large flag
319 hanging from the note.
322 && Stem::duration_log (stem) > 2 && !Stem::get_beam (stem))
323 correct_stem_dirs = false;
325 Interval hp = Stem::head_positions (stem);
328 Real chord_start = hp[sd];
329 Real stem_end = Stem::stem_end_position (stem);
331 stem_posns[d] = Interval (min (chord_start, stem_end), max (chord_start, stem_end));
332 head_posns[d].unite (hp);
336 while (flip (&d) != LEFT);
339 don't correct if accidentals are sticking out of the right side.
344 Real correction = 0.0;
346 if (!bar_yextent.is_empty ())
348 stem_dirs[RIGHT] = -stem_dirs[LEFT];
349 stem_posns[RIGHT] = bar_yextent;
352 if (correct_stem_dirs && stem_dirs[LEFT] * stem_dirs[RIGHT] == -1)
354 if (beams_drul[LEFT] && beams_drul[LEFT] == beams_drul[RIGHT])
358 this is a knee: maximal correction.
360 Real note_head_width = increment;
361 Grob *st = stems_drul[RIGHT];
362 Grob *head = st ? Stem::support_head (st) : 0;
364 Interval head_extent;
367 head_extent = head->extent (rcolumn, X_AXIS);
369 if (!head_extent.is_empty ())
370 note_head_width = head_extent[RIGHT];
374 Real thick = Stem::thickness (st);
376 note_head_width -= thick;
380 correction = note_head_width * stem_dirs[LEFT];
381 correction *= robust_scm2double (me->get_property ("knee-spacing-correction"), 0);
382 *fixed += correction;
386 intersect = stem_posns[LEFT];
387 intersect.intersect (stem_posns[RIGHT]);
388 correct_stem_dirs = correct_stem_dirs && !intersect.is_empty ();
390 if (correct_stem_dirs)
392 correction = abs (intersect.length ());
397 correction = min (correction / 7, 1.0);
398 correction *= stem_dirs[LEFT];
400 *= robust_scm2double (me->get_property ("stem-spacing-correction"), 0);
403 if (!bar_yextent.is_empty ())
407 else if (correct_stem_dirs && stem_dirs[LEFT] * stem_dirs[RIGHT] == UP)
410 Correct for the following situation:
419 ^ move the center one to the left.
422 this effect seems to be much more subtle than the
423 stem-direction stuff (why?), and also does not scale with the
424 difference in stem length.
428 Interval hp = head_posns[LEFT];
429 hp.intersect (head_posns[RIGHT]);
434 = (head_posns[LEFT][DOWN] > head_posns[RIGHT][UP]) ? RIGHT : LEFT;
436 Real delta = head_posns[-lowest][DOWN] - head_posns[lowest][UP];
437 Real corr = robust_scm2double (me->get_property ("same-direction-correction"), 0);
440 correction = -lowest * corr;
443 *space += correction;
445 /* there used to be a correction for bar_xextent () here, but
446 it's unclear what that was good for ?
450 ADD_INTERFACE (Note_spacing, "note-spacing-interface",
451 "This object calculates spacing wishes for individual voices.",
452 "left-items right-items stem-spacing-correction same-direction-correction knee-spacing-correction");