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