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