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 "paper-column.hh"
13 #include "note-column.hh"
16 #include "separation-item.hh"
17 #include "staff-spacing.hh"
18 #include "accidental-placement.hh"
19 #include "output-def.hh"
22 TODO: detect hshifts due to collisions, and account for them in
27 Note_spacing::get_spacing (Grob *me, Item *right_col,
28 Real base_space, Real increment, Real *space, Real *fixed)
31 Drul_array<SCM> props (me->get_property ("left-items"),
32 me->get_property ("right-items"));
34 Direction col_dir = right_col->break_status_dir ();
35 Drul_array<Interval> extents;
37 Interval left_head_wid;
40 for (SCM s = props[d]; scm_is_pair (s); s = scm_cdr (s))
42 Item *it = dynamic_cast<Item *> (unsmob_grob (scm_car (s)));
44 if (d == RIGHT && it->break_status_dir () != col_dir)
46 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_property ("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);
98 if (extents[d].is_empty ())
99 extents[d] = Interval (0, 0);
101 while (flip (&d) != LEFT);
104 We look at the width of the note head, since smaller heads get less space
105 eg. a quarter rest gets almost 0.5 ss less horizontal space than a note.
107 What is sticking out of the note head (eg. a flag), doesn't get
108 the full amount of space.
110 FIXED also includes the left part of the right object.
113 = (left_head_wid.is_empty () ? increment :
117 (left_head_wid[RIGHT]+
120 What's sticking out of the head, eg. a flag:
122 (extents[LEFT][RIGHT] - left_head_wid[RIGHT]) / 2))
125 What is sticking out of the right note:
127 + (extents[RIGHT].is_empty () ? 0.0 : - extents[RIGHT][LEFT] / 2);
130 We don't do complicated stuff: (base_space - increment) is the
131 normal amount of white, which also determines the amount of
132 stretch. Upon (extreme) stretching, notes with accidentals should
133 stretch as much as notes without accidentals.
135 *space = (base_space - increment) + *fixed;
137 if (!extents[RIGHT].is_empty ()
138 && (Item::is_breakable (right_col)
139 || right_col->original_))
142 This is for the situation
146 Since we only take half of the right-object space above, the
147 barline will bump into the notes preceding it, if the right
148 thing is big. We add the rest of the extents here:
151 *space += -extents[RIGHT][LEFT] / 2;
152 *fixed += -extents[RIGHT][LEFT] / 2;
155 stem_dir_correction (me, right_col, increment, space, fixed);
159 Note_spacing::left_column (Grob *me)
164 return dynamic_cast<Item *> (me)->get_column ();
168 Compute the column of the right-items. This is a big function,
169 since RIGHT-ITEMS may span more columns (eg. if a clef if inserted,
170 this will add a new columns to RIGHT-ITEMS. Here we look at the
171 columns, and return the left-most. If there are multiple columns, we
175 Note_spacing::right_column (Grob *me)
180 SCM right = me->get_property ("right-items");
182 int min_rank = INT_MAX;
184 for (SCM s = right; scm_is_pair (s); s = scm_cdr (s))
186 Item *ri = unsmob_item (scm_car (s));
188 Item *col = ri->get_column ();
189 int rank = Paper_column::get_rank (col);
203 // I'm a lazy bum. We could do this in-place.
204 SCM newright = SCM_EOL;
205 for (SCM s = right; scm_is_pair (s); s = scm_cdr (s))
207 if (unsmob_item (scm_car (s))->get_column () == mincol)
208 newright = scm_cons (scm_car (s), newright);
211 me->set_property ("right-items", newright);
217 int r = Paper_column::get_rank (dynamic_cast<Item*>(me)->get_column ());
218 programming_error (_f ("Spacing wish column %d has no right item.", r));
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_property ("left-items"),
243 me->get_property ("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 for (SCM s = props[d]; scm_is_pair (s); s = scm_cdr (s))
261 Item *it = dynamic_cast<Item *> (unsmob_grob (scm_car (s)));
264 acc_right = acc_right || Note_column::accidentals (it);
266 Grob *stem = Note_column::get_stem (it);
268 if (!stem || !stem->is_live ())
270 if (d == RIGHT && Separation_item::has_interface (it))
272 if (it->get_column () != rcolumn)
274 it = it->find_prebroken_piece (rcolumn->break_status_dir ());
277 Grob *last = Separation_item::extremal_break_aligned_grob (it, LEFT, &bar_xextent);
280 bar_yextent = Staff_spacing::bar_y_positions (last);
288 if (Stem::is_invisible (stem))
290 correct_stem_dirs = false;
294 stems_drul[d] = stem;
295 beams_drul[d] = Stem::get_beam (stem);
297 Direction sd = Stem::get_direction (stem);
298 if (stem_dirs[d] && stem_dirs[d] != sd)
300 correct_stem_dirs = false;
306 Correction doesn't seem appropriate when there is a large flag
307 hanging from the note.
310 && Stem::duration_log (stem) > 2 && !Stem::get_beam (stem))
312 correct_stem_dirs = false;
315 Interval hp = Stem::head_positions (stem);
318 Real chord_start = hp[sd];
319 Real stem_end = Stem::stem_end_position (stem);
321 stem_posns[d] = Interval (chord_start <? stem_end, chord_start>? stem_end);
322 head_posns[d].unite (hp);
326 while (flip (&d) != LEFT);
329 don't correct if accidentals are sticking out of the right side.
334 Real correction = 0.0;
336 if (!bar_yextent.is_empty ())
338 stem_dirs[RIGHT] = -stem_dirs[LEFT];
339 stem_posns[RIGHT] = bar_yextent;
342 if (correct_stem_dirs && stem_dirs[LEFT] *stem_dirs[RIGHT] == -1)
344 if (beams_drul[LEFT] && beams_drul[LEFT] == beams_drul[RIGHT])
348 this is a knee: maximal correction.
350 Real note_head_width = increment;
351 Grob *st = stems_drul[RIGHT];
352 Grob *head = st ? Stem::support_head (st) : 0;
354 Interval head_extent;
357 head_extent = head->extent (rcolumn, X_AXIS);
359 if (!head_extent.is_empty ())
360 note_head_width = head_extent[RIGHT];
364 Real thick = Stem::thickness (st);
366 note_head_width -= thick;
370 correction = note_head_width* stem_dirs[LEFT];
371 correction *= robust_scm2double (me->get_property ("knee-spacing-correction"), 0);
372 *fixed += correction;
376 intersect = stem_posns[LEFT];
377 intersect.intersect (stem_posns[RIGHT]);
378 correct_stem_dirs = correct_stem_dirs && !intersect.is_empty ();
380 if (correct_stem_dirs)
382 correction = abs (intersect.length ());
387 correction = (correction / 7) <? 1.0;
388 correction *= stem_dirs[LEFT];
390 *= robust_scm2double (me->get_property ("stem-spacing-correction"), 0);
393 if (!bar_yextent.is_empty ())
399 else if (correct_stem_dirs && stem_dirs[LEFT] *stem_dirs[RIGHT] == UP)
402 Correct for the following situation:
411 ^ move the center one to the left.
414 this effect seems to be much more subtle than the
415 stem-direction stuff (why?), and also does not scale with the
416 difference in stem length.
420 Interval hp = head_posns[LEFT];
421 hp.intersect (head_posns[RIGHT]);
426 = (head_posns[LEFT][DOWN] > head_posns[RIGHT][UP]) ? RIGHT : LEFT;
428 Real delta = head_posns[-lowest][DOWN] - head_posns[lowest][UP];
429 Real corr = robust_scm2double (me->get_property ("stem-spacing-correction"), 0);
430 corr = (delta <= 1) ? 0.0 : 0.25;
432 correction = -lowest * corr;
435 *space += correction;
437 /* there used to be a correction for bar_xextent () here, but
438 it's unclear what that was good for ?
444 ADD_INTERFACE (Note_spacing, "note-spacing-interface",
445 "This object calculates spacing wishes for individual voices.",
446 "left-items right-items stem-spacing-correction knee-spacing-correction");