]> git.donarmstrong.com Git - lilypond.git/blob - lily/span-bar.cc
476d028e2af0d2008d05f0c77023752c224efe58
[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   Interval prev_extent;
96   for (SCM elts = first_elt;
97        gh_pair_p (elts);
98        elts = gh_cdr (elts))
99   {
100     SCM smobbed_staff_bar = gh_car (elts);
101     Grob *staff_bar = unsmob_grob (smobbed_staff_bar);
102     Interval ext = staff_bar->extent (refp, Y_AXIS);
103
104     if (ext.empty_b ())
105       continue; 
106     
107     if (!prev_extent.empty_b ()) {
108
109       Interval l;
110       l[LEFT] = prev_extent[UP];
111       l[RIGHT] = ext[DOWN];
112       
113       //SCM smobbed_staff_bar = gh_car (elts);
114       //Grob *staff_bar = unsmob_grob (smobbed_staff_bar);
115       SCM smobbed_interstaff_bar_molecule = 
116         Bar::compound_barline (staff_bar, glyph_str, l.length()).
117         smobbed_copy ();
118       
119       Molecule *interstaff_bar_mol =
120         unsmob_molecule (smobbed_interstaff_bar_molecule);
121       
122       yoffs += prev_extent.length (); // skip staff bar
123       yoffs += 0.5 * (l[RIGHT] - l[LEFT]); // compensate interstaff bar centering
124       interstaff_bar_mol->translate_axis (yoffs, Y_AXIS);
125       yoffs += 0.5 * (l[RIGHT] - l[LEFT]);
126       
127       span_bar_mol.add_molecule (*interstaff_bar_mol);
128     }
129     prev_extent = ext;
130   }
131
132   return span_bar_mol.smobbed_copy ();
133 }
134
135 MAKE_SCHEME_CALLBACK (Span_bar,width_callback,2);
136 SCM
137 Span_bar::width_callback (SCM element_smob, SCM scm_axis)
138 {
139   Grob *se = unsmob_grob (element_smob);
140   Axis a = (Axis) gh_scm2int (scm_axis);
141   assert (a == X_AXIS);
142   String gl = ly_scm2string (se->get_grob_property ("glyph"));
143
144   /*
145     urg.
146    */
147   Molecule m = Bar::compound_barline (se, gl, 40 PT);
148   
149   return ly_interval2scm (m.extent (X_AXIS));
150 }
151
152 MAKE_SCHEME_CALLBACK (Span_bar,before_line_breaking,1);
153 SCM
154 Span_bar::before_line_breaking (SCM smob)
155 {
156   evaluate_empty (unsmob_grob (smob));
157   evaluate_glyph (unsmob_grob (smob));
158
159   /*
160     no need to call   Bar::before_line_breaking (), because the info
161     in ELEMENTS already has been procced by Bar::before_line_breaking ().
162    */
163   return SCM_UNSPECIFIED;
164 }
165
166 MAKE_SCHEME_CALLBACK (Span_bar,center_on_spanned_callback,2);
167
168 SCM
169 Span_bar::center_on_spanned_callback (SCM element_smob, SCM axis)
170 {
171   Grob *me = unsmob_grob (element_smob);
172   Axis a = (Axis) gh_scm2int (axis);
173   assert (a == Y_AXIS);
174   Interval i (get_spanned_interval (me));
175
176   /*
177     Bar::brew_molecule delivers a barline of y-extent (-h/2,h/2), so
178     we have to translate ourselves to be in the center of the 
179     interval that we span.  */
180   if (i.empty_b ())
181     {
182       me->suicide ();
183       return gh_double2scm (0.0);
184     }
185   
186   return gh_double2scm (i.center ());
187 }
188
189 void
190 Span_bar::evaluate_empty (Grob*me)
191 {
192   /*
193     TODO: filter all hara-kiried out of ELEMENS list, and then
194     optionally do suicide. Call this cleanage function from
195     center_on_spanned_callback () as well.
196     
197    */
198   if (!gh_pair_p (me->get_grob_property ("elements")))
199     {
200       me->suicide ();
201     }
202 }
203
204 void
205 Span_bar::evaluate_glyph (Grob*me)
206 {
207   SCM elts = me->get_grob_property ("elements");
208   Grob * b = unsmob_grob (gh_car (elts));
209   SCM glsym =ly_symbol2scm ("glyph");
210   SCM gl =b ->get_grob_property (glsym);
211   if (!gh_string_p (gl))
212     {
213       me->suicide ();
214       return ; 
215     }
216
217   String type = ly_scm2string (gl);
218   
219   if (type == "|:") 
220     {
221       type = ".|";
222     }
223   else if (type== ":|")
224     {
225       type = "|.";
226     }
227   else if (type== ":|:")
228     {
229       type = ".|.";
230     }
231
232   gl = ly_str02scm (type.ch_C ());
233   if (scm_equal_p (me->get_grob_property (glsym), gl) != SCM_BOOL_T)
234     me->set_grob_property (glsym, gl);
235 }
236
237 Interval
238 Span_bar::get_spanned_interval (Grob*me) 
239 {
240   return ly_scm2interval (Axis_group_interface::group_extent_callback (me->self_scm (), gh_int2scm (Y_AXIS))); 
241 }
242
243
244 MAKE_SCHEME_CALLBACK (Span_bar,get_bar_size,1);
245 SCM
246 Span_bar::get_bar_size (SCM smob)
247 {
248   Grob* me =  unsmob_grob (smob);
249   Interval iv (get_spanned_interval (me));
250   if (iv.empty_b ())
251     {
252       /*
253         This happens if the bars are hara-kiried from under us.
254        */
255       me->suicide ();
256       return gh_double2scm (-1);
257     }
258   return gh_double2scm (iv.length ());
259 }
260
261 void
262 Span_bar::set_interface (Grob *me)
263 {
264   Bar::set_interface (me);
265   
266   me->set_interface (ly_symbol2scm ("span-bar-interface"));
267   me->set_extent_callback (SCM_EOL, Y_AXIS);
268 }
269
270 bool
271 Span_bar::has_interface (Grob*m)
272 {
273   return m && m->has_interface (ly_symbol2scm ("span-bar-interface"));
274 }