]> git.donarmstrong.com Git - lilypond.git/blob - lily/staff-spacing.cc
Merge branch 'master' of ssh+git://hanwen@git.sv.gnu.org/srv/git/lilypond
[lilypond.git] / lily / staff-spacing.cc
1 /*
2   staff-spacing.cc -- implement Staff_spacing
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 2001--2006  Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
8
9 #include "staff-spacing.hh"
10
11 #include <cstdio>
12 using namespace std;
13
14 #include "paper-column.hh"
15 #include "separation-item.hh"
16 #include "warn.hh"
17 #include "bar-line.hh"
18 #include "staff-symbol-referencer.hh"
19 #include "note-column.hh"
20 #include "stem.hh"
21 #include "accidental-placement.hh"
22 #include "pointer-group-interface.hh"
23 #include "directional-element-interface.hh"
24
25 /*
26   Insert some more space for the next note, in case it has a stem in
27   the wrong direction
28 */
29 void
30 Staff_spacing::next_note_correction (Grob *me,
31                                      Grob *g,
32                                      Interval bar_size,
33                                      Real current_space, Real current_fixed,
34                                      Real *space,
35                                      Real *fix,
36                                      int *wish_count)
37 {
38   (void) current_fixed; 
39   if (!g || !Note_column::has_interface (g))
40     return ;
41
42   Item *col = dynamic_cast<Item *> (g)->get_column ();
43   Real left_stickout_correction = max (0., (- g->extent (col, X_AXIS)[LEFT]));
44
45   /*
46     Duh. If this gets out of hand, we should invent something more generic.
47   */
48   Grob *accs = Note_column::accidentals (g);
49   if (accs)
50     {
51       Interval v;
52       if (Accidental_placement::has_interface (accs))
53         v = Accidental_placement::get_relevant_accidental_extent (accs, col, me);
54       else
55         v = accs->extent (col, X_AXIS);
56       
57       left_stickout_correction = max (left_stickout_correction, (- v[LEFT]));
58     }
59   Grob *arpeggio = unsmob_grob (g->get_object ("arpeggio"));
60   if (arpeggio)
61     left_stickout_correction = max (left_stickout_correction, - arpeggio->extent (col, X_AXIS)[LEFT]);
62
63   
64   /*
65     Let's decrease the space a little if the problem is not located
66     after a barline.
67   */
68   if (bar_size.is_empty ())
69     left_stickout_correction *= 0.75;
70
71   /*
72     We want 0.3 ss before the sticking-out object.
73     
74     current_fixed/2 is our guess at (right side of left object + 0.3)
75    */
76   left_stickout_correction += current_fixed/2 - current_space;
77   left_stickout_correction = max (left_stickout_correction, 0.0);
78
79   
80   Real optical_corr = 0.0;
81   Grob *stem = Note_column::get_stem (g);
82   if (!bar_size.is_empty ()
83       && !arpeggio
84       && !accs
85       && stem)
86     {
87       Direction d = get_grob_direction (stem);
88       if (Stem::is_normal_stem (stem) && d == DOWN)
89         {
90
91           /*
92             can't look at stem-end-position, since that triggers
93             beam slope computations.
94           */
95           Real stem_start = Stem::head_positions (stem) [d];
96           Real stem_end = stem_start + 
97             d * robust_scm2double (stem->get_property ("length"), 7);
98           
99           Interval stem_posns (min (stem_start, stem_end),
100                                max (stem_end, stem_start));
101
102           stem_posns.intersect (bar_size);
103
104           optical_corr = min (abs (stem_posns.length () / 7.0), 1.0);
105           optical_corr *= robust_scm2double (me->get_property ("stem-spacing-correction"), 1);
106         }
107     }
108
109
110   Real correction = optical_corr + left_stickout_correction;
111   if (correction)
112     {
113       wish_count ++; 
114
115       /*
116         This minute adjustments don't make sense for widely spaced scores.
117         Hence, we need to keep the stretchable (that is, space - fix)
118         distance equal.
119       */
120       *space += correction;
121       *fix += correction;
122     }
123 }
124 /*
125   Y-positions that are covered by BAR_GROB, in the case that it is a
126   barline.  */
127 Interval
128 Staff_spacing::bar_y_positions (Grob *bar_grob)
129 {
130   Interval bar_size;
131   bar_size.set_empty ();
132   if (Bar_line::has_interface (bar_grob))
133     {
134       SCM glyph = bar_grob->get_property ("glyph-name");
135       Grob *staff_sym = Staff_symbol_referencer::get_staff_symbol (bar_grob);
136
137       string glyph_string = scm_is_string (glyph) ? ly_scm2string (glyph) : "";
138       if (glyph_string.substr (0, 1) == "|"
139           || glyph_string.substr (0, 1) == ".")
140         {
141           Grob *common = bar_grob->common_refpoint (staff_sym, Y_AXIS);
142           bar_size = bar_grob->extent (common, Y_AXIS);
143           bar_size *= 1.0 / Staff_symbol_referencer::staff_space (bar_grob);
144         }
145     }
146   return bar_size;
147 }
148
149 /*
150   Do corrections for the following notes.
151
152   This is slightly convoluted, since the staffspacing grob gets
153   pointers to the separation-items, not the note-columns or
154   note-spacings.
155 */
156
157 void
158 Staff_spacing::next_notes_correction (Grob *me, Grob *last_grob,
159                                       Real current_space, Real current_fixed,
160                                       Real *compound_space, Real *compound_fixed
161                                       )
162 {
163   Interval bar_size = bar_y_positions (last_grob);
164
165   extract_grob_set (me, "right-items", right_items);
166
167   *compound_fixed = 0.0;
168   *compound_space = 0.0;
169   int wish_count = 0;
170
171   for (vsize i = right_items.size (); i--;)
172     {
173       Grob *g = right_items[i];
174       if (Note_column::has_interface (right_items[i]))
175         {
176           Grob *g = right_items[i];
177
178           Real space = 0.0;
179           Real fixed = 0.0;
180       
181           next_note_correction (me, g, bar_size,
182                                 current_space, current_fixed,
183                                 &space, &fixed, &wish_count);
184       
185           *compound_space += space;
186           *compound_fixed += fixed; 
187         }
188       else
189         {
190           extract_grob_set (g, "elements", elts);
191           for (vsize j = elts.size (); j--;)
192             {
193               Real space = 0.0;
194               Real fixed = 0.0;
195               next_note_correction (me, elts[j], bar_size,
196                                     current_space, current_fixed,
197                                     &space, &fixed,
198                                     &wish_count);
199               *compound_fixed += fixed;
200               *compound_space += space;
201             }
202         }
203     }
204   
205   if (wish_count > 1)
206     {
207       *compound_space /= wish_count;
208       *compound_fixed /= wish_count;
209     }
210 }
211
212 void
213 Staff_spacing::get_spacing_params (Grob *me, Real *space, Real *fixed)
214 {
215   *space = 1.0;
216   *fixed = 1.0;
217
218   Grob *separation_item = 0;
219   Item *me_item = dynamic_cast<Item *> (me);
220
221   extract_grob_set (me, "left-items", items);
222   for (vsize i = items.size (); i--;)
223     {
224       Grob *cand = items[i];
225       if (cand && Separation_item::has_interface (cand))
226         separation_item = cand;
227     }
228
229   //  printf ("doing col %d\n" , Paper_column::get_rank (left_col));
230   if (!separation_item)
231     {
232       programming_error ("no sep item");
233       return;
234     }
235
236   Interval last_ext;
237   Grob *last_grob = Separation_item::extremal_break_aligned_grob (separation_item, RIGHT,
238                                                                   &last_ext);
239   if (!last_grob)
240     {
241       /*
242         TODO:
243
244         Should  insert a adjustable space here? For excercises, you might want to
245         use a staff without a clef in the beginning.
246       */
247
248       /*
249         we used to have a warning here, but it generates a lot of
250         spurious error messages.
251       */
252       return;
253     }
254
255   *fixed = last_ext[RIGHT];
256   *space = *fixed + 1.0;
257
258   SCM alist = last_grob->get_property ("space-alist");
259   if (!scm_list_p (alist))
260     return;
261
262   SCM space_def = scm_sloppy_assq (ly_symbol2scm ("first-note"), alist);
263   if (me_item->break_status_dir () == CENTER)
264     {
265       SCM nndef = scm_sloppy_assq (ly_symbol2scm ("next-note"), alist);
266       if (scm_is_pair (nndef))
267         space_def = nndef;
268     }
269
270   if (!scm_is_pair (space_def))
271     {
272       programming_error ("unknown prefatory spacing");
273       return;
274     }
275
276   space_def = scm_cdr (space_def);
277   Real distance = scm_to_double (scm_cdr (space_def));
278   SCM type = scm_car (space_def);
279
280   *fixed = last_ext[RIGHT];
281   if (type == ly_symbol2scm ("fixed-space"))
282     {
283       *fixed += distance;
284       *space = *fixed;
285     }
286   else if (type == ly_symbol2scm ("extra-space"))
287     *space = *fixed + distance;
288   else if (type == ly_symbol2scm ("semi-fixed-space"))
289     {
290       *fixed += distance / 2;
291       *space = *fixed + distance / 2;
292     }
293   else if (type == ly_symbol2scm ("minimum-space"))
294     *space = last_ext[LEFT] + max (last_ext.length (), distance);
295   else if (type == ly_symbol2scm ("minimum-fixed-space"))
296     {
297       *space = last_ext[LEFT] + max (last_ext.length (), distance);
298       *fixed = *space;
299     }
300
301   Real correction_fixed = 0.0;
302   Real correction_space = 0.0;
303   next_notes_correction (me, last_grob,
304                          *space, *fixed,
305                          &correction_space, &correction_fixed );
306   *space += correction_space;
307   *fixed += correction_fixed;
308 }
309
310 ADD_INTERFACE (Staff_spacing,
311                "This object calculates spacing details from a "
312                " breakable symbol (left) to another object. For example, it takes care "
313                " of  optical spacing from  a bar lines to a note.",
314
315                /* properties */
316                "stem-spacing-correction "
317                "left-items "
318                "right-items "
319                );