]> git.donarmstrong.com Git - lilypond.git/blob - lily/side-position-interface.cc
* lily/lily-guile.cc (parse_symbol_list): Rewrite. Grok multiple
[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--2005 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8
9 #include "side-position-interface.hh"
10
11 #include <math.h>               // ceil.
12 #include <algorithm>
13
14
15 #include "note-head.hh"
16 #include "warn.hh"
17 #include "warn.hh"
18 #include "dimensions.hh"
19 #include "staff-symbol-referencer.hh"
20 #include "pointer-group-interface.hh"
21 #include "directional-element-interface.hh"
22 #include "staff-symbol-referencer.hh"
23 #include "string-convert.hh"
24
25 using namespace std;
26
27 void
28 Side_position_interface::add_support (Grob *me, Grob *e)
29 {
30   Pointer_group_interface::add_grob (me, ly_symbol2scm ("side-support-elements"), e);
31 }
32
33 Direction
34 Side_position_interface::get_direction (Grob *me)
35 {
36   SCM d = me->get_property ("direction");
37   if (is_direction (d) && to_dir (d))
38     return to_dir (d);
39
40   Direction relative_dir = Direction (1);
41   SCM reldir = me->get_property ("side-relative-direction");    // should use a lambda.
42   if (is_direction (reldir))
43     {
44       relative_dir = to_dir (reldir);
45     }
46
47   SCM other_elt = me->get_object ("direction-source");
48   Grob *e = unsmob_grob (other_elt);
49   if (e)
50     {
51       return (Direction) (relative_dir * get_grob_direction (e));
52     }
53
54   return CENTER;
55 }
56
57 MAKE_SCHEME_CALLBACK (Side_position_interface, aligned_on_support_extents, 2);
58 SCM
59 Side_position_interface::aligned_on_support_extents (SCM element_smob, SCM axis)
60 {
61   Grob *me = unsmob_grob (element_smob);
62   Axis a = (Axis) scm_to_int (axis);
63
64   return general_side_position (me, a, true);
65 }
66
67 /* Put the element next to the support, optionally taking in
68    account the extent of the support.  */
69 SCM
70 Side_position_interface::general_side_position (Grob *me, Axis a, bool use_extents)
71 {
72   Real ss = Staff_symbol_referencer::staff_space (me);
73
74   extract_grob_set (me, "side-support-elements", support);
75
76   Grob *common = common_refpoint_of_array (support, me->get_parent (a), a);
77   Grob *st = Staff_symbol_referencer::get_staff_symbol (me);
78   bool include_staff = (st
79                         && a == Y_AXIS
80                         && scm_is_number (me->get_property ("staff-padding")));
81
82   Interval dim;
83   if (include_staff)
84     {
85       common = st->common_refpoint (common, Y_AXIS);
86       dim = st->extent (common, Y_AXIS);
87     }
88
89   for (int i = 0; i < support.size (); i++)
90     {
91       Grob *e = support[i];
92       if (e)
93         if (use_extents)
94           dim.unite (e->extent (common, a));
95         else
96           {
97             Real x = e->relative_coordinate (common, a);
98             dim.unite (Interval (x, x));
99           }
100     }
101
102   if (dim.is_empty ())
103     dim = Interval (0, 0);
104
105   Direction dir = Side_position_interface::get_direction (me);
106
107   Real off = me->get_parent (a)->relative_coordinate (common, a);
108   Real minimum_space = ss * robust_scm2double (me->get_property ("minimum-space"), -1);
109
110   Real total_off = dim.linear_combination (dir) - off;
111   total_off += dir * ss * robust_scm2double (me->get_property ("padding"), 0);
112
113   if (minimum_space >= 0
114       && dir
115       && total_off * dir < minimum_space)
116     total_off = minimum_space * dir;
117
118   /* FIXME: 100CM should relate to paper size.  */
119   if (fabs (total_off) > 100 CM)
120     {
121       String msg
122         = String_convert::form_string ("Improbable offset for grob %s: %f%s",
123                                        me->name ().to_str0 (), total_off,
124                                        INTERNAL_UNIT);
125
126       programming_error (msg);
127     }
128   return scm_from_double (total_off);
129 }
130
131 /*
132   Cut & paste (ugh.)
133 */
134 MAKE_SCHEME_CALLBACK (Side_position_interface, aligned_on_support_refpoints, 2);
135 SCM
136 Side_position_interface::aligned_on_support_refpoints (SCM smob, SCM axis)
137 {
138   Grob *me = unsmob_grob (smob);
139   Axis a = (Axis) scm_to_int (axis);
140
141   return general_side_position (me, a, false);
142 }
143
144 Real
145 directed_round (Real f, Direction d)
146 {
147   if (d < 0)
148     return floor (f);
149   else
150     return ceil (f);
151 }
152
153 /*
154   Callback that quantises in staff-spaces, rounding in the direction
155   of the elements "direction" elt property.
156
157   Only rounds when we're inside the staff, as determined by
158   Staff_symbol_referencer::staff_radius () */
159 MAKE_SCHEME_CALLBACK (Side_position_interface, quantised_position, 2);
160 SCM
161 Side_position_interface::quantised_position (SCM element_smob, SCM)
162 {
163   Grob *me = unsmob_grob (element_smob);
164
165   Direction d = Side_position_interface::get_direction (me);
166
167   Grob *stsym = Staff_symbol_referencer::get_staff_symbol (me);
168   if (stsym)
169     {
170       Real p = Staff_symbol_referencer::get_position (me);
171       Real rp = directed_round (p, d);
172       Real rad = Staff_symbol_referencer::staff_radius (me) * 2;
173       int ip = int (rp);
174
175       Grob *head = me->get_parent (X_AXIS);
176
177       if (Staff_symbol_referencer::on_staffline (me, ip)
178           && ((abs (ip) <= rad)
179               || (Note_head::has_interface (head)
180                   && sign (Staff_symbol_referencer::get_position (head))
181                   == -d)))
182         {
183           ip += d;
184           rp += d;
185         }
186
187       return scm_from_double ((rp - p) * Staff_symbol_referencer::staff_space (me) / 2.0);
188     }
189   return scm_from_double (0.0);
190 }
191
192 /*
193   Position next to support, taking into account my own dimensions and padding.
194 */
195 MAKE_SCHEME_CALLBACK (Side_position_interface, aligned_side, 2);
196 SCM
197 Side_position_interface::aligned_side (SCM element_smob, SCM axis)
198 {
199   Grob *me = unsmob_grob (element_smob);
200   Axis a = (Axis) scm_to_int (axis);
201
202   Direction d = Side_position_interface::get_direction (me);
203
204   Real o = scm_to_double (aligned_on_support_extents (element_smob, axis));
205
206   Interval iv = me->extent (me, a);
207
208   if (!iv.is_empty ())
209     {
210       if (!d)
211         {
212           programming_error ("direction unknown, but aligned-side wanted");
213           d = DOWN;
214         }
215       o += -iv[-d];
216     }
217
218   /*
219     Maintain a minimum distance to the staff. This is similar to side
220     position with padding, but it will put adjoining objects on a row if
221     stuff sticks out of the staff a little.
222   */
223   Grob *st = Staff_symbol_referencer::get_staff_symbol (me);
224   if (st && a == Y_AXIS
225       && scm_is_number (me->get_property ("staff-padding")))
226     {
227       Real padding
228         = Staff_symbol_referencer::staff_space (me)
229         * scm_to_double (me->get_property ("staff-padding"));
230
231       Grob *common = me->common_refpoint (st, Y_AXIS);
232
233       Interval staff_size = st->extent (common, Y_AXIS);
234       Real diff = d*staff_size[d] + padding - d * (o + iv[-d]);
235       o += d * max (diff, 0.0);
236     }
237
238   return scm_from_double (o);
239 }
240
241 void
242 Side_position_interface::set_axis (Grob *me, Axis a)
243 {
244   me->add_offset_callback (Side_position_interface::aligned_side_proc, a);
245 }
246
247 // ugh. doesn't catch all variants. 
248 Axis
249 Side_position_interface::get_axis (Grob *me)
250 {
251   if (me->has_offset_callback (Side_position_interface::aligned_side_proc, X_AXIS)
252       || me->has_offset_callback (Side_position_interface::aligned_side_proc, X_AXIS))
253     return X_AXIS;
254
255   return Y_AXIS;
256 }
257
258 ADD_INTERFACE (Side_position_interface, "side-position-interface",
259                "Position a victim object (this one) next to other objects (the "
260                "support).   The property @code{direction} signifies where to put the  "
261                "victim object relative to the support (left or right, up or down?)\n\n "
262                "The routine also takes the size the staff into account if "
263                "@code{staff-padding} is set. If undefined, the staff symbol is ignored.",
264                "direction "
265                "direction-source "
266                "minimum-space "
267                "padding "
268                "side-relative-direction "
269                "side-support-elements "
270                "slur-padding "
271                "staff-padding "
272                );