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