]> git.donarmstrong.com Git - lilypond.git/blob - lily/side-position-interface.cc
Merge branch 'lilypond/translation' of ssh://thsoft@git.sv.gnu.org/srv/git/lilypond...
[lilypond.git] / lily / side-position-interface.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1998--2011 Han-Wen Nienhuys <hanwen@xs4all.nl>
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 "side-position-interface.hh"
21
22 #include <cmath>                // ceil.
23 #include <algorithm>
24
25 using namespace std;
26
27 #include "axis-group-interface.hh"
28 #include "directional-element-interface.hh"
29 #include "grob.hh"
30 #include "grob-array.hh"
31 #include "main.hh"
32 #include "misc.hh"
33 #include "note-head.hh"
34 #include "pointer-group-interface.hh"
35 #include "staff-symbol-referencer.hh"
36 #include "staff-symbol.hh"
37 #include "stem.hh"
38 #include "string-convert.hh"
39 #include "system.hh"
40 #include "warn.hh"
41
42 void
43 Side_position_interface::add_support (Grob *me, Grob *e)
44 {
45   Pointer_group_interface::add_unordered_grob (me, ly_symbol2scm ("side-support-elements"), e);
46 }
47
48 /* Put the element next to the support, optionally taking in
49    account the extent of the support.
50
51    Does not take into account the extent of ME.
52 */
53 SCM
54 Side_position_interface::general_side_position (Grob *me, Axis a, bool use_extents,
55                                                 bool include_my_extent,
56                                                 bool pure, int start, int end,
57                                                 Real *current_offset)
58 {
59   Real ss = Staff_symbol_referencer::staff_space (me);
60
61   extract_grob_set (me, "side-support-elements", support);
62
63   Grob *common = common_refpoint_of_array (support, me->get_parent (a), a);
64   Grob *staff_symbol = Staff_symbol_referencer::get_staff_symbol (me);
65   bool include_staff =
66     staff_symbol
67     && a == Y_AXIS
68     && scm_is_number (me->get_property ("staff-padding"))
69     && !to_boolean (me->get_property ("quantize-position"));
70
71   Interval dim;
72   Interval staff_extents;
73   if (include_staff)
74     {
75       common = staff_symbol->common_refpoint (common, Y_AXIS);
76       staff_extents = staff_symbol->maybe_pure_extent (common, Y_AXIS, pure, start, end);
77
78       if (include_staff)
79         dim.unite (staff_extents);
80     }
81
82   Direction dir = get_grob_direction (me);
83
84   for (vsize i = 0; i < support.size (); i++)
85     {
86       Grob *e = support[i];
87
88       // In the case of a stem, we will find a note head as well
89       // ignoring the stem solves cyclic dependencies if the stem is
90       // attached to a cross-staff beam.
91       if (a == Y_AXIS
92           && Stem::has_interface (e)
93           && dir == - get_grob_direction (e))
94         continue;
95
96       if (e)
97         {
98           if (use_extents)
99             dim.unite (e->maybe_pure_extent (common, a, pure, start, end));
100           else
101             {
102               Real x = e->maybe_pure_coordinate (common, a, pure, start, end);
103               dim.unite (Interval (x, x));
104             }
105         }
106     }
107
108   if (dim.is_empty ())
109     dim = Interval (0, 0);
110
111   Real off = me->get_parent (a)->maybe_pure_coordinate (common, a, pure, start, end);
112   Real minimum_space = ss * robust_scm2double (me->get_property ("minimum-space"), -1);
113
114   Real total_off = dim.linear_combination (dir) - off;
115   total_off += dir * ss * robust_scm2double (me->get_property ("padding"), 0);
116
117   if (minimum_space >= 0
118       && dir
119       && total_off * dir < minimum_space)
120     total_off = minimum_space * dir;
121
122   if (include_my_extent)
123     {
124       Interval iv = me->maybe_pure_extent (me, a, pure, start, end);
125       if (!iv.is_empty ())
126         {
127           if (!dir)
128             {
129               programming_error ("direction unknown, but aligned-side wanted");
130               dir = DOWN;
131             }
132           total_off += -iv[-dir];
133         }
134     }
135
136   if (current_offset)
137     total_off = dir * max (dir * total_off,
138                            dir * (*current_offset));
139
140
141   /* FIXME: 1000 should relate to paper size.  */
142   if (fabs (total_off) > 1000)
143     {
144       string msg
145         = String_convert::form_string ("Improbable offset for grob %s: %f",
146                                        me->name ().c_str (), total_off);
147
148       programming_error (msg);
149       if (strict_infinity_checking)
150         scm_misc_error (__FUNCTION__, "Improbable offset.", SCM_EOL);
151     }
152
153   return scm_from_double (total_off);
154 }
155
156
157 MAKE_SCHEME_CALLBACK (Side_position_interface, y_aligned_on_support_refpoints, 1);
158 SCM
159 Side_position_interface::y_aligned_on_support_refpoints (SCM smob)
160 {
161   return general_side_position (unsmob_grob (smob), Y_AXIS, false, false, false, 0, 0, 0);
162 }
163
164 MAKE_SCHEME_CALLBACK (Side_position_interface, pure_y_aligned_on_support_refpoints, 3);
165 SCM
166 Side_position_interface::pure_y_aligned_on_support_refpoints (SCM smob, SCM start, SCM end)
167 {
168   return general_side_position (unsmob_grob (smob), Y_AXIS, false, false,
169                                 true, scm_to_int (start), scm_to_int (end), 0);
170 }
171
172
173 /*
174   Position next to support, taking into account my own dimensions and padding.
175 */
176 SCM
177 axis_aligned_side_helper (SCM smob, Axis a, bool pure, int start, int end, SCM current_off_scm)
178 {
179   Real r;
180   Real *current_off_ptr = 0;
181   if (scm_is_number (current_off_scm))
182     {
183       r = scm_to_double (current_off_scm);
184       current_off_ptr = &r;
185     }
186
187   return Side_position_interface::aligned_side (unsmob_grob (smob), a, pure, start, end, current_off_ptr);
188 }
189
190
191 MAKE_SCHEME_CALLBACK_WITH_OPTARGS (Side_position_interface, x_aligned_side, 2, 1, "");
192 SCM
193 Side_position_interface::x_aligned_side (SCM smob, SCM current_off)
194 {
195   return axis_aligned_side_helper (smob, X_AXIS, false, 0, 0, current_off);
196 }
197
198 MAKE_SCHEME_CALLBACK_WITH_OPTARGS (Side_position_interface, y_aligned_side, 2, 1, "");
199 SCM
200 Side_position_interface::y_aligned_side (SCM smob, SCM current_off)
201 {
202   return axis_aligned_side_helper (smob, Y_AXIS, false, 0, 0, current_off);
203 }
204
205 MAKE_SCHEME_CALLBACK_WITH_OPTARGS (Side_position_interface, pure_y_aligned_side, 4, 1, "");
206 SCM
207 Side_position_interface::pure_y_aligned_side (SCM smob, SCM start, SCM end, SCM cur_off)
208 {
209   return axis_aligned_side_helper (smob, Y_AXIS, true,
210                                    scm_to_int (start),
211                                    scm_to_int (end),
212                                    cur_off);
213 }
214
215 MAKE_SCHEME_CALLBACK (Side_position_interface, calc_cross_staff, 1)
216 SCM
217 Side_position_interface::calc_cross_staff (SCM smob)
218 {
219   Grob *me = unsmob_grob (smob);
220   extract_grob_set (me, "side-support-elements", elts);
221
222   for (vsize i = 0; i < elts.size (); i++)
223     if (to_boolean (elts[i]->get_property ("cross-staff")))
224       return SCM_BOOL_T;
225
226   Grob *common = common_refpoint_of_array (elts, me->get_parent (Y_AXIS), Y_AXIS);
227   return scm_from_bool (common != me->get_parent (Y_AXIS));
228 }
229
230 SCM
231 Side_position_interface::aligned_side (Grob *me, Axis a, bool pure, int start, int end,
232                                        Real *current_off)
233 {
234   Direction dir = get_grob_direction (me);
235
236   Real o = scm_to_double (general_side_position (me, a, true, true, pure, start, end, current_off));
237
238   /*
239     Maintain a minimum distance to the staff. This is similar to side
240     position with padding, but it will put adjoining objects on a row if
241     stuff sticks out of the staff a little.
242   */
243   Grob *staff = Staff_symbol_referencer::get_staff_symbol (me);
244   if (staff && a == Y_AXIS)
245     {
246       if (to_boolean (me->get_property ("quantize-position")))
247         {
248           Grob *common = me->common_refpoint (staff, Y_AXIS);
249           Real my_off = me->get_parent (Y_AXIS)->maybe_pure_coordinate (common, Y_AXIS, pure, start, end);
250           Real staff_off = staff->maybe_pure_coordinate (common, Y_AXIS, pure, start, end);
251           Real ss = Staff_symbol::staff_space (staff);
252           Real position = 2 * (my_off + o - staff_off) / ss;
253           Real rounded = directed_round (position, dir);
254           Grob *head = me->get_parent (X_AXIS);
255
256           if (fabs (position) <= 2 * Staff_symbol_referencer::staff_radius (me) + 1
257               /* In case of a ledger lines, quantize even if we're outside the staff. */
258               || (Note_head::has_interface (head)
259
260                   && abs (Staff_symbol_referencer::get_position (head)) > abs (position)))
261             {
262               o += (rounded - position) * 0.5 * ss;
263               if (Staff_symbol_referencer::on_line (me, int (rounded)))
264                 o += dir * 0.5 * ss;
265             }
266         }
267       else if (scm_is_number (me->get_property ("staff-padding")) && dir)
268         {
269           Interval iv = me->maybe_pure_extent (me, a, pure, start, end);
270
271           Real staff_padding
272             = Staff_symbol_referencer::staff_space (me)
273             * scm_to_double (me->get_property ("staff-padding"));
274
275           Grob *parent = me->get_parent (Y_AXIS);
276           Grob *common = me->common_refpoint (staff, Y_AXIS);
277           Real parent_position = parent->maybe_pure_coordinate (common, Y_AXIS, pure, start, end);
278           Real staff_position = staff->maybe_pure_coordinate (common, Y_AXIS, pure, start, end);
279           Interval staff_extent = staff->maybe_pure_extent (staff, a, pure, start, end);
280           Real diff = (dir * staff_extent[dir] + staff_padding
281                        - dir * (o + iv[-dir])
282                        + dir * (staff_position - parent_position));
283           o += dir * max (diff, 0.0);
284         }
285     }
286   return scm_from_double (o);
287 }
288
289 void
290 Side_position_interface::set_axis (Grob *me, Axis a)
291 {
292   if (!scm_is_number (me->get_property ("side-axis")))
293     {
294       me->set_property ("side-axis", scm_from_int (a));
295       chain_offset_callback (me,
296                              (a==X_AXIS)
297                              ? x_aligned_side_proc
298                              : y_aligned_side_proc,
299                              a);
300     }
301 }
302
303 Axis
304 Side_position_interface::get_axis (Grob *me)
305 {
306   if (scm_is_number (me->get_property ("side-axis")))
307     return Axis (scm_to_int (me->get_property ("side-axis")));
308
309   string msg = String_convert::form_string ("side-axis not set for grob %s.",
310                                             me->name ().c_str ());
311   me->programming_error (msg);
312   return NO_AXES;
313 }
314
315 MAKE_SCHEME_CALLBACK (Side_position_interface, move_to_extremal_staff, 1);
316 SCM
317 Side_position_interface::move_to_extremal_staff (SCM smob)
318 {
319   Grob *me = unsmob_grob (smob);
320   System *sys = dynamic_cast<System*> (me->get_system ());
321   Direction dir = get_grob_direction (me);
322   if (dir != DOWN)
323     dir = UP;
324
325   Interval iv = me->extent (sys, X_AXIS);
326   iv.widen (1.0);
327   Grob *top_staff = sys->get_extremal_staff (dir, iv);
328
329   if (!top_staff)
330     return SCM_BOOL_F;
331
332   // Only move this grob if it is a direct child of the system.  We
333   // are not interested in moving marks from other staves to the top
334   // staff; we only want to move marks from the system to the top
335   // staff.
336   if (sys != me->get_parent (Y_AXIS))
337     return SCM_BOOL_F;
338
339   me->set_parent (top_staff, Y_AXIS);
340   me->flush_extent_cache (Y_AXIS);
341   Axis_group_interface::add_element (top_staff, me);
342
343   // Remove any cross-staff side-support dependencies
344   Grob_array *ga = unsmob_grob_array (me->get_object ("side-support-elements"));
345   if (ga)
346     {
347       vector<Grob*> const& elts = ga->array ();
348       vector<Grob*> new_elts;
349       for (vsize i = 0; i < elts.size (); ++i)
350         {
351           if (me->common_refpoint (elts[i], Y_AXIS) == top_staff)
352             new_elts.push_back (elts[i]);
353         }
354       ga->set_array (new_elts);
355     }
356   return SCM_BOOL_T;
357 }
358
359
360 ADD_INTERFACE (Side_position_interface,
361                "Position a victim object (this one) next to other objects"
362                " (the support).  The property @code{direction} signifies where"
363                " to put the victim object relative to the support (left or"
364                " right, up or down?)\n"
365                "\n"
366                "The routine also takes the size of the staff into account if"
367                " @code{staff-padding} is set.  If undefined, the staff symbol"
368                " is ignored.",
369
370                /* properties */
371                "direction "
372                "minimum-space "
373                "padding "
374                "quantize-position "
375                "side-axis "
376                "side-support-elements "
377                "slur-padding "
378                "staff-padding "
379                );