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 "output-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_property ("left-items"),
37 me->get_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]; ly_c_pair_p (s); s = ly_cdr (s))
47 Item * it= dynamic_cast<Item*> (unsmob_grob (ly_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_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 (!extents[RIGHT].is_empty ()
144 && (Item::is_breakable (right_col)
145 || right_col->original_))
148 This is for the situation
152 Since we only take half of the right-object space above, the
153 barline will bump into the notes preceding it, if the right
154 thing is big. We add the rest of the extents here:
157 *space += -extents[RIGHT][LEFT] / 2;
158 *fixed += -extents[RIGHT][LEFT] / 2;
161 stem_dir_correction (me, right_col, increment, space, fixed);
165 Note_spacing::left_column (Grob *me)
170 return dynamic_cast<Item*> (me)->get_column ();
174 Compute the column of the right-items. This is a big function,
175 since RIGHT-ITEMS may span more columns (eg. if a clef if inserted,
176 this will add a new columns to RIGHT-ITEMS. Here we look at the
177 columns, and return the left-most. If there are multiple columns, we
182 Note_spacing::right_column (Grob*me)
187 SCM right = me->get_property ("right-items");
189 int min_rank = INT_MAX;
191 for (SCM s = right ; ly_c_pair_p (s) ; s = ly_cdr (s))
193 Item * ri = unsmob_item (ly_car (s));
195 Item * col = ri->get_column ();
196 int rank = Paper_column::get_rank (col);
210 // I'm a lazy bum. We could do this in-place.
211 SCM newright = SCM_EOL;
212 for (SCM s = right ; ly_c_pair_p (s) ; s =ly_cdr (s))
214 if (unsmob_item (ly_car (s))->get_column () == mincol)
215 newright = scm_cons (ly_car (s), newright);
218 me->set_property ("right-items", newright);
224 int r = Paper_column::get_rank (dynamic_cast<Item*>(me)->get_column ());
225 programming_error (_f ("Spacing wish column %d has no right item.", r));
235 Correct for optical illusions. See [Wanske] p. 138. The combination
236 up-stem + down-stem should get extra space, the combination
237 down-stem + up-stem less.
239 TODO: have to check wether the stems are in the same staff.
243 Note_spacing::stem_dir_correction (Grob*me, Item * rcolumn,
245 Real * space, Real *fixed)
247 Drul_array<Direction> stem_dirs (CENTER,CENTER);
248 Drul_array<Interval> stem_posns;
249 Drul_array<Interval> head_posns;
250 Drul_array<SCM> props (me->get_property ("left-items"),
251 me->get_property ("right-items"));
253 Drul_array<Spanner*> beams_drul (0,0);
254 Drul_array<Grob*> stems_drul (0,0);
256 stem_dirs[LEFT] = stem_dirs[RIGHT] = CENTER;
258 Interval bar_xextent;
259 Interval bar_yextent;
261 bool correct_stem_dirs = true;
263 bool acc_right = false;
267 for (SCM s = props[d]; ly_c_pair_p (s); s = ly_cdr (s))
269 Item * it= dynamic_cast<Item*> (unsmob_grob (ly_car (s)));
272 acc_right = acc_right || Note_column::accidentals (it);
274 Grob *stem = Note_column::get_stem (it);
276 if (!stem || !stem->is_live ())
278 if (d == RIGHT && Separation_item::has_interface (it))
280 if (it->get_column () != rcolumn)
282 it = it->find_prebroken_piece (rcolumn->break_status_dir ());
285 Grob *last = Separation_item::extremal_break_aligned_grob (it, LEFT, &bar_xextent);
288 bar_yextent = Staff_spacing::bar_y_positions (last);
296 if (Stem::is_invisible (stem))
298 correct_stem_dirs = false;
302 stems_drul[d] = stem;
303 beams_drul[d] = Stem::get_beam (stem);
305 Direction sd = Stem::get_direction (stem);
306 if (stem_dirs[d] && stem_dirs[d] != sd)
308 correct_stem_dirs = false;
314 Correction doesn't seem appropriate when there is a large flag
315 hanging from the note.
318 && Stem::duration_log (stem) > 2 && !Stem::get_beam (stem))
320 correct_stem_dirs = false;
323 Interval hp = Stem::head_positions (stem);
324 Real chord_start = hp[sd];
325 Real stem_end = Stem::stem_end_position (stem);
327 stem_posns[d] = Interval (chord_start<?stem_end, chord_start>? stem_end);
328 head_posns[d].unite (hp);
331 while (flip (&d) != LEFT);
335 don't correct if accidentals are sticking out of the right side.
340 Real correction = 0.0;
342 if (!bar_yextent.is_empty ())
344 stem_dirs[RIGHT] = - stem_dirs[LEFT];
345 stem_posns[RIGHT] = bar_yextent;
348 if (correct_stem_dirs && stem_dirs[LEFT] *stem_dirs[RIGHT] == -1)
350 if (beams_drul[LEFT] && beams_drul[LEFT] == beams_drul[RIGHT])
354 this is a knee: maximal correction.
356 Real note_head_width = increment;
357 Grob * st = stems_drul[RIGHT];
358 Grob * head = st ? Stem::support_head (st) : 0;
360 Interval head_extent;
363 head_extent = head->extent (rcolumn, X_AXIS);
365 if (!head_extent.is_empty ())
366 note_head_width = head_extent[RIGHT];
370 Real thick = Stem::thickness (st);
372 note_head_width -= thick;
376 correction = note_head_width* stem_dirs[LEFT];
377 correction *= robust_scm2double (me->get_property ("knee-spacing-correction"), 0);
378 *fixed += correction;
382 intersect = stem_posns[LEFT];
383 intersect.intersect (stem_posns[RIGHT]);
384 correct_stem_dirs = correct_stem_dirs && !intersect.is_empty ();
386 if (correct_stem_dirs)
388 correction =abs (intersect.length ());
394 correction = (correction/7) <? 1.0;
395 correction *= stem_dirs[LEFT] ;
397 robust_scm2double (me->get_property ("stem-spacing-correction"), 0);
400 if (!bar_yextent.is_empty ())
406 else if (correct_stem_dirs && stem_dirs[LEFT] *stem_dirs[RIGHT] == UP)
409 Correct for the following situation:
418 ^ move the center one to the left.
421 this effect seems to be much more subtle than the
422 stem-direction stuff (why?), and also does not scale with the
423 difference in stem length.
428 Interval hp = head_posns[LEFT];
429 hp.intersect (head_posns[RIGHT]);
434 (head_posns[LEFT][DOWN] > head_posns[RIGHT][UP]) ? RIGHT : LEFT;
436 Real delta = head_posns[-lowest][DOWN] - head_posns[lowest][UP] ;
437 Real corr = robust_scm2double (me->get_property ("stem-spacing-correction"), 0);
438 corr = (delta <= 1) ? 0.0 : 0.25;
440 correction= -lowest * corr ;
443 *space += correction;
445 /* there used to be a correction for bar_xextent () here, but
446 it's unclear what that was good for ?
454 ADD_INTERFACE (Note_spacing,"note-spacing-interface",
455 "This object calculates spacing wishes for individual voices.",
456 "left-items right-items stem-spacing-correction knee-spacing-correction");