]> git.donarmstrong.com Git - lilypond.git/blob - lily/side-position-interface.cc
release: 1.3.131
[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--2001 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7   
8  */
9 #include <math.h>               // ceil.
10
11 #include "side-position-interface.hh"
12 #include "debug.hh"
13 #include "warn.hh"
14 #include "dimensions.hh"
15 #include "staff-symbol-referencer.hh"
16 #include "group-interface.hh"
17
18 void
19 Side_position::add_support (Grob*me, Grob*e)
20 {
21   Pointer_group_interface::add_element (me, "side-support-elements",e);
22 }
23
24
25
26 Direction
27 Side_position::get_direction (Grob*me)
28 {
29   SCM d = me->get_grob_property ("direction");
30   if (isdir_b (d) && to_dir (d))
31     return to_dir (d);
32
33   Direction relative_dir = Direction (1);
34   SCM reldir = me->get_grob_property ("side-relative-direction");       // should use a lambda.
35   if (isdir_b (reldir))
36     {
37       relative_dir = to_dir (reldir);
38     }
39   
40   SCM other_elt = me->get_grob_property ("direction-source");
41   Grob * e = unsmob_grob(other_elt);
42   if (e)
43     {
44       return (Direction)(relative_dir * Side_position::get_direction (e));
45     }
46   
47   return DOWN;
48 }
49   
50 /*
51    Callback that does the aligning. Puts the element next to the support
52  */
53
54 MAKE_SCHEME_CALLBACK(Side_position,side_position,2);
55 SCM
56 Side_position::side_position (SCM element_smob, SCM axis)
57 {
58   Grob *me = unsmob_grob (element_smob);
59   Axis a = (Axis) gh_scm2int (axis);
60
61   Grob *common = me->parent_l (a);
62   SCM support = me->get_grob_property ("side-support-elements");
63   for (SCM s = support; s != SCM_EOL; s = gh_cdr (s))
64     {
65       Grob * e  = unsmob_grob (gh_car (s));
66       if (e)
67         common = common->common_refpoint (e, a);
68     }
69   
70   Interval dim;
71   for (SCM s = support; s != SCM_EOL; s = gh_cdr (s))
72     {
73
74       Grob * e  = unsmob_grob ( gh_car (s));
75       if (e)
76         {
77           dim.unite (e->extent (common, a));
78         }
79     }
80
81   if (dim.empty_b ())
82     {
83       dim = Interval(0,0);
84     }
85
86   Direction dir = Side_position::get_direction (me);
87     
88   Real off =  me->parent_l (a)->relative_coordinate (common, a);
89   SCM minimum = me->remove_grob_property ("minimum-space");
90
91   Real total_off = dim[dir] + off;
92   SCM padding = me->remove_grob_property ("padding");
93   if (gh_number_p (padding))
94     {
95       total_off += gh_scm2double (padding) * dir;
96     }
97   if (gh_number_p (minimum) && total_off * dir < gh_scm2double (minimum))
98     {
99       total_off = gh_scm2double (minimum) * dir;
100     }
101   if (fabs (total_off) > 100 CM)
102     programming_error ("Huh ? Improbable staff side dim.");
103
104   return gh_double2scm (total_off);
105 }
106
107 /**
108   callback that centers the element on itself
109  */
110 MAKE_SCHEME_CALLBACK(Side_position,aligned_on_self,2);
111 SCM
112 Side_position::aligned_on_self (SCM element_smob, SCM axis)
113 {
114   Grob *me = unsmob_grob (element_smob);
115   Axis a = (Axis) gh_scm2int (axis);
116   String s ("self-alignment-");
117
118   s +=  (a == X_AXIS) ? "X" : "Y";
119
120   SCM align (me->get_grob_property (s.ch_C()));
121   if (gh_number_p (align))
122     {
123       Interval ext(me->extent (me,a));
124
125       if (ext.empty_b ())
126         {
127           programming_error ("I'm empty. Can't align on self");
128           return gh_double2scm (0.0);
129         }
130       else
131         {
132           return gh_double2scm (- ext.linear_combination (gh_scm2double (align)));
133         }
134     }
135   else if (unsmob_grob (align))
136     {
137       return gh_double2scm (- unsmob_grob (align)->relative_coordinate (me,  a));
138     }
139     return gh_double2scm (0.0);
140 }
141
142
143
144 Real
145 directed_round (Real f, Direction d)
146 {
147   if (d < 0)
148     return floor (f);
149   else
150     return ceil (f);
151 }
152
153 /*
154   Callback that quantises in staff-spaces, rounding in the direction
155   of the elements "direction" elt property.
156
157   Only rounds when we're inside the staff, as determined by
158   Staff_symbol_referencer::staff_radius() */
159 MAKE_SCHEME_CALLBACK(Side_position,quantised_position,2);
160 SCM
161 Side_position::quantised_position (SCM element_smob, SCM )
162 {
163   Grob *me = unsmob_grob (element_smob);
164   
165   
166   Direction d = Side_position::get_direction (me);
167
168   if (Staff_symbol_referencer::has_interface (me))
169     {
170       Real p = Staff_symbol_referencer::position_f (me);
171       Real rp = directed_round (p, d);
172       Real rad = Staff_symbol_referencer::staff_radius (me) *2 ;
173       int ip = int  (rp);
174
175       if (abs (ip) <= rad && Staff_symbol_referencer::on_staffline (me,ip))
176         {
177           ip += d;
178           rp += d;
179         }
180
181       return gh_double2scm ((rp - p) * Staff_symbol_referencer::staff_space (me) / 2.0);
182     }
183   return gh_double2scm (0.0);
184 }
185
186 /*
187   Position next to support, taking into account my own dimensions and padding.
188  */
189 MAKE_SCHEME_CALLBACK(Side_position,aligned_side,2);
190 SCM
191 Side_position::aligned_side (SCM element_smob, SCM axis)
192 {
193   Grob *me = unsmob_grob (element_smob);
194   Axis a = (Axis) gh_scm2int (axis);
195   
196   Direction d = Side_position::get_direction (me);
197   Real o = gh_scm2double (side_position (element_smob,axis));
198
199   Interval iv =  me->extent (me, a);
200
201   if (!iv.empty_b ())
202     {
203       o += - iv[-d];
204
205       SCM pad = me->get_grob_property ("padding");
206       if (gh_number_p (pad))
207         o += d *gh_scm2double (pad) ; 
208     }
209   return gh_double2scm (o);
210 }
211
212 /*
213   Position centered on parent.
214  */
215 MAKE_SCHEME_CALLBACK(Side_position,centered_on_parent,2);
216 SCM
217 Side_position::centered_on_parent (SCM element_smob, SCM axis)
218 {
219   Grob *me = unsmob_grob (element_smob);
220   Axis a = (Axis) gh_scm2int (axis);
221   Grob *him = me->parent_l (a);
222
223   return gh_double2scm (him->extent (him,a).center ());  
224 }
225
226
227 void
228 Side_position::add_staff_support (Grob*me)
229 {
230   Grob* st = Staff_symbol_referencer::staff_symbol_l (me);
231   if (st)
232     {
233       add_support (me,st);
234     }
235 }
236
237 void
238 Side_position::set_axis (Grob*me, Axis a)
239 {
240   me->add_offset_callback (Side_position::aligned_side_proc, a);
241 }
242
243
244
245 // ugh. doesn't cactch all variants. 
246 Axis
247 Side_position::get_axis (Grob*me)
248 {
249   if (me->has_offset_callback_b (Side_position::aligned_side_proc, X_AXIS)
250       || me->has_offset_callback_b (Side_position::aligned_side_proc , X_AXIS))
251     return X_AXIS;
252
253   
254   return Y_AXIS;
255 }
256
257 void
258 Side_position::set_direction (Grob*me, Direction d)
259 {
260   me->set_grob_property ("direction", gh_int2scm (d));
261 }
262
263 void
264 Side_position::set_minimum_space (Grob*me, Real m)
265 {
266   me->set_grob_property ("minimum-space", gh_double2scm (m));
267 }
268
269 void
270 Side_position::set_padding (Grob*me, Real p)
271 {
272   me->set_grob_property ("padding", gh_double2scm (p));
273 }
274
275 bool
276 Side_position::has_interface (Grob*me) 
277 {
278   return me->has_interface (ly_symbol2scm ("side-position-interface"));
279 }
280
281 bool
282 Side_position::supported_b (Grob*me) 
283 {
284   SCM s = me->get_grob_property  ("side-support-elements"); 
285   return gh_pair_p(s);
286 }
287
288