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