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