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