]> git.donarmstrong.com Git - lilypond.git/blob - lily/span-bar.cc
* lily/beam.cc: use length-fraction too.
[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 smob, SCM scm_axis)
113 {
114   Grob *me = unsmob_grob (smob);
115   (void) scm_axis;
116
117   assert ((Axis) scm_to_int (scm_axis) == X_AXIS);
118   SCM gn = me->get_property ("glyph-name");
119   if (!me->is_live ())
120     return ly_interval2scm (Interval ());
121   
122   String gl = ly_scm2string (gn);
123
124   /*
125     urg.
126   */
127   Stencil m = Bar_line::compound_barline (me, 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 *me = unsmob_grob (smob);
137   extract_grob_set (me, "elements", elements);
138   if (elements.is_empty ())
139     me->suicide ();
140
141   return SCM_UNSPECIFIED;
142 }
143
144 MAKE_SCHEME_CALLBACK (Span_bar, center_on_spanned_callback, 2);
145
146 SCM
147 Span_bar::center_on_spanned_callback (SCM element_smob, SCM axis)
148 {
149   Grob *me = unsmob_grob (element_smob);
150   (void) axis;
151   assert (scm_to_int (axis) == Y_AXIS);
152   Interval i (get_spanned_interval (me));
153
154   /* Bar_line::print delivers a barline of y-extent (-h/2, h/2), so
155      we have to translate ourselves to be in the center of the
156      interval that we span. */
157   if (i.is_empty ())
158     {
159       me->suicide ();
160       return scm_from_double (0.0);
161     }
162
163   return scm_from_double (i.center ());
164 }
165
166
167
168 MAKE_SCHEME_CALLBACK(Span_bar, calc_glyph_name, 1);
169 SCM
170 Span_bar::calc_glyph_name (SCM smob)
171 {
172   Grob *me = unsmob_grob (smob);
173   extract_grob_set (me, "elements", elements);
174   SCM gl = SCM_EOL;
175   for (int i = elements.size ();
176        i-- && !scm_is_string (gl);)
177     gl = elements[i]->get_property ("glyph");
178
179   if (!scm_is_string (gl))
180     {
181       me->suicide ();
182       return SCM_UNSPECIFIED;
183     }
184
185   String type = ly_scm2string (gl);
186   if (type == "|:")
187     type = ".|";
188   else if (type == ":|")
189     type = "|.";
190   else if (type == ":|:")
191     type = ".|.";
192
193   return scm_makfrom0str (type.to_str0 ());
194 }
195
196 Interval
197 Span_bar::get_spanned_interval (Grob *me)
198 {
199   return ly_scm2interval (Axis_group_interface::group_extent_callback
200                           (me->self_scm (), scm_from_int (Y_AXIS)));
201 }
202
203 MAKE_SCHEME_CALLBACK (Span_bar, calc_bar_size, 1);
204 SCM
205 Span_bar::calc_bar_size (SCM smob)
206 {
207   Grob *me = unsmob_grob (smob);
208   Interval iv (get_spanned_interval (me));
209   if (iv.is_empty ())
210     {
211       /* This happens if the bars are hara-kiried from under us. */
212       me->suicide ();
213       return scm_from_double (-1);
214     }
215   return scm_from_double (iv.length ());
216 }
217
218 ADD_INTERFACE (Span_bar, "span-bar-interface",
219                "A bar line that spanned between other barlines. This interface is "
220                " used for  bar lines that connect different staves.",
221
222                /* properties */
223                "glyph-name "
224                "elements");
225