]> git.donarmstrong.com Git - lilypond.git/blob - lily/span-bar.cc
* lily/tie-performer.cc (stop_translation_timestep): reset
[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--2004 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8
9 #include "span-bar.hh"
10 #include "font-interface.hh"
11 #include "dimensions.hh"
12 #include "output-def.hh"
13 #include "stencil.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"
19
20 void
21 Span_bar::add_bar (Grob*me, Grob*b)
22 {
23   Pointer_group_interface::add_grob (me, ly_symbol2scm ("elements"),  b);
24
25   me->add_dependency (b);
26 }
27
28 MAKE_SCHEME_CALLBACK (Span_bar,print,1);
29
30 /* Limitations/Bugs:
31
32    (1) Elements from 'me->get_property ("elements")' must be
33    ordered according to their y coordinates relative to their common
34    axis group parent.  Otherwise, the computation goes mad.
35
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.
40
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. */
45
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::print (SCM smobbed_me) 
50 {
51   Grob *me = unsmob_grob (smobbed_me);
52   SCM first_elt = me->get_property ("elements");
53
54   /* compute common refpoint of elements */
55   Grob *refp = me;
56   for (SCM elts = first_elt; ly_c_pair_p (elts); elts = ly_cdr (elts))
57     {
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);
61     }
62
63   Span_bar::evaluate_glyph (me);
64   SCM glyph = me->get_property ("glyph");
65
66   /* glyph may not be a string, when ME is killed by Hara Kiri in
67     between. */
68   if (!ly_c_string_p (glyph))
69     return SCM_EOL;
70   
71   String glyph_string = ly_scm2string (glyph);
72
73   /* compose span_bar_mol */
74   Stencil span_bar_mol;
75
76   Interval prev_extent;
77   for (SCM elts = first_elt; ly_c_pair_p (elts); elts = ly_cdr (elts))
78     {
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.is_empty ())
83         continue; 
84       
85       if (!prev_extent.is_empty ())
86         {
87           Interval l (prev_extent [UP],
88                      ext[DOWN]);
89
90           if (l.is_empty ())
91             {
92               /* There is overlap between the bar lines.  Do nothing. */
93             }
94           else
95             {
96               Stencil 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_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   Axis a = (Axis) ly_scm2int (scm_axis);
118   assert (a == X_AXIS);
119   String gl = ly_scm2string (se->get_property ("glyph"));
120
121   /*
122     urg.
123   */
124   Stencil m = Bar_line::compound_barline (se, gl, 40 PT);
125   
126   return ly_interval2scm (m.extent (X_AXIS));
127 }
128
129 MAKE_SCHEME_CALLBACK (Span_bar,before_line_breaking,1);
130 SCM
131 Span_bar::before_line_breaking (SCM smob)
132 {
133   Grob * g = unsmob_grob (smob);
134   evaluate_empty (g);
135   evaluate_glyph (g);
136
137   /* No need to call Bar_line::before_line_breaking (), because the info
138      in ELEMENTS already has been procced by
139      Bar_line::before_line_breaking (). */
140   return SCM_UNSPECIFIED;
141 }
142
143 MAKE_SCHEME_CALLBACK (Span_bar,center_on_spanned_callback,2);
144
145 SCM
146 Span_bar::center_on_spanned_callback (SCM element_smob, SCM axis)
147 {
148   Grob *me = unsmob_grob (element_smob);
149   Axis a = (Axis) ly_scm2int (axis);
150   assert (a == Y_AXIS);
151   Interval i (get_spanned_interval (me));
152
153   /* Bar_line::print delivers a barline of y-extent (-h/2,h/2), so
154      we have to translate ourselves to be in the center of the 
155      interval that we span. */
156   if (i.is_empty ())
157     {
158       me->suicide ();
159       return scm_make_real (0.0);
160     }
161   
162   return scm_make_real (i.center ());
163 }
164
165 void
166 Span_bar::evaluate_empty (Grob*me)
167 {
168   /* TODO: filter all hara-kiried out of ELEMENS list, and then
169      optionally do suicide. Call this cleanage function from
170      center_on_spanned_callback () as well. */
171   if (!ly_c_pair_p (me->get_property ("elements")))
172     {
173       me->suicide ();
174     }
175 }
176
177 void
178 Span_bar::evaluate_glyph (Grob*me)
179 {
180   SCM gl = me->get_property ("glyph");
181
182   if (ly_c_string_p (gl))
183     return ;
184   
185   for (SCM s = me->get_property ("elements");
186        !ly_c_string_p (gl) && ly_c_pair_p (s); s = ly_cdr (s))
187     {
188       gl = unsmob_grob (ly_car (s))
189         ->get_property ("glyph");
190     }
191
192   if (!ly_c_string_p (gl))
193     {
194       me->suicide ();
195       return;
196     }
197   
198   String type = ly_scm2string (gl);
199   if (type == "|:") 
200     {
201       type = ".|";
202     }
203   else if (type== ":|")
204     {
205       type = "|.";
206     }
207   else if (type== ":|:")
208     {
209       type = ".|.";
210     }
211
212   gl = scm_makfrom0str (type.to_str0 ());
213   if (scm_equal_p (me->get_property ("glyph"), gl)
214       != SCM_BOOL_T)
215     me->set_property ("glyph", gl);
216 }
217
218 Interval
219 Span_bar::get_spanned_interval (Grob*me) 
220 {
221   return ly_scm2interval (Axis_group_interface::group_extent_callback
222                           (me->self_scm (), scm_int2num (Y_AXIS))); 
223 }
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
242
243 ADD_INTERFACE (Span_bar,"span-bar-interface",
244   "A bar line that spanned between other barlines. This interface is "
245                " used for  bar lines that connect different staves.",
246   "elements");
247
248