2 note-spacing.cc -- implement Note_spacing
4 source file of the GNU LilyPond music typesetter
6 (c) 2001--2004 Han-Wen Nienhuys <hanwen@cs.uu.nl>
10 #include "paper-column.hh"
13 #include "note-spacing.hh"
15 #include "note-column.hh"
18 #include "separation-item.hh"
19 #include "staff-spacing.hh"
20 #include "accidental-placement.hh"
21 #include "paper-def.hh"
26 TODO: detect hshifts due to collisions, and account for them in
32 Note_spacing::get_spacing (Grob *me, Item* right_col,
33 Real base_space, Real increment, Real *space, Real *fixed)
36 Drul_array<SCM> props (me->get_property ("left-items"),
37 me->get_property ("right-items"));
39 Direction col_dir = right_col->break_status_dir ();
40 Drul_array<Interval> extents;
42 Interval left_head_wid;
45 for (SCM s = props[d]; gh_pair_p (s); s = gh_cdr (s))
47 Item * it= dynamic_cast<Item*> (unsmob_grob (gh_car (s)));
49 if (d == RIGHT && it->break_status_dir () != col_dir)
51 it = it -> find_prebroken_piece (col_dir);
55 some kind of mismatch, eg. a note column, that is behind a
61 Item *it_col = it->get_column ();
62 if (d == RIGHT && right_col != it_col)
65 if (Separation_item::has_interface (it))
67 extents[d].unite (Separation_item::width (it));
73 SCM r = it->get_property ("rest");
74 Grob * g = unsmob_grob (r);
76 g = Note_column::first_head (it);
79 Ugh. If Stem is switched off, we don't know what the
80 first note head will be.
83 left_head_wid = g->extent (it_col, X_AXIS);
86 extents[d].unite (it->extent (it_col, X_AXIS));
89 Grob * accs = Note_column::accidentals (it);
91 accs = Note_column::accidentals (it->get_parent (X_AXIS));
96 Accidental_placement::get_relevant_accidental_extent (accs, it_col, me);
103 if (extents[d].is_empty ())
104 extents[d] = Interval (0,0);
106 while (flip (&d) != LEFT);
110 We look at the width of the note head, since smaller heads get less space
111 eg. a quarter rest gets almost 0.5 ss less horizontal space than a note.
113 What is sticking out of the note head (eg. a flag), doesn't get
114 the full amount of space.
116 FIXED also includes the left part of the right object.
119 (left_head_wid.is_empty () ? increment :
123 (left_head_wid[RIGHT]+
126 What's sticking out of the head, eg. a flag:
128 (extents[LEFT][RIGHT] - left_head_wid[RIGHT])/2))
131 What is sticking out of the right note:
133 + (extents[RIGHT].is_empty () ? 0.0 : - extents[RIGHT][LEFT] / 2);
136 We don't do complicated stuff: (base_space - increment) is the
137 normal amount of white, which also determines the amount of
138 stretch. Upon (extreme) stretching, notes with accidentals should
139 stretch as much as notes without accidentals.
141 *space = (base_space - increment) + *fixed ;
143 if (!extents[RIGHT].is_empty ()
144 && (Item::is_breakable (right_col)
145 || right_col->original_))
148 This is for the situation
152 Since we only take half of the right-object space above, the
153 barline will bump into the notes preceding it, if the right
154 thing is big. We add the rest of the extents here:
157 *space += -extents[RIGHT][LEFT] / 2;
158 *fixed += -extents[RIGHT][LEFT] / 2;
161 stem_dir_correction (me, right_col, increment, space, fixed);
165 Note_spacing::left_column (Grob *me)
170 return dynamic_cast<Item*> (me)->get_column ();
174 Compute the column of the right-items. This is a big function,
175 since RIGHT-ITEMS may span more columns (eg. if a clef if inserted,
176 this will add a new columns to RIGHT-ITEMS. Here we look at the
177 columns, and return the left-most. If there are multiple columns, we
182 Note_spacing::right_column (Grob*me)
187 SCM right = me->get_property ("right-items");
189 int min_rank = INT_MAX;
191 for (SCM s = right ; gh_pair_p (s) ; s = gh_cdr (s))
193 Item * ri = unsmob_item (gh_car (s));
195 Item * col = ri->get_column ();
196 int rank = Paper_column::get_rank (col);
210 // I'm a lazy bum. We could do this in-place.
211 SCM newright = SCM_EOL;
212 for (SCM s = right ; gh_pair_p (s) ; s =gh_cdr (s))
214 if (unsmob_item (gh_car (s))->get_column () == mincol)
215 newright = gh_cons (gh_car (s), newright);
218 me->set_property ("right-items", newright);
224 int r = Paper_column::get_rank (dynamic_cast<Item*>(me)->get_column ());
225 programming_error (_f ("Spacing wish column %d has no right item.", r));
235 Correct for optical illusions. See [Wanske] p. 138. The combination
236 up-stem + down-stem should get extra space, the combination
237 down-stem + up-stem less.
239 TODO: have to check wether the stems are in the same staff.
243 Note_spacing::stem_dir_correction (Grob*me, Item * rcolumn,
245 Real * space, Real *fixed)
247 Drul_array<Direction> stem_dirs (CENTER,CENTER);
248 Drul_array<Interval> stem_posns;
249 Drul_array<Interval> head_posns;
250 Drul_array<SCM> props (me->get_property ("left-items"),
251 me->get_property ("right-items"));
253 Drul_array<Grob*> beams_drul (0,0);
254 Drul_array<Grob*> stems_drul (0,0);
256 stem_dirs[LEFT] = stem_dirs[RIGHT] = CENTER;
258 Interval bar_xextent;
259 Interval bar_yextent;
261 bool correct_stem_dirs = true;
263 bool acc_right = false;
267 for (SCM s = props[d]; gh_pair_p (s); s = gh_cdr (s))
269 Item * it= dynamic_cast<Item*> (unsmob_grob (gh_car (s)));
272 acc_right = acc_right || Note_column::accidentals (it);
274 Grob *stem = Note_column::get_stem (it);
276 if (!stem || !stem->live ())
278 if (d == RIGHT && Separation_item::has_interface (it))
280 if (it->get_column () != rcolumn)
282 it = it->find_prebroken_piece (rcolumn->break_status_dir ());
285 Grob *last = Separation_item::extremal_break_aligned_grob (it, LEFT, &bar_xextent);
288 bar_yextent = Staff_spacing::bar_y_positions (last);
296 if (Stem::is_invisible (stem))
298 correct_stem_dirs = false;
302 stems_drul[d] = stem;
303 beams_drul[d] = Stem::get_beam (stem);
306 Direction sd = Stem::get_direction (stem);
307 if (stem_dirs[d] && stem_dirs[d] != sd)
309 correct_stem_dirs = false;
315 Correction doesn't seem appropriate when there is a large flag
316 hanging from the note.
319 && Stem::duration_log (stem) > 2 && !Stem::get_beam (stem))
321 correct_stem_dirs = false;
324 Interval hp = Stem::head_positions (stem);
325 Real chord_start = hp[sd];
326 Real stem_end = Stem::stem_end_position (stem);
328 stem_posns[d] = Interval (chord_start<?stem_end, chord_start>? stem_end);
329 head_posns[d].unite (hp);
332 while (flip (&d) != LEFT);
336 don't correct if accidentals are sticking out of the right side.
341 Real correction = 0.0;
343 if (!bar_yextent.is_empty ())
345 stem_dirs[RIGHT] = - stem_dirs[LEFT];
346 stem_posns[RIGHT] = bar_yextent;
349 if (correct_stem_dirs && stem_dirs[LEFT] *stem_dirs[RIGHT] == -1)
351 if (beams_drul[LEFT] && beams_drul[LEFT] == beams_drul[RIGHT])
355 this is a knee: maximal correction.
357 Real note_head_width = increment;
358 Grob * st = stems_drul[RIGHT];
359 Grob * head = st ? Stem::support_head (st) : 0;
361 Interval head_extent;
364 head_extent = head->extent (rcolumn, X_AXIS);
366 if (!head_extent.is_empty ())
367 note_head_width = head_extent[RIGHT];
371 Real thick = Stem::thickness (st);
373 note_head_width -= thick;
377 correction = note_head_width* stem_dirs[LEFT];
378 correction *= robust_scm2double (me->get_property ("knee-spacing-correction"), 0);
379 *fixed += correction;
383 intersect = stem_posns[LEFT];
384 intersect.intersect (stem_posns[RIGHT]);
385 correct_stem_dirs = correct_stem_dirs && !intersect.is_empty ();
387 if (correct_stem_dirs)
389 correction =abs (intersect.length ());
395 correction = (correction/7) <? 1.0;
396 correction *= stem_dirs[LEFT] ;
398 robust_scm2double (me->get_property ("stem-spacing-correction"), 0);
401 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.
429 Interval hp = head_posns[LEFT];
430 hp.intersect (head_posns[RIGHT]);
435 (head_posns[LEFT][DOWN] > head_posns[RIGHT][UP]) ? RIGHT : LEFT;
437 Real delta = head_posns[-lowest][DOWN] - head_posns[lowest][UP] ;
438 Real corr = robust_scm2double (me->get_property ("stem-spacing-correction"), 0);
439 corr = (delta <= 1) ? 0.0 : 0.25;
441 correction= -lowest * corr ;
444 *space += correction;
446 /* there used to be a correction for bar_xextent () here, but
447 it's unclear what that was good for ?
455 ADD_INTERFACE (Note_spacing,"note-spacing-interface",
456 "This object calculates spacing wishes for individual voices.",
457 "left-items right-items stem-spacing-correction knee-spacing-correction");