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)
30 Drul_array<SCM> props(me->get_grob_property ("left-items"),
31 me->get_grob_property ("right-items"));
33 Drul_array<Interval> extents;
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)));
39 extents[d].unite (it->extent (it->column_l (), X_AXIS));
43 Grob * accs = Note_column::accidentals (it);
45 accs = Note_column::accidentals (it->get_parent (X_AXIS));
48 extents[d].unite (accs->extent (it->column_l (), X_AXIS));
52 if (extents[d].empty_b ())
53 extents[d] = Interval (0,0);
55 while (flip (&d) != LEFT);
59 What's sticking out at the left of the right side has less
63 Real dx= extents[LEFT][RIGHT] - 0.5 * extents[RIGHT][LEFT];
68 Note_spacing::left_column (Grob *me)
70 if (me->immutable_property_alist_ == SCM_EOL)
73 return dynamic_cast<Item*> (me)->column_l ();
77 Compute the column of the right-items. This is a big function,
78 since RIGHT-ITEMS may span more columns (eg. if a clef if inserted,
79 this will add a new columns to RIGHT-ITEMS. Here we look at the
80 columns, and return the left-most. If there are multiple columns, we
85 Note_spacing::right_column (Grob*me)
88 ugh. should have generic is_live() method?
90 if (me->immutable_property_alist_ == SCM_EOL)
93 SCM right = me->get_grob_property ("right-items");
95 int min_rank = INT_MAX;
97 for (SCM s = right ; gh_pair_p (s) ; s = gh_cdr (s))
99 Item * ri = unsmob_item (gh_car (s));
101 Item * col = ri->column_l ();
102 int rank = Paper_column::rank_i (col);
116 // I'm a lazy bum. We could do this in-place.
117 SCM newright = SCM_EOL;
118 for (SCM s = right ; gh_pair_p (s) ; s =gh_cdr (s))
120 if (unsmob_item (gh_car (s))->column_l () == mincol)
121 newright = gh_cons (gh_car (s), newright);
124 me->set_grob_property ("right-items", newright);
130 int r = Paper_column::rank_i (dynamic_cast<Item*>(me)->column_l ());
131 programming_error (_f("Spacing wish column %d has no right item.", r));
141 Correct for optical illusions. See [Wanske] p. 138. The combination
142 up-stem + down-stem should get extra space, the combination
143 down-stem + up-stem less.
145 TODO: have to check wether the stems are in the same staff.
147 TODO: also correct for bar lines in RIGHT-ITEMS. Should check if
148 the barline is the leftmost object of the break alignment.
152 Note_spacing::stem_dir_correction (Grob*me)
154 Drul_array<Direction> stem_dirs(CENTER,CENTER);
155 Drul_array<Interval> stem_posns;
156 Drul_array<Interval> head_posns;
157 Drul_array<SCM> props(me->get_grob_property ("left-items"),
158 me->get_grob_property ("right-items"));
160 Real correction = 0.0;
162 stem_dirs[LEFT] = stem_dirs[RIGHT] = CENTER;
164 Interval bar_xextent;
165 Interval bar_yextent;
169 bool acc_right = false;
173 for (SCM s = props[d]; gh_pair_p (s); s = gh_cdr (s))
175 Item * it= dynamic_cast<Item*> (unsmob_grob (gh_car(s)));
178 acc_right = acc_right || Note_column::accidentals (it);
180 Grob *stem = Note_column::stem_l (it);
184 if (d == RIGHT && Separation_item::has_interface (it))
186 Grob *last = Staff_spacing::extremal_break_aligned_grob (it, LEFT, &bar_xextent);
189 bar_yextent = Staff_spacing::bar_y_positions (last);
196 if(Stem::invisible_b (stem))
202 Direction sd = Stem::get_direction (stem);
203 if (stem_dirs[d] && stem_dirs[d] != sd)
210 Interval hp = Stem::head_positions (stem);
211 Real chord_start = hp[sd];
212 Real stem_end = Stem::stem_end_position (stem);
214 stem_posns[d] = Interval(chord_start<?stem_end, chord_start>? stem_end);
215 head_posns[d].unite (hp);
218 while (flip (&d) != LEFT);
222 don't correct if accidentals are sticking out of the right side.
228 if (!bar_yextent.empty_b())
230 stem_dirs[RIGHT] = - stem_dirs[LEFT];
231 stem_posns[RIGHT] = bar_yextent;
234 if (correct &&stem_dirs[LEFT] *stem_dirs[RIGHT] == -1)
237 intersect = stem_posns[LEFT];
238 intersect.intersect(stem_posns[RIGHT]);
239 correct = correct && !intersect.empty_b ();
246 correction = abs (intersect.length ());
247 correction = (correction/7) <? 1.0;
248 correction *= stem_dirs[LEFT] ;
249 correction *= gh_scm2double (me->get_grob_property ("stem-spacing-correction"));
251 if (!bar_yextent.empty_b())
259 Correct for the following situation:
268 ^ move the center one to the left.
271 this effect seems to be much more subtle than the
272 stem-direction stuff (why?), and also does not scale with the
273 difference in stem length.
278 Interval hp = head_posns[LEFT];
279 hp.intersect (head_posns[RIGHT]);
284 (head_posns[LEFT][DOWN] > head_posns[RIGHT][UP]) ? RIGHT : LEFT;
286 Real delta = head_posns[-lowest][DOWN] - head_posns[lowest][UP] ;
287 Real corr = gh_scm2double (me->get_grob_property ("stem-spacing-correction"));
288 corr = (delta <= 1) ? 0.0 : 0.25;
290 correction= -lowest * corr ;
293 if (!bar_xextent.empty_b())
294 correction += - bar_xextent[LEFT];