2 note-spacing.cc -- implement Note_spacing
4 source file of the GNU LilyPond music typesetter
6 (c) 2001--2003 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"
23 Note_spacing::get_spacing (Grob *me, Item* right_col,
24 Real base_space, Real increment, Real *space, Real *fixed)
27 Drul_array<SCM> props(me->get_grob_property ("left-items"),
28 me->get_grob_property ("right-items"));
30 Direction col_dir = right_col->break_status_dir ();
31 Drul_array<Interval> extents;
33 Interval left_head_wid;
36 for (SCM s = props[d]; gh_pair_p (s); s = gh_cdr (s))
38 Item * it= dynamic_cast<Item*> (unsmob_grob (gh_car(s)));
40 if (d == RIGHT && it->break_status_dir () != col_dir)
42 it = it -> find_prebroken_piece (col_dir);
46 some kind of mismatch, eg. a note column, that is behind a
52 Item *it_col = it->get_column ();
53 if (d == RIGHT && right_col != it_col)
56 if (Separation_item::has_interface (it))
58 extents[d].unite (Separation_item::width (it));
64 SCM r = it->get_grob_property ("rest");
65 Grob * g = unsmob_grob (r);
67 g = Note_column::first_head (it);
70 Ugh. If Stem is switched off, we don't know what the
71 first note head will be.
74 left_head_wid = g->extent(it_col, X_AXIS);
77 extents[d].unite (it->extent (it_col, X_AXIS));
80 Grob * accs = Note_column::accidentals (it);
82 accs = Note_column::accidentals (it->get_parent (X_AXIS));
87 Accidental_placement::get_relevant_accidental_extent (accs, it_col, me);
94 if (extents[d].empty_b ())
95 extents[d] = Interval (0,0);
97 while (flip (&d) != LEFT);
101 We look at the width of the note head, since smaller heads get less space
102 eg. a quarter rest gets almost 0.5 ss less horizontal space than a note.
104 What is sticking out of the note head (eg. a flag), doesn't get
105 the full amount of space.
107 *fixed = left_head_wid.empty_b () ? increment : left_head_wid[RIGHT];
108 *space = (base_space - increment) + *fixed +
109 (extents[LEFT][RIGHT]
110 - (left_head_wid.empty_b () ? 0.0 : left_head_wid[RIGHT]))/ 2;
112 if (Item::breakable_b (right_col)
113 || right_col->original_)
116 This is for the situation
121 *space += -extents[RIGHT][LEFT];
122 *fixed += -extents[RIGHT][LEFT];
124 else if (*space - *fixed < 2 * ((- extents[RIGHT][LEFT]) >? 0))
128 What's sticking out at the left of the right side has less
129 influence. We only take it into account if there is not enough
132 this sucks: this criterion is discontinuous; FIXME.
134 *space += 0.5 * (( -extents[RIGHT][LEFT]) >? 0);
137 stem_dir_correction (me, right_col, increment, space, fixed);
141 Note_spacing::left_column (Grob *me)
146 return dynamic_cast<Item*> (me)->get_column ();
150 Compute the column of the right-items. This is a big function,
151 since RIGHT-ITEMS may span more columns (eg. if a clef if inserted,
152 this will add a new columns to RIGHT-ITEMS. Here we look at the
153 columns, and return the left-most. If there are multiple columns, we
158 Note_spacing::right_column (Grob*me)
163 SCM right = me->get_grob_property ("right-items");
165 int min_rank = INT_MAX;
167 for (SCM s = right ; gh_pair_p (s) ; s = gh_cdr (s))
169 Item * ri = unsmob_item (gh_car (s));
171 Item * col = ri->get_column ();
172 int rank = Paper_column::get_rank (col);
186 // I'm a lazy bum. We could do this in-place.
187 SCM newright = SCM_EOL;
188 for (SCM s = right ; gh_pair_p (s) ; s =gh_cdr (s))
190 if (unsmob_item (gh_car (s))->get_column () == mincol)
191 newright = gh_cons (gh_car (s), newright);
194 me->set_grob_property ("right-items", newright);
200 int r = Paper_column::get_rank (dynamic_cast<Item*>(me)->get_column ());
201 programming_error (_f("Spacing wish column %d has no right item.", r));
211 Correct for optical illusions. See [Wanske] p. 138. The combination
212 up-stem + down-stem should get extra space, the combination
213 down-stem + up-stem less.
215 TODO: have to check wether the stems are in the same staff.
219 Note_spacing::stem_dir_correction (Grob*me, Item * rcolumn,
221 Real * space, Real *fixed)
223 Drul_array<Direction> stem_dirs(CENTER,CENTER);
224 Drul_array<Interval> stem_posns;
225 Drul_array<Interval> head_posns;
226 Drul_array<SCM> props(me->get_grob_property ("left-items"),
227 me->get_grob_property ("right-items"));
229 Drul_array<Grob*> beams_drul(0,0);
231 stem_dirs[LEFT] = stem_dirs[RIGHT] = CENTER;
233 Interval bar_xextent;
234 Interval bar_yextent;
236 bool correct_stem_dirs = true;
238 bool acc_right = false;
242 for (SCM s = props[d]; gh_pair_p (s); s = gh_cdr (s))
244 Item * it= dynamic_cast<Item*> (unsmob_grob (gh_car(s)));
247 acc_right = acc_right || Note_column::accidentals (it);
249 Grob *stem = Note_column::get_stem (it);
251 if (!stem || !stem->live ())
253 if (d == RIGHT && Separation_item::has_interface (it))
255 if (it->get_column () != rcolumn)
257 it = it->find_prebroken_piece (rcolumn->break_status_dir ());
260 Grob *last = Staff_spacing::extremal_break_aligned_grob (it, LEFT, &bar_xextent);
263 bar_yextent = Staff_spacing::bar_y_positions (last);
271 if(Stem::invisible_b (stem))
273 correct_stem_dirs = false;
277 beams_drul[d] = Stem::get_beam (stem);
280 Direction sd = Stem::get_direction (stem);
281 if (stem_dirs[d] && stem_dirs[d] != sd)
283 correct_stem_dirs = false;
289 Correction doesn't seem appropriate when there is a large flag
290 hanging from the note.
293 && Stem::duration_log (stem) > 2 && !Stem::get_beam (stem))
295 correct_stem_dirs = false;
300 Interval hp = Stem::head_positions (stem);
301 Real chord_start = hp[sd];
302 Real stem_end = Stem::stem_end_position (stem);
304 stem_posns[d] = Interval(chord_start<?stem_end, chord_start>? stem_end);
305 head_posns[d].unite (hp);
308 while (flip (&d) != LEFT);
312 don't correct if accidentals are sticking out of the right side.
317 Real correction = 0.0;
319 if (!bar_yextent.empty_b())
321 stem_dirs[RIGHT] = - stem_dirs[LEFT];
322 stem_posns[RIGHT] = bar_yextent;
325 if (correct_stem_dirs && stem_dirs[LEFT] *stem_dirs[RIGHT] == -1)
327 if (beams_drul[LEFT] && beams_drul[LEFT] == beams_drul[RIGHT])
330 this is a knee: maximal correction.
333 correction = increment* stem_dirs[LEFT];
334 correction *= gh_scm2double (me->get_grob_property ("knee-spacing-correction"));
335 *fixed += correction;
339 intersect = stem_posns[LEFT];
340 intersect.intersect(stem_posns[RIGHT]);
341 correct_stem_dirs = correct_stem_dirs && !intersect.empty_b ();
343 if (correct_stem_dirs)
345 correction =abs (intersect.length ());
351 correction = (correction/7) <? 1.0;
352 correction *= stem_dirs[LEFT] ;
354 gh_scm2double (me->get_grob_property ("stem-spacing-correction"));
357 if (!bar_yextent.empty_b())
363 else if (correct_stem_dirs && stem_dirs[LEFT] *stem_dirs[RIGHT] == UP)
366 Correct for the following situation:
375 ^ move the center one to the left.
378 this effect seems to be much more subtle than the
379 stem-direction stuff (why?), and also does not scale with the
380 difference in stem length.
385 Interval hp = head_posns[LEFT];
386 hp.intersect (head_posns[RIGHT]);
391 (head_posns[LEFT][DOWN] > head_posns[RIGHT][UP]) ? RIGHT : LEFT;
393 Real delta = head_posns[-lowest][DOWN] - head_posns[lowest][UP] ;
394 Real corr = gh_scm2double (me->get_grob_property ("stem-spacing-correction"));
395 corr = (delta <= 1) ? 0.0 : 0.25;
397 correction= -lowest * corr ;
400 *space += correction;
403 /* there used to be a correction for bar_xextent() here, but
404 it's unclear what that was good for ?
413 ADD_INTERFACE (Note_spacing,"note-spacing-interface",
415 "left-items right-items stem-spacing-correction knee-spacing-correction");