]> git.donarmstrong.com Git - lilypond.git/blob - lily/staff-spacing.cc
use springs in simple-spacer and scrap separating-group-spanner
[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 "spacing-interface.hh"
23 #include "accidental-placement.hh"
24 #include "pointer-group-interface.hh"
25 #include "directional-element-interface.hh"
26
27 Real
28 Staff_spacing::optical_correction (Grob *me, Grob *g, Interval bar_height)
29 {
30   if (!g || !Note_column::has_interface (g))
31     return 0;
32
33   Grob *stem = Note_column::get_stem (g);
34   Real ret = 0.0;
35
36   if (!bar_height.is_empty () && stem)
37     {
38       Direction d = get_grob_direction (stem);
39       if (Stem::is_normal_stem (stem) && d == DOWN)
40         {
41
42           /*
43             can't look at stem-end-position, since that triggers
44             beam slope computations.
45           */
46           Real stem_start = Stem::head_positions (stem) [d];
47           Real stem_end = stem_start + 
48             d * robust_scm2double (stem->get_property ("length"), 7);
49           
50           Interval stem_posns (min (stem_start, stem_end),
51                                max (stem_end, stem_start));
52
53           stem_posns.intersect (bar_height);
54
55           ret = min (abs (stem_posns.length () / 7.0), 1.0);
56           ret *= robust_scm2double (me->get_property ("stem-spacing-correction"), 1);
57         }
58     }
59   return ret;
60 }
61
62 /*
63   Y-positions that are covered by BAR_GROB, in the case that it is a
64   barline.  */
65 Interval
66 Staff_spacing::bar_y_positions (Grob *bar_grob)
67 {
68   Interval bar_size;
69   bar_size.set_empty ();
70   if (Bar_line::has_interface (bar_grob))
71     {
72       SCM glyph = bar_grob->get_property ("glyph-name");
73       Grob *staff_sym = Staff_symbol_referencer::get_staff_symbol (bar_grob);
74
75       string glyph_string = scm_is_string (glyph) ? ly_scm2string (glyph) : "";
76       if (glyph_string.substr (0, 1) == "|"
77           || glyph_string.substr (0, 1) == ".")
78         {
79           Grob *common = bar_grob->common_refpoint (staff_sym, Y_AXIS);
80           bar_size = bar_grob->extent (common, Y_AXIS);
81           bar_size *= 1.0 / Staff_symbol_referencer::staff_space (bar_grob);
82         }
83     }
84   return bar_size;
85 }
86
87 Real
88 Staff_spacing::next_notes_correction (Grob *me,
89                                       Grob *last_grob)
90 {
91   Interval bar_size = bar_y_positions (last_grob);
92   Grob *orig = me->original () ? me->original () : me;
93   vector<Item*> note_columns = Spacing_interface::right_note_columns (orig);
94
95   Real max_optical = 0.0;
96
97   for (vsize i = 0; i < note_columns.size (); i++)
98     max_optical = max (max_optical, optical_correction (me, note_columns[i], bar_size));
99
100   return max_optical;
101 }
102
103 /* This routine does not impose any minimum distances between columns; it only
104    affects springs. As such, the FIXED variable does not refer to a minimum
105    distance between columns, but instead to a minimum desired distance between
106    columns -- this ends up affecting the stiffness of a spring. In fact, FIXED
107    will be the distance between columns if there is a compression force of 1.0
108    applied to the line. */
109 Spring
110 Staff_spacing::get_spacing (Grob *me, Grob *right_col)
111 {
112   Grob *separation_item = 0;
113   Item *me_item = dynamic_cast<Item *> (me);
114
115   extract_grob_set (me, "left-items", items);
116   for (vsize i = items.size (); i--;)
117     {
118       Grob *cand = items[i];
119       if (cand && Separation_item::has_interface (cand))
120         separation_item = cand;
121     }
122
123   //  printf ("doing col %d\n" , Paper_column::get_rank (left_col));
124   if (!separation_item)
125     {
126       programming_error ("no sep item");
127       return Spring ();
128     }
129
130   Interval last_ext;
131   Grob *last_grob = Separation_item::extremal_break_aligned_grob (separation_item, RIGHT,
132                                                                   &last_ext);
133   if (!last_grob)
134     {
135       /*
136         TODO:
137
138         Should  insert a adjustable space here? For excercises, you might want to
139         use a staff without a clef in the beginning.
140       */
141
142       /*
143         we used to have a warning here, but it generates a lot of
144         spurious error messages.
145       */
146       return Spring ();
147     }
148
149   SCM alist = last_grob->get_property ("space-alist");
150   if (!scm_list_p (alist))
151     return Spring ();
152
153   SCM space_def = scm_sloppy_assq (ly_symbol2scm ("first-note"), alist);
154   if (me_item->break_status_dir () == CENTER)
155     {
156       SCM nndef = scm_sloppy_assq (ly_symbol2scm ("next-note"), alist);
157       if (scm_is_pair (nndef))
158         space_def = nndef;
159     }
160
161   if (!scm_is_pair (space_def))
162     {
163       programming_error ("unknown prefatory spacing");
164       return Spring ();
165     }
166
167   space_def = scm_cdr (space_def);
168   Real distance = scm_to_double (scm_cdr (space_def));
169   SCM type = scm_car (space_def);
170
171   Real fixed = last_ext[RIGHT];
172   Real ideal = fixed + 1.0;
173
174   if (type == ly_symbol2scm ("fixed-space"))
175     {
176       fixed += distance;
177       ideal = fixed;
178     }
179   else if (type == ly_symbol2scm ("extra-space"))
180     ideal = fixed + distance;
181   else if (type == ly_symbol2scm ("semi-fixed-space"))
182     {
183       fixed += distance / 2;
184       ideal = fixed + distance / 2;
185     }
186   else if (type == ly_symbol2scm ("minimum-space"))
187     ideal = last_ext[LEFT] + max (last_ext.length (), distance);
188   else if (type == ly_symbol2scm ("minimum-fixed-space"))
189     {
190       fixed = last_ext[LEFT] + max (last_ext.length (), distance);
191       ideal = fixed;
192     }
193
194   Real optical_correction = next_notes_correction (me, last_grob);
195   Real min_dist = Spacing_interface::minimum_distance (me, right_col);
196   Real min_dist_correction = max (0.0, 0.3 + min_dist - fixed);
197   Real correction = max (optical_correction, min_dist_correction);
198
199   fixed += correction;
200   ideal += correction;
201
202   Spring ret (ideal, min_dist);
203   ret.set_inverse_stretch_strength (ideal - fixed);
204   return ret;
205 }
206
207 ADD_INTERFACE (Staff_spacing,
208                "This object calculates spacing details from a "
209                " breakable symbol (left) to another object. For example, it takes care "
210                " of  optical spacing from  a bar lines to a note.",
211
212                /* properties */
213                "stem-spacing-correction "
214                );