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