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