]> git.donarmstrong.com Git - lilypond.git/blob - lily/span-bar.cc
patch::: 1.3.151.jcn3
[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 SCM
47 Span_bar::brew_molecule (SCM smobbed_me) 
48 {
49   Grob *me = unsmob_grob (smobbed_me);
50   Span_bar::evaluate_glyph(me);
51   SCM glyph = me->get_grob_property (ly_symbol2scm ("glyph"));
52   String glyph_str = ly_scm2string (glyph);
53   SCM first_elt = me->get_grob_property ("elements");
54
55   // first walk: compute axis_group parent via common_refpoint () on all bars
56   Grob *refpoint = 0;
57   int staff_bar_count = 0;
58   for (SCM elts = first_elt;
59        elts != SCM_EOL;
60        elts = gh_cdr (elts))
61   {
62     SCM smobbed_staff_bar = gh_car (elts);
63     Grob *staff_bar = unsmob_grob (smobbed_staff_bar);
64     refpoint = (staff_bar_count > 0) ?
65       staff_bar->common_refpoint (refpoint, Y_AXIS) :
66       staff_bar;
67     staff_bar_count++;
68   }
69   /* assert: refpoint is an axis-group object */
70   Grob *axis_group = refpoint;
71
72   // second walk: collect span bar components;
73   // compute extent of axis_group
74   Real last_staff_bar_length;
75   Real *interstaff_bar_length = new Real[staff_bar_count];
76   Real *interstaff_bar_yoffs = new Real[staff_bar_count];
77   Molecule *interstaff_bar_molecule = new Molecule[staff_bar_count];
78   Real axis_group_extent = 0.0;
79   staff_bar_count = 0;
80   for (SCM elts = first_elt;
81        elts != SCM_EOL;
82        elts = gh_cdr (elts))
83   {
84     SCM smobbed_staff_bar = gh_car (elts);
85     SCM smobbed_staff_bar_molecule =
86       Bar::brew_molecule (smobbed_staff_bar);
87     Grob *staff_bar = unsmob_grob (smobbed_staff_bar);
88     interstaff_bar_yoffs[staff_bar_count] =
89       staff_bar->relative_coordinate (axis_group, (Axis)Y_AXIS);
90     if (smobbed_staff_bar_molecule != SCM_EOL)
91     {
92       Real staff_bar_length =
93         unsmob_molecule (smobbed_staff_bar_molecule)->
94         extent (Y_AXIS).length ();
95       if (staff_bar_count > 0)
96       {
97         // clone bar_molecule and fix y extent
98         interstaff_bar_length[staff_bar_count] =
99           interstaff_bar_yoffs[staff_bar_count] -
100           interstaff_bar_yoffs[staff_bar_count - 1] -
101           last_staff_bar_length;
102         SCM smobbed_interstaff_bar_molecule = 
103           Bar::compound_barline (staff_bar, glyph_str,
104                                  interstaff_bar_length[staff_bar_count]).
105           smobbed_copy ();
106         interstaff_bar_molecule[staff_bar_count] =
107           *unsmob_molecule (smobbed_interstaff_bar_molecule);
108       }
109       else
110       {
111         interstaff_bar_molecule[staff_bar_count] = Molecule::Molecule ();
112       }
113       last_staff_bar_length = staff_bar_length;
114     }
115     else
116     {
117       last_staff_bar_length = 0;
118       interstaff_bar_length[staff_bar_count] = 0;
119       interstaff_bar_molecule[staff_bar_count] = Molecule::Molecule ();
120     }
121     axis_group_extent += last_staff_bar_length;
122     axis_group_extent += interstaff_bar_length[staff_bar_count];
123     staff_bar_count++;
124   }
125   // assert(abs(axis_group_extent -
126   //            axis_group->extent (axis_group, Y_AXIS).length ()) < EPSILON);
127
128   // third walk: correct y axis on all span bar components;
129   // put all components into a single span bar molecule
130   Molecule span_bar_molecule = Molecule::Molecule ();
131   staff_bar_count = 0;
132   for (SCM elts = first_elt;
133        elts != SCM_EOL;
134        elts = gh_cdr (elts))
135   {
136     interstaff_bar_yoffs[staff_bar_count] +=
137       (axis_group_extent - interstaff_bar_length[staff_bar_count]) / 2;
138     interstaff_bar_molecule[staff_bar_count].//DEBUG
139       translate_axis (-1.0 PT, X_AXIS);//DEBUG
140     interstaff_bar_molecule[staff_bar_count].
141       translate_axis (interstaff_bar_yoffs[staff_bar_count], Y_AXIS);
142     span_bar_molecule.add_molecule (interstaff_bar_molecule[staff_bar_count]);
143     staff_bar_count++;
144   }
145
146   // clean-up & exit
147   delete interstaff_bar_length;
148   delete interstaff_bar_yoffs;
149   delete interstaff_bar_molecule;
150   return span_bar_molecule.smobbed_copy ();
151 }
152
153 MAKE_SCHEME_CALLBACK (Span_bar,width_callback,2);
154 SCM
155 Span_bar::width_callback (SCM element_smob, SCM scm_axis)
156 {
157   Grob *se = unsmob_grob (element_smob);
158   Axis a = (Axis) gh_scm2int (scm_axis);
159   assert (a == X_AXIS);
160   String gl = ly_scm2string (se->get_grob_property ("glyph"));
161
162   /*
163     urg.
164    */
165   Molecule m = Bar::compound_barline (se, gl, 40 PT);
166   
167   return ly_interval2scm (m.extent (X_AXIS));
168 }
169
170 MAKE_SCHEME_CALLBACK (Span_bar,before_line_breaking,1);
171 SCM
172 Span_bar::before_line_breaking (SCM smob)
173 {
174   evaluate_empty (unsmob_grob (smob));
175   evaluate_glyph (unsmob_grob (smob));
176
177   /*
178     no need to call   Bar::before_line_breaking (), because the info
179     in ELEMENTS already has been procced by Bar::before_line_breaking ().
180    */
181   return SCM_UNSPECIFIED;
182 }
183
184 MAKE_SCHEME_CALLBACK (Span_bar,center_on_spanned_callback,2);
185
186 SCM
187 Span_bar::center_on_spanned_callback (SCM element_smob, SCM axis)
188 {
189   Grob *me = unsmob_grob (element_smob);
190   Axis a = (Axis) gh_scm2int (axis);
191   assert (a == Y_AXIS);
192   Interval i (get_spanned_interval (me));
193
194   /*
195     Bar::brew_molecule delivers a barline of y-extent (-h/2,h/2), so
196     we have to translate ourselves to be in the center of the 
197     interval that we span.  */
198   if (i.empty_b ())
199     {
200       me->suicide ();
201       return gh_double2scm (0.0);
202     }
203   
204   return gh_double2scm (i.center ());
205 }
206
207 void
208 Span_bar::evaluate_empty (Grob*me)
209 {
210   /*
211     TODO: filter all hara-kiried out of ELEMENS list, and then
212     optionally do suicide. Call this cleanage function from
213     center_on_spanned_callback () as well.
214     
215    */
216   if (!gh_pair_p (me->get_grob_property ("elements")))
217     {
218       me->suicide ();
219     }
220 }
221
222 void
223 Span_bar::evaluate_glyph (Grob*me)
224 {
225   SCM elts = me->get_grob_property ("elements");
226   Grob * b = unsmob_grob (gh_car (elts));
227   SCM glsym =ly_symbol2scm ("glyph");
228   SCM gl =b ->get_grob_property (glsym);
229   if (!gh_string_p (gl))
230     {
231       me->suicide ();
232       return ; 
233     }
234
235   String type = ly_scm2string (gl);
236   
237   if (type == "|:") 
238     {
239       type = ".|";
240     }
241   else if (type== ":|")
242     {
243       type = "|.";
244     }
245   else if (type== ":|:")
246     {
247       type = ".|.";
248     }
249
250   gl = ly_str02scm (type.ch_C ());
251   if (scm_equal_p (me->get_grob_property (glsym), gl) != SCM_BOOL_T)
252     me->set_grob_property (glsym, gl);
253 }
254
255 Interval
256 Span_bar::get_spanned_interval (Grob*me) 
257 {
258   return ly_scm2interval (Axis_group_interface::group_extent_callback (me->self_scm (), gh_int2scm (Y_AXIS))); 
259 }
260
261
262 MAKE_SCHEME_CALLBACK (Span_bar,get_bar_size,1);
263 SCM
264 Span_bar::get_bar_size (SCM smob)
265 {
266   Grob* me =  unsmob_grob (smob);
267   Interval iv (get_spanned_interval (me));
268   if (iv.empty_b ())
269     {
270       /*
271         This happens if the bars are hara-kiried from under us.
272        */
273       me->suicide ();
274       return gh_double2scm (-1);
275     }
276   return gh_double2scm (iv.length ());
277 }
278
279 void
280 Span_bar::set_interface (Grob *me)
281 {
282   Bar::set_interface (me);
283   
284   me->set_interface (ly_symbol2scm ("span-bar-interface"));
285   me->set_extent_callback (SCM_EOL, Y_AXIS);
286 }
287
288 bool
289 Span_bar::has_interface (Grob*m)
290 {
291   return m && m->has_interface (ly_symbol2scm ("span-bar-interface"));
292 }