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