]> git.donarmstrong.com Git - lilypond.git/blob - lily/span-bar.cc
Imported Upstream version 2.14.2
[lilypond.git] / lily / span-bar.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1997--2011 Han-Wen Nienhuys <hanwen@xs4all.nl>
5
6   LilyPond is free software: you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation, either version 3 of the License, or
9   (at your option) any later version.
10
11   LilyPond is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15
16   You should have received a copy of the GNU General Public License
17   along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "span-bar.hh"
21
22 #include "font-interface.hh"
23 #include "dimensions.hh"
24 #include "output-def.hh"
25 #include "stencil.hh"
26 #include "warn.hh"
27 #include "axis-group-interface.hh"
28 #include "bar-line.hh"
29 #include "grob.hh"
30 #include "pointer-group-interface.hh"
31 #include "staff-symbol-referencer.hh"
32
33
34 void
35 Span_bar::add_bar (Grob *me, Grob *b)
36 {
37   Pointer_group_interface::add_grob (me, ly_symbol2scm ("elements"), b);
38 }
39
40 MAKE_SCHEME_CALLBACK (Span_bar, print, 1);
41
42 /* Limitations/Bugs:
43
44 (1) Elements from 'me->get_object ("elements")' must be
45 ordered according to their y coordinates relative to their common
46 axis group parent.  Otherwise, the computation goes mad.
47
48 (2) This method depends on bar_engraver not being removed from
49 staff context.  If bar_engraver is removed, the size of the staff
50 lines is evaluated as 0, which results in a solid span bar line
51 with faulty y coordinate. */
52
53 /* This routine was originally by Juergen Reuter, but it was a on the
54    bulky side. Rewritten by Han-Wen. */
55 SCM
56 Span_bar::print (SCM smobbed_me)
57 {
58   Grob *me = unsmob_grob (smobbed_me);
59   extract_grob_set (me, "elements", elements);
60   Grob *refp = common_refpoint_of_array (elements, me, Y_AXIS);
61
62   SCM glyph = me->get_property ("glyph-name");
63
64   /* glyph may not be a string, when ME is killed by Hara Kiri in
65      between. */
66   if (!scm_is_string (glyph))
67     return SCM_EOL;
68
69   string glyph_string = ly_scm2string (glyph);
70
71   /* compose span_bar_mol */
72   vector<Interval> extents;
73   vector<bool> make_span_bar;
74   Grob *model_bar = 0;
75   for (vsize i = elements.size (); i--;)
76     {
77       Grob *bar = elements[i];
78       Interval ext = Bar_line::bar_y_extent (bar, refp);
79       if (Grob *staff = Staff_symbol_referencer::get_staff_symbol (bar))
80         ext.unite (staff->extent (refp, Y_AXIS));
81       if (ext.is_empty ())
82         continue;
83
84       extents.push_back (ext);
85       make_span_bar.push_back (to_boolean (bar->get_property ("allow-span-bar")));
86       model_bar = bar;
87     }
88
89   if (!model_bar)
90     model_bar = me;
91
92   vector_sort (extents, Interval::left_less);
93
94   Stencil span_bar;
95   for (vsize i = 1; i < extents.size (); i++)
96     {
97       Interval prev_extent = extents[i - 1];
98       Interval ext = extents[i];
99       if (!prev_extent.is_empty ())
100         {
101           Interval l (prev_extent [UP],
102                       ext[DOWN]);
103
104           if (l.is_empty () || !make_span_bar[i])
105             {
106               /* There is overlap between the bar lines.  Do nothing. */
107             }
108           else
109             {
110               Stencil interbar = Bar_line::compound_barline (model_bar,
111                                                              glyph_string,
112                                                              l,
113                                                              false);
114               span_bar.add_stencil (interbar);
115             }
116         }
117       prev_extent = ext;
118     }
119
120   span_bar.translate_axis (- me->relative_coordinate (refp, Y_AXIS),
121                            Y_AXIS);
122
123   return span_bar.smobbed_copy ();
124 }
125
126 MAKE_SCHEME_CALLBACK (Span_bar, width, 1);
127 SCM
128 Span_bar::width (SCM smob)
129 {
130   Grob *me = unsmob_grob (smob);
131   SCM gn = me->get_property ("glyph-name");
132   if (!me->is_live ())
133     return ly_interval2scm (Interval ());
134
135   string gl = ly_scm2string (gn);
136
137   /*
138     urg.
139   */
140   Stencil m =
141     Bar_line::compound_barline (me, gl, Interval (-20 PT, 20 PT), false);
142
143   return ly_interval2scm (m.extent (X_AXIS));
144 }
145
146 MAKE_SCHEME_CALLBACK (Span_bar, before_line_breaking, 1);
147 SCM
148 Span_bar::before_line_breaking (SCM smob)
149 {
150   Grob *me = unsmob_grob (smob);
151   extract_grob_set (me, "elements", elements);
152   if (elements.empty ())
153     me->suicide ();
154
155   return SCM_UNSPECIFIED;
156 }
157
158 MAKE_SCHEME_CALLBACK (Span_bar, center_on_spanned_callback, 1);
159
160 SCM
161 Span_bar::center_on_spanned_callback (SCM smob)
162 {
163   Grob *me = unsmob_grob (smob);
164   Interval i (get_spanned_interval (me));
165
166   /* Bar_line::print delivers a barline of y-extent (-h/2, h/2), so
167      we have to translate ourselves to be in the center of the
168      interval that we span. */
169   if (i.is_empty ())
170     {
171       me->suicide ();
172       return scm_from_double (0.0);
173     }
174
175   return scm_from_double (i.center ());
176 }
177
178
179
180 MAKE_SCHEME_CALLBACK(Span_bar, calc_glyph_name, 1);
181 SCM
182 Span_bar::calc_glyph_name (SCM smob)
183 {
184   Grob *me = unsmob_grob (smob);
185   extract_grob_set (me, "elements", elements);
186   SCM gl = SCM_EOL;
187   for (vsize i = elements.size ();
188        i-- && !scm_is_string (gl);)
189     gl = elements[i]->get_property ("glyph-name");
190
191   if (!scm_is_string (gl))
192     {
193       me->suicide ();
194       return SCM_UNSPECIFIED;
195     }
196
197   string type = ly_scm2string (gl);
198   if (type == "|:" || type == "||:")
199     type = ".|";
200   else if (type == ":|")
201     type = "|.";
202   else if (type == ":|:")
203     type = ".|.";
204   else if (type == ":|.|:")
205     type = "|.|";
206   else if (type == ":|.:")
207     type = "|.";
208   else if (type == "S" || type == "S|" || type == "|S")
209     type = "||";
210   else if (type == "S|:" || type == ".S|:")
211     type = ".|";
212   else if (type == ":|S" || type == ":|S.")
213     type = "|.";
214     else if (type == ":|S|:" || type == ":|S.|:")
215     type = "|._.|";
216   else if (type == "'")
217     type = "";
218
219   return ly_string2scm (type);
220 }
221
222 Interval
223 Span_bar::get_spanned_interval (Grob *me)
224 {
225   return ly_scm2interval (Axis_group_interface::generic_group_extent (me, Y_AXIS));
226 }
227
228 ADD_INTERFACE (Span_bar,
229                "A bar line that is spanned between other barlines.  This"
230                " interface is used for bar lines that connect different"
231                " staves.",
232
233                /* properties */
234                "glyph-name "
235                "elements "
236                "pure-Y-common "
237                "pure-relevant-grobs "
238                "pure-relevant-items "
239                "pure-relevant-spanners "
240                );
241