]> git.donarmstrong.com Git - lilypond.git/blob - lily/span-bar.cc
* lily/script-engraver.cc (make_script_from_event): don't trigger callback.
[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   SCM glyph = me->get_property ("glyph-name");
50
51   /* glyph may not be a string, when ME is killed by Hara Kiri in
52      between. */
53   if (!scm_is_string (glyph))
54     return SCM_EOL;
55
56   String glyph_string = ly_scm2string (glyph);
57
58   /* compose span_bar_mol */
59   Array<Interval> extents;
60   Grob *model_bar = 0;
61   for (int i = elements.size (); i--;)
62     {
63       Grob *bar = elements[i];
64       Interval ext = bar->extent (refp, Y_AXIS);
65       if (ext.is_empty ())
66         continue;
67
68       extents.push (ext);
69       model_bar = bar;
70     }
71
72   if (!model_bar)
73     model_bar = me;
74
75   extents.sort (&Interval::left_comparison);
76
77   Stencil span_bar;
78   for (int i = 1; i < extents.size (); i++)
79     {
80       Interval prev_extent = extents[i - 1];
81       Interval ext = extents[i];
82       if (!prev_extent.is_empty ())
83         {
84           Interval l (prev_extent [UP],
85                       ext[DOWN]);
86
87           if (l.is_empty ())
88             {
89               /* There is overlap between the bar lines.  Do nothing. */
90             }
91           else
92             {
93               Stencil interbar = Bar_line::compound_barline (model_bar,
94                                                              glyph_string,
95                                                              l.length (),
96                                                              false);
97               interbar.translate_axis (l.center (), Y_AXIS);
98               span_bar.add_stencil (interbar);
99             }
100         }
101       prev_extent = ext;
102     }
103
104   span_bar.translate_axis (- me->relative_coordinate (refp, Y_AXIS),
105                            Y_AXIS);
106
107   return span_bar.smobbed_copy ();
108 }
109
110 MAKE_SCHEME_CALLBACK (Span_bar, width_callback, 2);
111 SCM
112 Span_bar::width_callback (SCM element_smob, SCM scm_axis)
113 {
114   Grob *se = unsmob_grob (element_smob);
115   (void) scm_axis;
116
117   assert ((Axis) scm_to_int (scm_axis) == X_AXIS);
118   String gl = ly_scm2string (se->get_property ("glyph-name"));
119
120   /*
121     urg.
122   */
123   Stencil m = Bar_line::compound_barline (se, gl, 40 PT, false);
124
125   return ly_interval2scm (m.extent (X_AXIS));
126 }
127
128 MAKE_SCHEME_CALLBACK (Span_bar, before_line_breaking, 1);
129 SCM
130 Span_bar::before_line_breaking (SCM smob)
131 {
132   Grob *me = unsmob_grob (smob);
133   extract_grob_set (me, "elements", elements);
134   if (elements.is_empty ())
135     me->suicide ();
136
137   return SCM_UNSPECIFIED;
138 }
139
140 MAKE_SCHEME_CALLBACK (Span_bar, center_on_spanned_callback, 2);
141
142 SCM
143 Span_bar::center_on_spanned_callback (SCM element_smob, SCM axis)
144 {
145   Grob *me = unsmob_grob (element_smob);
146   (void) axis;
147   assert (scm_to_int (axis) == Y_AXIS);
148   Interval i (get_spanned_interval (me));
149
150   /* Bar_line::print delivers a barline of y-extent (-h/2, h/2), so
151      we have to translate ourselves to be in the center of the
152      interval that we span. */
153   if (i.is_empty ())
154     {
155       me->suicide ();
156       return scm_from_double (0.0);
157     }
158
159   return scm_from_double (i.center ());
160 }
161
162
163
164 MAKE_SCHEME_CALLBACK(Span_bar, calc_glyph_name, 1);
165 SCM
166 Span_bar::calc_glyph_name (SCM smob)
167 {
168   Grob *me = unsmob_grob (smob);
169   extract_grob_set (me, "elements", elements);
170   SCM gl = SCM_EOL;
171   for (int i = elements.size ();
172        i-- && !scm_is_string (gl);)
173     gl = elements[i]->get_property ("glyph");
174
175   if (!scm_is_string (gl))
176     {
177       me->suicide ();
178       return SCM_UNSPECIFIED;
179     }
180
181   String type = ly_scm2string (gl);
182   if (type == "|:")
183     type = ".|";
184   else if (type == ":|")
185     type = "|.";
186   else if (type == ":|:")
187     type = ".|.";
188
189   return scm_makfrom0str (type.to_str0 ());
190 }
191
192 Interval
193 Span_bar::get_spanned_interval (Grob *me)
194 {
195   return ly_scm2interval (Axis_group_interface::group_extent_callback
196                           (me->self_scm (), scm_from_int (Y_AXIS)));
197 }
198
199 MAKE_SCHEME_CALLBACK (Span_bar, calc_bar_size, 1);
200 SCM
201 Span_bar::calc_bar_size (SCM smob)
202 {
203   Grob *me = unsmob_grob (smob);
204   Interval iv (get_spanned_interval (me));
205   if (iv.is_empty ())
206     {
207       /* This happens if the bars are hara-kiried from under us. */
208       me->suicide ();
209       return scm_from_double (-1);
210     }
211   return scm_from_double (iv.length ());
212 }
213
214 ADD_INTERFACE (Span_bar, "span-bar-interface",
215                "A bar line that spanned between other barlines. This interface is "
216                " used for  bar lines that connect different staves.",
217
218                /* properties */
219                "glyph-name "
220                "elements");
221