]> git.donarmstrong.com Git - lilypond.git/blob - lily/span-bar.cc
[mf] Improve and simplify Kievan outlines.
[lilypond.git] / lily / span-bar.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1997--2012 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 void
34 Span_bar::add_bar (Grob *me, Grob *b)
35 {
36   Pointer_group_interface::add_grob (me, ly_symbol2scm ("elements"), b);
37 }
38
39 MAKE_SCHEME_CALLBACK (Span_bar, print, 1);
40
41 /* Limitations/Bugs:
42
43 (1) Elements from 'me->get_object ("elements")' must be
44 ordered according to their y coordinates relative to their common
45 axis group parent.  Otherwise, the computation goes mad.
46
47 (2) This method depends on bar_engraver not being removed from
48 staff context.  If bar_engraver is removed, the size of the staff
49 lines is evaluated as 0, which results in a solid span bar line
50 with faulty y coordinate. */
51
52 /* This routine was originally by Juergen Reuter, but it was a on the
53    bulky side. Rewritten by Han-Wen. */
54 SCM
55 Span_bar::print (SCM smobbed_me)
56 {
57   Grob *me = unsmob_grob (smobbed_me);
58   extract_grob_set (me, "elements", elements);
59   Grob *refp = common_refpoint_of_array (elements, me, Y_AXIS);
60
61   SCM glyph = me->get_property ("glyph-name");
62
63   /* glyph may not be a string, when ME is killed by Hara Kiri in
64      between. */
65   if (!scm_is_string (glyph))
66     return SCM_EOL;
67
68   string glyph_string = ly_scm2string (glyph);
69
70   /* compose span_bar_mol */
71   vector<Interval> extents;
72   vector<bool> make_span_bar;
73   Grob *model_bar = 0;
74   for (vsize i = elements.size (); i--;)
75     {
76       Grob *bar = elements[i];
77       Interval ext = Bar_line::bar_y_extent (bar, refp);
78       if (Grob *staff = Staff_symbol_referencer::get_staff_symbol (bar))
79         ext.unite (staff->extent (refp, Y_AXIS));
80       if (ext.is_empty ())
81         continue;
82
83       extents.push_back (ext);
84       make_span_bar.push_back (to_boolean (bar->get_property ("allow-span-bar")));
85       model_bar = bar;
86     }
87
88   if (!model_bar)
89     model_bar = me;
90
91   Stencil span_bar;
92   for (vsize i = 1; i < extents.size (); i++)
93     {
94       Interval prev_extent = extents[i - 1];
95       Interval ext = extents[i];
96       if (!prev_extent.is_empty ())
97         {
98           Interval l (prev_extent [UP],
99                       ext[DOWN]);
100
101           if (l.is_empty () || !make_span_bar[i])
102             {
103               /* There is overlap between the bar lines.  Do nothing. */
104             }
105           else
106             {
107               Stencil interbar = Bar_line::compound_barline (model_bar,
108                                                              glyph_string,
109                                                              l,
110                                                              false);
111               span_bar.add_stencil (interbar);
112             }
113         }
114       prev_extent = ext;
115     }
116
117   span_bar.translate_axis (- me->relative_coordinate (refp, Y_AXIS),
118                            Y_AXIS);
119
120   return span_bar.smobbed_copy ();
121 }
122
123 MAKE_SCHEME_CALLBACK (Span_bar, width, 1);
124 SCM
125 Span_bar::width (SCM smob)
126 {
127   Grob *me = unsmob_grob (smob);
128   SCM gn = me->get_property ("glyph-name");
129   if (!me->is_live ())
130     return ly_interval2scm (Interval ());
131
132   string gl = ly_scm2string (gn);
133
134   /*
135     urg.
136   */
137   Stencil m
138     = Bar_line::compound_barline (me, gl, Interval (-20 PT, 20 PT), false);
139
140   return ly_interval2scm (m.extent (X_AXIS));
141 }
142
143 MAKE_SCHEME_CALLBACK (Span_bar, before_line_breaking, 1);
144 SCM
145 Span_bar::before_line_breaking (SCM smob)
146 {
147   Grob *me = unsmob_grob (smob);
148   extract_grob_set (me, "elements", elements);
149   if (elements.empty ())
150     me->suicide ();
151
152   return SCM_UNSPECIFIED;
153 }
154
155 MAKE_SCHEME_CALLBACK (Span_bar, center_on_spanned_callback, 1);
156
157 SCM
158 Span_bar::center_on_spanned_callback (SCM smob)
159 {
160   Grob *me = unsmob_grob (smob);
161   Interval i (get_spanned_interval (me));
162
163   /* Bar_line::print delivers a barline of y-extent (-h/2, h/2), so
164      we have to translate ourselves to be in the center of the
165      interval that we span. */
166   if (i.is_empty ())
167     {
168       me->suicide ();
169       return scm_from_double (0.0);
170     }
171
172   return scm_from_double (i.center ());
173 }
174
175 MAKE_SCHEME_CALLBACK (Span_bar, calc_glyph_name, 1);
176 SCM
177 Span_bar::calc_glyph_name (SCM smob)
178 {
179   Grob *me = unsmob_grob (smob);
180   extract_grob_set (me, "elements", elements);
181   SCM gl = SCM_EOL;
182   for (vsize i = elements.size ();
183        i-- && !scm_is_string (gl);)
184     gl = elements[i]->get_property ("glyph-name");
185
186   if (!scm_is_string (gl))
187     {
188       me->suicide ();
189       return SCM_UNSPECIFIED;
190     }
191
192   string type = ly_scm2string (gl);
193   if (type == "|:" || type == "||:")
194     type = ".|";
195   else if (type == ":|")
196     type = "|.";
197   else if (type == ":|:")
198     type = ".|.";
199   else if (type == ":|.|:")
200     type = "|.|";
201   else if (type == ":|.:")
202     type = "|.";
203   else if (type == "S" || type == "S|" || type == "|S")
204     type = "||";
205   else if (type == "S|:" || type == ".S|:")
206     type = ".|";
207   else if (type == ":|S" || type == ":|S.")
208     type = "|.";
209   else if (type == ":|S|:" || type == ":|S.|:")
210     type = "|._.|";
211   else if (type == "'")
212     type = "";
213
214   return ly_string2scm (type);
215 }
216
217 void
218 Span_bar::notify_grobs_of_my_existence (Grob *me)
219 {
220   extract_grob_set (me, "elements", elts);
221   vector<Grob *> sortable (elts.begin (), elts.end ());
222   vector_sort (sortable, Grob::vertical_less);
223   for (vsize i = 0; i < sortable.size (); i++)
224     sortable[i]->set_property ("has-span-bar",
225                                scm_cons (i != sortable.size () - 1 ? me->self_scm () : scm_from_bool (false),
226                                          i != 0 ? me->self_scm () : scm_from_bool (false)));
227 }
228
229 Interval
230 Span_bar::get_spanned_interval (Grob *me)
231 {
232   return ly_scm2interval (Axis_group_interface::generic_group_extent (me, Y_AXIS));
233 }
234
235 ADD_INTERFACE (Span_bar,
236                "A bar line that is spanned between other barlines.  This"
237                " interface is used for bar lines that connect different"
238                " staves.",
239
240                /* properties */
241                "glyph-name "
242                "elements "
243                "pure-Y-common "
244                "pure-relevant-grobs "
245                "pure-relevant-items "
246                "pure-relevant-spanners "
247               );
248