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