]> git.donarmstrong.com Git - lilypond.git/blob - lily/staff-spacing.cc
introduce Spacing_interface for code sharing between Note_spacing and Staff_spacing
[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--2007  Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
8
9 #include "staff-spacing.hh"
10
11 #include <cstdio>
12 using namespace std;
13
14 #include "international.hh"
15 #include "paper-column.hh"
16 #include "separation-item.hh"
17 #include "warn.hh"
18 #include "bar-line.hh"
19 #include "staff-symbol-referencer.hh"
20 #include "note-column.hh"
21 #include "stem.hh"
22 #include "accidental-placement.hh"
23 #include "pointer-group-interface.hh"
24 #include "directional-element-interface.hh"
25
26 Real
27 Staff_spacing::left_stickout (Grob *me, Grob *g)
28 {
29   if (!g || !Note_column::has_interface (g))
30     return 0;
31
32   Item *col = dynamic_cast<Item *> (g)->get_column ();
33   Real ret = max (0., (- g->extent (col, X_AXIS)[LEFT]));
34
35   /*
36     Duh. If this gets out of hand, we should invent something more generic.
37   */
38   Grob *accs = Note_column::accidentals (g);
39   if (accs)
40     {
41       Interval v;
42       if (Accidental_placement::has_interface (accs))
43         v = Accidental_placement::get_relevant_accidental_extent (accs, col, me);
44       else
45         v = accs->extent (col, X_AXIS);
46       
47       ret = max (ret, (- v[LEFT]));
48     }
49   Grob *arpeggio = unsmob_grob (g->get_object ("arpeggio"));
50   if (arpeggio)
51     ret = max (ret, - arpeggio->extent (col, X_AXIS)[LEFT]);
52
53   return ret;
54 }
55
56 Real
57 Staff_spacing::optical_correction (Grob *me, Grob *g, Interval bar_height)
58 {
59   if (!g || !Note_column::has_interface (g))
60     return 0;
61
62   Grob *stem = Note_column::get_stem (g);
63   Real ret = 0.0;
64
65   if (!bar_height.is_empty () && stem)
66     {
67       Direction d = get_grob_direction (stem);
68       if (Stem::is_normal_stem (stem) && d == DOWN)
69         {
70
71           /*
72             can't look at stem-end-position, since that triggers
73             beam slope computations.
74           */
75           Real stem_start = Stem::head_positions (stem) [d];
76           Real stem_end = stem_start + 
77             d * robust_scm2double (stem->get_property ("length"), 7);
78           
79           Interval stem_posns (min (stem_start, stem_end),
80                                max (stem_end, stem_start));
81
82           stem_posns.intersect (bar_height);
83
84           ret = min (abs (stem_posns.length () / 7.0), 1.0);
85           ret *= robust_scm2double (me->get_property ("stem-spacing-correction"), 1);
86         }
87     }
88   return ret;
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-name");
102       Grob *staff_sym = Staff_symbol_referencer::get_staff_symbol (bar_grob);
103
104       string glyph_string = scm_is_string (glyph) ? ly_scm2string (glyph) : "";
105       if (glyph_string.substr (0, 1) == "|"
106           || glyph_string.substr (0, 1) == ".")
107         {
108           Grob *common = bar_grob->common_refpoint (staff_sym, Y_AXIS);
109           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
124 Real
125 Staff_spacing::next_notes_correction (Grob *me,
126                                       Grob *last_grob,
127                                       Real current_fixed,
128                                       Real right_stickout)
129 {
130   Interval bar_size = bar_y_positions (last_grob);
131   Grob *orig = me->original () ? me->original () : me;
132
133   extract_grob_set (orig, "right-items", right_items);
134
135   Real max_left_stickout = 0.0;
136   Real max_optical = 0.0;
137
138   for (vsize i = right_items.size (); i--;)
139     {
140       Grob *g = right_items[i];
141       if (Note_column::has_interface (right_items[i]))
142         {
143           max_left_stickout = max (max_left_stickout, left_stickout (me, g));
144           max_optical = max (max_optical, optical_correction (me, g, bar_size));
145         }
146       else
147         {
148           extract_grob_set (g, "elements", elts);
149           for (vsize j = elts.size (); j--;)
150             {
151               max_left_stickout = max (max_left_stickout, left_stickout (me, elts[j]));
152               max_optical = max (max_optical, optical_correction (me, g, bar_size));
153             }
154         }
155     }
156
157   /* we put a minimum distance of 0.3 between the right edge of the left grob and
158      the left edge of the right grob */
159   max_left_stickout = max (0.0, max_left_stickout + right_stickout + 0.3 - current_fixed);
160   return max (max_left_stickout, max_optical);
161 }
162
163 /* This routine does not impose any minimum distances between columns; it only
164    affects springs. As such, the FIXED variable does not refer to a minimum
165    distance between columns, but instead to a minimum desired distance between
166    columns -- this ends up affecting the stiffness of a spring. In fact, FIXED
167    will be the distance between columns if there is a compression force of 1.0
168    applied to the line. */
169 Spring
170 Staff_spacing::get_spacing_params (Grob *me)
171 {
172   Grob *separation_item = 0;
173   Item *me_item = dynamic_cast<Item *> (me);
174
175   extract_grob_set (me, "left-items", items);
176   for (vsize i = items.size (); i--;)
177     {
178       Grob *cand = items[i];
179       if (cand && Separation_item::has_interface (cand))
180         separation_item = cand;
181     }
182
183   //  printf ("doing col %d\n" , Paper_column::get_rank (left_col));
184   if (!separation_item)
185     {
186       programming_error ("no sep item");
187       return Spring ();
188     }
189
190   Interval last_ext;
191   Grob *last_grob = Separation_item::extremal_break_aligned_grob (separation_item, RIGHT,
192                                                                   &last_ext);
193   if (!last_grob)
194     {
195       /*
196         TODO:
197
198         Should  insert a adjustable space here? For excercises, you might want to
199         use a staff without a clef in the beginning.
200       */
201
202       /*
203         we used to have a warning here, but it generates a lot of
204         spurious error messages.
205       */
206       return Spring ();
207     }
208
209   SCM alist = last_grob->get_property ("space-alist");
210   if (!scm_list_p (alist))
211     return Spring ();
212
213   SCM space_def = scm_sloppy_assq (ly_symbol2scm ("first-note"), alist);
214   if (me_item->break_status_dir () == CENTER)
215     {
216       SCM nndef = scm_sloppy_assq (ly_symbol2scm ("next-note"), alist);
217       if (scm_is_pair (nndef))
218         space_def = nndef;
219     }
220
221   if (!scm_is_pair (space_def))
222     {
223       programming_error ("unknown prefatory spacing");
224       return Spring ();
225     }
226
227   space_def = scm_cdr (space_def);
228   Real distance = scm_to_double (scm_cdr (space_def));
229   SCM type = scm_car (space_def);
230
231   Real fixed = last_ext[RIGHT];
232   Real ideal = fixed + 1.0;
233
234   if (type == ly_symbol2scm ("fixed-space"))
235     {
236       fixed += distance;
237       ideal = fixed;
238     }
239   else if (type == ly_symbol2scm ("extra-space"))
240     ideal = fixed + distance;
241   else if (type == ly_symbol2scm ("semi-fixed-space"))
242     {
243       fixed += distance / 2;
244       ideal = fixed + distance / 2;
245     }
246   else if (type == ly_symbol2scm ("minimum-space"))
247     ideal = last_ext[LEFT] + max (last_ext.length (), distance);
248   else if (type == ly_symbol2scm ("minimum-fixed-space"))
249     {
250       fixed = last_ext[LEFT] + max (last_ext.length (), distance);
251       ideal = fixed;
252     }
253
254   Real correction = next_notes_correction (me, last_grob, fixed, last_ext[RIGHT]);
255   fixed += correction;
256   ideal += correction;
257
258   Spring ret;
259   ret.min_distance_ = 0.0;
260   ret.distance_ = ideal;
261   ret.inverse_stretch_strength_ = ret.inverse_compress_strength_ = ideal - fixed;
262   return ret;
263 }
264
265 ADD_INTERFACE (Staff_spacing,
266                "This object calculates spacing details from a "
267                " breakable symbol (left) to another object. For example, it takes care "
268                " of  optical spacing from  a bar lines to a note.",
269
270                /* properties */
271                "stem-spacing-correction "
272                );