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;
38 Interval left_head_wid;
41 for (SCM s = props[d]; gh_pair_p (s); s = gh_cdr (s))
43 Item * it= dynamic_cast<Item*> (unsmob_grob (gh_car(s)));
45 if (d == RIGHT && it->break_status_dir () != col_dir)
47 it = it -> find_prebroken_piece (col_dir);
51 some kind of mismatch, eg. a note column, that is behind a
57 Item *it_col = it->column_l ();
58 if (d == RIGHT && right_col != it_col)
61 if (Separation_item::has_interface (it))
63 extents[d].unite (Separation_item::my_width (it));
69 SCM r = it->get_grob_property ("rest");
70 Grob * g = unsmob_grob (r);
72 g = Note_column::first_head (it);
75 left_head_wid = g->extent(it_col, X_AXIS);
78 extents[d].unite (it->extent (it_col, X_AXIS));
81 Grob * accs = Note_column::accidentals (it);
83 accs = Note_column::accidentals (it->get_parent (X_AXIS));
86 extents[d].unite (accs->extent (it_col, X_AXIS));
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 *space += stem_dir_correction (me, right_col, increment);
125 Note_spacing::left_column (Grob *me)
127 if (me->immutable_property_alist_ == SCM_EOL)
130 return dynamic_cast<Item*> (me)->column_l ();
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)
145 ugh. should have generic is_live() method?
147 if (me->immutable_property_alist_ == SCM_EOL)
150 SCM right = me->get_grob_property ("right-items");
152 int min_rank = INT_MAX;
154 for (SCM s = right ; gh_pair_p (s) ; s = gh_cdr (s))
156 Item * ri = unsmob_item (gh_car (s));
158 Item * col = ri->column_l ();
159 int rank = Paper_column::rank_i (col);
173 // I'm a lazy bum. We could do this in-place.
174 SCM newright = SCM_EOL;
175 for (SCM s = right ; gh_pair_p (s) ; s =gh_cdr (s))
177 if (unsmob_item (gh_car (s))->column_l () == mincol)
178 newright = gh_cons (gh_car (s), newright);
181 me->set_grob_property ("right-items", newright);
187 int r = Paper_column::rank_i (dynamic_cast<Item*>(me)->column_l ());
188 programming_error (_f("Spacing wish column %d has no right item.", r));
198 Correct for optical illusions. See [Wanske] p. 138. The combination
199 up-stem + down-stem should get extra space, the combination
200 down-stem + up-stem less.
202 TODO: have to check wether the stems are in the same staff.
206 Note_spacing::stem_dir_correction (Grob*me, Item * rcolumn,
209 Drul_array<Direction> stem_dirs(CENTER,CENTER);
210 Drul_array<Interval> stem_posns;
211 Drul_array<Interval> head_posns;
212 Drul_array<SCM> props(me->get_grob_property ("left-items"),
213 me->get_grob_property ("right-items"));
215 Drul_array<Grob*> beams_drul(0,0);
216 Real correction = 0.0;
218 stem_dirs[LEFT] = stem_dirs[RIGHT] = CENTER;
220 Interval bar_xextent;
221 Interval bar_yextent;
225 bool acc_right = false;
229 for (SCM s = props[d]; gh_pair_p (s); s = gh_cdr (s))
231 Item * it= dynamic_cast<Item*> (unsmob_grob (gh_car(s)));
234 acc_right = acc_right || Note_column::accidentals (it);
236 Grob *stem = Note_column::stem_l (it);
240 if (d == RIGHT && Separation_item::has_interface (it))
242 if (it->column_l () != rcolumn)
244 it = it->find_prebroken_piece (rcolumn->break_status_dir ());
247 Grob *last = Staff_spacing::extremal_break_aligned_grob (it, LEFT, &bar_xextent);
250 bar_yextent = Staff_spacing::bar_y_positions (last);
258 if(Stem::invisible_b (stem))
264 beams_drul[d] = Stem::beam_l (stem);
267 Direction sd = Stem::get_direction (stem);
268 if (stem_dirs[d] && stem_dirs[d] != sd)
276 Correction doesn't seem appropriate when there is a large flag
277 hanging from the note.
280 && Stem::duration_log (stem) > 2 && !Stem::beam_l (stem))
288 Interval hp = Stem::head_positions (stem);
289 Real chord_start = hp[sd];
290 Real stem_end = Stem::stem_end_position (stem);
292 stem_posns[d] = Interval(chord_start<?stem_end, chord_start>? stem_end);
293 head_posns[d].unite (hp);
296 while (flip (&d) != LEFT);
300 don't correct if accidentals are sticking out of the right side.
306 if (!bar_yextent.empty_b())
308 stem_dirs[RIGHT] = - stem_dirs[LEFT];
309 stem_posns[RIGHT] = bar_yextent;
312 if (correct &&stem_dirs[LEFT] *stem_dirs[RIGHT] == -1)
314 if (beams_drul[LEFT] && beams_drul[LEFT] == beams_drul[RIGHT])
317 this is a knee: maximal correction.
320 correction = increment* stem_dirs[LEFT];
324 intersect = stem_posns[LEFT];
325 intersect.intersect(stem_posns[RIGHT]);
326 correct = correct && !intersect.empty_b ();
331 correction = abs (intersect.length ());
337 correction = (correction/7) <? 1.0;
338 correction *= stem_dirs[LEFT] ;
339 correction *= gh_scm2double (me->get_grob_property ("stem-spacing-correction"));
341 if (!bar_yextent.empty_b())
347 else if (correct && stem_dirs[LEFT] *stem_dirs[RIGHT] == UP)
350 Correct for the following situation:
359 ^ move the center one to the left.
362 this effect seems to be much more subtle than the
363 stem-direction stuff (why?), and also does not scale with the
364 difference in stem length.
369 Interval hp = head_posns[LEFT];
370 hp.intersect (head_posns[RIGHT]);
375 (head_posns[LEFT][DOWN] > head_posns[RIGHT][UP]) ? RIGHT : LEFT;
377 Real delta = head_posns[-lowest][DOWN] - head_posns[lowest][UP] ;
378 Real corr = gh_scm2double (me->get_grob_property ("stem-spacing-correction"));
379 corr = (delta <= 1) ? 0.0 : 0.25;
381 correction= -lowest * corr ;
384 if (!bar_xextent.empty_b())
385 correction += - bar_xextent[LEFT];
394 ADD_INTERFACE (Note_spacing,"note-spacing-interface",
396 "left-items right-items stem-spacing-correction");