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);
49 some kind of mismatch, eg. a note column, that is behind a
55 Item *it_col = it->get_column ();
56 if (d == RIGHT && right_col != it_col)
59 if (Separation_item::has_interface (it))
61 extents[d].unite (Separation_item::width (it));
67 SCM r = it->get_property ("rest");
68 Grob *g = unsmob_grob (r);
70 g = Note_column::first_head (it);
73 Ugh. If Stem is switched off, we don't know what the
74 first note head will be.
77 left_head_wid = g->extent (it_col, X_AXIS);
80 extents[d].unite (it->extent (it_col, X_AXIS));
83 Grob *accs = Note_column::accidentals (it);
85 accs = Note_column::accidentals (it->get_parent (X_AXIS));
90 = Accidental_placement::get_relevant_accidental_extent (accs, it_col, me);
97 if (extents[d].is_empty ())
98 extents[d] = Interval (0, 0);
100 while (flip (&d) != LEFT);
103 We look at the width of the note head, since smaller heads get less space
104 eg. a quarter rest gets almost 0.5 ss less horizontal space than a note.
106 What is sticking out of the note head (eg. a flag), doesn't get
107 the full amount of space.
109 FIXED also includes the left part of the right object.
112 = (left_head_wid.is_empty () ? increment
116 (left_head_wid[RIGHT]+
119 What's sticking out of the head, eg. a flag:
121 (extents[LEFT][RIGHT] - left_head_wid[RIGHT]) / 2))
124 What is sticking out of the right note:
126 + (extents[RIGHT].is_empty () ? 0.0 : - extents[RIGHT][LEFT] / 2);
129 We don't do complicated stuff: (base_space - increment) is the
130 normal amount of white, which also determines the amount of
131 stretch. Upon (extreme) stretching, notes with accidentals should
132 stretch as much as notes without accidentals.
134 *space = (base_space - increment) + *fixed;
136 if (!extents[RIGHT].is_empty ()
137 && (Item::is_breakable (right_col)
138 || right_col->original_))
141 This is for the situation
145 Since we only take half of the right-object space above, the
146 barline will bump into the notes preceding it, if the right
147 thing is big. We add the rest of the extents here:
150 *space += -extents[RIGHT][LEFT] / 2;
151 *fixed += -extents[RIGHT][LEFT] / 2;
154 stem_dir_correction (me, right_col, increment, space, fixed);
158 Note_spacing::left_column (Grob *me)
163 return dynamic_cast<Item *> (me)->get_column ();
167 Compute the column of the right-items. This is a big function,
168 since RIGHT-ITEMS may span more columns (eg. if a clef if inserted,
169 this will add a new columns to RIGHT-ITEMS. Here we look at the
170 columns, and return the left-most. If there are multiple columns, we
174 Note_spacing::right_column (Grob *me)
179 SCM right = me->get_property ("right-items");
181 int min_rank = INT_MAX;
183 for (SCM s = right; scm_is_pair (s); s = scm_cdr (s))
185 Item *ri = unsmob_item (scm_car (s));
187 Item *col = ri->get_column ();
188 int rank = Paper_column::get_rank (col);
202 // I'm a lazy bum. We could do this in-place.
203 SCM newright = SCM_EOL;
204 for (SCM s = right; scm_is_pair (s); s = scm_cdr (s))
206 if (unsmob_item (scm_car (s))->get_column () == mincol)
207 newright = scm_cons (scm_car (s), newright);
210 me->set_property ("right-items", newright);
216 int r = Paper_column::get_rank (dynamic_cast<Item*>(me)->get_column ());
217 programming_error (_f ("Spacing wish column %d has no right item.", r));
227 Correct for optical illusions. See [Wanske] p. 138. The combination
228 up-stem + down-stem should get extra space, the combination
229 down-stem + up-stem less.
231 TODO: have to check wether the stems are in the same staff.
234 Note_spacing::stem_dir_correction (Grob *me, Item *rcolumn,
236 Real *space, Real *fixed)
238 Drul_array<Direction> stem_dirs (CENTER, CENTER);
239 Drul_array<Interval> stem_posns;
240 Drul_array<Interval> head_posns;
241 Drul_array<SCM> props (me->get_property ("left-items"),
242 me->get_property ("right-items"));
244 Drul_array<Spanner *> beams_drul (0, 0);
245 Drul_array<Grob *> stems_drul (0, 0);
247 stem_dirs[LEFT] = stem_dirs[RIGHT] = CENTER;
249 Interval bar_xextent;
250 Interval bar_yextent;
252 bool correct_stem_dirs = true;
254 bool acc_right = false;
258 for (SCM s = props[d]; scm_is_pair (s); s = scm_cdr (s))
260 Item *it = dynamic_cast<Item *> (unsmob_grob (scm_car (s)));
263 acc_right = acc_right || Note_column::accidentals (it);
265 Grob *stem = Note_column::get_stem (it);
267 if (!stem || !stem->is_live ())
269 if (d == RIGHT && Separation_item::has_interface (it))
271 if (it->get_column () != rcolumn)
273 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))
311 correct_stem_dirs = false;
314 Interval hp = Stem::head_positions (stem);
317 Real chord_start = hp[sd];
318 Real stem_end = Stem::stem_end_position (stem);
320 stem_posns[d] = Interval (min (chord_start, stem_end), max (chord_start, stem_end));
321 head_posns[d].unite (hp);
325 while (flip (&d) != LEFT);
328 don't correct if accidentals are sticking out of the right side.
333 Real correction = 0.0;
335 if (!bar_yextent.is_empty ())
337 stem_dirs[RIGHT] = -stem_dirs[LEFT];
338 stem_posns[RIGHT] = bar_yextent;
341 if (correct_stem_dirs && stem_dirs[LEFT] * stem_dirs[RIGHT] == -1)
343 if (beams_drul[LEFT] && beams_drul[LEFT] == beams_drul[RIGHT])
347 this is a knee: maximal correction.
349 Real note_head_width = increment;
350 Grob *st = stems_drul[RIGHT];
351 Grob *head = st ? Stem::support_head (st) : 0;
353 Interval head_extent;
356 head_extent = head->extent (rcolumn, X_AXIS);
358 if (!head_extent.is_empty ())
359 note_head_width = head_extent[RIGHT];
363 Real thick = Stem::thickness (st);
365 note_head_width -= thick;
369 correction = note_head_width * stem_dirs[LEFT];
370 correction *= robust_scm2double (me->get_property ("knee-spacing-correction"), 0);
371 *fixed += correction;
375 intersect = stem_posns[LEFT];
376 intersect.intersect (stem_posns[RIGHT]);
377 correct_stem_dirs = correct_stem_dirs && !intersect.is_empty ();
379 if (correct_stem_dirs)
381 correction = abs (intersect.length ());
386 correction = min (correction / 7, 1.0);
387 correction *= stem_dirs[LEFT];
389 *= robust_scm2double (me->get_property ("stem-spacing-correction"), 0);
392 if (!bar_yextent.is_empty ())
398 else if (correct_stem_dirs && stem_dirs[LEFT] * stem_dirs[RIGHT] == UP)
401 Correct for the following situation:
410 ^ move the center one to the left.
413 this effect seems to be much more subtle than the
414 stem-direction stuff (why?), and also does not scale with the
415 difference in stem length.
419 Interval hp = head_posns[LEFT];
420 hp.intersect (head_posns[RIGHT]);
425 = (head_posns[LEFT][DOWN] > head_posns[RIGHT][UP]) ? RIGHT : LEFT;
427 Real delta = head_posns[-lowest][DOWN] - head_posns[lowest][UP];
428 Real corr = robust_scm2double (me->get_property ("same-direction-correction"), 0);
431 correction = -lowest * corr;
434 *space += correction;
436 /* there used to be a correction for bar_xextent () here, but
437 it's unclear what that was good for ?
441 ADD_INTERFACE (Note_spacing, "note-spacing-interface",
442 "This object calculates spacing wishes for individual voices.",
443 "left-items right-items stem-spacing-correction same-direction-correction knee-spacing-correction");