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