]> git.donarmstrong.com Git - lilypond.git/blob - lily/side-position-interface.cc
38880cb52ef9d201927ae4229e7b97250513628c
[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--2006 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
26 void
27 Side_position_interface::add_support (Grob *me, Grob *e)
28 {
29   Pointer_group_interface::add_unordered_grob (me, ly_symbol2scm ("side-support-elements"), e);
30 }
31
32 Direction
33 Side_position_interface::get_direction (Grob *me)
34 {
35   Direction relative_dir = Direction (1);
36   SCM reldir = me->get_property ("side-relative-direction");
37   if (is_direction (reldir))
38     relative_dir = to_dir (reldir);
39
40   SCM other_elt = me->get_object ("direction-source");
41   Grob *e = unsmob_grob (other_elt);
42   if (e)
43     return (Direction) (relative_dir * get_grob_direction (e));
44
45   return CENTER;
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   for (vsize i = 0; i < support.size (); i++)
83     {
84       Grob *e = support[i];
85       if (e)
86         if (use_extents)
87           dim.unite (e->maybe_pure_extent (common, a, pure, start, end));
88         else
89           {
90             Real x = e->maybe_pure_coordinate (common, a, pure, start, end);
91             dim.unite (Interval (x, x));
92           }
93     }
94
95   if (dim.is_empty ())
96     dim = Interval (0, 0);
97
98   Direction dir = get_grob_direction (me);
99
100   Real off = me->get_parent (a)->maybe_pure_coordinate (common, a, pure, start, end);
101   Real minimum_space = ss * robust_scm2double (me->get_property ("minimum-space"), -1);
102
103   Real total_off = dim.linear_combination (dir) - off;
104   total_off += dir * ss * robust_scm2double (me->get_property ("padding"), 0);
105
106   if (minimum_space >= 0
107       && dir
108       && total_off * dir < minimum_space)
109     total_off = minimum_space * dir;
110
111   if (include_my_extent)
112     {
113       Interval iv = me->maybe_pure_extent (me, a, pure, start, end);
114       if (!iv.is_empty ())
115         {
116           if (!dir)
117             {
118               programming_error ("direction unknown, but aligned-side wanted");
119               dir = DOWN;
120             }
121           total_off += -iv[-dir];
122         }
123     }
124
125   if (current_offset)
126     total_off = dir * max (dir * total_off,
127                            dir * (*current_offset));
128   
129   
130   /* FIXME: 1000 should relate to paper size.  */
131   if (fabs (total_off) > 1000)
132     {
133       string msg
134         = String_convert::form_string ("Improbable offset for grob %s: %f",
135                                        me->name ().c_str (), total_off);
136
137       programming_error (msg);
138       if (strict_infinity_checking)
139         scm_misc_error (__FUNCTION__, "Improbable offset.", SCM_EOL);
140     }
141   return scm_from_double (total_off);
142 }
143
144
145 MAKE_SCHEME_CALLBACK (Side_position_interface, y_aligned_on_support_refpoints, 1);
146 SCM
147 Side_position_interface::y_aligned_on_support_refpoints (SCM smob)
148 {
149   return general_side_position (unsmob_grob (smob), Y_AXIS, false, false, false, 0, 0, 0); 
150 }
151
152 MAKE_SCHEME_CALLBACK (Side_position_interface, pure_y_aligned_on_support_refpoints, 3);
153 SCM
154 Side_position_interface::pure_y_aligned_on_support_refpoints (SCM smob, SCM start, SCM end)
155 {
156   return general_side_position (unsmob_grob (smob), Y_AXIS, false, false, 
157                                 true, scm_to_int (start), scm_to_int (end), 0); 
158 }
159
160
161 /*
162   Position next to support, taking into account my own dimensions and padding.
163 */
164 SCM
165 axis_aligned_side_helper (SCM smob, Axis a, bool pure, int start, int end, SCM current_off_scm)
166 {
167   Real r;
168   Real *current_off_ptr = 0;
169   if (scm_is_number (current_off_scm))
170     {
171       r = scm_to_double (current_off_scm);
172       current_off_ptr = &r; 
173     }
174   
175   return Side_position_interface::aligned_side (unsmob_grob (smob), a, pure, start, end, current_off_ptr);
176 }
177
178
179 MAKE_SCHEME_CALLBACK_WITH_OPTARGS (Side_position_interface, x_aligned_side, 2, 1);
180 SCM
181 Side_position_interface::x_aligned_side (SCM smob, SCM current_off)
182 {
183   return axis_aligned_side_helper (smob, X_AXIS, false, 0, 0, current_off);
184 }
185
186 MAKE_SCHEME_CALLBACK_WITH_OPTARGS (Side_position_interface, y_aligned_side, 2, 1);
187 SCM
188 Side_position_interface::y_aligned_side (SCM smob, SCM current_off)
189 {
190   return axis_aligned_side_helper (smob, Y_AXIS, false, 0, 0, current_off);
191 }
192
193 MAKE_SCHEME_CALLBACK (Side_position_interface, pure_y_aligned_side, 3);
194 SCM
195 Side_position_interface::pure_y_aligned_side (SCM smob, SCM start, SCM end)
196 {
197   return aligned_side (unsmob_grob (smob), Y_AXIS, true, scm_to_int (start), scm_to_int (end), 0);
198 }
199
200 SCM
201 Side_position_interface::aligned_side (Grob *me, Axis a, bool pure, int start, int end,
202                                        Real *current_off)
203 {
204   Direction dir = get_grob_direction (me);
205
206   Real o = scm_to_double (general_side_position (me, a, true, true, pure, start, end, current_off));
207
208   /*
209     Maintain a minimum distance to the staff. This is similar to side
210     position with padding, but it will put adjoining objects on a row if
211     stuff sticks out of the staff a little.
212   */
213   Grob *staff = Staff_symbol_referencer::get_staff_symbol (me);
214   if (staff && a == Y_AXIS)
215     {
216       if (to_boolean (me->get_property ("quantize-position")))
217         {
218           Grob *common = me->common_refpoint (staff, Y_AXIS);
219           Real my_off = me->get_parent (Y_AXIS)->maybe_pure_coordinate (common, Y_AXIS, pure, start, end);
220           Real staff_off = staff->maybe_pure_coordinate (common, Y_AXIS, pure, start, end);
221           Real ss = Staff_symbol::staff_space (staff);
222           Real position = 2 * (my_off + o - staff_off) / ss;
223           Real rounded = directed_round (position, dir);
224           Grob *head = me->get_parent (X_AXIS);
225
226           if (fabs (position) <= 2 * Staff_symbol_referencer::staff_radius (me) + 1 
227               || (Note_head::has_interface (head)
228                   && sign (Staff_symbol_referencer::get_position (head)) == - dir))
229             {
230               o += (rounded - position) * 0.5 * ss;
231               if (Staff_symbol_referencer::on_line (me, int (rounded)))
232                 o += dir * 0.5 * ss;
233             }
234         }
235       else if (scm_is_number (me->get_property ("staff-padding")))
236         {
237           Interval iv = me->maybe_pure_extent (me, a, pure, start, end);
238           
239           Real padding
240             = Staff_symbol_referencer::staff_space (me)
241             * scm_to_double (me->get_property ("staff-padding"));
242
243           Grob *common = me->common_refpoint (staff, Y_AXIS);
244
245           Interval staff_size = staff->maybe_pure_extent (common, Y_AXIS, pure, start, end);
246           Real diff = dir*staff_size[dir] + padding - dir * (o + iv[-dir]);
247           o += dir * max (diff, 0.0);
248         }
249     }
250   return scm_from_double (o);
251 }
252
253 void
254 Side_position_interface::set_axis (Grob *me, Axis a)
255 {
256   if (!scm_is_number (me->get_property ("side-axis")))
257     {
258       me->set_property ("side-axis", scm_from_int (a));
259       chain_offset_callback (me,
260                              (a==X_AXIS)
261                              ? x_aligned_side_proc
262                              : y_aligned_side_proc,
263                              a);
264     }
265 }
266 Axis
267 Side_position_interface::get_axis (Grob *me)
268 {
269   if (scm_is_number (me->get_property ("side-axis")))
270     return Axis (scm_to_int (me->get_property ("side-axis")));
271   
272   me->programming_error ("side-axis not set.");
273   return NO_AXES;
274 }
275
276 ADD_INTERFACE (Side_position_interface, "side-position-interface",
277                "Position a victim object (this one) next to other objects (the "
278                "support).   The property @code{direction} signifies where to put the  "
279                "victim object relative to the support (left or right, up or down?)\n\n "
280                "The routine also takes the size the staff into account if "
281                "@code{staff-padding} is set. If undefined, the staff symbol is ignored.",
282
283                /* properties */
284                "direction "
285                "direction-source "
286                "minimum-space "
287                "padding "
288                "quantize-position "
289                "side-axis "
290                "side-relative-direction "
291                "side-support-elements "
292                "slur-padding "
293                "staff-padding "
294                );