]> git.donarmstrong.com Git - lilypond.git/blob - lily/note-spacing.cc
* scm/music-functions.scm (voicify-music): add \context Staff for
[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--2004  Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8
9
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"
22
23
24 /*
25
26 TODO: detect hshifts due to collisions, and account for them in
27 spacing?
28
29 */ 
30
31 void
32 Note_spacing::get_spacing (Grob *me, Item* right_col,
33                            Real base_space, Real increment, Real *space, Real *fixed)
34 {
35
36   Drul_array<SCM> props(me->get_grob_property ("left-items"),
37                         me->get_grob_property ("right-items"));
38   Direction d = LEFT;
39   Direction col_dir =  right_col->break_status_dir ();
40   Drul_array<Interval> extents;
41
42   Interval left_head_wid; 
43   do
44     {
45       for (SCM  s = props[d]; gh_pair_p (s); s = gh_cdr (s))
46         {
47           Item * it= dynamic_cast<Item*> (unsmob_grob (gh_car(s)));
48           
49           if (d == RIGHT && it->break_status_dir () != col_dir)
50             {
51               it = it -> find_prebroken_piece (col_dir);
52
53             }
54           /*
55             some kind of mismatch, eg. a note column, that is behind a
56             linebreak.
57            */
58           if (!it)
59             continue; 
60
61           Item *it_col = it->get_column ();
62           if (d == RIGHT && right_col != it_col)
63             continue;
64           
65           if (Separation_item::has_interface (it))
66             {
67               extents[d].unite (Separation_item::width (it));
68               continue;
69             }
70
71           if (d == LEFT)
72             {
73               SCM r = it->get_grob_property ("rest");
74               Grob * g = unsmob_grob (r);
75               if (!g)
76                 g =  Note_column::first_head (it);
77
78               /*
79                 Ugh. If Stem is switched off, we don't know what the
80                 first note head will be.
81                */
82               if (g)
83                 left_head_wid = g->extent(it_col, X_AXIS);
84             }
85           
86           extents[d].unite (it->extent (it_col, X_AXIS));
87           if (d == RIGHT)
88             {
89               Grob * accs = Note_column::accidentals (it);
90               if (!accs)
91                 accs = Note_column::accidentals (it->get_parent (X_AXIS));
92               
93               if (accs)
94                 {
95                   Interval v =
96                     Accidental_placement::get_relevant_accidental_extent (accs, it_col, me);
97                     
98                   extents[d].unite (v);
99                 }
100             }
101         }
102
103       if (extents[d].is_empty ())
104         extents[d] = Interval (0,0);
105     }
106   while (flip (&d) != LEFT);
107
108
109   /*
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.
112
113     What is sticking out of the note head (eg. a flag), doesn't get
114     the full amount of space.
115
116     FIXED also includes the left part of the right object.
117   */
118   *fixed =
119     (left_head_wid.is_empty () ? increment :
120      /*
121        Size of the head:
122       */
123      (left_head_wid[RIGHT]+
124
125       /*
126        What's sticking out of the head, eg. a flag: 
127       */
128       (extents[LEFT][RIGHT] - left_head_wid[RIGHT])/2))
129
130     /*
131       What is sticking out of the right note:
132      */
133     + (extents[RIGHT].is_empty () ?  0.0 : - extents[RIGHT][LEFT] / 2);
134
135   /*
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.
140    */
141   *space = (base_space - increment) + *fixed ;
142
143   if (Item::breakable_b (right_col)
144       || right_col->original_)
145     {
146       /*
147         This is for the situation
148
149         rest | 3/4 (eol)
150         
151        */
152       *space += -extents[RIGHT][LEFT];
153       *fixed += -extents[RIGHT][LEFT];
154     }
155   
156   stem_dir_correction (me, right_col, increment, space, fixed);
157 }
158
159 Item *
160 Note_spacing::left_column (Grob *me)
161 {
162   if (!me->live())
163     return 0;
164   
165   return dynamic_cast<Item*> (me)->get_column ();
166 }
167
168 /*
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
173 prune RIGHT-ITEMS.
174    
175  */
176 Item *
177 Note_spacing::right_column (Grob*me)
178 {
179   if (!me->live())
180     return 0;
181   
182   SCM right = me->get_grob_property ("right-items");
183   Item *mincol = 0;
184   int min_rank = INT_MAX;
185   bool prune = false;
186   for (SCM s = right ; gh_pair_p (s) ; s = gh_cdr (s))
187     {
188       Item * ri = unsmob_item (gh_car (s));
189
190       Item * col = ri->get_column ();
191       int rank = Paper_column::get_rank (col);
192
193       if (rank < min_rank)
194         {
195           min_rank = rank;
196           if (mincol)
197             prune = true;
198
199           mincol = col;
200         }
201     }
202   
203   if (prune)
204     {
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))
208         {
209           if (unsmob_item (gh_car (s))->get_column () == mincol)
210             newright = gh_cons (gh_car (s), newright);
211         }
212
213       me->set_grob_property ("right-items", newright);
214     }
215   
216   if (!mincol)
217     {
218       /*
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));
221       */
222
223       return 0;
224     }
225
226   return mincol;
227 }
228
229 /**
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.
233
234    TODO: have to check wether the stems are in the same staff.
235
236 */
237 void
238 Note_spacing::stem_dir_correction (Grob*me, Item * rcolumn,
239                                    Real increment,
240                                    Real * space, Real *fixed)  
241 {
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"));
247
248   Drul_array<Grob*> beams_drul(0,0);
249   Drul_array<Grob*> stems_drul(0,0);
250   
251   stem_dirs[LEFT] = stem_dirs[RIGHT] = CENTER;
252   Interval intersect;
253   Interval bar_xextent;
254   Interval bar_yextent;  
255   
256   bool correct_stem_dirs = true;
257   Direction d = LEFT;
258   bool acc_right = false;
259   
260   do
261     {
262       for (SCM  s = props[d]; gh_pair_p (s); s = gh_cdr (s))
263         {
264           Item * it= dynamic_cast<Item*> (unsmob_grob (gh_car(s)));
265
266           if (d == RIGHT)
267             acc_right = acc_right || Note_column::accidentals (it);
268           
269           Grob *stem = Note_column::get_stem (it);
270
271           if (!stem || !stem->live ())
272             {
273               if (d == RIGHT && Separation_item::has_interface (it))
274                 {
275                   if (it->get_column () != rcolumn)
276                     {
277                       it = it->find_prebroken_piece (rcolumn->break_status_dir ());
278                     }
279                   
280                   Grob *last = Separation_item::extremal_break_aligned_grob (it, LEFT, &bar_xextent);
281
282                   if (last)
283                     bar_yextent = Staff_spacing::bar_y_positions (last);
284
285                   break;
286                 }
287
288               return ;
289             }
290           
291           if(Stem::invisible_b (stem))
292             {
293               correct_stem_dirs = false;
294               continue;
295             }
296
297           stems_drul[d] = stem;
298           beams_drul[d] = Stem::get_beam (stem);
299             
300           
301           Direction sd = Stem::get_direction (stem);
302           if (stem_dirs[d] && stem_dirs[d] != sd)
303             {
304               correct_stem_dirs = false;
305               continue;
306             }
307           stem_dirs[d] = sd;
308
309           /*
310             Correction doesn't seem appropriate  when there is a large flag
311             hanging from the note.
312            */
313           if (d == LEFT
314               && Stem::duration_log (stem) > 2  && !Stem::get_beam (stem))
315             {
316               correct_stem_dirs = false;
317             }
318           
319           Interval hp  = Stem::head_positions (stem);
320           Real chord_start = hp[sd];      
321           Real stem_end = Stem::stem_end_position (stem);
322           
323           stem_posns[d] = Interval(chord_start<?stem_end, chord_start>? stem_end);
324           head_posns[d].unite (hp);
325         }
326     }
327   while (flip (&d) != LEFT);
328   
329
330   /*
331     don't correct if accidentals are sticking out of the right side.
332   */
333   if (acc_right)
334     return ;
335
336   Real correction = 0.0;
337
338   if (!bar_yextent.is_empty ())
339     {
340       stem_dirs[RIGHT] = - stem_dirs[LEFT];
341       stem_posns[RIGHT] = bar_yextent;
342     }
343   
344   if (correct_stem_dirs && stem_dirs[LEFT] *stem_dirs[RIGHT] == -1)
345     {
346       if (beams_drul[LEFT] && beams_drul[LEFT] == beams_drul[RIGHT])
347         {
348           
349           /*
350             this is a knee: maximal correction.
351           */
352           Real note_head_width = increment;
353           Grob * st = stems_drul[RIGHT];
354           Grob * head = st ? Stem::support_head (st)  : 0;
355
356           Interval head_extent;
357           if (head)
358             {
359               head_extent = head->extent (rcolumn, X_AXIS);
360
361               if (!head_extent.is_empty ())
362                 note_head_width = head_extent[RIGHT];
363
364               if (st)
365                 {
366                   Real thick = Stem::thickness (st);
367
368                   note_head_width -= thick;
369                 }
370             }
371
372           correction = note_head_width* stem_dirs[LEFT];
373           correction *= robust_scm2double (me->get_grob_property ("knee-spacing-correction"), 0);
374           *fixed += correction;
375         }
376       else
377         {
378           intersect = stem_posns[LEFT];  
379           intersect.intersect(stem_posns[RIGHT]);
380           correct_stem_dirs = correct_stem_dirs && !intersect.is_empty ();
381
382           if (correct_stem_dirs)
383             {
384               correction =abs (intersect.length ());
385
386       
387               /*
388                 Ugh. 7 is hardcoded.
389               */
390               correction = (correction/7) <? 1.0;
391               correction *= stem_dirs[LEFT] ;
392               correction *=
393                 robust_scm2double (me->get_grob_property ("stem-spacing-correction"), 0);
394             }
395           
396           if (!bar_yextent.is_empty ())
397             {
398               correction *= 0.5;
399             }
400         }
401     }
402   else if (correct_stem_dirs && stem_dirs[LEFT] *stem_dirs[RIGHT] == UP)
403     {
404       /*
405         Correct for the following situation:
406
407          X      X
408         |      | 
409         |      |
410         |   X  |
411         |  |   |
412         ========
413
414            ^ move the center one to the left.
415         
416
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.
420         
421        */
422
423       
424       Interval hp = head_posns[LEFT];
425       hp.intersect  (head_posns[RIGHT]);
426       if (!hp.is_empty ())
427         return ;
428
429       Direction lowest =
430         (head_posns[LEFT][DOWN] > head_posns[RIGHT][UP]) ? RIGHT : LEFT;
431
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;
435       
436       correction=  -lowest * corr ;
437     }
438
439   *space += correction;
440
441   /* there used to be a correction for bar_xextent() here, but
442      it's unclear what that was good for ?
443   */
444
445 }
446  
447
448
449
450 ADD_INTERFACE (Note_spacing,"note-spacing-interface",
451   "",
452   "left-items right-items stem-spacing-correction knee-spacing-correction");
453