]> 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--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 *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.substr (0, 1) == "|"
107           || glyph_string.substr (0, 1) == ".")
108         {
109           Grob *common = bar_grob->common_refpoint (staff_sym, Y_AXIS);
110           bar_size = bar_grob->extent (common, Y_AXIS);
111           bar_size *= 1.0 / Staff_symbol_referencer::staff_space (bar_grob);
112         }
113     }
114   return bar_size;
115 }
116
117 /*
118   Do corrections for the following notes.
119
120   This is slightly convoluted, since the staffspacing grob gets
121   pointers to the separation-items, not the note-columns or
122   note-spacings.
123 */
124 void
125 Staff_spacing::next_notes_correction (Grob *me, Grob *last_grob,
126                                       Real *compound_space, Real *compound_fixed
127                                       )
128 {
129   Interval bar_size = bar_y_positions (last_grob);
130
131   extract_grob_set (me, "right-items", right_items);
132
133   *compound_fixed = 0.0;
134   *compound_space = 0.0;
135   int wish_count = 0;
136   
137   for (vsize i = right_items.size (); i--;)
138     {
139       Grob *g = right_items[i];
140
141
142       if (Note_column::has_interface (g))
143         {
144           Real space = 0.0;
145           Real fixed = 0.0;
146       
147           next_note_correction (me, g, bar_size, &space, &fixed);
148
149           *compound_space += space;
150           *compound_fixed += fixed; 
151           wish_count ++;
152         }
153       else
154         {
155           extract_grob_set (g, "elements", elts);
156           for (vsize j = elts.size (); j--;)
157             {
158               Real space = 0.0;
159               Real fixed = 0.0;
160               next_note_correction (me, elts[j], bar_size, &space, &fixed);
161               *compound_fixed += fixed;
162               *compound_space += space;
163               wish_count ++;
164             }
165         }
166     }
167   if (wish_count > 1)
168     {
169       *compound_space /= wish_count;
170       *compound_fixed /= wish_count;
171     }
172 }
173
174 void
175 Staff_spacing::get_spacing_params (Grob *me, Real *space, Real *fixed)
176 {
177   *space = 1.0;
178   *fixed = 1.0;
179
180   Grob *separation_item = 0;
181   Item *me_item = dynamic_cast<Item *> (me);
182
183   extract_grob_set (me, "left-items", items);
184   for (vsize i = items.size (); i--;)
185     {
186       Grob *cand = items[i];
187       if (cand && Separation_item::has_interface (cand))
188         separation_item = cand;
189     }
190
191   //  printf ("doing col %d\n" , Paper_column::get_rank (left_col));
192   if (!separation_item)
193     {
194       programming_error ("no sep item");
195       return;
196     }
197
198   Interval last_ext;
199   Grob *last_grob = Separation_item::extremal_break_aligned_grob (separation_item, RIGHT,
200                                                                   &last_ext);
201   if (!last_grob)
202     {
203       /*
204         TODO:
205
206         Should  insert a adjustable space here? For excercises, you might want to
207         use a staff without a clef in the beginning.
208       */
209
210       /*
211         we used to have a warning here, but it generates a lot of
212         spurious error messages.
213       */
214       return;
215     }
216
217   *fixed = last_ext[RIGHT];
218   *space = *fixed + 1.0;
219
220   SCM alist = last_grob->get_property ("space-alist");
221   if (!scm_list_p (alist))
222     return;
223
224   SCM space_def = scm_sloppy_assq (ly_symbol2scm ("first-note"), alist);
225   if (me_item->break_status_dir () == CENTER)
226     {
227       SCM nndef = scm_sloppy_assq (ly_symbol2scm ("next-note"), alist);
228       if (scm_is_pair (nndef))
229         space_def = nndef;
230     }
231
232   if (!scm_is_pair (space_def))
233     {
234       programming_error ("unknown prefatory spacing");
235       return;
236     }
237
238   space_def = scm_cdr (space_def);
239   Real distance = scm_to_double (scm_cdr (space_def));
240   SCM type = scm_car (space_def);
241
242   *fixed = last_ext[RIGHT];
243   if (type == ly_symbol2scm ("fixed-space"))
244     {
245       *fixed += distance;
246       *space = *fixed;
247     }
248   else if (type == ly_symbol2scm ("extra-space"))
249     *space = *fixed + distance;
250   else if (type == ly_symbol2scm ("semi-fixed-space"))
251     {
252       *fixed += distance / 2;
253       *space = *fixed + distance / 2;
254     }
255   else if (type == ly_symbol2scm ("minimum-space"))
256     *space = last_ext[LEFT] + max (last_ext.length (), distance);
257   else if (type == ly_symbol2scm ("minimum-fixed-space"))
258     {
259       *space = last_ext[LEFT] + max (last_ext.length (), distance);
260       *fixed = *space;
261     }
262
263   Real correction_fixed, correction_space;
264   next_notes_correction (me, last_grob, &correction_space, &correction_fixed );
265   *space += correction_space;
266   *fixed += correction_fixed;
267 }
268
269 ADD_INTERFACE (Staff_spacing,
270                "This object calculates spacing details from a "
271                " breakable symbol (left) to another object. For example, it takes care "
272                " of  optical spacing from  a bar lines to a note.",
273
274                /* properties */
275                "stem-spacing-correction "
276                "left-items "
277                "right-items "
278                );