2 note-spacing.cc -- implement Note_spacing
4 source file of the GNU LilyPond music typesetter
6 (c) 2001--2004 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"
26 TODO: detect hshifts due to collisions, and account for them in
32 Note_spacing::get_spacing (Grob *me, Item* right_col,
33 Real base_space, Real increment, Real *space, Real *fixed)
36 Drul_array<SCM> props(me->get_grob_property ("left-items"),
37 me->get_grob_property ("right-items"));
39 Direction col_dir = right_col->break_status_dir ();
40 Drul_array<Interval> extents;
42 Interval left_head_wid;
45 for (SCM s = props[d]; gh_pair_p (s); s = gh_cdr (s))
47 Item * it= dynamic_cast<Item*> (unsmob_grob (gh_car(s)));
49 if (d == RIGHT && it->break_status_dir () != col_dir)
51 it = it -> find_prebroken_piece (col_dir);
55 some kind of mismatch, eg. a note column, that is behind a
61 Item *it_col = it->get_column ();
62 if (d == RIGHT && right_col != it_col)
65 if (Separation_item::has_interface (it))
67 extents[d].unite (Separation_item::width (it));
73 SCM r = it->get_grob_property ("rest");
74 Grob * g = unsmob_grob (r);
76 g = Note_column::first_head (it);
79 Ugh. If Stem is switched off, we don't know what the
80 first note head will be.
83 left_head_wid = g->extent(it_col, X_AXIS);
86 extents[d].unite (it->extent (it_col, X_AXIS));
89 Grob * accs = Note_column::accidentals (it);
91 accs = Note_column::accidentals (it->get_parent (X_AXIS));
96 Accidental_placement::get_relevant_accidental_extent (accs, it_col, me);
103 if (extents[d].is_empty ())
104 extents[d] = Interval (0,0);
106 while (flip (&d) != LEFT);
110 We look at the width of the note head, since smaller heads get less space
111 eg. a quarter rest gets almost 0.5 ss less horizontal space than a note.
113 What is sticking out of the note head (eg. a flag), doesn't get
114 the full amount of space.
116 FIXED also includes the left part of the right object.
119 (left_head_wid.is_empty () ? increment :
123 (left_head_wid[RIGHT]+
126 What's sticking out of the head, eg. a flag:
128 (extents[LEFT][RIGHT] - left_head_wid[RIGHT])/2))
131 What is sticking out of the right note:
133 + (extents[RIGHT].is_empty () ? 0.0 : - extents[RIGHT][LEFT] / 2);
136 We don't do complicated stuff: (base_space - increment) is the
137 normal amount of white, which also determines the amount of
138 stretch. Upon (extreme) stretching, notes with accidentals should
139 stretch as much as notes without accidentals.
141 *space = (base_space - increment) + *fixed ;
143 if (Item::breakable_b (right_col)
144 || right_col->original_)
147 This is for the situation
152 *space += -extents[RIGHT][LEFT];
153 *fixed += -extents[RIGHT][LEFT];
156 stem_dir_correction (me, right_col, increment, space, fixed);
160 Note_spacing::left_column (Grob *me)
165 return dynamic_cast<Item*> (me)->get_column ();
169 Compute the column of the right-items. This is a big function,
170 since RIGHT-ITEMS may span more columns (eg. if a clef if inserted,
171 this will add a new columns to RIGHT-ITEMS. Here we look at the
172 columns, and return the left-most. If there are multiple columns, we
177 Note_spacing::right_column (Grob*me)
182 SCM right = me->get_grob_property ("right-items");
184 int min_rank = INT_MAX;
186 for (SCM s = right ; gh_pair_p (s) ; s = gh_cdr (s))
188 Item * ri = unsmob_item (gh_car (s));
190 Item * col = ri->get_column ();
191 int rank = Paper_column::get_rank (col);
205 // I'm a lazy bum. We could do this in-place.
206 SCM newright = SCM_EOL;
207 for (SCM s = right ; gh_pair_p (s) ; s =gh_cdr (s))
209 if (unsmob_item (gh_car (s))->get_column () == mincol)
210 newright = gh_cons (gh_car (s), newright);
213 me->set_grob_property ("right-items", newright);
219 int r = Paper_column::get_rank (dynamic_cast<Item*>(me)->get_column ());
220 programming_error (_f("Spacing wish column %d has no right item.", r));
230 Correct for optical illusions. See [Wanske] p. 138. The combination
231 up-stem + down-stem should get extra space, the combination
232 down-stem + up-stem less.
234 TODO: have to check wether the stems are in the same staff.
238 Note_spacing::stem_dir_correction (Grob*me, Item * rcolumn,
240 Real * space, Real *fixed)
242 Drul_array<Direction> stem_dirs(CENTER,CENTER);
243 Drul_array<Interval> stem_posns;
244 Drul_array<Interval> head_posns;
245 Drul_array<SCM> props(me->get_grob_property ("left-items"),
246 me->get_grob_property ("right-items"));
248 Drul_array<Grob*> beams_drul(0,0);
249 Drul_array<Grob*> stems_drul(0,0);
251 stem_dirs[LEFT] = stem_dirs[RIGHT] = CENTER;
253 Interval bar_xextent;
254 Interval bar_yextent;
256 bool correct_stem_dirs = true;
258 bool acc_right = false;
262 for (SCM s = props[d]; gh_pair_p (s); s = gh_cdr (s))
264 Item * it= dynamic_cast<Item*> (unsmob_grob (gh_car(s)));
267 acc_right = acc_right || Note_column::accidentals (it);
269 Grob *stem = Note_column::get_stem (it);
271 if (!stem || !stem->live ())
273 if (d == RIGHT && Separation_item::has_interface (it))
275 if (it->get_column () != rcolumn)
277 it = it->find_prebroken_piece (rcolumn->break_status_dir ());
280 Grob *last = Separation_item::extremal_break_aligned_grob (it, LEFT, &bar_xextent);
283 bar_yextent = Staff_spacing::bar_y_positions (last);
291 if(Stem::invisible_b (stem))
293 correct_stem_dirs = false;
297 stems_drul[d] = stem;
298 beams_drul[d] = Stem::get_beam (stem);
301 Direction sd = Stem::get_direction (stem);
302 if (stem_dirs[d] && stem_dirs[d] != sd)
304 correct_stem_dirs = false;
310 Correction doesn't seem appropriate when there is a large flag
311 hanging from the note.
314 && Stem::duration_log (stem) > 2 && !Stem::get_beam (stem))
316 correct_stem_dirs = false;
319 Interval hp = Stem::head_positions (stem);
320 Real chord_start = hp[sd];
321 Real stem_end = Stem::stem_end_position (stem);
323 stem_posns[d] = Interval(chord_start<?stem_end, chord_start>? stem_end);
324 head_posns[d].unite (hp);
327 while (flip (&d) != LEFT);
331 don't correct if accidentals are sticking out of the right side.
336 Real correction = 0.0;
338 if (!bar_yextent.is_empty ())
340 stem_dirs[RIGHT] = - stem_dirs[LEFT];
341 stem_posns[RIGHT] = bar_yextent;
344 if (correct_stem_dirs && stem_dirs[LEFT] *stem_dirs[RIGHT] == -1)
346 if (beams_drul[LEFT] && beams_drul[LEFT] == beams_drul[RIGHT])
350 this is a knee: maximal correction.
352 Real note_head_width = increment;
353 Grob * st = stems_drul[RIGHT];
354 Grob * head = st ? Stem::support_head (st) : 0;
356 Interval head_extent;
359 head_extent = head->extent (rcolumn, X_AXIS);
361 if (!head_extent.is_empty ())
362 note_head_width = head_extent[RIGHT];
366 Real thick = Stem::thickness (st);
368 note_head_width -= thick;
372 correction = note_head_width* stem_dirs[LEFT];
373 correction *= robust_scm2double (me->get_grob_property ("knee-spacing-correction"), 0);
374 *fixed += correction;
378 intersect = stem_posns[LEFT];
379 intersect.intersect(stem_posns[RIGHT]);
380 correct_stem_dirs = correct_stem_dirs && !intersect.is_empty ();
382 if (correct_stem_dirs)
384 correction =abs (intersect.length ());
390 correction = (correction/7) <? 1.0;
391 correction *= stem_dirs[LEFT] ;
393 robust_scm2double (me->get_grob_property ("stem-spacing-correction"), 0);
396 if (!bar_yextent.is_empty ())
402 else if (correct_stem_dirs && stem_dirs[LEFT] *stem_dirs[RIGHT] == UP)
405 Correct for the following situation:
414 ^ move the center one to the left.
417 this effect seems to be much more subtle than the
418 stem-direction stuff (why?), and also does not scale with the
419 difference in stem length.
424 Interval hp = head_posns[LEFT];
425 hp.intersect (head_posns[RIGHT]);
430 (head_posns[LEFT][DOWN] > head_posns[RIGHT][UP]) ? RIGHT : LEFT;
432 Real delta = head_posns[-lowest][DOWN] - head_posns[lowest][UP] ;
433 Real corr = robust_scm2double (me->get_grob_property ("stem-spacing-correction"), 0);
434 corr = (delta <= 1) ? 0.0 : 0.25;
436 correction= -lowest * corr ;
439 *space += correction;
441 /* there used to be a correction for bar_xextent() here, but
442 it's unclear what that was good for ?
450 ADD_INTERFACE (Note_spacing,"note-spacing-interface",
452 "left-items right-items stem-spacing-correction knee-spacing-correction");