]> git.donarmstrong.com Git - lilypond.git/blob - lily/side-position-interface.cc
Merge branch 'master' into lilypond/translation
[lilypond.git] / lily / side-position-interface.cc
1 /*
2   side-position-interface.cc -- implement Side_position_interface
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1998--2007 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
8
9 #include "side-position-interface.hh"
10
11 #include <cmath>                // ceil.
12 #include <algorithm>
13 using namespace std;
14
15 #include "note-head.hh"
16 #include "warn.hh"
17 #include "main.hh"
18 #include "staff-symbol-referencer.hh"
19 #include "pointer-group-interface.hh"
20 #include "directional-element-interface.hh"
21 #include "staff-symbol-referencer.hh"
22 #include "staff-symbol.hh"
23 #include "string-convert.hh"
24 #include "misc.hh"
25 #include "grob.hh"
26
27 void
28 Side_position_interface::add_support (Grob *me, Grob *e)
29 {
30   Pointer_group_interface::add_unordered_grob (me, ly_symbol2scm ("side-support-elements"), e);
31 }
32
33 Direction
34 Side_position_interface::get_direction (Grob *me)
35 {
36   Direction relative_dir = Direction (1);
37   SCM reldir = me->get_property ("side-relative-direction");
38   if (is_direction (reldir))
39     relative_dir = to_dir (reldir);
40
41   SCM other_elt = me->get_object ("direction-source");
42   Grob *e = unsmob_grob (other_elt);
43   if (e)
44     return (Direction) (relative_dir * get_grob_direction (e));
45
46   return CENTER;
47 }
48
49 /* Put the element next to the support, optionally taking in
50    account the extent of the support.
51
52    Does not take into account the extent of ME.
53 */
54 SCM
55 Side_position_interface::general_side_position (Grob *me, Axis a, bool use_extents,
56                                                 bool include_my_extent,
57                                                 bool pure, int start, int end,
58                                                 Real *current_offset)
59 {
60   Real ss = Staff_symbol_referencer::staff_space (me);
61
62   extract_grob_set (me, "side-support-elements", support);
63
64   Grob *common = common_refpoint_of_array (support, me->get_parent (a), a);
65   Grob *staff_symbol = Staff_symbol_referencer::get_staff_symbol (me);
66   bool include_staff = 
67     staff_symbol
68     && a == Y_AXIS
69     && scm_is_number (me->get_property ("staff-padding"))
70     && !to_boolean (me->get_property ("quantize-position"));
71
72   Interval dim;
73   Interval staff_extents;
74   if (include_staff)
75     {
76       common = staff_symbol->common_refpoint (common, Y_AXIS);
77       staff_extents = staff_symbol->maybe_pure_extent (common, Y_AXIS, pure, start, end);
78
79       if (include_staff)
80         dim.unite (staff_extents);
81     }
82
83   for (vsize i = 0; i < support.size (); i++)
84     {
85       Grob *e = support[i];
86       if (e)
87         if (use_extents)
88           dim.unite (e->maybe_pure_extent (common, a, pure, start, end));
89         else
90           {
91             Real x = e->maybe_pure_coordinate (common, a, pure, start, end);
92             dim.unite (Interval (x, x));
93           }
94     }
95
96   if (dim.is_empty ())
97     dim = Interval (0, 0);
98
99   Direction dir = get_grob_direction (me);
100
101   Real off = me->get_parent (a)->maybe_pure_coordinate (common, a, pure, start, end);
102   Real minimum_space = ss * robust_scm2double (me->get_property ("minimum-space"), -1);
103
104   Real total_off = dim.linear_combination (dir) - off;
105   total_off += dir * ss * robust_scm2double (me->get_property ("padding"), 0);
106
107   if (minimum_space >= 0
108       && dir
109       && total_off * dir < minimum_space)
110     total_off = minimum_space * dir;
111
112   if (include_my_extent)
113     {
114       Interval iv = me->maybe_pure_extent (me, a, pure, start, end);
115       if (!iv.is_empty ())
116         {
117           if (!dir)
118             {
119               programming_error ("direction unknown, but aligned-side wanted");
120               dir = DOWN;
121             }
122           total_off += -iv[-dir];
123         }
124     }
125
126   if (current_offset)
127     total_off = dir * max (dir * total_off,
128                            dir * (*current_offset));
129   
130   
131   /* FIXME: 1000 should relate to paper size.  */
132   if (fabs (total_off) > 1000)
133     {
134       string msg
135         = String_convert::form_string ("Improbable offset for grob %s: %f",
136                                        me->name ().c_str (), total_off);
137
138       programming_error (msg);
139       if (strict_infinity_checking)
140         scm_misc_error (__FUNCTION__, "Improbable offset.", SCM_EOL);
141     }
142   return scm_from_double (total_off);
143 }
144
145
146 MAKE_SCHEME_CALLBACK (Side_position_interface, y_aligned_on_support_refpoints, 1);
147 SCM
148 Side_position_interface::y_aligned_on_support_refpoints (SCM smob)
149 {
150   return general_side_position (unsmob_grob (smob), Y_AXIS, false, false, false, 0, 0, 0); 
151 }
152
153 MAKE_SCHEME_CALLBACK (Side_position_interface, pure_y_aligned_on_support_refpoints, 3);
154 SCM
155 Side_position_interface::pure_y_aligned_on_support_refpoints (SCM smob, SCM start, SCM end)
156 {
157   return general_side_position (unsmob_grob (smob), Y_AXIS, false, false, 
158                                 true, scm_to_int (start), scm_to_int (end), 0); 
159 }
160
161
162 /*
163   Position next to support, taking into account my own dimensions and padding.
164 */
165 SCM
166 axis_aligned_side_helper (SCM smob, Axis a, bool pure, int start, int end, SCM current_off_scm)
167 {
168   Real r;
169   Real *current_off_ptr = 0;
170   if (scm_is_number (current_off_scm))
171     {
172       r = scm_to_double (current_off_scm);
173       current_off_ptr = &r; 
174     }
175   
176   return Side_position_interface::aligned_side (unsmob_grob (smob), a, pure, start, end, current_off_ptr);
177 }
178
179
180 MAKE_SCHEME_CALLBACK_WITH_OPTARGS (Side_position_interface, x_aligned_side, 2, 1, "");
181 SCM
182 Side_position_interface::x_aligned_side (SCM smob, SCM current_off)
183 {
184   return axis_aligned_side_helper (smob, X_AXIS, false, 0, 0, current_off);
185 }
186
187 MAKE_SCHEME_CALLBACK_WITH_OPTARGS (Side_position_interface, y_aligned_side, 2, 1, "");
188 SCM
189 Side_position_interface::y_aligned_side (SCM smob, SCM current_off)
190 {
191   return axis_aligned_side_helper (smob, Y_AXIS, false, 0, 0, current_off);
192 }
193
194 MAKE_SCHEME_CALLBACK_WITH_OPTARGS (Side_position_interface, pure_y_aligned_side, 4, 1, "");
195 SCM
196 Side_position_interface::pure_y_aligned_side (SCM smob, SCM start, SCM end, SCM cur_off)
197 {
198   return axis_aligned_side_helper (smob, Y_AXIS, true,
199                                    scm_to_int (start),
200                                    scm_to_int (end),
201                                    cur_off);
202 }
203
204 MAKE_SCHEME_CALLBACK (Side_position_interface, calc_cross_staff, 1)
205 SCM
206 Side_position_interface::calc_cross_staff (SCM smob)
207 {
208   Grob *me = unsmob_grob (smob);
209   extract_grob_set (me, "side-support-elements", elts);
210
211   Grob *common = common_refpoint_of_array (elts, me->get_parent (Y_AXIS), Y_AXIS);
212   return scm_from_bool (common != me->get_parent (Y_AXIS));
213 }
214
215 SCM
216 Side_position_interface::aligned_side (Grob *me, Axis a, bool pure, int start, int end,
217                                        Real *current_off)
218 {
219   Direction dir = get_grob_direction (me);
220
221   Real o = scm_to_double (general_side_position (me, a, true, true, pure, start, end, current_off));
222
223   /*
224     Maintain a minimum distance to the staff. This is similar to side
225     position with padding, but it will put adjoining objects on a row if
226     stuff sticks out of the staff a little.
227   */
228   Grob *staff = Staff_symbol_referencer::get_staff_symbol (me);
229   if (staff && a == Y_AXIS)
230     {
231       if (to_boolean (me->get_property ("quantize-position")))
232         {
233           Grob *common = me->common_refpoint (staff, Y_AXIS);
234           Real my_off = me->get_parent (Y_AXIS)->maybe_pure_coordinate (common, Y_AXIS, pure, start, end);
235           Real staff_off = staff->maybe_pure_coordinate (common, Y_AXIS, pure, start, end);
236           Real ss = Staff_symbol::staff_space (staff);
237           Real position = 2 * (my_off + o - staff_off) / ss;
238           Real rounded = directed_round (position, dir);
239           Grob *head = me->get_parent (X_AXIS);
240
241           if (fabs (position) <= 2 * Staff_symbol_referencer::staff_radius (me) + 1 
242               || (Note_head::has_interface (head)
243                   && sign (Staff_symbol_referencer::get_position (head)) == - dir))
244             {
245               o += (rounded - position) * 0.5 * ss;
246               if (Staff_symbol_referencer::on_line (me, int (rounded)))
247                 o += dir * 0.5 * ss;
248             }
249         }
250       else if (scm_is_number (me->get_property ("staff-padding")) && dir)
251         {
252           Interval iv = me->maybe_pure_extent (me, a, pure, start, end);
253           
254           Real padding
255             = Staff_symbol_referencer::staff_space (me)
256             * scm_to_double (me->get_property ("staff-padding"));
257
258           Grob *common = me->common_refpoint (staff, Y_AXIS);
259
260           Interval staff_size = staff->maybe_pure_extent (common, Y_AXIS, pure, start, end);
261           Real diff = dir*staff_size[dir] + padding - dir * (o + iv[-dir]);
262           o += dir * max (diff, 0.0);
263         }
264     }
265   return scm_from_double (o);
266 }
267
268 void
269 Side_position_interface::set_axis (Grob *me, Axis a)
270 {
271   if (!scm_is_number (me->get_property ("side-axis")))
272     {
273       me->set_property ("side-axis", scm_from_int (a));
274       chain_offset_callback (me,
275                              (a==X_AXIS)
276                              ? x_aligned_side_proc
277                              : y_aligned_side_proc,
278                              a);
279     }
280 }
281 Axis
282 Side_position_interface::get_axis (Grob *me)
283 {
284   if (scm_is_number (me->get_property ("side-axis")))
285     return Axis (scm_to_int (me->get_property ("side-axis")));
286   
287   me->programming_error ("side-axis not set.");
288   return NO_AXES;
289 }
290
291 ADD_INTERFACE (Side_position_interface,
292                "Position a victim object (this one) next to other objects (the "
293                "support).   The property @code{direction} signifies where to put the  "
294                "victim object relative to the support (left or right, up or down?)\n\n "
295                "The routine also takes the size the staff into account if "
296                "@code{staff-padding} is set. If undefined, the staff symbol is ignored.",
297
298                /* properties */
299                "direction "
300                "direction-source "
301                "minimum-space "
302                "padding "
303                "quantize-position "
304                "side-axis "
305                "side-relative-direction "
306                "side-support-elements "
307                "slur-padding "
308                "staff-padding "
309                );