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