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