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