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