2 staff-spacing.cc -- implement Staff_spacing
4 source file of the GNU LilyPond music typesetter
6 (c) 2001--2005 Han-Wen Nienhuys <hanwen@cs.uu.nl>
9 #include "staff-spacing.hh"
13 #include "paper-column.hh"
14 #include "separation-item.hh"
16 #include "bar-line.hh"
17 #include "staff-symbol-referencer.hh"
18 #include "note-column.hh"
20 #include "accidental-placement.hh"
23 Insert some more space for the next note, in case it has a stem in
27 Staff_spacing::next_note_correction (Grob
*me
,
31 if (!g
|| !Note_column::has_interface (g
))
34 Item
*col
= dynamic_cast<Item
*> (g
)->get_column ();
35 Real max_corr
= 0. >? (- g
->extent (col
, X_AXIS
)[LEFT
]);
38 Duh. If this gets out of hand, we should invent something more generic.
40 if (Grob
*a
= Note_column::accidentals (g
))
43 if (Accidental_placement::has_interface (a
))
45 v
= Accidental_placement::get_relevant_accidental_extent (a
, col
, me
);
48 v
= a
->extent (col
, X_AXIS
);
50 max_corr
= max_corr
>? (- v
[LEFT
]);
52 if (Grob
*a
= unsmob_grob (g
->get_property ("arpeggio")))
54 max_corr
= max_corr
>? (- a
->extent (col
, X_AXIS
)[LEFT
]);
58 Let's decrease the space a little if the problem is not located
61 if (bar_size
.is_empty ())
64 if (!bar_size
.is_empty ())
65 if (Grob
*stem
= Note_column::get_stem (g
))
67 Direction d
= Stem::get_direction (stem
);
70 Real stem_start
= Stem::head_positions (stem
) [DOWN
];
71 Real stem_end
= Stem::stem_end_position (stem
);
72 Interval
stem_posns (stem_start
<? stem_end
,
73 stem_end
>? stem_start
);
75 stem_posns
.intersect (bar_size
);
77 Real corr
= abs (stem_posns
.length () / 7.) <? 1.0;
79 *= robust_scm2double (me
->get_property ("stem-spacing-correction"), 1);
83 max_corr
= max_corr
>? corr
;
90 Y-positions that are covered by BAR_GROB, in the case that it is a
93 Staff_spacing::bar_y_positions (Grob
*bar_grob
)
96 bar_size
.set_empty ();
97 if (Bar_line::has_interface (bar_grob
))
99 SCM glyph
= bar_grob
->get_property ("glyph");
101 String glyph_string
= scm_is_string (glyph
) ? ly_scm2string (glyph
) : "";
102 if (glyph_string
.left_string (1) == "|" || glyph_string
.left_string (1) == ".")
104 SCM sz
= Bar_line::get_staff_bar_size (bar_grob
->self_scm ());
105 bar_size
= Interval (-1, 1);
106 bar_size
*= robust_scm2double (sz
, 1)
107 / Staff_symbol_referencer::staff_space (bar_grob
);
114 Do corrections for the following notes.
116 This is slightly convoluted, since the staffspacing grob gets
117 pointers to the separation-items, not the note-columns or
121 Staff_spacing::next_notes_correction (Grob
*me
, Grob
*last_grob
)
123 Interval bar_size
= bar_y_positions (last_grob
);
126 for (SCM s
= me
->get_property ("right-items");
127 scm_is_pair (s
); s
= scm_cdr (s
))
129 Grob
*g
= unsmob_grob (scm_car (s
));
131 max_corr
= max_corr
>? next_note_correction (me
, g
, bar_size
);
132 for (SCM t
= g
->get_property ("elements");
133 scm_is_pair (t
); t
= scm_cdr (t
))
134 max_corr
= max_corr
>? next_note_correction (me
, unsmob_grob (scm_car (t
)), bar_size
);
141 Staff_spacing::get_spacing_params (Grob
*me
, Real
*space
, Real
*fixed
)
146 Grob
*separation_item
= 0;
147 Item
*me_item
= dynamic_cast<Item
*> (me
);
149 for (SCM s
= me
->get_property ("left-items");
150 scm_is_pair (s
); s
= scm_cdr (s
))
152 Grob
*cand
= unsmob_grob (scm_car (s
));
153 if (cand
&& Separation_item::has_interface (cand
))
154 separation_item
= cand
;
157 // printf ("doing col %d\n" , Paper_column::get_rank (left_col));
159 if (!separation_item
)
161 programming_error ("no sep item");
166 Grob
*last_grob
= Separation_item::extremal_break_aligned_grob (separation_item
, RIGHT
,
173 Should insert a adjustable space here? For excercises, you might want to
174 use a staff without a clef in the beginning.
178 we used to have a warning here, but it generates a lot of
179 spurious error messages.
184 *fixed
= last_ext
[RIGHT
];
185 *space
= *fixed
+ 1.0;
187 SCM alist
= last_grob
->get_property ("space-alist");
188 if (!scm_list_p (alist
))
191 SCM space_def
= scm_sloppy_assq (ly_symbol2scm ("first-note"), alist
);
192 if (me_item
->break_status_dir () == CENTER
)
194 SCM nndef
= scm_sloppy_assq (ly_symbol2scm ("next-note"), alist
);
195 if (scm_is_pair (nndef
))
199 if (!scm_is_pair (space_def
))
201 programming_error ("unknown prefatory spacing");
205 space_def
= scm_cdr (space_def
);
206 Real distance
= scm_to_double (scm_cdr (space_def
));
207 SCM type
= scm_car (space_def
);
209 *fixed
= last_ext
[RIGHT
];
210 if (type
== ly_symbol2scm ("fixed-space"))
215 else if (type
== ly_symbol2scm ("extra-space"))
217 *space
= *fixed
+ distance
;
219 else if (type
== ly_symbol2scm ("semi-fixed-space"))
221 *fixed
+= distance
/ 2;
222 *space
= *fixed
+ distance
/ 2;
224 else if (type
== ly_symbol2scm ("minimum-space"))
226 *space
= last_ext
[LEFT
] + (last_ext
.length () >? distance
);
228 else if (type
== ly_symbol2scm ("minimum-fixed-space"))
230 *space
= last_ext
[LEFT
] + (last_ext
.length () >? distance
);
234 *space
+= next_notes_correction (me
, last_grob
);
237 ADD_INTERFACE (Staff_spacing
, "staff-spacing-interface",
238 "This object calculates spacing details from a "
239 " breakable symbol (left) to another object. For example, it takes care "
240 " of optical spacing from a bar lines to a note.",
241 "stem-spacing-correction left-items right-items");