]> git.donarmstrong.com Git - lilypond.git/blob - lily/span-bar.cc
7f7768b2d16f17251fbb76678d1801b192b17dc7
[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--2001 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 "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.hh"
19
20 void
21 Span_bar::add_bar (Grob*me, Grob*b)
22 {
23   Pointer_group_interface::add_element (me,"elements", b);
24
25   me->add_dependency (b);
26 }
27
28 MAKE_SCHEME_CALLBACK (Span_bar,brew_molecule,1);
29
30 /**
31  * Limitations/Bugs:
32  *
33  * (1) Elements from 'me->get_grob_property ("elements")' must be
34  * ordered according to their y coordinates relative to their common
35  * axis group parent.  Otherwise, the computation goes mad.  (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 SCM
46 Span_bar::brew_molecule (SCM smobbed_me) 
47 {
48   Grob *me = unsmob_grob (smobbed_me);
49   SCM first_elt = me->get_grob_property ("elements");
50   Grob *first_staff_bar = unsmob_grob (gh_car (first_elt));
51   Grob *last_staff_bar = 0;
52
53   // compute common refpoint of elements & last_staff_bar
54   Grob *refp = me;
55   for (SCM elts = first_elt;
56        gh_pair_p (elts);
57        elts = gh_cdr (elts))
58   {
59     SCM smobbed_staff_bar = gh_car (elts);
60     Grob *staff_bar = unsmob_grob (smobbed_staff_bar);
61     refp = staff_bar->common_refpoint (refp, Y_AXIS);
62     last_staff_bar = staff_bar;
63   }
64
65   // determine refp->extent, but ignore lyrics etc. above and below
66   Interval refp_extent;
67   refp_extent[LEFT] =
68     first_staff_bar->relative_coordinate (refp, (Axis)Y_AXIS) -
69     0.5 * (first_staff_bar->extent (refp, Y_AXIS)[UP] -
70            first_staff_bar->extent (refp, Y_AXIS)[DOWN]);
71   refp_extent[RIGHT] =
72     last_staff_bar->relative_coordinate (refp, (Axis)Y_AXIS) +
73     0.5 * (last_staff_bar->extent (refp, Y_AXIS)[UP] -
74            last_staff_bar->extent (refp, Y_AXIS)[DOWN]);
75
76   // global yoffs correction (compensate centering around refp)
77   Real yoffs = 0.5 * (refp_extent[LEFT] - refp_extent[RIGHT]);
78
79   // evaluate glyph
80   Span_bar::evaluate_glyph(me);
81   SCM glyph = me->get_grob_property (ly_symbol2scm ("glyph"));
82
83
84   /*
85     glyph may not be a string, when ME is killed by Hara Kiri in
86     between.
87   */
88   if (!gh_string_p (glyph))
89     return SCM_EOL;
90
91   String glyph_str = ly_scm2string (glyph);
92
93   // compose span_bar_mol
94   Molecule span_bar_mol = Molecule::Molecule ();
95   Grob *prev_staff_bar = 0;
96   Real prev_staff_bar_length = 0.0;
97   for (SCM elts = first_elt;
98        gh_pair_p (elts);
99        elts = gh_cdr (elts))
100   {
101     SCM smobbed_staff_bar = gh_car (elts);
102     SCM smobbed_staff_bar_molecule =
103       Bar::brew_molecule (smobbed_staff_bar);
104     Grob *staff_bar = unsmob_grob (smobbed_staff_bar);
105     Real staff_bar_length =
106       unsmob_molecule (smobbed_staff_bar_molecule)->
107       extent (Y_AXIS).length ();
108
109     if (prev_staff_bar) {
110
111       Interval l;
112       l[LEFT] = prev_staff_bar->extent (refp, Y_AXIS)[UP];
113       l[RIGHT] = staff_bar->extent (refp, Y_AXIS)[DOWN];
114
115       SCM smobbed_staff_bar = gh_car (elts);
116       Grob *staff_bar = unsmob_grob (smobbed_staff_bar);
117       SCM smobbed_interstaff_bar_molecule = 
118         Bar::compound_barline (staff_bar, glyph_str, l.length()).
119         smobbed_copy ();
120
121       Molecule *interstaff_bar_mol =
122         unsmob_molecule (smobbed_interstaff_bar_molecule);
123
124       yoffs += prev_staff_bar_length; // skip staff bar
125       yoffs += 0.5 * (l[RIGHT] - l[LEFT]); // compensate interstaff bar centering
126       interstaff_bar_mol->translate_axis (yoffs, Y_AXIS);
127       yoffs += 0.5 * (l[RIGHT] - l[LEFT]);
128
129       span_bar_mol.add_molecule (*interstaff_bar_mol);
130     }
131     prev_staff_bar = staff_bar;
132     prev_staff_bar_length = staff_bar_length;
133   }
134
135   return span_bar_mol.smobbed_copy ();
136 }
137
138 MAKE_SCHEME_CALLBACK (Span_bar,width_callback,2);
139 SCM
140 Span_bar::width_callback (SCM element_smob, SCM scm_axis)
141 {
142   Grob *se = unsmob_grob (element_smob);
143   Axis a = (Axis) gh_scm2int (scm_axis);
144   assert (a == X_AXIS);
145   String gl = ly_scm2string (se->get_grob_property ("glyph"));
146
147   /*
148     urg.
149    */
150   Molecule m = Bar::compound_barline (se, gl, 40 PT);
151   
152   return ly_interval2scm (m.extent (X_AXIS));
153 }
154
155 MAKE_SCHEME_CALLBACK (Span_bar,before_line_breaking,1);
156 SCM
157 Span_bar::before_line_breaking (SCM smob)
158 {
159   evaluate_empty (unsmob_grob (smob));
160   evaluate_glyph (unsmob_grob (smob));
161
162   /*
163     no need to call   Bar::before_line_breaking (), because the info
164     in ELEMENTS already has been procced by Bar::before_line_breaking ().
165    */
166   return SCM_UNSPECIFIED;
167 }
168
169 MAKE_SCHEME_CALLBACK (Span_bar,center_on_spanned_callback,2);
170
171 SCM
172 Span_bar::center_on_spanned_callback (SCM element_smob, SCM axis)
173 {
174   Grob *me = unsmob_grob (element_smob);
175   Axis a = (Axis) gh_scm2int (axis);
176   assert (a == Y_AXIS);
177   Interval i (get_spanned_interval (me));
178
179   /*
180     Bar::brew_molecule delivers a barline of y-extent (-h/2,h/2), so
181     we have to translate ourselves to be in the center of the 
182     interval that we span.  */
183   if (i.empty_b ())
184     {
185       me->suicide ();
186       return gh_double2scm (0.0);
187     }
188   
189   return gh_double2scm (i.center ());
190 }
191
192 void
193 Span_bar::evaluate_empty (Grob*me)
194 {
195   /*
196     TODO: filter all hara-kiried out of ELEMENS list, and then
197     optionally do suicide. Call this cleanage function from
198     center_on_spanned_callback () as well.
199     
200    */
201   if (!gh_pair_p (me->get_grob_property ("elements")))
202     {
203       me->suicide ();
204     }
205 }
206
207 void
208 Span_bar::evaluate_glyph (Grob*me)
209 {
210   SCM elts = me->get_grob_property ("elements");
211   Grob * b = unsmob_grob (gh_car (elts));
212   SCM glsym =ly_symbol2scm ("glyph");
213   SCM gl =b ->get_grob_property (glsym);
214   if (!gh_string_p (gl))
215     {
216       me->suicide ();
217       return ; 
218     }
219
220   String type = ly_scm2string (gl);
221   
222   if (type == "|:") 
223     {
224       type = ".|";
225     }
226   else if (type== ":|")
227     {
228       type = "|.";
229     }
230   else if (type== ":|:")
231     {
232       type = ".|.";
233     }
234
235   gl = ly_str02scm (type.ch_C ());
236   if (scm_equal_p (me->get_grob_property (glsym), gl) != SCM_BOOL_T)
237     me->set_grob_property (glsym, gl);
238 }
239
240 Interval
241 Span_bar::get_spanned_interval (Grob*me) 
242 {
243   return ly_scm2interval (Axis_group_interface::group_extent_callback (me->self_scm (), gh_int2scm (Y_AXIS))); 
244 }
245
246
247 MAKE_SCHEME_CALLBACK (Span_bar,get_bar_size,1);
248 SCM
249 Span_bar::get_bar_size (SCM smob)
250 {
251   Grob* me =  unsmob_grob (smob);
252   Interval iv (get_spanned_interval (me));
253   if (iv.empty_b ())
254     {
255       /*
256         This happens if the bars are hara-kiried from under us.
257        */
258       me->suicide ();
259       return gh_double2scm (-1);
260     }
261   return gh_double2scm (iv.length ());
262 }
263
264 void
265 Span_bar::set_interface (Grob *me)
266 {
267   Bar::set_interface (me);
268   
269   me->set_interface (ly_symbol2scm ("span-bar-interface"));
270   me->set_extent_callback (SCM_EOL, Y_AXIS);
271 }
272
273 bool
274 Span_bar::has_interface (Grob*m)
275 {
276   return m && m->has_interface (ly_symbol2scm ("span-bar-interface"));
277 }