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