]> git.donarmstrong.com Git - lilypond.git/blob - lily/span-bar.cc
* scm/define-context-properties.scm
[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--2005 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8
9 #include "span-bar.hh"
10
11 #include "font-interface.hh"
12 #include "dimensions.hh"
13 #include "output-def.hh"
14 #include "stencil.hh"
15 #include "warn.hh"
16 #include "axis-group-interface.hh"
17 #include "bar-line.hh"
18 #include "grob.hh"
19 #include "pointer-group-interface.hh"
20
21 void
22 Span_bar::add_bar (Grob *me, Grob *b)
23 {
24   Pointer_group_interface::add_grob (me, ly_symbol2scm ("elements"), b);
25 }
26
27 MAKE_SCHEME_CALLBACK (Span_bar, print, 1);
28
29 /* Limitations/Bugs:
30
31 (1) Elements from 'me->get_object ("elements")' must be
32 ordered according to their y coordinates relative to their common
33 axis group parent.  Otherwise, the computation goes mad.
34
35 (2) This method depends on bar_engraver not being removed from
36 staff context.  If bar_engraver is removed, the size of the staff
37 lines is evaluated as 0, which results in a solid span bar line
38 with faulty y coordinate. */
39
40 /* This routine was originally by Juergen Reuter, but it was a on the
41    bulky side. Rewritten by Han-Wen. */
42 SCM
43 Span_bar::print (SCM smobbed_me)
44 {
45   Grob *me = unsmob_grob (smobbed_me);
46   extract_grob_set (me, "elements", elements);
47   Grob *refp = common_refpoint_of_array (elements, me, Y_AXIS);
48
49   Span_bar::evaluate_glyph (me);
50   SCM glyph = me->get_property ("glyph");
51
52   /* glyph may not be a string, when ME is killed by Hara Kiri in
53      between. */
54   if (!scm_is_string (glyph))
55     return SCM_EOL;
56
57   String glyph_string = ly_scm2string (glyph);
58
59   /* compose span_bar_mol */
60   Array<Interval> extents;
61   Grob *model_bar = 0;
62   for (int i = elements.size (); i--;)
63     {
64       Grob *bar = elements[i];
65       Interval ext = bar->extent (refp, Y_AXIS);
66       if (ext.is_empty ())
67         continue;
68
69       extents.push (ext);
70       model_bar = bar;
71     }
72
73   if (!model_bar)
74     model_bar = me;
75
76   extents.sort (&Interval::left_comparison);
77
78   Stencil span_bar;
79   for (int i = 1; i < extents.size (); i++)
80     {
81       Interval prev_extent = extents[i - 1];
82       Interval ext = extents[i];
83       if (!prev_extent.is_empty ())
84         {
85           Interval l (prev_extent [UP],
86                       ext[DOWN]);
87
88           if (l.is_empty ())
89             {
90               /* There is overlap between the bar lines.  Do nothing. */
91             }
92           else
93             {
94               Stencil interbar = Bar_line::compound_barline (model_bar,
95                                                              glyph_string,
96                                                              l.length (),
97                                                              false);
98               interbar.translate_axis (l.center (), Y_AXIS);
99               span_bar.add_stencil (interbar);
100             }
101         }
102       prev_extent = ext;
103     }
104
105   span_bar.translate_axis (- me->relative_coordinate (refp, Y_AXIS),
106                            Y_AXIS);
107
108   return span_bar.smobbed_copy ();
109 }
110
111 MAKE_SCHEME_CALLBACK (Span_bar, width_callback, 2);
112 SCM
113 Span_bar::width_callback (SCM element_smob, SCM scm_axis)
114 {
115   Grob *se = unsmob_grob (element_smob);
116   (void) scm_axis;
117
118   assert ((Axis) scm_to_int (scm_axis) == X_AXIS);
119   String gl = ly_scm2string (se->get_property ("glyph"));
120
121   /*
122     urg.
123   */
124   Stencil m = Bar_line::compound_barline (se, gl, 40 PT, false);
125
126   return ly_interval2scm (m.extent (X_AXIS));
127 }
128
129 MAKE_SCHEME_CALLBACK (Span_bar, before_line_breaking, 1);
130 SCM
131 Span_bar::before_line_breaking (SCM smob)
132 {
133   Grob *g = unsmob_grob (smob);
134   evaluate_empty (g);
135   evaluate_glyph (g);
136
137   /* No need to call Bar_line::before_line_breaking (), because the info
138      in ELEMENTS already has been procced by
139      Bar_line::before_line_breaking (). */
140   return SCM_UNSPECIFIED;
141 }
142
143 MAKE_SCHEME_CALLBACK (Span_bar, center_on_spanned_callback, 2);
144
145 SCM
146 Span_bar::center_on_spanned_callback (SCM element_smob, SCM axis)
147 {
148   Grob *me = unsmob_grob (element_smob);
149   (void) axis;
150   assert (scm_to_int (axis) == Y_AXIS);
151   Interval i (get_spanned_interval (me));
152
153   /* Bar_line::print delivers a barline of y-extent (-h/2, h/2), so
154      we have to translate ourselves to be in the center of the
155      interval that we span. */
156   if (i.is_empty ())
157     {
158       me->suicide ();
159       return scm_from_double (0.0);
160     }
161
162   return scm_from_double (i.center ());
163 }
164
165 void
166 Span_bar::evaluate_empty (Grob *me)
167 {
168   /* TODO: filter all hara-kiried out of ELEMENS list, and then
169      optionally do suicide. Call this cleanage function from
170      center_on_spanned_callback () as well. */
171
172   extract_grob_set (me, "elements", elements);
173   if (elements.is_empty ())
174     me->suicide ();
175 }
176
177 void
178 Span_bar::evaluate_glyph (Grob *me)
179 {
180   SCM gl = me->get_property ("glyph");
181
182   if (scm_is_string (gl))
183     return;
184
185   extract_grob_set (me, "elements", elements);
186   for (int i = elements.size ();
187        i-- && !scm_is_string (gl);)
188     gl = elements[i]->get_property ("glyph");
189
190   if (!scm_is_string (gl))
191     {
192       me->suicide ();
193       return;
194     }
195
196   String type = ly_scm2string (gl);
197   if (type == "|:")
198     type = ".|";
199   else if (type == ":|")
200     type = "|.";
201   else if (type == ":|:")
202     type = ".|.";
203
204   gl = scm_makfrom0str (type.to_str0 ());
205   if (scm_equal_p (me->get_property ("glyph"), gl)
206       != SCM_BOOL_T)
207     me->set_property ("glyph", gl);
208 }
209
210 Interval
211 Span_bar::get_spanned_interval (Grob *me)
212 {
213   return ly_scm2interval (Axis_group_interface::group_extent_callback
214                           (me->self_scm (), scm_from_int (Y_AXIS)));
215 }
216
217 MAKE_SCHEME_CALLBACK (Span_bar, get_bar_size, 1);
218 SCM
219 Span_bar::get_bar_size (SCM smob)
220 {
221   Grob *me = unsmob_grob (smob);
222   Interval iv (get_spanned_interval (me));
223   if (iv.is_empty ())
224     {
225       /* This happens if the bars are hara-kiried from under us. */
226       me->suicide ();
227       return scm_from_double (-1);
228     }
229   return scm_from_double (iv.length ());
230 }
231
232 ADD_INTERFACE (Span_bar, "span-bar-interface",
233                "A bar line that spanned between other barlines. This interface is "
234                " used for  bar lines that connect different staves.",
235                "elements");
236