]> git.donarmstrong.com Git - lilypond.git/blob - lily/note-spacing.cc
cleanups 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 (min_dist, left_head_end + min_dist/2);
70
71   /*
72     We don't do complicated stuff: (base_space - increment) is the
73     normal amount of white, which also determines the amount of
74     stretch. Upon (extreme) stretching, notes with accidentals should
75     stretch as much as notes without accidentals.
76   */
77   *space = (base_space - increment) + *fixed;
78
79   stem_dir_correction (me, right_col, increment, space, fixed);
80 }
81
82
83 /**
84    Correct for optical illusions. See [Wanske] p. 138. The combination
85    up-stem + down-stem should get extra space, the combination
86    down-stem + up-stem less.
87
88    TODO: have to check wether the stems are in the same staff.
89 */
90 void
91 Note_spacing::stem_dir_correction (Grob *me, Item *rcolumn,
92                                    Real increment,
93                                    Real *space, Real *fixed)
94 {
95   Drul_array<Direction> stem_dirs (CENTER, CENTER);
96   Drul_array<Interval> stem_posns;
97   Drul_array<Interval> head_posns;
98   Drul_array<SCM> props (me->get_object ("left-items"),
99                          me->get_object ("right-items"));
100
101   Drul_array<Spanner *> beams_drul (0, 0);
102   Drul_array<Grob *> stems_drul (0, 0);
103
104   stem_dirs[LEFT] = stem_dirs[RIGHT] = CENTER;
105   Interval intersect;
106   Interval bar_xextent;
107   Interval bar_yextent;
108
109   bool correct_stem_dirs = true;
110   Direction d = LEFT;
111   bool acc_right = false;
112
113   do
114     {
115       vector<Grob*> const &items (ly_scm2link_array (props [d]));
116       for (vsize i = 0; i < items.size (); i++)
117         {
118           Item *it = dynamic_cast<Item *> (items[i]);
119
120           if (d == RIGHT)
121             acc_right = acc_right || Note_column::accidentals (it);
122
123           Grob *stem = Note_column::get_stem (it);
124
125           if (!stem || !stem->is_live ())
126             {
127               if (d == RIGHT && Separation_item::has_interface (it))
128                 {
129                   if (it->get_column () != rcolumn)
130                     it = it->find_prebroken_piece (rcolumn->break_status_dir ());
131
132                   Grob *last = Separation_item::extremal_break_aligned_grob (it, LEFT, &bar_xextent);
133
134                   if (last)
135                     bar_yextent = Staff_spacing::bar_y_positions (last);
136
137                   break;
138                 }
139
140               return;
141             }
142
143           if (Stem::is_invisible (stem))
144             {
145               correct_stem_dirs = false;
146               continue;
147             }
148
149           stems_drul[d] = stem;
150           beams_drul[d] = Stem::get_beam (stem);
151
152           Direction stem_dir = get_grob_direction (stem);
153           if (stem_dirs[d] && stem_dirs[d] != stem_dir)
154             {
155               correct_stem_dirs = false;
156               continue;
157             }
158           stem_dirs[d] = stem_dir;
159
160           /*
161             Correction doesn't seem appropriate  when there is a large flag
162             hanging from the note.
163           */
164           if (d == LEFT
165               && Stem::duration_log (stem) > 2 && !Stem::get_beam (stem))
166             correct_stem_dirs = false;
167
168           Interval hp = Stem::head_positions (stem);
169           if (correct_stem_dirs
170               && !hp.is_empty ())
171             {
172               Real chord_start = hp[stem_dir];
173
174               /*
175                 can't look at stem-end-position, since that triggers
176                 beam slope computations.
177               */
178               Real stem_end = hp[stem_dir] +
179                 stem_dir * robust_scm2double (stem->get_property ("length"), 7);
180
181               stem_posns[d] = Interval (min (chord_start, stem_end),
182                                         max (chord_start, stem_end));
183               head_posns[d].unite (hp);
184             }
185         }
186     }
187   while (flip (&d) != LEFT);
188
189   /*
190     don't correct if accidentals are sticking out of the right side.
191   */
192   if (acc_right)
193     return;
194
195   Real correction = 0.0;
196
197   if (!bar_yextent.is_empty ())
198     {
199       stem_dirs[RIGHT] = -stem_dirs[LEFT];
200       stem_posns[RIGHT] = bar_yextent;
201       stem_posns[RIGHT] *= 2;
202     }
203
204   if (correct_stem_dirs && stem_dirs[LEFT] * stem_dirs[RIGHT] == -1)
205     {
206       if (beams_drul[LEFT] && beams_drul[LEFT] == beams_drul[RIGHT])
207         {
208
209           /*
210             this is a knee: maximal correction.
211           */
212           Real note_head_width = increment;
213           Grob *st = stems_drul[RIGHT];
214           Grob *head = st ? Stem::support_head (st) : 0;
215
216           Interval head_extent;
217           if (head)
218             {
219               head_extent = head->extent (rcolumn, X_AXIS);
220
221               if (!head_extent.is_empty ())
222                 note_head_width = head_extent[RIGHT];
223
224               note_head_width -= Stem::thickness (st);
225             }
226
227           correction = note_head_width * stem_dirs[LEFT];
228           correction *= robust_scm2double (me->get_property ("knee-spacing-correction"), 0);
229           *fixed += correction;
230         }
231       else
232         {
233           intersect = stem_posns[LEFT];
234           intersect.intersect (stem_posns[RIGHT]);
235           correct_stem_dirs = correct_stem_dirs && !intersect.is_empty ();
236
237           if (correct_stem_dirs)
238             {
239               correction = abs (intersect.length ());
240
241               /*
242                 Ugh. 7 is hardcoded.
243               */
244               correction = min (correction / 7, 1.0);
245               correction *= stem_dirs[LEFT];
246               correction
247                 *= robust_scm2double (me->get_property ("stem-spacing-correction"), 0);
248             }
249
250           if (!bar_yextent.is_empty ())
251             correction *= 0.5;
252         }
253     }
254   else if (correct_stem_dirs && stem_dirs[LEFT] * stem_dirs[RIGHT] == UP)
255     {
256       /*
257         Correct for the following situation:
258
259         X      X
260         |      |
261         |      |
262         |   X  |
263         |  |   |
264         ========
265
266         ^ move the center one to the left.
267
268
269         this effect seems to be much more subtle than the
270         stem-direction stuff (why?), and also does not scale with the
271         difference in stem length.
272
273       */
274
275       Interval hp = head_posns[LEFT];
276       hp.intersect (head_posns[RIGHT]);
277       if (!hp.is_empty ())
278         return;
279
280       Direction lowest
281         = (head_posns[LEFT][DOWN] > head_posns[RIGHT][UP]) ? RIGHT : LEFT;
282
283       Real delta = head_posns[-lowest][DOWN] - head_posns[lowest][UP];
284       Real corr = robust_scm2double (me->get_property ("same-direction-correction"), 0);
285
286       if (delta > 1)
287         correction = -lowest * corr;
288     }
289
290   *space += correction;
291
292   /* there used to be a correction for bar_xextent () here, but
293      it's unclear what that was good for ?
294   */
295 }
296
297 ADD_INTERFACE (Note_spacing,
298                "This object calculates spacing wishes for individual voices.",
299
300                
301                "knee-spacing-correction "
302                "left-items "
303                "right-items "
304                "same-direction-correction "
305                "stem-spacing-correction "
306
307                );
308