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"
22 Note_spacing::has_interface (Grob* g)
24 return g && g->has_interface (ly_symbol2scm ("note-spacing-interface"));
28 Note_spacing::get_spacing (Grob *me, Item* right_col,
29 Real base_space, Real increment, Real *space, Real *fixed)
32 Drul_array<SCM> props(me->get_grob_property ("left-items"),
33 me->get_grob_property ("right-items"));
35 Direction col_dir = right_col->break_status_dir ();
36 Drul_array<Interval> extents;
39 for (SCM s = props[d]; gh_pair_p (s); s = gh_cdr (s))
41 Item * it= dynamic_cast<Item*> (unsmob_grob (gh_car(s)));
43 if (d == RIGHT && it->break_status_dir () != col_dir)
45 it = it -> find_prebroken_piece (col_dir);
49 some kind of mismatch, eg. a note column, that is behind a
55 if (d == RIGHT && right_col != it->column_l ())
58 if (Separation_item::has_interface (it))
60 extents[d].unite (Separation_item::my_width (it));
64 extents[d].unite (it->extent (it->column_l (), X_AXIS));
67 Grob * accs = Note_column::accidentals (it);
69 accs = Note_column::accidentals (it->get_parent (X_AXIS));
72 extents[d].unite (accs->extent (it->column_l (), X_AXIS));
76 if (extents[d].empty_b ())
77 extents[d] = Interval (0,0);
79 while (flip (&d) != LEFT);
81 *fixed = (extents[LEFT][RIGHT] >? increment);
82 *space = (base_space - increment) + *fixed ;
84 if (*space - *fixed < 2 * ((- extents[RIGHT][LEFT]) >? 0))
88 What's sticking out at the left of the right side has less
89 influence. We only take it into account if there is not enough
92 this sucks: this criterion is discontinuous; FIXME.
94 *space += 0.5 * (( -extents[RIGHT][LEFT]) >? 0);
97 *space += stem_dir_correction (me, right_col, increment);
101 Note_spacing::left_column (Grob *me)
103 if (me->immutable_property_alist_ == SCM_EOL)
106 return dynamic_cast<Item*> (me)->column_l ();
110 Compute the column of the right-items. This is a big function,
111 since RIGHT-ITEMS may span more columns (eg. if a clef if inserted,
112 this will add a new columns to RIGHT-ITEMS. Here we look at the
113 columns, and return the left-most. If there are multiple columns, we
118 Note_spacing::right_column (Grob*me)
121 ugh. should have generic is_live() method?
123 if (me->immutable_property_alist_ == SCM_EOL)
126 SCM right = me->get_grob_property ("right-items");
128 int min_rank = INT_MAX;
130 for (SCM s = right ; gh_pair_p (s) ; s = gh_cdr (s))
132 Item * ri = unsmob_item (gh_car (s));
134 Item * col = ri->column_l ();
135 int rank = Paper_column::rank_i (col);
149 // I'm a lazy bum. We could do this in-place.
150 SCM newright = SCM_EOL;
151 for (SCM s = right ; gh_pair_p (s) ; s =gh_cdr (s))
153 if (unsmob_item (gh_car (s))->column_l () == mincol)
154 newright = gh_cons (gh_car (s), newright);
157 me->set_grob_property ("right-items", newright);
163 int r = Paper_column::rank_i (dynamic_cast<Item*>(me)->column_l ());
164 programming_error (_f("Spacing wish column %d has no right item.", r));
174 Correct for optical illusions. See [Wanske] p. 138. The combination
175 up-stem + down-stem should get extra space, the combination
176 down-stem + up-stem less.
178 TODO: have to check wether the stems are in the same staff.
182 Note_spacing::stem_dir_correction (Grob*me, Item * rcolumn,
185 Drul_array<Direction> stem_dirs(CENTER,CENTER);
186 Drul_array<Interval> stem_posns;
187 Drul_array<Interval> head_posns;
188 Drul_array<SCM> props(me->get_grob_property ("left-items"),
189 me->get_grob_property ("right-items"));
191 Drul_array<Grob*> beams_drul(0,0);
192 Real correction = 0.0;
194 stem_dirs[LEFT] = stem_dirs[RIGHT] = CENTER;
196 Interval bar_xextent;
197 Interval bar_yextent;
201 bool acc_right = false;
205 for (SCM s = props[d]; gh_pair_p (s); s = gh_cdr (s))
207 Item * it= dynamic_cast<Item*> (unsmob_grob (gh_car(s)));
210 acc_right = acc_right || Note_column::accidentals (it);
212 Grob *stem = Note_column::stem_l (it);
216 if (d == RIGHT && Separation_item::has_interface (it))
218 if (it->column_l () != rcolumn)
220 it = it->find_prebroken_piece (rcolumn->break_status_dir ());
223 Grob *last = Staff_spacing::extremal_break_aligned_grob (it, LEFT, &bar_xextent);
226 bar_yextent = Staff_spacing::bar_y_positions (last);
234 if(Stem::invisible_b (stem))
240 beams_drul[d] = Stem::beam_l (stem);
243 Direction sd = Stem::get_direction (stem);
244 if (stem_dirs[d] && stem_dirs[d] != sd)
251 Interval hp = Stem::head_positions (stem);
252 Real chord_start = hp[sd];
253 Real stem_end = Stem::stem_end_position (stem);
255 stem_posns[d] = Interval(chord_start<?stem_end, chord_start>? stem_end);
256 head_posns[d].unite (hp);
259 while (flip (&d) != LEFT);
263 don't correct if accidentals are sticking out of the right side.
269 if (!bar_yextent.empty_b())
271 stem_dirs[RIGHT] = - stem_dirs[LEFT];
272 stem_posns[RIGHT] = bar_yextent;
275 if (correct &&stem_dirs[LEFT] *stem_dirs[RIGHT] == -1)
277 if (beams_drul[LEFT] == beams_drul[RIGHT])
280 this is a knee: maximal correction.
283 correction = increment* stem_dirs[LEFT];
287 intersect = stem_posns[LEFT];
288 intersect.intersect(stem_posns[RIGHT]);
289 correct = correct && !intersect.empty_b ();
294 correction = abs (intersect.length ());
300 correction = (correction/7) <? 1.0;
301 correction *= stem_dirs[LEFT] ;
302 correction *= gh_scm2double (me->get_grob_property ("stem-spacing-correction"));
304 if (!bar_yextent.empty_b())
310 else if (correct && stem_dirs[LEFT] *stem_dirs[RIGHT] == UP)
313 Correct for the following situation:
322 ^ move the center one to the left.
325 this effect seems to be much more subtle than the
326 stem-direction stuff (why?), and also does not scale with the
327 difference in stem length.
332 Interval hp = head_posns[LEFT];
333 hp.intersect (head_posns[RIGHT]);
338 (head_posns[LEFT][DOWN] > head_posns[RIGHT][UP]) ? RIGHT : LEFT;
340 Real delta = head_posns[-lowest][DOWN] - head_posns[lowest][UP] ;
341 Real corr = gh_scm2double (me->get_grob_property ("stem-spacing-correction"));
342 corr = (delta <= 1) ? 0.0 : 0.25;
344 correction= -lowest * corr ;
347 if (!bar_xextent.empty_b())
348 correction += - bar_xextent[LEFT];
357 ADD_INTERFACE (Note_spacing,"note-spacing-interface",
359 "left-items right-items stem-spacing-correction");