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