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