* lily/include/lily-guile.hh: many new ly_ functions. Thanks to
[lilypond.git] / lily / note-spacing.cc
blob6df5ab17c301ff8630c577c14fca4fecde254863
1 /*
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>
7 */
10 #include "paper-column.hh"
11 #include "item.hh"
12 #include "moment.hh"
13 #include "note-spacing.hh"
14 #include "grob.hh"
15 #include "note-column.hh"
16 #include "warn.hh"
17 #include "stem.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
27 spacing?
29 */
31 void
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"));
38 Direction d = LEFT;
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_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
56 linebreak.
58 if (!it)
59 continue;
61 Item *it_col = it->get_column ();
62 if (d == RIGHT && right_col != it_col)
63 continue;
65 if (Separation_item::has_interface (it))
67 extents[d].unite (Separation_item::width (it));
68 continue;
71 if (d == LEFT)
73 SCM r = it->get_property ("rest");
74 Grob * g = unsmob_grob (r);
75 if (!g)
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.
82 if (g)
83 left_head_wid = g->extent (it_col, X_AXIS);
86 extents[d].unite (it->extent (it_col, X_AXIS));
87 if (d == RIGHT)
89 Grob * accs = Note_column::accidentals (it);
90 if (!accs)
91 accs = Note_column::accidentals (it->get_parent (X_AXIS));
93 if (accs)
95 Interval v =
96 Accidental_placement::get_relevant_accidental_extent (accs, it_col, me);
98 extents[d].unite (v);
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.
118 *fixed =
119 (left_head_wid.is_empty () ? increment :
121 Size of the head:
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
150 rest | 3/4 (eol)
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);
164 Item *
165 Note_spacing::left_column (Grob *me)
167 if (!me->live ())
168 return 0;
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
178 prune RIGHT-ITEMS.
181 Item *
182 Note_spacing::right_column (Grob*me)
184 if (!me->live ())
185 return 0;
187 SCM right = me->get_property ("right-items");
188 Item *mincol = 0;
189 int min_rank = INT_MAX;
190 bool prune = false;
191 for (SCM s = right ; ly_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);
198 if (rank < min_rank)
200 min_rank = rank;
201 if (mincol)
202 prune = true;
204 mincol = col;
208 if (prune)
210 // I'm a lazy bum. We could do this in-place.
211 SCM newright = SCM_EOL;
212 for (SCM s = right ; ly_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);
221 if (!mincol)
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));
228 return 0;
231 return mincol;
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.
242 void
243 Note_spacing::stem_dir_correction (Grob*me, Item * rcolumn,
244 Real increment,
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<Grob*> beams_drul (0,0);
254 Drul_array<Grob*> stems_drul (0,0);
256 stem_dirs[LEFT] = stem_dirs[RIGHT] = CENTER;
257 Interval intersect;
258 Interval bar_xextent;
259 Interval bar_yextent;
261 bool correct_stem_dirs = true;
262 Direction d = LEFT;
263 bool acc_right = false;
267 for (SCM s = props[d]; ly_pair_p (s); s = ly_cdr (s))
269 Item * it= dynamic_cast<Item*> (unsmob_grob (ly_car (s)));
271 if (d == RIGHT)
272 acc_right = acc_right || Note_column::accidentals (it);
274 Grob *stem = Note_column::get_stem (it);
276 if (!stem || !stem->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);
287 if (last)
288 bar_yextent = Staff_spacing::bar_y_positions (last);
290 break;
293 return ;
296 if (Stem::is_invisible (stem))
298 correct_stem_dirs = false;
299 continue;
302 stems_drul[d] = stem;
303 beams_drul[d] = Stem::get_beam (stem);
306 Direction sd = Stem::get_direction (stem);
307 if (stem_dirs[d] && stem_dirs[d] != sd)
309 correct_stem_dirs = false;
310 continue;
312 stem_dirs[d] = sd;
315 Correction doesn't seem appropriate when there is a large flag
316 hanging from the note.
318 if (d == LEFT
319 && Stem::duration_log (stem) > 2 && !Stem::get_beam (stem))
321 correct_stem_dirs = false;
324 Interval hp = Stem::head_positions (stem);
325 Real chord_start = hp[sd];
326 Real stem_end = Stem::stem_end_position (stem);
328 stem_posns[d] = Interval (chord_start<?stem_end, chord_start>? stem_end);
329 head_posns[d].unite (hp);
332 while (flip (&d) != LEFT);
336 don't correct if accidentals are sticking out of the right side.
338 if (acc_right)
339 return ;
341 Real correction = 0.0;
343 if (!bar_yextent.is_empty ())
345 stem_dirs[RIGHT] = - stem_dirs[LEFT];
346 stem_posns[RIGHT] = bar_yextent;
349 if (correct_stem_dirs && stem_dirs[LEFT] *stem_dirs[RIGHT] == -1)
351 if (beams_drul[LEFT] && beams_drul[LEFT] == beams_drul[RIGHT])
355 this is a knee: maximal correction.
357 Real note_head_width = increment;
358 Grob * st = stems_drul[RIGHT];
359 Grob * head = st ? Stem::support_head (st) : 0;
361 Interval head_extent;
362 if (head)
364 head_extent = head->extent (rcolumn, X_AXIS);
366 if (!head_extent.is_empty ())
367 note_head_width = head_extent[RIGHT];
369 if (st)
371 Real thick = Stem::thickness (st);
373 note_head_width -= thick;
377 correction = note_head_width* stem_dirs[LEFT];
378 correction *= robust_scm2double (me->get_property ("knee-spacing-correction"), 0);
379 *fixed += correction;
381 else
383 intersect = stem_posns[LEFT];
384 intersect.intersect (stem_posns[RIGHT]);
385 correct_stem_dirs = correct_stem_dirs && !intersect.is_empty ();
387 if (correct_stem_dirs)
389 correction =abs (intersect.length ());
393 Ugh. 7 is hardcoded.
395 correction = (correction/7) <? 1.0;
396 correction *= stem_dirs[LEFT] ;
397 correction *=
398 robust_scm2double (me->get_property ("stem-spacing-correction"), 0);
401 if (!bar_yextent.is_empty ())
403 correction *= 0.5;
407 else if (correct_stem_dirs && stem_dirs[LEFT] *stem_dirs[RIGHT] == UP)
410 Correct for the following situation:
413 | |
415 | X |
416 | | |
417 ========
419 ^ move the center one to the left.
422 this effect seems to be much more subtle than the
423 stem-direction stuff (why?), and also does not scale with the
424 difference in stem length.
429 Interval hp = head_posns[LEFT];
430 hp.intersect (head_posns[RIGHT]);
431 if (!hp.is_empty ())
432 return ;
434 Direction lowest =
435 (head_posns[LEFT][DOWN] > head_posns[RIGHT][UP]) ? RIGHT : LEFT;
437 Real delta = head_posns[-lowest][DOWN] - head_posns[lowest][UP] ;
438 Real corr = robust_scm2double (me->get_property ("stem-spacing-correction"), 0);
439 corr = (delta <= 1) ? 0.0 : 0.25;
441 correction= -lowest * corr ;
444 *space += correction;
446 /* there used to be a correction for bar_xextent () here, but
447 it's unclear what that was good for ?
455 ADD_INTERFACE (Note_spacing,"note-spacing-interface",
456 "This object calculates spacing wishes for individual voices.",
457 "left-items right-items stem-spacing-correction knee-spacing-correction");