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