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