]> git.donarmstrong.com Git - lilypond.git/blob - lily/staff-spacing.cc
* lily/script-engraver.cc (make_script_from_event): don't trigger callback.
[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 using namespace std;
13
14 #include "paper-column.hh"
15 #include "separation-item.hh"
16 #include "warn.hh"
17 #include "bar-line.hh"
18 #include "staff-symbol-referencer.hh"
19 #include "note-column.hh"
20 #include "stem.hh"
21 #include "accidental-placement.hh"
22 #include "pointer-group-interface.hh"
23
24 /*
25   Insert some more space for the next note, in case it has a stem in
26   the wrong direction
27 */
28 void
29 Staff_spacing::next_note_correction (Grob *me,
30                                      Grob *g,
31                                      Interval bar_size,
32                                      Real *space,
33                                      Real *fix)
34 {
35   (void)space;
36   
37   if (!g || !Note_column::has_interface (g))
38     return ;
39
40   Item *col = dynamic_cast<Item *> (g)->get_column ();
41   Real max_corr = max (0., (- g->extent (col, X_AXIS)[LEFT]));
42
43   /*
44     Duh. If this gets out of hand, we should invent something more generic.
45   */
46   if (Grob *a = Note_column::accidentals (g))
47     {
48       Interval v;
49       if (Accidental_placement::has_interface (a))
50         v = Accidental_placement::get_relevant_accidental_extent (a, col, me);
51       else
52         v = a->extent (col, X_AXIS);
53
54       max_corr = max (max_corr, (- v[LEFT]));
55     }
56   if (Grob *a = unsmob_grob (g->get_object ("arpeggio")))
57     max_corr = max (max_corr, - a->extent (col, X_AXIS)[LEFT]);
58
59   /*
60     Let's decrease the space a little if the problem is not located
61     after a barline.
62   */
63   if (bar_size.is_empty ())
64     max_corr *= 0.75;
65
66   if (!bar_size.is_empty ())
67     if (Grob *stem = Note_column::get_stem (g))
68       {
69         Direction d = Stem::get_direction (stem);
70         if (d == DOWN)
71           {
72             Real stem_start = Stem::head_positions (stem) [DOWN];
73             Real stem_end = Stem::stem_end_position (stem);
74             Interval stem_posns (min (stem_start, stem_end),
75                                  max (stem_end, stem_start));
76
77             stem_posns.intersect (bar_size);
78
79             Real corr = min (abs (stem_posns.length () / 7.0), 1.0);
80             corr *= 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-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.left_string (1) == "|" || glyph_string.left_string (1) == ".")
106         {
107           Grob *common = bar_grob->common_refpoint (staff_sym, Y_AXIS);
108           Interval bar_size = bar_grob->extent (common, Y_AXIS);
109           bar_size *= 1.0 / 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");