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