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"
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->column_l ();
53 if (d == RIGHT && right_col != it_col)
56 if (Separation_item::has_interface (it))
58 extents[d].unite (Separation_item::my_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));
81 extents[d].unite (accs->extent (it_col, X_AXIS));
85 if (extents[d].empty_b ())
86 extents[d] = Interval (0,0);
88 while (flip (&d) != LEFT);
92 We look at the width of the note head, since smaller heads get less space
93 eg. a quarter rest gets almost 0.5 ss less horizontal space than a note.
95 What is sticking out of the note head (eg. a flag), doesn't get
96 the full amount of space.
98 *fixed = left_head_wid.empty_b () ? increment : left_head_wid[RIGHT];
99 *space = (base_space - increment) + *fixed +
100 (extents[LEFT][RIGHT] - left_head_wid[RIGHT])/ 2;
103 if (*space - *fixed < 2 * ((- extents[RIGHT][LEFT]) >? 0))
107 What's sticking out at the left of the right side has less
108 influence. We only take it into account if there is not enough
111 this sucks: this criterion is discontinuous; FIXME.
113 *space += 0.5 * (( -extents[RIGHT][LEFT]) >? 0);
116 stem_dir_correction (me, right_col, increment, space, fixed);
120 Note_spacing::left_column (Grob *me)
125 return dynamic_cast<Item*> (me)->column_l ();
129 Compute the column of the right-items. This is a big function,
130 since RIGHT-ITEMS may span more columns (eg. if a clef if inserted,
131 this will add a new columns to RIGHT-ITEMS. Here we look at the
132 columns, and return the left-most. If there are multiple columns, we
137 Note_spacing::right_column (Grob*me)
142 SCM right = me->get_grob_property ("right-items");
144 int min_rank = INT_MAX;
146 for (SCM s = right ; gh_pair_p (s) ; s = gh_cdr (s))
148 Item * ri = unsmob_item (gh_car (s));
150 Item * col = ri->column_l ();
151 int rank = Paper_column::rank_i (col);
165 // I'm a lazy bum. We could do this in-place.
166 SCM newright = SCM_EOL;
167 for (SCM s = right ; gh_pair_p (s) ; s =gh_cdr (s))
169 if (unsmob_item (gh_car (s))->column_l () == mincol)
170 newright = gh_cons (gh_car (s), newright);
173 me->set_grob_property ("right-items", newright);
179 int r = Paper_column::rank_i (dynamic_cast<Item*>(me)->column_l ());
180 programming_error (_f("Spacing wish column %d has no right item.", r));
190 Correct for optical illusions. See [Wanske] p. 138. The combination
191 up-stem + down-stem should get extra space, the combination
192 down-stem + up-stem less.
194 TODO: have to check wether the stems are in the same staff.
198 Note_spacing::stem_dir_correction (Grob*me, Item * rcolumn,
200 Real * space, Real *fixed)
202 Drul_array<Direction> stem_dirs(CENTER,CENTER);
203 Drul_array<Interval> stem_posns;
204 Drul_array<Interval> head_posns;
205 Drul_array<SCM> props(me->get_grob_property ("left-items"),
206 me->get_grob_property ("right-items"));
208 Drul_array<Grob*> beams_drul(0,0);
209 Real correction = 0.0;
211 stem_dirs[LEFT] = stem_dirs[RIGHT] = CENTER;
213 Interval bar_xextent;
214 Interval bar_yextent;
218 bool acc_right = false;
222 for (SCM s = props[d]; gh_pair_p (s); s = gh_cdr (s))
224 Item * it= dynamic_cast<Item*> (unsmob_grob (gh_car(s)));
227 acc_right = acc_right || Note_column::accidentals (it);
229 Grob *stem = Note_column::stem_l (it);
233 if (d == RIGHT && Separation_item::has_interface (it))
235 if (it->column_l () != rcolumn)
237 it = it->find_prebroken_piece (rcolumn->break_status_dir ());
240 Grob *last = Staff_spacing::extremal_break_aligned_grob (it, LEFT, &bar_xextent);
243 bar_yextent = Staff_spacing::bar_y_positions (last);
251 if(Stem::invisible_b (stem))
256 beams_drul[d] = Stem::beam_l (stem);
259 Direction sd = Stem::get_direction (stem);
260 if (stem_dirs[d] && stem_dirs[d] != sd)
267 Correction doesn't seem appropriate when there is a large flag
268 hanging from the note.
271 && Stem::duration_log (stem) > 2 && !Stem::beam_l (stem))
279 Interval hp = Stem::head_positions (stem);
280 Real chord_start = hp[sd];
281 Real stem_end = Stem::stem_end_position (stem);
283 stem_posns[d] = Interval(chord_start<?stem_end, chord_start>? stem_end);
284 head_posns[d].unite (hp);
287 while (flip (&d) != LEFT);
291 don't correct if accidentals are sticking out of the right side.
297 if (!bar_yextent.empty_b())
299 stem_dirs[RIGHT] = - stem_dirs[LEFT];
300 stem_posns[RIGHT] = bar_yextent;
303 if (correct &&stem_dirs[LEFT] *stem_dirs[RIGHT] == -1)
305 if (beams_drul[LEFT] && beams_drul[LEFT] == beams_drul[RIGHT])
308 this is a knee: maximal correction.
311 correction = increment* stem_dirs[LEFT];
312 *fixed += increment* stem_dirs[LEFT];
316 intersect = stem_posns[LEFT];
317 intersect.intersect(stem_posns[RIGHT]);
318 correct = correct && !intersect.empty_b ();
323 correction = abs (intersect.length ());
329 correction = (correction/7) <? 1.0;
330 correction *= stem_dirs[LEFT] ;
331 correction *= gh_scm2double (me->get_grob_property ("stem-spacing-correction"));
333 if (!bar_yextent.empty_b())
339 else if (correct && stem_dirs[LEFT] *stem_dirs[RIGHT] == UP)
342 Correct for the following situation:
351 ^ move the center one to the left.
354 this effect seems to be much more subtle than the
355 stem-direction stuff (why?), and also does not scale with the
356 difference in stem length.
361 Interval hp = head_posns[LEFT];
362 hp.intersect (head_posns[RIGHT]);
367 (head_posns[LEFT][DOWN] > head_posns[RIGHT][UP]) ? RIGHT : LEFT;
369 Real delta = head_posns[-lowest][DOWN] - head_posns[lowest][UP] ;
370 Real corr = gh_scm2double (me->get_grob_property ("stem-spacing-correction"));
371 corr = (delta <= 1) ? 0.0 : 0.25;
373 correction= -lowest * corr ;
376 if (!bar_xextent.empty_b())
377 correction += - bar_xextent[LEFT];
379 *space += correction;
385 ADD_INTERFACE (Note_spacing,"note-spacing-interface",
387 "left-items right-items stem-spacing-correction");