]> git.donarmstrong.com Git - lilypond.git/blob - lily/span-bar.cc
5c1c76e85590febc2e757f56c731e54ff27c874f
[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--2006 Han-Wen Nienhuys <hanwen@xs4all.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   vector<Interval> extents;
60   Grob *model_bar = 0;
61   for (vsize 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_back (ext);
69       model_bar = bar;
70     }
71
72   if (!model_bar)
73     model_bar = me;
74
75   vector_sort (extents, Interval::left_less);
76
77   Stencil span_bar;
78   for (vsize 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, 1);
111 SCM
112 Span_bar::width (SCM smob)
113 {
114   Grob *me = unsmob_grob (smob);
115   SCM gn = me->get_property ("glyph-name");
116   if (!me->is_live ())
117     return ly_interval2scm (Interval ());
118   
119   string gl = ly_scm2string (gn);
120
121   /*
122     urg.
123   */
124   Stencil m = Bar_line::compound_barline (me, 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 *me = unsmob_grob (smob);
134   extract_grob_set (me, "elements", elements);
135   if (elements.empty ())
136     me->suicide ();
137
138   return SCM_UNSPECIFIED;
139 }
140
141 MAKE_SCHEME_CALLBACK (Span_bar, center_on_spanned_callback, 1);
142
143 SCM
144 Span_bar::center_on_spanned_callback (SCM smob)
145 {
146   Grob *me = unsmob_grob (smob);
147   Interval i (get_spanned_interval (me));
148
149   /* Bar_line::print delivers a barline of y-extent (-h/2, h/2), so
150      we have to translate ourselves to be in the center of the
151      interval that we span. */
152   if (i.is_empty ())
153     {
154       me->suicide ();
155       return scm_from_double (0.0);
156     }
157
158   return scm_from_double (i.center ());
159 }
160
161
162
163 MAKE_SCHEME_CALLBACK(Span_bar, calc_glyph_name, 1);
164 SCM
165 Span_bar::calc_glyph_name (SCM smob)
166 {
167   Grob *me = unsmob_grob (smob);
168   extract_grob_set (me, "elements", elements);
169   SCM gl = SCM_EOL;
170   for (vsize i = elements.size ();
171        i-- && !scm_is_string (gl);)
172     gl = elements[i]->get_property ("glyph-name");
173
174   if (!scm_is_string (gl))
175     {
176       me->suicide ();
177       return SCM_UNSPECIFIED;
178     }
179
180   string type = ly_scm2string (gl);
181   if (type == "|:")
182     type = ".|";
183   else if (type == ":|")
184     type = "|.";
185   else if (type == ":|:")
186     type = ".|.";
187
188   return scm_makfrom0str (type.c_str ());
189 }
190
191 Interval
192 Span_bar::get_spanned_interval (Grob *me)
193 {
194   return ly_scm2interval (Axis_group_interface::generic_group_extent (me, Y_AXIS));
195 }
196
197 MAKE_SCHEME_CALLBACK (Span_bar, calc_bar_size, 1);
198 SCM
199 Span_bar::calc_bar_size (SCM smob)
200 {
201   Grob *me = unsmob_grob (smob);
202   Interval iv (get_spanned_interval (me));
203   if (iv.is_empty ())
204     {
205       /* This happens if the bars are hara-kiried from under us. */
206       me->suicide ();
207       return scm_from_double (-1);
208     }
209   return scm_from_double (iv.length ());
210 }
211
212 ADD_INTERFACE (Span_bar, "span-bar-interface",
213                "A bar line that spanned between other barlines. This interface is "
214                " used for  bar lines that connect different staves.",
215
216                /* properties */
217                "glyph-name "
218                "elements");
219