]> git.donarmstrong.com Git - lilypond.git/blob - lily/side-position-interface.cc
* scm/paper-system.scm (paper-system-annotate): also annotate the
[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 SCM
51 Side_position_interface::general_side_position (Grob *me, Axis a, bool use_extents,
52                                                 bool pure, int start, int end)
53 {
54   Real ss = Staff_symbol_referencer::staff_space (me);
55
56   extract_grob_set (me, "side-support-elements", support);
57
58   Grob *common = common_refpoint_of_array (support, me->get_parent (a), a);
59   Grob *staff_symbol = Staff_symbol_referencer::get_staff_symbol (me);
60   bool include_staff = 
61     staff_symbol
62     && a == Y_AXIS
63     && scm_is_number (me->get_property ("staff-padding"))
64     && !to_boolean (me->get_property ("quantize-position"));
65
66   Interval dim;
67   Interval staff_extents;
68   if (include_staff)
69     {
70       common = staff_symbol->common_refpoint (common, Y_AXIS);
71       staff_extents = staff_symbol->maybe_pure_extent (common, Y_AXIS, pure, start, end);
72
73       if (include_staff)
74         dim.unite (staff_extents);
75     }
76
77   for (vsize i = 0; i < support.size (); i++)
78     {
79       Grob *e = support[i];
80       if (e)
81         if (use_extents)
82           dim.unite (e->maybe_pure_extent (common, a, pure, start, end));
83         else
84           {
85             Real x = e->maybe_pure_coordinate (common, a, pure, start, end);
86             dim.unite (Interval (x, x));
87           }
88     }
89
90   if (dim.is_empty ())
91     dim = Interval (0, 0);
92
93   Direction dir = get_grob_direction (me);
94
95   Real off = me->get_parent (a)->maybe_pure_coordinate (common, a, pure, start, end);
96   Real minimum_space = ss * robust_scm2double (me->get_property ("minimum-space"), -1);
97
98   Real total_off = dim.linear_combination (dir) - off;
99   total_off += dir * ss * robust_scm2double (me->get_property ("padding"), 0);
100
101   if (minimum_space >= 0
102       && dir
103       && total_off * dir < minimum_space)
104     total_off = minimum_space * dir;
105   
106   /* FIXME: 1000 should relate to paper size.  */
107   if (fabs (total_off) > 1000)
108     {
109       string msg
110         = String_convert::form_string ("Improbable offset for grob %s: %f",
111                                        me->name ().c_str (), total_off);
112
113       programming_error (msg);
114       if (strict_infinity_checking)
115         scm_misc_error (__FUNCTION__, "Improbable offset.", SCM_EOL);
116     }
117   return scm_from_double (total_off);
118 }
119
120
121 MAKE_SCHEME_CALLBACK (Side_position_interface, y_aligned_on_support_refpoints, 1);
122 SCM
123 Side_position_interface::y_aligned_on_support_refpoints (SCM smob)
124 {
125   return general_side_position (unsmob_grob (smob), Y_AXIS, false, false, 0, 0); 
126 }
127
128 MAKE_SCHEME_CALLBACK (Side_position_interface, pure_y_aligned_on_support_refpoints, 3);
129 SCM
130 Side_position_interface::pure_y_aligned_on_support_refpoints (SCM smob, SCM start, SCM end)
131 {
132   return general_side_position (unsmob_grob (smob), Y_AXIS, false,
133                                 true, scm_to_int (start), scm_to_int (end)); 
134 }
135
136
137 /*
138   Position next to support, taking into account my own dimensions and padding.
139 */
140
141 MAKE_SCHEME_CALLBACK (Side_position_interface, x_aligned_side, 1);
142 SCM
143 Side_position_interface::x_aligned_side (SCM smob)
144 {
145   return aligned_side (unsmob_grob (smob), X_AXIS, false, 0, 0);
146 }
147
148 MAKE_SCHEME_CALLBACK (Side_position_interface, y_aligned_side, 1);
149 SCM
150 Side_position_interface::y_aligned_side (SCM smob)
151 {
152   return aligned_side (unsmob_grob (smob), Y_AXIS, false, 0, 0);
153 }
154
155 MAKE_SCHEME_CALLBACK (Side_position_interface, pure_y_aligned_side, 3);
156 SCM
157 Side_position_interface::pure_y_aligned_side (SCM smob, SCM start, SCM end)
158 {
159   return aligned_side (unsmob_grob (smob), Y_AXIS, true, scm_to_int (start), scm_to_int (end));
160 }
161
162 SCM
163 Side_position_interface::aligned_side (Grob *me, Axis a, bool pure, int start, int end)
164 {
165   Direction dir = get_grob_direction (me);
166
167   Real o = scm_to_double (general_side_position (me, a, true, pure, start, end));
168   Interval iv = me->maybe_pure_extent (me, a, pure, start, end);
169
170   if (!iv.is_empty ())
171     {
172       if (!dir)
173         {
174           programming_error ("direction unknown, but aligned-side wanted");
175           dir = DOWN;
176         }
177       o += -iv[-dir];
178     }
179
180   /*
181     Maintain a minimum distance to the staff. This is similar to side
182     position with padding, but it will put adjoining objects on a row if
183     stuff sticks out of the staff a little.
184   */
185   Grob *staff = Staff_symbol_referencer::get_staff_symbol (me);
186   if (staff && a == Y_AXIS)
187     {
188       if (to_boolean (me->get_property ("quantize-position")))
189         {
190           Grob *common = me->common_refpoint (staff, Y_AXIS);
191           Real my_off = me->get_parent (Y_AXIS)->maybe_pure_coordinate (common, Y_AXIS, pure, start, end);
192           Real staff_off = staff->maybe_pure_coordinate (common, Y_AXIS, pure, start, end);
193           Real ss = Staff_symbol::staff_space (staff);
194           Real position = 2 * (my_off + o - staff_off) / ss;
195           Real rounded = directed_round (position, dir);
196           Grob *head = me->get_parent (X_AXIS);
197
198           if (fabs (position) <= 2 * Staff_symbol_referencer::staff_radius (me) + 1 
199               || (Note_head::has_interface (head)
200                   && sign (Staff_symbol_referencer::get_position (head)) == - dir))
201             {
202               o += (rounded - position) * 0.5 * ss;
203               if (Staff_symbol_referencer::on_line (me, int (rounded)))
204                 o += dir * 0.5 * ss;
205             }
206         }
207       else if (scm_is_number (me->get_property ("staff-padding")))
208         {
209           Real padding
210             = Staff_symbol_referencer::staff_space (me)
211             * scm_to_double (me->get_property ("staff-padding"));
212
213           Grob *common = me->common_refpoint (staff, Y_AXIS);
214
215           Interval staff_size = staff->maybe_pure_extent (common, Y_AXIS, pure, start, end);
216           Real diff = dir*staff_size[dir] + padding - dir * (o + iv[-dir]);
217           o += dir * max (diff, 0.0);
218         }
219     }
220   return scm_from_double (o);
221 }
222
223 void
224 Side_position_interface::set_axis (Grob *me, Axis a)
225 {
226   if (!scm_is_number (me->get_property ("side-axis")))
227     {
228       me->set_property ("side-axis", scm_from_int (a));
229       add_offset_callback (me,
230                            (a==X_AXIS)
231                            ? x_aligned_side_proc
232                            : y_aligned_side_proc,
233                            a);
234     }
235 }
236 Axis
237 Side_position_interface::get_axis (Grob *me)
238 {
239   if (scm_is_number (me->get_property ("side-axis")))
240     return Axis (scm_to_int (me->get_property ("side-axis")));
241   
242   me->programming_error ("side-axis not set.");
243   return NO_AXES;
244 }
245
246 ADD_INTERFACE (Side_position_interface, "side-position-interface",
247                "Position a victim object (this one) next to other objects (the "
248                "support).   The property @code{direction} signifies where to put the  "
249                "victim object relative to the support (left or right, up or down?)\n\n "
250                "The routine also takes the size the staff into account if "
251                "@code{staff-padding} is set. If undefined, the staff symbol is ignored.",
252
253                /* properties */
254                "direction "
255                "direction-source "
256                "minimum-space "
257                "padding "
258                "quantize-position "
259                "side-axis "
260                "side-relative-direction "
261                "side-support-elements "
262                "slur-padding "
263                "staff-padding "
264                );