2 note-spacing.cc -- implement Note_spacing
4 source file of the GNU LilyPond music typesetter
6 (c) 2001--2002 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 left_head_wid = g->extent(it_col, X_AXIS);
73 extents[d].unite (it->extent (it_col, X_AXIS));
76 Grob * accs = Note_column::accidentals (it);
78 accs = Note_column::accidentals (it->get_parent (X_AXIS));
83 Accidental_placement::get_relevant_accidental_extent (accs, it_col, me);
90 if (extents[d].empty_b ())
91 extents[d] = Interval (0,0);
93 while (flip (&d) != LEFT);
97 We look at the width of the note head, since smaller heads get less space
98 eg. a quarter rest gets almost 0.5 ss less horizontal space than a note.
100 What is sticking out of the note head (eg. a flag), doesn't get
101 the full amount of space.
103 *fixed = left_head_wid.empty_b () ? increment : left_head_wid[RIGHT];
104 *space = (base_space - increment) + *fixed +
105 (extents[LEFT][RIGHT] - left_head_wid[RIGHT])/ 2;
108 if (*space - *fixed < 2 * ((- extents[RIGHT][LEFT]) >? 0))
112 What's sticking out at the left of the right side has less
113 influence. We only take it into account if there is not enough
116 this sucks: this criterion is discontinuous; FIXME.
118 *space += 0.5 * (( -extents[RIGHT][LEFT]) >? 0);
121 stem_dir_correction (me, right_col, increment, space, fixed);
125 Note_spacing::left_column (Grob *me)
130 return dynamic_cast<Item*> (me)->get_column ();
134 Compute the column of the right-items. This is a big function,
135 since RIGHT-ITEMS may span more columns (eg. if a clef if inserted,
136 this will add a new columns to RIGHT-ITEMS. Here we look at the
137 columns, and return the left-most. If there are multiple columns, we
142 Note_spacing::right_column (Grob*me)
147 SCM right = me->get_grob_property ("right-items");
149 int min_rank = INT_MAX;
151 for (SCM s = right ; gh_pair_p (s) ; s = gh_cdr (s))
153 Item * ri = unsmob_item (gh_car (s));
155 Item * col = ri->get_column ();
156 int rank = Paper_column::get_rank (col);
170 // I'm a lazy bum. We could do this in-place.
171 SCM newright = SCM_EOL;
172 for (SCM s = right ; gh_pair_p (s) ; s =gh_cdr (s))
174 if (unsmob_item (gh_car (s))->get_column () == mincol)
175 newright = gh_cons (gh_car (s), newright);
178 me->set_grob_property ("right-items", newright);
184 int r = Paper_column::get_rank (dynamic_cast<Item*>(me)->get_column ());
185 programming_error (_f("Spacing wish column %d has no right item.", r));
195 Correct for optical illusions. See [Wanske] p. 138. The combination
196 up-stem + down-stem should get extra space, the combination
197 down-stem + up-stem less.
199 TODO: have to check wether the stems are in the same staff.
203 Note_spacing::stem_dir_correction (Grob*me, Item * rcolumn,
205 Real * space, Real *fixed)
207 Drul_array<Direction> stem_dirs(CENTER,CENTER);
208 Drul_array<Interval> stem_posns;
209 Drul_array<Interval> head_posns;
210 Drul_array<SCM> props(me->get_grob_property ("left-items"),
211 me->get_grob_property ("right-items"));
213 Drul_array<Grob*> beams_drul(0,0);
214 Real correction = 0.0;
216 stem_dirs[LEFT] = stem_dirs[RIGHT] = CENTER;
218 Interval bar_xextent;
219 Interval bar_yextent;
223 bool acc_right = false;
227 for (SCM s = props[d]; gh_pair_p (s); s = gh_cdr (s))
229 Item * it= dynamic_cast<Item*> (unsmob_grob (gh_car(s)));
232 acc_right = acc_right || Note_column::accidentals (it);
234 Grob *stem = Note_column::get_stem (it);
238 if (d == RIGHT && Separation_item::has_interface (it))
240 if (it->get_column () != rcolumn)
242 it = it->find_prebroken_piece (rcolumn->break_status_dir ());
245 Grob *last = Staff_spacing::extremal_break_aligned_grob (it, LEFT, &bar_xextent);
248 bar_yextent = Staff_spacing::bar_y_positions (last);
256 if(Stem::invisible_b (stem))
261 beams_drul[d] = Stem::get_beam (stem);
264 Direction sd = Stem::get_direction (stem);
265 if (stem_dirs[d] && stem_dirs[d] != sd)
272 Correction doesn't seem appropriate when there is a large flag
273 hanging from the note.
276 && Stem::duration_log (stem) > 2 && !Stem::get_beam (stem))
284 Interval hp = Stem::head_positions (stem);
285 Real chord_start = hp[sd];
286 Real stem_end = Stem::stem_end_position (stem);
288 stem_posns[d] = Interval(chord_start<?stem_end, chord_start>? stem_end);
289 head_posns[d].unite (hp);
292 while (flip (&d) != LEFT);
296 don't correct if accidentals are sticking out of the right side.
302 if (!bar_yextent.empty_b())
304 stem_dirs[RIGHT] = - stem_dirs[LEFT];
305 stem_posns[RIGHT] = bar_yextent;
308 if (correct &&stem_dirs[LEFT] *stem_dirs[RIGHT] == -1)
310 if (beams_drul[LEFT] && beams_drul[LEFT] == beams_drul[RIGHT])
313 this is a knee: maximal correction.
316 correction = increment* stem_dirs[LEFT];
317 correction *= gh_scm2double (me->get_grob_property ("knee-spacing-correction"));
318 *fixed += correction;
322 intersect = stem_posns[LEFT];
323 intersect.intersect(stem_posns[RIGHT]);
324 correct = correct && !intersect.empty_b ();
329 correction = abs (intersect.length ());
335 correction = (correction/7) <? 1.0;
336 correction *= stem_dirs[LEFT] ;
337 correction *= gh_scm2double (me->get_grob_property ("stem-spacing-correction"));
339 if (!bar_yextent.empty_b())
345 else if (correct && stem_dirs[LEFT] *stem_dirs[RIGHT] == UP)
348 Correct for the following situation:
357 ^ move the center one to the left.
360 this effect seems to be much more subtle than the
361 stem-direction stuff (why?), and also does not scale with the
362 difference in stem length.
367 Interval hp = head_posns[LEFT];
368 hp.intersect (head_posns[RIGHT]);
373 (head_posns[LEFT][DOWN] > head_posns[RIGHT][UP]) ? RIGHT : LEFT;
375 Real delta = head_posns[-lowest][DOWN] - head_posns[lowest][UP] ;
376 Real corr = gh_scm2double (me->get_grob_property ("stem-spacing-correction"));
377 corr = (delta <= 1) ? 0.0 : 0.25;
379 correction= -lowest * corr ;
382 if (!bar_xextent.empty_b())
383 correction += - bar_xextent[LEFT];
385 *space += correction;
391 ADD_INTERFACE (Note_spacing,"note-spacing-interface",
393 "left-items right-items stem-spacing-correction knee-spacing-correction");