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