]> git.donarmstrong.com Git - lilypond.git/blob - lily/staff-spacing.cc
''
[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--2002  Han-Wen Nienhuys <hanwen@cs.uu.nl>
7
8 */
9 #include <stdio.h>
10
11 #include "paper-column.hh" 
12 #include "separation-item.hh"
13 #include "item.hh"
14 #include "staff-spacing.hh"
15 #include "grob.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
22 bool
23 Staff_spacing::has_interface (Grob* g)
24 {
25   return g && g->has_interface (ly_symbol2scm ("staff-spacing-interface"));
26 }
27
28 /*
29   Insert some more space for the next note, in case it has a stem in
30   the wrong direction
31
32  */
33 Real
34 Staff_spacing::next_note_correction (Grob * me,
35                                      Grob * g,
36                                      Interval bar_size)
37 {
38   if (!g || !Note_column::has_interface (g))
39     return 0.0;
40
41   Item *col =dynamic_cast<Item*> (g)->column_l ();
42   Real max_corr = 0. >? (- g->extent (col, X_AXIS)[LEFT]);
43
44   /*
45     Duh. If this gets out of hand, we should invent something more generic.
46    */
47   if (Grob * a = Note_column::accidentals (g))
48     {
49       max_corr = max_corr >? (- a->extent (col, X_AXIS)[LEFT]);
50     }
51   if (Grob* a = unsmob_grob (g->get_grob_property ("arpeggio")))
52     {
53       max_corr = max_corr >? (- a->extent (col, X_AXIS)[LEFT]);
54     }
55   
56   /*
57     Let's decrease the space a little if the problem is not located
58     after a barline.
59   */
60   if (bar_size.empty_b ())
61     max_corr *= 0.75;
62   
63   if (!bar_size.empty_b())
64     if (Grob *stem = Note_column::stem_l  (g))
65       {
66         Direction d = Stem::get_direction (stem);
67         if (d == DOWN)
68           {
69             Real stem_start = Stem::head_positions (stem) [DOWN];
70             Real stem_end = Stem::stem_end_position (stem); 
71             Interval stem_posns (stem_start <? stem_end,
72                                  stem_end >? stem_start);
73
74             stem_posns.intersect (bar_size);
75
76             Real corr = abs (stem_posns.length ()/7.) <? 1.0;
77             corr *=
78               gh_scm2double (me->get_grob_property ("stem-spacing-correction"));
79
80             if (d != DOWN)
81               corr = 0.0;
82             max_corr = max_corr >? corr;
83           }
84       }
85   return max_corr;
86 }
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_grob_property ("glyph");
100       
101       String glyph_str = gh_string_p (glyph) ? ly_scm2string (glyph) : "";
102       if (glyph_str.left_str (1) == "|" || glyph_str.left_str (1) == ".")
103         {
104           SCM sz = Bar_line::get_staff_bar_size (bar_grob->self_scm());
105           bar_size = Interval (-1,1);
106           bar_size *= gh_scm2double (sz)
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  */
121 Real
122 Staff_spacing::next_notes_correction (Grob *me, Grob * last_grob)
123 {
124   Interval bar_size = bar_y_positions (last_grob);
125   Real max_corr =0.0;
126   for (SCM s = me->get_grob_property ("right-items");
127        gh_pair_p (s);  s = gh_cdr (s))
128     {
129       Grob * g = unsmob_grob (gh_car (s));
130       max_corr = max_corr >?  next_note_correction (me, g,  bar_size);
131       for (SCM t = g->get_grob_property ("elements");
132            gh_pair_p (t); t  = gh_cdr (t))
133         max_corr = max_corr >? next_note_correction (me, unsmob_grob (gh_car (t)), bar_size);
134       
135     }
136   return max_corr;
137 }
138
139 /*
140   Try to find the break-aligned symbol in SEPARATION_ITEM that is
141   sticking out at direction D. The x size is put in LAST_EXT
142 */
143 Grob*
144 Staff_spacing::extremal_break_aligned_grob (Grob *separation_item, Direction d,
145                                    Interval * last_ext)
146 {
147   Grob *left_col = dynamic_cast<Item*> (separation_item)->column_l ();
148   last_ext->set_empty ();
149   Grob *last_grob = 0;
150   for (SCM s = separation_item->get_grob_property ("elements");
151        gh_pair_p (s); s = gh_cdr (s))
152     {
153       Grob * break_item = unsmob_grob (gh_car (s));
154       
155       if (!gh_symbol_p (break_item->get_grob_property ("break-align-symbol")))
156         continue;
157
158       Interval ext = break_item->extent (left_col, X_AXIS);
159
160       if (ext.empty_b ())
161         continue;
162       if (!last_grob
163           || (last_grob && d * (ext[d]- (*last_ext)[d]) > 0) )
164         {
165           *last_ext = ext;
166           last_grob = break_item; 
167         }
168     }
169
170   return last_grob;  
171 }
172
173 /*
174 */
175 void
176 Staff_spacing::get_spacing_params (Grob *me, Real * space, Real * fixed)
177 {
178   *space = 1.0;
179   *fixed = 1.0;
180
181   Grob * separation_item=0;
182   Item * me_item  = dynamic_cast<Item*> (me);
183     
184   for (SCM s = me->get_grob_property ("left-items");
185        gh_pair_p (s); s = gh_cdr(s))
186     {
187       Grob * cand = unsmob_grob(gh_car (s));
188       if (cand && Separation_item::has_interface (cand))
189         separation_item = cand ;
190     }
191
192   //  printf ("doing col %d\n" , Paper_column::rank_i(left_col));
193
194   if (!separation_item)
195     {
196       programming_error ("no sep item");
197       return;
198     }
199
200   Interval last_ext;
201   Grob *last_grob = extremal_break_aligned_grob (separation_item, RIGHT,
202                                                  &last_ext);
203   if (!last_grob)
204     {
205       programming_error ("empty break column? --fixme");
206       return ;
207     }
208
209   *fixed = last_ext[RIGHT];
210   *space = *fixed + 1.0;
211   
212   SCM alist = last_grob->get_grob_property ("space-alist");
213   if (!scm_list_p (alist))
214     return ;
215
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 (gh_pair_p (nndef ))
222         space_def = nndef;
223     }
224
225   if (!gh_pair_p (space_def))
226     {
227       programming_error ("Unknown prefatory spacing. "); 
228       return; 
229     }
230
231   space_def = gh_cdr (space_def);
232   Real distance = gh_scm2double (gh_cdr (space_def));
233   SCM type = gh_car (space_def) ;
234
235   *fixed = last_ext[RIGHT];
236   if (type == ly_symbol2scm ("extra-space"))
237     *space = *fixed + distance;
238   else if (type == ly_symbol2scm("minimum-space"))
239     *space = last_ext[LEFT] + (last_ext.length () >? distance);
240
241
242   *space += next_notes_correction (me, last_grob);
243 }
244
245
246 ADD_INTERFACE (Staff_spacing,"staff-spacing-interface",
247   "",
248   "stem-spacing-correction left-items right-items");