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);
48 some kind of mismatch, eg. a note column, that is behind a
54 extents[d].unite (it->extent (it->column_l (), X_AXIS));
57 Grob * accs = Note_column::accidentals (it);
59 accs = Note_column::accidentals (it->get_parent (X_AXIS));
62 extents[d].unite (accs->extent (it->column_l (), X_AXIS));
66 if (extents[d].empty_b ())
67 extents[d] = Interval (0,0);
69 while (flip (&d) != LEFT);
71 *fixed = (extents[LEFT][RIGHT] >? increment);
72 *space = (base_space - increment) + *fixed ;
74 if (*space - *fixed < 2 * ((- extents[RIGHT][LEFT]) >? 0))
78 What's sticking out at the left of the right side has less
79 influence. We only take it into account if there is not enough
82 this sucks: this criterion is discontinuous; FIXME.
84 *space += 0.5 * (( -extents[RIGHT][LEFT]) >? 0);
87 *space += stem_dir_correction (me, right_col);
91 Note_spacing::left_column (Grob *me)
93 if (me->immutable_property_alist_ == SCM_EOL)
96 return dynamic_cast<Item*> (me)->column_l ();
100 Compute the column of the right-items. This is a big function,
101 since RIGHT-ITEMS may span more columns (eg. if a clef if inserted,
102 this will add a new columns to RIGHT-ITEMS. Here we look at the
103 columns, and return the left-most. If there are multiple columns, we
108 Note_spacing::right_column (Grob*me)
111 ugh. should have generic is_live() method?
113 if (me->immutable_property_alist_ == SCM_EOL)
116 SCM right = me->get_grob_property ("right-items");
118 int min_rank = INT_MAX;
120 for (SCM s = right ; gh_pair_p (s) ; s = gh_cdr (s))
122 Item * ri = unsmob_item (gh_car (s));
124 Item * col = ri->column_l ();
125 int rank = Paper_column::rank_i (col);
139 // I'm a lazy bum. We could do this in-place.
140 SCM newright = SCM_EOL;
141 for (SCM s = right ; gh_pair_p (s) ; s =gh_cdr (s))
143 if (unsmob_item (gh_car (s))->column_l () == mincol)
144 newright = gh_cons (gh_car (s), newright);
147 me->set_grob_property ("right-items", newright);
153 int r = Paper_column::rank_i (dynamic_cast<Item*>(me)->column_l ());
154 programming_error (_f("Spacing wish column %d has no right item.", r));
164 Correct for optical illusions. See [Wanske] p. 138. The combination
165 up-stem + down-stem should get extra space, the combination
166 down-stem + up-stem less.
168 TODO: have to check wether the stems are in the same staff.
170 TODO: also correct for bar lines in RIGHT-ITEMS. Should check if
171 the barline is the leftmost object of the break alignment.
175 Note_spacing::stem_dir_correction (Grob*me, Item * rcolumn)
177 Drul_array<Direction> stem_dirs(CENTER,CENTER);
178 Drul_array<Interval> stem_posns;
179 Drul_array<Interval> head_posns;
180 Drul_array<SCM> props(me->get_grob_property ("left-items"),
181 me->get_grob_property ("right-items"));
183 Real correction = 0.0;
185 stem_dirs[LEFT] = stem_dirs[RIGHT] = CENTER;
187 Interval bar_xextent;
188 Interval bar_yextent;
192 bool acc_right = false;
196 for (SCM s = props[d]; gh_pair_p (s); s = gh_cdr (s))
198 Item * it= dynamic_cast<Item*> (unsmob_grob (gh_car(s)));
201 acc_right = acc_right || Note_column::accidentals (it);
203 Grob *stem = Note_column::stem_l (it);
207 if (d == RIGHT && Separation_item::has_interface (it))
209 if (it->column_l () != rcolumn)
211 it = it->find_prebroken_piece (rcolumn->break_status_dir ());
214 Grob *last = Staff_spacing::extremal_break_aligned_grob (it, LEFT, &bar_xextent);
217 bar_yextent = Staff_spacing::bar_y_positions (last);
225 if(Stem::invisible_b (stem))
231 Direction sd = Stem::get_direction (stem);
232 if (stem_dirs[d] && stem_dirs[d] != sd)
239 Interval hp = Stem::head_positions (stem);
240 Real chord_start = hp[sd];
241 Real stem_end = Stem::stem_end_position (stem);
243 stem_posns[d] = Interval(chord_start<?stem_end, chord_start>? stem_end);
244 head_posns[d].unite (hp);
247 while (flip (&d) != LEFT);
251 don't correct if accidentals are sticking out of the right side.
257 if (!bar_yextent.empty_b())
259 stem_dirs[RIGHT] = - stem_dirs[LEFT];
260 stem_posns[RIGHT] = bar_yextent;
263 if (correct &&stem_dirs[LEFT] *stem_dirs[RIGHT] == -1)
266 intersect = stem_posns[LEFT];
267 intersect.intersect(stem_posns[RIGHT]);
268 correct = correct && !intersect.empty_b ();
275 correction = abs (intersect.length ());
276 correction = (correction/7) <? 1.0;
277 correction *= stem_dirs[LEFT] ;
278 correction *= gh_scm2double (me->get_grob_property ("stem-spacing-correction"));
280 if (!bar_yextent.empty_b())
288 Correct for the following situation:
297 ^ move the center one to the left.
300 this effect seems to be much more subtle than the
301 stem-direction stuff (why?), and also does not scale with the
302 difference in stem length.
307 Interval hp = head_posns[LEFT];
308 hp.intersect (head_posns[RIGHT]);
313 (head_posns[LEFT][DOWN] > head_posns[RIGHT][UP]) ? RIGHT : LEFT;
315 Real delta = head_posns[-lowest][DOWN] - head_posns[lowest][UP] ;
316 Real corr = gh_scm2double (me->get_grob_property ("stem-spacing-correction"));
317 corr = (delta <= 1) ? 0.0 : 0.25;
319 correction= -lowest * corr ;
322 if (!bar_xextent.empty_b())
323 correction += - bar_xextent[LEFT];