*** empty log message ***
[lilypond.git] / lily / staff-spacing.cc
blob124cb364884e2c21adb375c17aa697e5c2977f7d
1 /*
2 staff-spacing.cc -- implement Staff_spacing
4 source file of the GNU LilyPond music typesetter
6 (c) 2001--2004 Han-Wen Nienhuys <hanwen@cs.uu.nl>
8 */
9 #include <stdio.h>
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"
24 Insert some more space for the next note, in case it has a stem in
25 the wrong direction
28 Real
29 Staff_spacing::next_note_correction (Grob * me,
30 Grob * g,
31 Interval bar_size)
33 if (!g || !Note_column::has_interface (g))
34 return 0.0;
36 Item *col =dynamic_cast<Item*> (g)->get_column ();
37 Real max_corr = 0. >? (- g->extent (col, X_AXIS)[LEFT]);
40 Duh. If this gets out of hand, we should invent something more generic.
42 if (Grob * a = Note_column::accidentals (g))
44 Interval v;
45 if (Accidental_placement::has_interface (a))
47 v = Accidental_placement::get_relevant_accidental_extent (a, col, me);
49 else
50 v = a->extent (col, X_AXIS);
52 max_corr = max_corr >? (- v[LEFT]);
54 if (Grob* a = unsmob_grob (g->get_property ("arpeggio")))
56 max_corr = max_corr >? (- a->extent (col, X_AXIS)[LEFT]);
60 Let's decrease the space a little if the problem is not located
61 after a barline.
63 if (bar_size.is_empty ())
64 max_corr *= 0.75;
66 if (!bar_size.is_empty ())
67 if (Grob *stem = Note_column::get_stem (g))
69 Direction d = Stem::get_direction (stem);
70 if (d == DOWN)
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);
77 stem_posns.intersect (bar_size);
79 Real corr = abs (stem_posns.length ()/7.) <? 1.0;
80 corr *=
81 robust_scm2double (me->get_property ("stem-spacing-correction"), 1);
83 if (d != DOWN)
84 corr = 0.0;
85 max_corr = max_corr >? corr;
88 return max_corr;
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)
98 Interval bar_size;
99 bar_size.set_empty ();
100 if (Bar_line::has_interface (bar_grob))
102 SCM glyph = bar_grob->get_property ("glyph");
104 String glyph_string = gh_string_p (glyph) ? ly_scm2string (glyph) : "";
105 if (glyph_string.left_string (1) == "|" || glyph_string.left_string (1) == ".")
107 SCM sz = Bar_line::get_staff_bar_size (bar_grob->self_scm ());
108 bar_size = Interval (-1,1);
109 bar_size *= robust_scm2double (sz, 1)
110 / Staff_symbol_referencer::staff_space (bar_grob);
113 return bar_size;
117 Do corrections for the following notes.
119 This is slightly convoluted, since the staffspacing grob gets
120 pointers to the separation-items, not the note-columns or
121 note-spacings.
124 Real
125 Staff_spacing::next_notes_correction (Grob *me, Grob * last_grob)
127 Interval bar_size = bar_y_positions (last_grob);
128 Real max_corr =0.0;
130 for (SCM s = me->get_property ("right-items");
131 gh_pair_p (s); s = gh_cdr (s))
133 Grob * g = unsmob_grob (gh_car (s));
135 max_corr = max_corr >? next_note_correction (me, g, bar_size);
136 for (SCM t = g->get_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);
142 return max_corr;
145 void
146 Staff_spacing::get_spacing_params (Grob *me, Real * space, Real * fixed)
148 *space = 1.0;
149 *fixed = 1.0;
151 Grob * separation_item=0;
152 Item * me_item = dynamic_cast<Item*> (me);
154 for (SCM s = me->get_property ("left-items");
155 gh_pair_p (s); s = gh_cdr (s))
157 Grob * cand = unsmob_grob (gh_car (s));
158 if (cand && Separation_item::has_interface (cand))
159 separation_item = cand ;
162 // printf ("doing col %d\n" , Paper_column::get_rank (left_col));
164 if (!separation_item)
166 programming_error ("no sep item");
167 return;
170 Interval last_ext;
171 Grob *last_grob = Separation_item::extremal_break_aligned_grob (separation_item, RIGHT,
172 &last_ext);
173 if (!last_grob)
176 TODO:
178 Should insert a adjustable space here? For excercises, you might want to
179 use a staff without a clef in the beginning.
183 we used to have a warning here, but itgenerates a lot of
184 spurious error messages.
186 return ;
189 *fixed = last_ext[RIGHT];
190 *space = *fixed + 1.0;
192 SCM alist = last_grob->get_property ("space-alist");
193 if (!scm_list_p (alist))
194 return ;
196 SCM space_def = scm_sloppy_assq (ly_symbol2scm ("first-note"), alist);
197 if (me_item->break_status_dir () == CENTER)
199 SCM nndef = scm_sloppy_assq (ly_symbol2scm ("next-note"), alist);
200 if (gh_pair_p (nndef))
201 space_def = nndef;
205 if (!gh_pair_p (space_def))
207 programming_error ("Unknown prefatory spacing. ");
208 return;
211 space_def = gh_cdr (space_def);
212 Real distance = gh_scm2double (gh_cdr (space_def));
213 SCM type = gh_car (space_def) ;
215 *fixed = last_ext[RIGHT];
216 if (type == ly_symbol2scm ("fixed-space"))
218 *fixed += distance;
219 *space = *fixed;
221 else if (type == ly_symbol2scm ("extra-space"))
223 *space = *fixed + distance;
225 else if (type == ly_symbol2scm ("semi-fixed-space"))
227 *fixed += distance / 2;
228 *space = *fixed + distance/2;
230 else if (type == ly_symbol2scm ("minimum-space"))
232 *space = last_ext[LEFT] + (last_ext.length () >? distance);
234 else if (type == ly_symbol2scm ("minimum-fixed-space"))
236 *space = last_ext[LEFT] + (last_ext.length () >? distance);
237 *fixed = *space;
240 *space += next_notes_correction (me, last_grob);
244 ADD_INTERFACE (Staff_spacing,"staff-spacing-interface",
245 "This object calculates spacing details from a "
246 " breakable symbol (left) to another object. For example, it takes care "
247 " of optical spacing from a bar lines to a note.",
248 "stem-spacing-correction left-items right-items");