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