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