]> git.donarmstrong.com Git - lilypond.git/blob - lily/span-bar.cc
*** empty log message ***
[lilypond.git] / lily / span-bar.cc
1 /*
2   span-bar.cc -- implement Span_bar
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1997--2005 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8
9 #include "span-bar.hh"
10
11 #include "font-interface.hh"
12 #include "dimensions.hh"
13 #include "output-def.hh"
14 #include "stencil.hh"
15 #include "warn.hh"
16 #include "axis-group-interface.hh"
17 #include "bar-line.hh"
18
19 void
20 Span_bar::add_bar (Grob *me, Grob *b)
21 {
22   Pointer_group_interface::add_grob (me, ly_symbol2scm ("elements"), b);
23
24   me->add_dependency (b);
25 }
26
27 MAKE_SCHEME_CALLBACK (Span_bar, print, 1);
28
29 /* Limitations/Bugs:
30
31 (1) Elements from 'me->get_property ("elements")' must be
32 ordered according to their y coordinates relative to their common
33 axis group parent.  Otherwise, the computation goes mad.
34
35 (TODO:
36 apply a sort algorithm that ensures this precondition.)  However,
37 until now, I have seen no case where lily has not fulfilled this
38 precondition.
39
40 (2) This method depends on bar_engraver not being removed from
41 staff context.  If bar_engraver is removed, the size of the staff
42 lines is evaluated as 0, which results in a solid span bar line
43 with faulty y coordinate. */
44
45 /* This routine was originally by Juergen Reuter, but it was a on the
46    bulky side. Rewritten by Han-Wen. */
47 SCM
48 Span_bar::print (SCM smobbed_me)
49 {
50   Grob *me = unsmob_grob (smobbed_me);
51   SCM first_elt = me->get_property ("elements");
52
53   /* compute common refpoint of elements */
54   Grob *refp = me;
55   for (SCM elts = first_elt; scm_is_pair (elts); elts = scm_cdr (elts))
56     {
57       SCM smobbed_staff_bar = scm_car (elts);
58       Grob *staff_bar = unsmob_grob (smobbed_staff_bar);
59       refp = staff_bar->common_refpoint (refp, Y_AXIS);
60     }
61
62   Span_bar::evaluate_glyph (me);
63   SCM glyph = me->get_property ("glyph");
64
65   /* glyph may not be a string, when ME is killed by Hara Kiri in
66      between. */
67   if (!scm_is_string (glyph))
68     return SCM_EOL;
69
70   String glyph_string = ly_scm2string (glyph);
71
72   /* compose span_bar_mol */
73   Stencil span_bar_mol;
74
75   Interval prev_extent;
76   for (SCM elts = first_elt; scm_is_pair (elts); elts = scm_cdr (elts))
77     {
78       SCM smobbed_staff_bar = scm_car (elts);
79       Grob *staff_bar = unsmob_grob (smobbed_staff_bar);
80       Interval ext = staff_bar->extent (refp, Y_AXIS);
81       if (ext.is_empty ())
82         continue;
83
84       if (!prev_extent.is_empty ())
85         {
86           Interval l (prev_extent [UP],
87                       ext[DOWN]);
88
89           if (l.is_empty ())
90             {
91               /* There is overlap between the bar lines.  Do nothing. */
92             }
93           else
94             {
95               Stencil interbar = Bar_line::compound_barline (staff_bar,
96                                                              glyph_string,
97                                                              l.length (),
98                                                              false);
99               interbar.translate_axis (l.center (), Y_AXIS);
100               span_bar_mol.add_stencil (interbar);
101             }
102         }
103       prev_extent = ext;
104     }
105
106   span_bar_mol.translate_axis (- me->relative_coordinate (refp, Y_AXIS),
107                                Y_AXIS);
108
109   return span_bar_mol.smobbed_copy ();
110 }
111
112 MAKE_SCHEME_CALLBACK (Span_bar, width_callback, 2);
113 SCM
114 Span_bar::width_callback (SCM element_smob, SCM scm_axis)
115 {
116   Grob *se = unsmob_grob (element_smob);
117   (void) scm_axis;
118
119   assert ( (Axis) scm_to_int (scm_axis) == X_AXIS);
120   String gl = ly_scm2string (se->get_property ("glyph"));
121
122   /*
123     urg.
124   */
125   Stencil m = Bar_line::compound_barline (se, gl, 40 PT, false);
126
127   return ly_interval2scm (m.extent (X_AXIS));
128 }
129
130 MAKE_SCHEME_CALLBACK (Span_bar, before_line_breaking, 1);
131 SCM
132 Span_bar::before_line_breaking (SCM smob)
133 {
134   Grob *g = unsmob_grob (smob);
135   evaluate_empty (g);
136   evaluate_glyph (g);
137
138   /* No need to call Bar_line::before_line_breaking (), because the info
139      in ELEMENTS already has been procced by
140      Bar_line::before_line_breaking (). */
141   return SCM_UNSPECIFIED;
142 }
143
144 MAKE_SCHEME_CALLBACK (Span_bar, center_on_spanned_callback, 2);
145
146 SCM
147 Span_bar::center_on_spanned_callback (SCM element_smob, SCM axis)
148 {
149   Grob *me = unsmob_grob (element_smob);
150   (void) axis;
151   assert (scm_to_int (axis) == Y_AXIS);
152   Interval i (get_spanned_interval (me));
153
154   /* Bar_line::print delivers a barline of y-extent (-h/2, h/2), so
155      we have to translate ourselves to be in the center of the
156      interval that we span. */
157   if (i.is_empty ())
158     {
159       me->suicide ();
160       return scm_make_real (0.0);
161     }
162
163   return scm_make_real (i.center ());
164 }
165
166 void
167 Span_bar::evaluate_empty (Grob *me)
168 {
169   /* TODO: filter all hara-kiried out of ELEMENS list, and then
170      optionally do suicide. Call this cleanage function from
171      center_on_spanned_callback () as well. */
172   if (!scm_is_pair (me->get_property ("elements")))
173     {
174       me->suicide ();
175     }
176 }
177
178 void
179 Span_bar::evaluate_glyph (Grob *me)
180 {
181   SCM gl = me->get_property ("glyph");
182
183   if (scm_is_string (gl))
184     return;
185
186   for (SCM s = me->get_property ("elements");
187        !scm_is_string (gl) && scm_is_pair (s); s = scm_cdr (s))
188     {
189       gl = unsmob_grob (scm_car (s))
190         ->get_property ("glyph");
191     }
192
193   if (!scm_is_string (gl))
194     {
195       me->suicide ();
196       return;
197     }
198
199   String type = ly_scm2string (gl);
200   if (type == "|:")
201     {
202       type = ".|";
203     }
204   else if (type == ":|")
205     {
206       type = "|.";
207     }
208   else if (type == ":|:")
209     {
210       type = ".|.";
211     }
212
213   gl = scm_makfrom0str (type.to_str0 ());
214   if (scm_equal_p (me->get_property ("glyph"), gl)
215       != SCM_BOOL_T)
216     me->set_property ("glyph", gl);
217 }
218
219 Interval
220 Span_bar::get_spanned_interval (Grob *me)
221 {
222   return ly_scm2interval (Axis_group_interface::group_extent_callback
223                           (me->self_scm (), scm_int2num (Y_AXIS)));
224 }
225
226 MAKE_SCHEME_CALLBACK (Span_bar, get_bar_size, 1);
227 SCM
228 Span_bar::get_bar_size (SCM smob)
229 {
230   Grob *me = unsmob_grob (smob);
231   Interval iv (get_spanned_interval (me));
232   if (iv.is_empty ())
233     {
234       /* This happens if the bars are hara-kiried from under us. */
235       me->suicide ();
236       return scm_make_real (-1);
237     }
238   return scm_make_real (iv.length ());
239 }
240
241 ADD_INTERFACE (Span_bar, "span-bar-interface",
242                "A bar line that spanned between other barlines. This interface is "
243                " used for  bar lines that connect different staves.",
244                "elements");
245