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