]> git.donarmstrong.com Git - lilypond.git/blob - lily/line-spanner.cc
f42628820e6e64b6a0ba019d93f0bf8666125472
[lilypond.git] / lily / line-spanner.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2000--2012 Jan Nieuwenhuizen <janneke@gnu.org>
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 "align-interface.hh"
21 #include "axis-group-interface.hh"
22 #include "font-interface.hh"
23 #include "grob-interface.hh"
24 #include "international.hh"
25 #include "item.hh"
26 #include "lily-proto.hh"
27 #include "line-interface.hh"
28 #include "note-column.hh"
29 #include "output-def.hh"
30 #include "paper-column.hh"
31 #include "pointer-group-interface.hh"
32 #include "spanner.hh"
33 #include "staff-symbol-referencer.hh"
34 #include "system.hh"
35 #include "text-interface.hh"
36 #include "warn.hh"
37
38 class Line_spanner
39 {
40 public:
41   DECLARE_SCHEME_CALLBACK (print, (SCM));
42   DECLARE_SCHEME_CALLBACK (calc_cross_staff, (SCM));
43   DECLARE_SCHEME_CALLBACK (calc_left_bound_info, (SCM));
44   DECLARE_SCHEME_CALLBACK (calc_left_bound_info_and_text, (SCM));
45   DECLARE_SCHEME_CALLBACK (calc_right_bound_info, (SCM));
46   DECLARE_SCHEME_CALLBACK (calc_bound_info, (SCM, Direction));
47   DECLARE_GROB_INTERFACE ();
48 };
49
50 Spanner *parent_spanner (Grob *g)
51 {
52   if (Spanner::has_interface (g))
53     return dynamic_cast<Spanner *> (g);
54   return parent_spanner (g->get_parent (Y_AXIS));
55 }
56
57 SCM
58 Line_spanner::calc_bound_info (SCM smob, Direction dir)
59 {
60   Spanner *me = unsmob_spanner (smob);
61
62   Grob *commonx = me->get_bound (LEFT)->common_refpoint (me->get_bound (RIGHT), X_AXIS);
63   commonx = me->common_refpoint (commonx, X_AXIS);
64
65   SCM bound_details = me->get_property ("bound-details");
66
67   SCM details = SCM_BOOL_F;
68   if (details == SCM_BOOL_F)
69     details = ly_assoc_get ((dir == LEFT)
70                             ? ly_symbol2scm ("left")
71                             : ly_symbol2scm ("right"), bound_details, SCM_BOOL_F);
72
73   if (me->get_bound (dir)->break_status_dir ())
74     {
75       SCM extra = ly_assoc_get ((dir == LEFT)
76                                 ? ly_symbol2scm ("left-broken")
77                                 : ly_symbol2scm ("right-broken"), bound_details, SCM_EOL);
78
79       details = scm_append (scm_list_2 (extra, details));
80     }
81
82   if (details == SCM_BOOL_F)
83     details = ly_assoc_get (ly_symbol2scm ("default"), bound_details, SCM_EOL);
84
85   SCM text = ly_assoc_get (ly_symbol2scm ("text"), details, SCM_BOOL_F);
86   if (Text_interface::is_markup (text))
87     {
88       Output_def *layout = me->layout ();
89       SCM properties = Font_interface::text_font_alist_chain (me);
90       details = scm_acons (ly_symbol2scm ("stencil"),
91                            Text_interface::interpret_markup (layout->self_scm (),
92                                                              properties, text),
93                            details);
94     }
95
96   if (!scm_is_number (ly_assoc_get (ly_symbol2scm ("X"), details, SCM_BOOL_F)))
97     {
98       Direction attach = (Direction)
99                          robust_scm2int (ly_assoc_get (ly_symbol2scm ("attach-dir"),
100                                                        details, SCM_BOOL_F),
101                                          CENTER);
102
103       Item *bound_item = me->get_bound (dir);
104       Grob *bound_grob = bound_item;
105       if (to_boolean (ly_assoc_get (ly_symbol2scm ("end-on-note"), details, SCM_BOOL_F))
106           && bound_item->break_status_dir ())
107         {
108           extract_grob_set (me, "note-columns", columns);
109           if (columns.size ())
110             bound_grob = (dir == LEFT)
111                          ? columns[0] : columns.back ();
112         }
113
114       Real x_coord = (Paper_column::has_interface (bound_grob)
115                       ? Axis_group_interface::generic_bound_extent (bound_grob, commonx, X_AXIS)
116                       : robust_relative_extent (bound_grob, commonx, X_AXIS)).linear_combination (attach);
117
118       Grob *acc = Note_column::accidentals (bound_grob->get_parent (X_AXIS));
119       if (acc && to_boolean (ly_assoc_get (ly_symbol2scm ("end-on-accidental"), details, SCM_BOOL_F)))
120         x_coord = robust_relative_extent (acc, commonx, X_AXIS).linear_combination (attach);
121
122       details = scm_acons (ly_symbol2scm ("X"),
123                            scm_from_double (x_coord),
124                            details);
125     }
126
127   if (!scm_is_number (ly_assoc_get (ly_symbol2scm ("Y"), details, SCM_BOOL_F)))
128     {
129       Real y = 0.0;
130
131       Real extra_dy = robust_scm2double (me->get_property ("extra-dy"),
132                                          0.0);
133
134       Grob *common_y = me->common_refpoint (me->get_bound (dir), Y_AXIS);
135       if (me->get_bound (dir)->break_status_dir ())
136         {
137           if (to_boolean (me->get_property ("simple-Y")))
138             {
139               Spanner *orig = dynamic_cast<Spanner *>(me->original ());
140               Spanner *extreme = dir == LEFT ? orig->broken_intos_.front () : orig->broken_intos_.back ();
141               Grob *e_bound = extreme->get_bound (dir);
142               Grob *e_common_y = extreme->common_refpoint (e_bound, Y_AXIS);
143               y = e_bound->extent (e_common_y, Y_AXIS).center ();
144             }
145           else
146             {
147               Spanner *next_sp = me->broken_neighbor (dir);
148               Item *next_bound = next_sp->get_bound (dir);
149
150               if (next_bound->break_status_dir ())
151                 {
152                   programming_error ("no note heads for the line spanner on neighbor line?"
153                                      " Confused.");
154                   me->suicide ();
155                   return SCM_EOL;
156                 }
157
158               Spanner *next_bound_parent = parent_spanner (next_bound);
159               Interval next_ext = next_bound->extent (next_bound_parent, Y_AXIS);
160
161               /*
162                 We want to know what would be the y-position of the next
163                 bound (relative to my y-parent) if it belonged to the
164                 same system as this bound.  We rely on the fact that the
165                 y-parent of the next bound is a spanner (probably the
166                 VerticalAxisGroup of a staff) that extends over the break.
167               */
168               Spanner *next_bound_parent_on_this_line
169                 = next_bound_parent->broken_neighbor (other_dir (dir));
170
171               if (next_bound_parent_on_this_line)
172                 {
173                   Grob *common = me->common_refpoint (next_bound_parent_on_this_line, Y_AXIS);
174                   Real bound_offset = next_bound_parent_on_this_line->relative_coordinate (common, Y_AXIS);
175                   y = next_ext.center () + bound_offset - me->relative_coordinate (common, Y_AXIS);
176                 }
177               else
178                 {
179                   /*
180                     We fall back to assuming that the distance between
181                     staves doesn't change over line breaks.
182                   */
183                   programming_error ("next-bound's parent doesn't extend to this line");
184                   Grob *next_system = next_bound->get_system ();
185                   Grob *this_system = me->get_system ();
186                   y = next_ext.center () + next_bound_parent->relative_coordinate (next_system, Y_AXIS)
187                       - me->relative_coordinate (this_system, Y_AXIS);
188                 }
189             }
190         }
191       else
192         {
193           y = me->get_bound (dir)->extent (common_y, Y_AXIS).center ();
194           details = scm_acons (ly_symbol2scm ("common-Y"), common_y->self_scm (), details);
195         }
196
197       y += dir * extra_dy / 2;
198       details = scm_acons (ly_symbol2scm ("Y"), scm_from_double (y), details);
199     }
200
201   return details;
202 }
203
204 MAKE_SCHEME_CALLBACK (Line_spanner, calc_cross_staff, 1);
205 SCM
206 Line_spanner::calc_cross_staff (SCM smob)
207 {
208   Spanner *me = unsmob_spanner (smob);
209   if (!me)
210     return SCM_BOOL_F;
211
212   if (to_boolean (me->get_bound (LEFT)->get_property ("non-musical"))
213       || to_boolean (me->get_bound (RIGHT)->get_property ("non-musical")))
214     return SCM_BOOL_F;
215
216   return scm_from_bool (Staff_symbol_referencer::get_staff_symbol (me->get_bound (LEFT))
217                         != Staff_symbol_referencer::get_staff_symbol (me->get_bound (RIGHT)));
218 }
219
220 MAKE_SCHEME_CALLBACK (Line_spanner, calc_right_bound_info, 1);
221 SCM
222 Line_spanner::calc_right_bound_info (SCM smob)
223 {
224   return Line_spanner::calc_bound_info (smob, RIGHT);
225 }
226
227 MAKE_SCHEME_CALLBACK (Line_spanner, calc_left_bound_info, 1);
228 SCM
229 Line_spanner::calc_left_bound_info (SCM smob)
230 {
231   return Line_spanner::calc_bound_info (smob, LEFT);
232 }
233
234 MAKE_SCHEME_CALLBACK (Line_spanner, calc_left_bound_info_and_text, 1);
235 SCM
236 Line_spanner::calc_left_bound_info_and_text (SCM smob)
237 {
238   SCM alist = Line_spanner::calc_bound_info (smob, LEFT);
239   Spanner *me = unsmob_spanner (smob);
240
241   SCM text = me->get_property ("text");
242   if (Text_interface::is_markup (text)
243       && me->get_bound (LEFT)->break_status_dir () == CENTER
244       && ly_assoc_get (ly_symbol2scm ("stencil"), alist, SCM_BOOL_F) == SCM_BOOL_F)
245     {
246       Output_def *layout = me->layout ();
247       SCM properties = Font_interface::text_font_alist_chain (me);
248       alist = scm_acons (ly_symbol2scm ("stencil"),
249                          Text_interface::interpret_markup (layout->self_scm (),
250                                                            properties, text),
251                          alist);
252     }
253
254   return alist;
255 }
256
257 MAKE_SCHEME_CALLBACK (Line_spanner, print, 1);
258 SCM
259 Line_spanner::print (SCM smob)
260 {
261   Spanner *me = dynamic_cast<Spanner *> (unsmob_grob (smob));
262
263   // Triggers simple-Y calculations
264   bool simple_y = to_boolean (me->get_property ("simple-Y")) && !to_boolean (me->get_property ("cross-staff"));
265
266   Drul_array<SCM> bounds (me->get_property ("left-bound-info"),
267                           me->get_property ("right-bound-info"));
268
269   Grob *commonx = me->get_bound (LEFT)->common_refpoint (me->get_bound (RIGHT), X_AXIS);
270   commonx = me->common_refpoint (commonx, X_AXIS);
271
272   Drul_array<Offset> span_points;
273
274   for (LEFT_and_RIGHT (d))
275     {
276       Offset z (robust_scm2double (ly_assoc_get (ly_symbol2scm ("X"),
277                                                  bounds[d], SCM_BOOL_F), 0.0),
278                 robust_scm2double (ly_assoc_get (ly_symbol2scm ("Y"),
279                                                  bounds[d], SCM_BOOL_F), 0.0));
280
281       span_points[d] = z;
282     }
283
284   Drul_array<Real> gaps (0, 0);
285   Drul_array<bool> arrows (0, 0);
286   Drul_array<Stencil *> stencils (0, 0);
287   Drul_array<Grob *> common_y (0, 0);
288
289   // For scaling of 'padding and 'stencil-offset
290   Real magstep
291     = pow (2, robust_scm2double (me->get_property ("font-size"), 0.0) / 6);
292
293   for (LEFT_and_RIGHT (d))
294     {
295       gaps[d] = robust_scm2double (ly_assoc_get (ly_symbol2scm ("padding"),
296                                                  bounds[d], SCM_BOOL_F), 0.0);
297       arrows[d] = to_boolean (ly_assoc_get (ly_symbol2scm ("arrow"),
298                                             bounds[d], SCM_BOOL_F));
299       stencils[d] = unsmob_stencil (ly_assoc_get (ly_symbol2scm ("stencil"),
300                                                   bounds[d], SCM_BOOL_F));
301       common_y[d] = unsmob_grob (ly_assoc_get (ly_symbol2scm ("common-Y"),
302                                                bounds[d], SCM_BOOL_F));
303       if (!common_y[d])
304         common_y[d] = me;
305     }
306
307   Grob *my_common_y = common_y[LEFT]->common_refpoint (common_y[RIGHT], Y_AXIS);
308
309   if (!simple_y)
310     {
311       for (LEFT_and_RIGHT (d))
312         span_points[d][Y_AXIS] += common_y[d]->relative_coordinate (my_common_y, Y_AXIS);
313     }
314
315   Interval normalized_endpoints = robust_scm2interval (me->get_property ("normalized-endpoints"), Interval (0, 1));
316   Real y_length = span_points[RIGHT][Y_AXIS] - span_points[LEFT][Y_AXIS];
317
318   span_points[LEFT][Y_AXIS] += normalized_endpoints[LEFT] * y_length;
319   span_points[RIGHT][Y_AXIS] -= (1 - normalized_endpoints[RIGHT]) * y_length;
320
321   Offset dz = (span_points[RIGHT] - span_points[LEFT]);
322   Offset dz_dir = dz.direction ();
323   if (gaps[LEFT] + gaps[RIGHT] > dz.length ())
324     {
325       return SCM_EOL;
326     }
327
328   Stencil line;
329   for (LEFT_and_RIGHT (d))
330     {
331       span_points[d] += -d * gaps[d] * magstep * dz.direction ();
332
333       if (stencils[d])
334         {
335           Stencil s = stencils[d]->translated (span_points[d]);
336           SCM align = ly_assoc_get (ly_symbol2scm ("stencil-align-dir-y"),
337                                     bounds[d], SCM_BOOL_F);
338           SCM off = ly_assoc_get (ly_symbol2scm ("stencil-offset"),
339                                   bounds[d], SCM_BOOL_F);
340
341           if (scm_is_number (align))
342             s.align_to (Y_AXIS, scm_to_double (align));
343
344           if (is_number_pair (off))
345             s.translate (ly_scm2offset (off) * magstep);
346
347           line.add_stencil (s);
348         }
349     }
350
351   for (LEFT_and_RIGHT (d))
352     {
353       if (stencils[d])
354         span_points[d] += dz_dir *
355                           (stencils[d]->extent (X_AXIS)[-d] / dz_dir[X_AXIS]);
356     }
357
358   Offset adjust = dz.direction () * Staff_symbol_referencer::staff_space (me);
359
360   Offset line_left = span_points[LEFT] + (arrows[LEFT] ? adjust * 1.4 : Offset (0, 0));
361   Offset line_right = span_points[RIGHT] - (arrows[RIGHT] ? adjust * 0.55 : Offset (0, 0));
362   if (line_right[X_AXIS] > line_left[X_AXIS])
363     {
364       line.add_stencil (Line_interface::line (me, line_left, line_right));
365
366       line.add_stencil (Line_interface::arrows (me,
367                                                 span_points[LEFT],
368                                                 span_points[RIGHT],
369                                                 arrows[LEFT],
370                                                 arrows[RIGHT]));
371     }
372   else
373     me->warning (_ ("Line spanner's left point is to the right of its right point."));
374
375   line.translate (Offset (-me->relative_coordinate (commonx, X_AXIS),
376                           simple_y ? 0.0 : -me->relative_coordinate (my_common_y, Y_AXIS)));
377
378   return line.smobbed_copy ();
379 }
380
381 ADD_INTERFACE (Line_spanner,
382                "Generic line drawn between two objects, e.g., for use with"
383                " glissandi.",
384
385                /* properties */
386                "bound-details "
387                "extra-dy "
388                "gap "
389                "left-bound-info "
390                "note-columns "
391                "right-bound-info "
392                "simple-Y "
393                "thickness "
394                "to-barline "
395               );