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