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