]> git.donarmstrong.com Git - lilypond.git/blob - lily/note-spacing.cc
include accidentals and arpeggios in note spacing
[lilypond.git] / lily / note-spacing.cc
1 /*
2   note-spacing.cc -- implement Note_spacing
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 2001--2007  Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
8
9 #include "note-spacing.hh"
10
11 #include "directional-element-interface.hh"
12 #include "grob-array.hh"
13 #include "paper-column.hh"
14 #include "moment.hh"
15 #include "note-column.hh"
16 #include "warn.hh"
17 #include "stem.hh"
18 #include "separation-item.hh"
19 #include "spacing-interface.hh"
20 #include "staff-spacing.hh"
21 #include "accidental-placement.hh"
22 #include "output-def.hh"
23 #include "pointer-group-interface.hh"
24
25 /*
26   TODO: detect hshifts due to collisions, and account for them in
27   spacing?
28 */
29
30 void
31 Note_spacing::get_spacing (Grob *me, Item *right_col,
32                            Real base_space, Real increment, Real *space, Real *fixed)
33 {
34   vector<Item*> note_columns = Spacing_interface::left_note_columns (me);
35   Real left_head_end = 0;
36
37   for (vsize i = 0; i < note_columns.size (); i++)
38     {
39        SCM r = note_columns[i]->get_object ("rest");
40        Grob *g = unsmob_grob (r);
41        Grob *col = note_columns[i]->get_column ();
42
43        if (!g)
44          g = Note_column::first_head (note_columns[i]);
45
46        /*
47          Ugh. If Stem is switched off, we don't know what the
48          first note head will be.
49        */
50        if (g)
51          {
52            if (g->common_refpoint (col, X_AXIS) != col)
53              programming_error ("Note_spacing::get_spacing (): Common refpoint incorrect");
54            else
55              left_head_end = g->extent (col, X_AXIS)[RIGHT];
56          }
57     }
58
59   /*
60     We look at the width of the note head, since smaller heads get less space
61
62     eg. a quarter rest gets almost 0.5 ss less horizontal space than a note.
63
64     What is sticking out of the note head (eg. a flag), doesn't get
65     the full amount of space.
66   */
67   Real min_dist = Spacing_interface::minimum_distance (me);
68
69   *fixed = max (left_head_end + (min_dist - left_head_end) / 2,
70                 min_dist - (base_space - increment) / 2);
71
72   /*
73     We don't do complicated stuff: (base_space - increment) is the
74     normal amount of white, which also determines the amount of
75     stretch. Upon (extreme) stretching, notes with accidentals should
76     stretch as much as notes without accidentals.
77   */
78   *space = (base_space - increment) + *fixed;
79
80   stem_dir_correction (me, right_col, increment, space, fixed);
81 }
82
83
84 /**
85    Correct for optical illusions. See [Wanske] p. 138. The combination
86    up-stem + down-stem should get extra space, the combination
87    down-stem + up-stem less.
88
89    TODO: have to check whether the stems are in the same staff.
90 */
91 void
92 Note_spacing::stem_dir_correction (Grob *me, Item *rcolumn,
93                                    Real increment,
94                                    Real *space, Real *fixed)
95 {
96   Drul_array<Direction> stem_dirs (CENTER, CENTER);
97   Drul_array<Interval> stem_posns;
98   Drul_array<Interval> head_posns;
99   Drul_array<SCM> props (me->get_object ("left-items"),
100                          me->get_object ("right-items"));
101
102   Drul_array<Spanner *> beams_drul (0, 0);
103   Drul_array<Grob *> stems_drul (0, 0);
104
105   stem_dirs[LEFT] = stem_dirs[RIGHT] = CENTER;
106   Interval intersect;
107   Interval bar_xextent;
108   Interval bar_yextent;
109
110   bool correct_stem_dirs = true;
111   Direction d = LEFT;
112   bool acc_right = false;
113
114   do
115     {
116       vector<Grob*> const &items (ly_scm2link_array (props [d]));
117       for (vsize i = 0; i < items.size (); i++)
118         {
119           Item *it = dynamic_cast<Item *> (items[i]);
120
121           if (d == RIGHT)
122             acc_right = acc_right || Note_column::accidentals (it);
123
124           Grob *stem = Note_column::get_stem (it);
125
126           if (!stem || !stem->is_live ())
127             {
128               if (d == RIGHT && Separation_item::has_interface (it))
129                 {
130                   if (it->get_column () != rcolumn)
131                     it = it->find_prebroken_piece (rcolumn->break_status_dir ());
132
133                   Grob *last = Separation_item::extremal_break_aligned_grob (it, LEFT, &bar_xextent);
134
135                   if (last)
136                     bar_yextent = Staff_spacing::bar_y_positions (last);
137
138                   break;
139                 }
140
141               return;
142             }
143
144           if (Stem::is_invisible (stem))
145             {
146               correct_stem_dirs = false;
147               continue;
148             }
149
150           stems_drul[d] = stem;
151           beams_drul[d] = Stem::get_beam (stem);
152
153           Direction stem_dir = get_grob_direction (stem);
154           if (stem_dirs[d] && stem_dirs[d] != stem_dir)
155             {
156               correct_stem_dirs = false;
157               continue;
158             }
159           stem_dirs[d] = stem_dir;
160
161           /*
162             Correction doesn't seem appropriate  when there is a large flag
163             hanging from the note.
164           */
165           if (d == LEFT
166               && Stem::duration_log (stem) > 2 && !Stem::get_beam (stem))
167             correct_stem_dirs = false;
168
169           Interval hp = Stem::head_positions (stem);
170           if (correct_stem_dirs
171               && !hp.is_empty ())
172             {
173               Real chord_start = hp[stem_dir];
174
175               /*
176                 can't look at stem-end-position, since that triggers
177                 beam slope computations.
178               */
179               Real stem_end = hp[stem_dir] +
180                 stem_dir * robust_scm2double (stem->get_property ("length"), 7);
181
182               stem_posns[d] = Interval (min (chord_start, stem_end),
183                                         max (chord_start, stem_end));
184               head_posns[d].unite (hp);
185             }
186         }
187     }
188   while (flip (&d) != LEFT);
189
190   /*
191     don't correct if accidentals are sticking out of the right side.
192   */
193   if (acc_right)
194     return;
195
196   Real correction = 0.0;
197
198   if (!bar_yextent.is_empty ())
199     {
200       stem_dirs[RIGHT] = -stem_dirs[LEFT];
201       stem_posns[RIGHT] = bar_yextent;
202       stem_posns[RIGHT] *= 2;
203     }
204
205   if (correct_stem_dirs && stem_dirs[LEFT] * stem_dirs[RIGHT] == -1)
206     {
207       if (beams_drul[LEFT] && beams_drul[LEFT] == beams_drul[RIGHT])
208         {
209
210           /*
211             this is a knee: maximal correction.
212           */
213           Real note_head_width = increment;
214           Grob *st = stems_drul[RIGHT];
215           Grob *head = st ? Stem::support_head (st) : 0;
216
217           Interval head_extent;
218           if (head)
219             {
220               head_extent = head->extent (rcolumn, X_AXIS);
221
222               if (!head_extent.is_empty ())
223                 note_head_width = head_extent[RIGHT];
224
225               note_head_width -= Stem::thickness (st);
226             }
227
228           correction = note_head_width * stem_dirs[LEFT];
229           correction *= robust_scm2double (me->get_property ("knee-spacing-correction"), 0);
230           *fixed += correction;
231         }
232       else
233         {
234           intersect = stem_posns[LEFT];
235           intersect.intersect (stem_posns[RIGHT]);
236           correct_stem_dirs = correct_stem_dirs && !intersect.is_empty ();
237
238           if (correct_stem_dirs)
239             {
240               correction = abs (intersect.length ());
241
242               /*
243                 Ugh. 7 is hardcoded.
244               */
245               correction = min (correction / 7, 1.0);
246               correction *= stem_dirs[LEFT];
247               correction
248                 *= robust_scm2double (me->get_property ("stem-spacing-correction"), 0);
249             }
250
251           if (!bar_yextent.is_empty ())
252             correction *= 0.5;
253         }
254     }
255   else if (correct_stem_dirs && stem_dirs[LEFT] * stem_dirs[RIGHT] == UP)
256     {
257       /*
258         Correct for the following situation:
259
260         X      X
261         |      |
262         |      |
263         |   X  |
264         |  |   |
265         ========
266
267         ^ move the center one to the left.
268
269
270         this effect seems to be much more subtle than the
271         stem-direction stuff (why?), and also does not scale with the
272         difference in stem length.
273
274       */
275
276       Interval hp = head_posns[LEFT];
277       hp.intersect (head_posns[RIGHT]);
278       if (!hp.is_empty ())
279         return;
280
281       Direction lowest
282         = (head_posns[LEFT][DOWN] > head_posns[RIGHT][UP]) ? RIGHT : LEFT;
283
284       Real delta = head_posns[-lowest][DOWN] - head_posns[lowest][UP];
285       Real corr = robust_scm2double (me->get_property ("same-direction-correction"), 0);
286
287       if (delta > 1)
288         correction = -lowest * corr;
289     }
290
291   *space += correction;
292
293   /* there used to be a correction for bar_xextent () here, but
294      it's unclear what that was good for ?
295   */
296 }
297
298 ADD_INTERFACE (Note_spacing,
299                "This object calculates spacing wishes for individual voices.",
300
301                
302                "knee-spacing-correction "
303                "left-items "
304                "right-items "
305                "same-direction-correction "
306                "stem-spacing-correction "
307
308                );
309