]> git.donarmstrong.com Git - lilypond.git/blob - lily/span-bar.cc
release: 1.5.9
[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
46 /*
47   This routine was originally by Juergen Reuter, but it was a on the
48   bulky side. Rewritten by Han-Wen. 
49  */
50 SCM
51 Span_bar::brew_molecule (SCM smobbed_me) 
52 {
53   Grob *me = unsmob_grob (smobbed_me);
54   SCM first_elt = me->get_grob_property ("elements");
55
56   // compute common refpoint of elements
57   Grob *refp = me;
58   for (SCM elts = first_elt; gh_pair_p (elts); elts = gh_cdr (elts))
59     {
60       SCM smobbed_staff_bar = gh_car (elts);
61       Grob *staff_bar = unsmob_grob (smobbed_staff_bar);
62       refp = staff_bar->common_refpoint (refp, Y_AXIS);
63     }
64
65   Span_bar::evaluate_glyph(me);
66   SCM glyph = me->get_grob_property (ly_symbol2scm ("glyph"));
67
68   /*
69     glyph may not be a string, when ME is killed by Hara Kiri in
70     between.
71   */
72   if (!gh_string_p (glyph))
73     return SCM_EOL;
74   
75   String glyph_str = ly_scm2string (glyph);
76
77   // compose span_bar_mol
78   Molecule span_bar_mol;
79
80   Interval prev_extent;
81   for (SCM elts = first_elt; gh_pair_p (elts); elts = gh_cdr (elts))
82     {
83       SCM smobbed_staff_bar = gh_car (elts);
84       Grob *staff_bar = unsmob_grob (smobbed_staff_bar);
85       Interval ext = staff_bar->extent (refp, Y_AXIS);
86       if (ext.empty_b ())
87         continue; 
88       
89       if (!prev_extent.empty_b ())
90         {
91           Interval l(prev_extent [UP],
92                      ext[DOWN]);
93
94           Molecule interbar
95             = Bar::compound_barline (staff_bar, glyph_str, l.length());
96           interbar.translate_axis (l.center (), Y_AXIS);
97           span_bar_mol.add_molecule (interbar);
98         }
99       prev_extent = ext;
100     }
101
102   span_bar_mol.translate_axis (- me->relative_coordinate (refp, Y_AXIS), Y_AXIS);
103   
104   return span_bar_mol.smobbed_copy ();
105 }
106
107 MAKE_SCHEME_CALLBACK (Span_bar,width_callback,2);
108 SCM
109 Span_bar::width_callback (SCM element_smob, SCM scm_axis)
110 {
111   Grob *se = unsmob_grob (element_smob);
112   Axis a = (Axis) gh_scm2int (scm_axis);
113   assert (a == X_AXIS);
114   String gl = ly_scm2string (se->get_grob_property ("glyph"));
115
116   /*
117     urg.
118   */
119   Molecule m = Bar::compound_barline (se, gl, 40 PT);
120   
121   return ly_interval2scm (m.extent (X_AXIS));
122 }
123
124 MAKE_SCHEME_CALLBACK (Span_bar,before_line_breaking,1);
125 SCM
126 Span_bar::before_line_breaking (SCM smob)
127 {
128   evaluate_empty (unsmob_grob (smob));
129   evaluate_glyph (unsmob_grob (smob));
130
131   /*
132     no need to call   Bar::before_line_breaking (), because the info
133     in ELEMENTS already has been procced by Bar::before_line_breaking ().
134   */
135   return SCM_UNSPECIFIED;
136 }
137
138 MAKE_SCHEME_CALLBACK (Span_bar,center_on_spanned_callback,2);
139
140 SCM
141 Span_bar::center_on_spanned_callback (SCM element_smob, SCM axis)
142 {
143   Grob *me = unsmob_grob (element_smob);
144   Axis a = (Axis) gh_scm2int (axis);
145   assert (a == Y_AXIS);
146   Interval i (get_spanned_interval (me));
147
148   /*
149     Bar::brew_molecule delivers a barline of y-extent (-h/2,h/2), so
150     we have to translate ourselves to be in the center of the 
151     interval that we span.  */
152   if (i.empty_b ())
153     {
154       me->suicide ();
155       return gh_double2scm (0.0);
156     }
157   
158   return gh_double2scm (i.center ());
159 }
160
161 void
162 Span_bar::evaluate_empty (Grob*me)
163 {
164   /*
165     TODO: filter all hara-kiried out of ELEMENS list, and then
166     optionally do suicide. Call this cleanage function from
167     center_on_spanned_callback () as well.
168     
169    */
170   if (!gh_pair_p (me->get_grob_property ("elements")))
171     {
172       me->suicide ();
173     }
174 }
175
176 void
177 Span_bar::evaluate_glyph (Grob*me)
178 {
179   SCM elts = me->get_grob_property ("elements");
180   Grob * b = unsmob_grob (gh_car (elts));
181   SCM glsym =ly_symbol2scm ("glyph");
182   SCM gl =b ->get_grob_property (glsym);
183   if (!gh_string_p (gl))
184     {
185       me->suicide ();
186       return ; 
187     }
188
189   String type = ly_scm2string (gl);
190   
191   if (type == "|:") 
192     {
193       type = ".|";
194     }
195   else if (type== ":|")
196     {
197       type = "|.";
198     }
199   else if (type== ":|:")
200     {
201       type = ".|.";
202     }
203
204   gl = ly_str02scm (type.ch_C ());
205   if (scm_equal_p (me->get_grob_property (glsym), gl) != SCM_BOOL_T)
206     me->set_grob_property (glsym, gl);
207 }
208
209 Interval
210 Span_bar::get_spanned_interval (Grob*me) 
211 {
212   return ly_scm2interval (Axis_group_interface::group_extent_callback (me->self_scm (), gh_int2scm (Y_AXIS))); 
213 }
214
215
216 MAKE_SCHEME_CALLBACK (Span_bar,get_bar_size,1);
217 SCM
218 Span_bar::get_bar_size (SCM smob)
219 {
220   Grob* me =  unsmob_grob (smob);
221   Interval iv (get_spanned_interval (me));
222   if (iv.empty_b ())
223     {
224       /*
225         This happens if the bars are hara-kiried from under us.
226       */
227       me->suicide ();
228       return gh_double2scm (-1);
229     }
230   return gh_double2scm (iv.length ());
231 }
232
233 void
234 Span_bar::set_interface (Grob *me)
235 {
236   Bar::set_interface (me);
237   
238   me->set_interface (ly_symbol2scm ("span-bar-interface"));
239   me->set_extent_callback (SCM_EOL, Y_AXIS);
240 }
241
242 bool
243 Span_bar::has_interface (Grob*m)
244 {
245   return m && m->has_interface (ly_symbol2scm ("span-bar-interface"));
246 }