2 note-spacing.cc -- implement Note_spacing
4 source file of the GNU LilyPond music typesetter
6 (c) 2001--2003 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"
20 #include "accidental-placement.hh"
21 #include "paper-def.hh"
24 Note_spacing::get_spacing (Grob *me, Item* right_col,
25 Real base_space, Real increment, Real *space, Real *fixed)
28 Drul_array<SCM> props(me->get_grob_property ("left-items"),
29 me->get_grob_property ("right-items"));
31 Direction col_dir = right_col->break_status_dir ();
32 Drul_array<Interval> extents;
34 Interval left_head_wid;
37 for (SCM s = props[d]; gh_pair_p (s); s = gh_cdr (s))
39 Item * it= dynamic_cast<Item*> (unsmob_grob (gh_car(s)));
41 if (d == RIGHT && it->break_status_dir () != col_dir)
43 it = it -> find_prebroken_piece (col_dir);
47 some kind of mismatch, eg. a note column, that is behind a
53 Item *it_col = it->get_column ();
54 if (d == RIGHT && right_col != it_col)
57 if (Separation_item::has_interface (it))
59 extents[d].unite (Separation_item::width (it));
65 SCM r = it->get_grob_property ("rest");
66 Grob * g = unsmob_grob (r);
68 g = Note_column::first_head (it);
71 Ugh. If Stem is switched off, we don't know what the
72 first note head will be.
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));
88 Accidental_placement::get_relevant_accidental_extent (accs, it_col, me);
95 if (extents[d].empty_b ())
96 extents[d] = Interval (0,0);
98 while (flip (&d) != LEFT);
102 We look at the width of the note head, since smaller heads get less space
103 eg. a quarter rest gets almost 0.5 ss less horizontal space than a note.
105 What is sticking out of the note head (eg. a flag), doesn't get
106 the full amount of space.
108 *fixed = left_head_wid.empty_b () ? increment : left_head_wid[RIGHT];
109 *space = (base_space - increment) + *fixed +
110 (extents[LEFT][RIGHT]
111 - (left_head_wid.empty_b () ? 0.0 : left_head_wid[RIGHT]))/ 2;
113 if (Item::breakable_b (right_col)
114 || right_col->original_)
117 This is for the situation
122 *space += -extents[RIGHT][LEFT];
123 *fixed += -extents[RIGHT][LEFT];
125 else if (*space - *fixed < 2 * ((- extents[RIGHT][LEFT]) >? 0))
129 What's sticking out at the left of the right side has less
130 influence. We only take it into account if there is not enough
133 this sucks: this criterion is discontinuous; FIXME.
135 *space += 0.5 * (( -extents[RIGHT][LEFT]) >? 0);
138 stem_dir_correction (me, right_col, increment, space, fixed);
142 Note_spacing::left_column (Grob *me)
147 return dynamic_cast<Item*> (me)->get_column ();
151 Compute the column of the right-items. This is a big function,
152 since RIGHT-ITEMS may span more columns (eg. if a clef if inserted,
153 this will add a new columns to RIGHT-ITEMS. Here we look at the
154 columns, and return the left-most. If there are multiple columns, we
159 Note_spacing::right_column (Grob*me)
164 SCM right = me->get_grob_property ("right-items");
166 int min_rank = INT_MAX;
168 for (SCM s = right ; gh_pair_p (s) ; s = gh_cdr (s))
170 Item * ri = unsmob_item (gh_car (s));
172 Item * col = ri->get_column ();
173 int rank = Paper_column::get_rank (col);
187 // I'm a lazy bum. We could do this in-place.
188 SCM newright = SCM_EOL;
189 for (SCM s = right ; gh_pair_p (s) ; s =gh_cdr (s))
191 if (unsmob_item (gh_car (s))->get_column () == mincol)
192 newright = gh_cons (gh_car (s), newright);
195 me->set_grob_property ("right-items", newright);
201 int r = Paper_column::get_rank (dynamic_cast<Item*>(me)->get_column ());
202 programming_error (_f("Spacing wish column %d has no right item.", r));
212 Correct for optical illusions. See [Wanske] p. 138. The combination
213 up-stem + down-stem should get extra space, the combination
214 down-stem + up-stem less.
216 TODO: have to check wether the stems are in the same staff.
220 Note_spacing::stem_dir_correction (Grob*me, Item * rcolumn,
222 Real * space, Real *fixed)
224 Drul_array<Direction> stem_dirs(CENTER,CENTER);
225 Drul_array<Interval> stem_posns;
226 Drul_array<Interval> head_posns;
227 Drul_array<SCM> props(me->get_grob_property ("left-items"),
228 me->get_grob_property ("right-items"));
230 Drul_array<Grob*> beams_drul(0,0);
231 Drul_array<Grob*> stems_drul(0,0);
233 stem_dirs[LEFT] = stem_dirs[RIGHT] = CENTER;
235 Interval bar_xextent;
236 Interval bar_yextent;
238 bool correct_stem_dirs = true;
240 bool acc_right = false;
244 for (SCM s = props[d]; gh_pair_p (s); s = gh_cdr (s))
246 Item * it= dynamic_cast<Item*> (unsmob_grob (gh_car(s)));
249 acc_right = acc_right || Note_column::accidentals (it);
251 Grob *stem = Note_column::get_stem (it);
253 if (!stem || !stem->live ())
255 if (d == RIGHT && Separation_item::has_interface (it))
257 if (it->get_column () != rcolumn)
259 it = it->find_prebroken_piece (rcolumn->break_status_dir ());
262 Grob *last = Staff_spacing::extremal_break_aligned_grob (it, LEFT, &bar_xextent);
265 bar_yextent = Staff_spacing::bar_y_positions (last);
273 if(Stem::invisible_b (stem))
275 correct_stem_dirs = false;
279 stems_drul[d] = stem;
280 beams_drul[d] = Stem::get_beam (stem);
283 Direction sd = Stem::get_direction (stem);
284 if (stem_dirs[d] && stem_dirs[d] != sd)
286 correct_stem_dirs = false;
292 Correction doesn't seem appropriate when there is a large flag
293 hanging from the note.
296 && Stem::duration_log (stem) > 2 && !Stem::get_beam (stem))
298 correct_stem_dirs = false;
301 Interval hp = Stem::head_positions (stem);
302 Real chord_start = hp[sd];
303 Real stem_end = Stem::stem_end_position (stem);
305 stem_posns[d] = Interval(chord_start<?stem_end, chord_start>? stem_end);
306 head_posns[d].unite (hp);
309 while (flip (&d) != LEFT);
313 don't correct if accidentals are sticking out of the right side.
318 Real correction = 0.0;
320 if (!bar_yextent.empty_b())
322 stem_dirs[RIGHT] = - stem_dirs[LEFT];
323 stem_posns[RIGHT] = bar_yextent;
326 if (correct_stem_dirs && stem_dirs[LEFT] *stem_dirs[RIGHT] == -1)
328 if (beams_drul[LEFT] && beams_drul[LEFT] == beams_drul[RIGHT])
332 this is a knee: maximal correction.
334 Real note_head_width = increment;
335 Grob * st = stems_drul[RIGHT];
336 Grob * head = st ? Stem::support_head (st) : 0;
338 Interval head_extent;
341 head_extent = head->extent (rcolumn, X_AXIS);
343 if (!head_extent.empty_b())
344 note_head_width = head_extent[RIGHT];
348 Real thick = gh_scm2double (st->get_grob_property ("thickness"))
349 * st->get_paper ()->get_var ("linethickness");
351 note_head_width -= thick;
355 correction = note_head_width* stem_dirs[LEFT];
356 correction *= gh_scm2double (me->get_grob_property ("knee-spacing-correction"));
357 *fixed += correction;
361 intersect = stem_posns[LEFT];
362 intersect.intersect(stem_posns[RIGHT]);
363 correct_stem_dirs = correct_stem_dirs && !intersect.empty_b ();
365 if (correct_stem_dirs)
367 correction =abs (intersect.length ());
373 correction = (correction/7) <? 1.0;
374 correction *= stem_dirs[LEFT] ;
376 gh_scm2double (me->get_grob_property ("stem-spacing-correction"));
379 if (!bar_yextent.empty_b())
385 else if (correct_stem_dirs && stem_dirs[LEFT] *stem_dirs[RIGHT] == UP)
388 Correct for the following situation:
397 ^ move the center one to the left.
400 this effect seems to be much more subtle than the
401 stem-direction stuff (why?), and also does not scale with the
402 difference in stem length.
407 Interval hp = head_posns[LEFT];
408 hp.intersect (head_posns[RIGHT]);
413 (head_posns[LEFT][DOWN] > head_posns[RIGHT][UP]) ? RIGHT : LEFT;
415 Real delta = head_posns[-lowest][DOWN] - head_posns[lowest][UP] ;
416 Real corr = gh_scm2double (me->get_grob_property ("stem-spacing-correction"));
417 corr = (delta <= 1) ? 0.0 : 0.25;
419 correction= -lowest * corr ;
422 *space += correction;
425 /* there used to be a correction for bar_xextent() here, but
426 it's unclear what that was good for ?
435 ADD_INTERFACE (Note_spacing,"note-spacing-interface",
437 "left-items right-items stem-spacing-correction knee-spacing-correction");