*** empty log message ***
[lilypond.git] / lily / span-bar.cc
blob5e8947b7e96fabbf0efbef2a1c83db0a89b3613b
1 /*
2 span-bar.cc -- implement Span_bar
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2003 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
9 #include "span-bar.hh"
10 #include "font-interface.hh"
11 #include "dimensions.hh"
12 #include "paper-def.hh"
13 #include "molecule.hh"
14 #include "warn.hh"
15 #include "axis-group-interface.hh"
16 #include "group-interface.hh"
17 #include "grob.hh"
18 #include "bar-line.hh"
20 void
21 Span_bar::add_bar (Grob*me, Grob*b)
23 Pointer_group_interface::add_grob (me, ly_symbol2scm ("elements"), b);
25 me->add_dependency (b);
28 MAKE_SCHEME_CALLBACK (Span_bar,brew_molecule,1);
30 /* Limitations/Bugs:
32 (1) Elements from 'me->get_grob_property ("elements")' must be
33 ordered according to their y coordinates relative to their common
34 axis group parent. Otherwise, the computation goes mad.
36 (TODO:
37 apply a sort algorithm that ensures this precondition.) However,
38 until now, I have seen no case where lily has not fulfilled this
39 precondition.
41 (2) This method depends on bar_engraver not being removed from
42 staff context. If bar_engraver is removed, the size of the staff
43 lines is evaluated as 0, which results in a solid span bar line
44 with faulty y coordinate. */
46 /* This routine was originally by Juergen Reuter, but it was a on the
47 bulky side. Rewritten by Han-Wen. */
48 SCM
49 Span_bar::brew_molecule (SCM smobbed_me)
51 Grob *me = unsmob_grob (smobbed_me);
52 SCM first_elt = me->get_grob_property ("elements");
54 /* compute common refpoint of elements */
55 Grob *refp = me;
56 for (SCM elts = first_elt; gh_pair_p (elts); elts = ly_cdr (elts))
58 SCM smobbed_staff_bar = ly_car (elts);
59 Grob *staff_bar = unsmob_grob (smobbed_staff_bar);
60 refp = staff_bar->common_refpoint (refp, Y_AXIS);
63 Span_bar::evaluate_glyph (me);
64 SCM glyph = me->get_grob_property ("glyph");
66 /* glyph may not be a string, when ME is killed by Hara Kiri in
67 between. */
68 if (!gh_string_p (glyph))
69 return SCM_EOL;
71 String glyph_string = ly_scm2string (glyph);
73 /* compose span_bar_mol */
74 Molecule span_bar_mol;
76 Interval prev_extent;
77 for (SCM elts = first_elt; gh_pair_p (elts); elts = ly_cdr (elts))
79 SCM smobbed_staff_bar = ly_car (elts);
80 Grob *staff_bar = unsmob_grob (smobbed_staff_bar);
81 Interval ext = staff_bar->extent (refp, Y_AXIS);
82 if (ext.empty_b ())
83 continue;
85 if (!prev_extent.empty_b ())
87 Interval l (prev_extent [UP],
88 ext[DOWN]);
90 if (l.empty_b ())
92 /* There is overlap between the bar lines. Do nothing. */
94 else
96 Molecule interbar = Bar_line::compound_barline (staff_bar,
97 glyph_string,
98 l.length ());
99 interbar.translate_axis (l.center (), Y_AXIS);
100 span_bar_mol.add_molecule (interbar);
103 prev_extent = ext;
106 span_bar_mol.translate_axis (- me->relative_coordinate (refp, Y_AXIS),
107 Y_AXIS);
109 return span_bar_mol.smobbed_copy ();
112 MAKE_SCHEME_CALLBACK (Span_bar,width_callback,2);
114 Span_bar::width_callback (SCM element_smob, SCM scm_axis)
116 Grob *se = unsmob_grob (element_smob);
117 Axis a = (Axis) gh_scm2int (scm_axis);
118 assert (a == X_AXIS);
119 String gl = ly_scm2string (se->get_grob_property ("glyph"));
122 urg.
124 Molecule m = Bar_line::compound_barline (se, gl, 40 PT);
126 return ly_interval2scm (m.extent (X_AXIS));
129 MAKE_SCHEME_CALLBACK (Span_bar,before_line_breaking,1);
131 Span_bar::before_line_breaking (SCM smob)
133 evaluate_empty (unsmob_grob (smob));
134 evaluate_glyph (unsmob_grob (smob));
136 /* No need to call Bar_line::before_line_breaking (), because the info
137 in ELEMENTS already has been procced by
138 Bar_line::before_line_breaking (). */
139 return SCM_UNSPECIFIED;
142 MAKE_SCHEME_CALLBACK (Span_bar,center_on_spanned_callback,2);
145 Span_bar::center_on_spanned_callback (SCM element_smob, SCM axis)
147 Grob *me = unsmob_grob (element_smob);
148 Axis a = (Axis) gh_scm2int (axis);
149 assert (a == Y_AXIS);
150 Interval i (get_spanned_interval (me));
152 /* Bar_line::brew_molecule delivers a barline of y-extent (-h/2,h/2), so
153 we have to translate ourselves to be in the center of the
154 interval that we span. */
155 if (i.empty_b ())
157 me->suicide ();
158 return gh_double2scm (0.0);
161 return gh_double2scm (i.center ());
164 void
165 Span_bar::evaluate_empty (Grob*me)
167 /* TODO: filter all hara-kiried out of ELEMENS list, and then
168 optionally do suicide. Call this cleanage function from
169 center_on_spanned_callback () as well. */
170 if (!gh_pair_p (me->get_grob_property ("elements")))
172 me->suicide ();
176 void
177 Span_bar::evaluate_glyph (Grob*me)
179 SCM elts = me->get_grob_property ("elements");
180 SCM glyph_symbol = ly_symbol2scm ("glyph");
181 SCM gl = SCM_EOL;
183 while (gh_pair_p (elts))
185 gl = unsmob_grob (gh_car (elts))
186 ->internal_get_grob_property (glyph_symbol);
187 if (gh_string_p (gl))
188 break;
189 elts =gh_cdr (elts);
192 if (!gh_string_p (gl))
194 me->suicide ();
195 return;
198 String type = ly_scm2string (gl);
199 if (type == "|:")
201 type = ".|";
203 else if (type== ":|")
205 type = "|.";
207 else if (type== ":|:")
209 type = ".|.";
212 gl = scm_makfrom0str (type.to_str0 ());
213 if (scm_equal_p (me->internal_get_grob_property (glyph_symbol), gl)
214 != SCM_BOOL_T)
215 me->internal_set_grob_property (glyph_symbol, gl);
218 Interval
219 Span_bar::get_spanned_interval (Grob*me)
221 return ly_scm2interval (Axis_group_interface::group_extent_callback
222 (me->self_scm (), gh_int2scm (Y_AXIS)));
226 MAKE_SCHEME_CALLBACK (Span_bar,get_bar_size,1);
228 Span_bar::get_bar_size (SCM smob)
230 Grob* me = unsmob_grob (smob);
231 Interval iv (get_spanned_interval (me));
232 if (iv.empty_b ())
234 /* This happens if the bars are hara-kiried from under us. */
235 me->suicide ();
236 return gh_double2scm (-1);
238 return gh_double2scm (iv.length ());
243 ADD_INTERFACE (Span_bar,"span-bar-interface",
244 "A bar line that spans other barlines (typically used to get cross-staff barlines.",
245 "");