]> git.donarmstrong.com Git - lilypond.git/blob - lily/side-position-interface.cc
release: 1.3.134
[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_interface::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_interface::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_interface::get_direction (e));
45     }
46   
47   return CENTER;
48 }
49   
50
51 MAKE_SCHEME_CALLBACK(Side_position_interface,aligned_on_support_extents, 2);
52 SCM
53 Side_position_interface::aligned_on_support_extents (SCM element_smob, SCM axis)
54 {
55   Grob *me = unsmob_grob (element_smob);
56   Axis a = (Axis) gh_scm2int (axis);
57
58   return general_side_position (me, a, true);
59 }
60
61
62 /*
63  Puts the element next to the support, optionally taking in
64  account the extent of the support.
65 */
66 SCM
67 Side_position_interface::general_side_position (Grob * me, Axis a, bool use_extents)
68 {
69   Grob *common = me->parent_l (a);
70   SCM support = me->get_grob_property ("side-support-elements");
71   for (SCM s = support; s != SCM_EOL; s = gh_cdr (s))
72     {
73       Grob * e  = unsmob_grob (gh_car (s));
74       if (e)
75         common = common->common_refpoint (e, a);
76     }
77   
78   Interval dim;
79   for (SCM s = support; s != SCM_EOL; s = gh_cdr (s))
80     {
81       Grob * e  = unsmob_grob ( gh_car (s));
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.empty_b ())
93     {
94       dim = Interval(0,0);
95     }
96
97   Direction dir = Side_position_interface::get_direction (me);
98     
99   Real off =  me->parent_l (a)->relative_coordinate (common, a);
100   SCM minimum = me->remove_grob_property ("minimum-space");
101
102   Real total_off = dim.linear_combination (dir) + off;
103   SCM padding = me->remove_grob_property ("padding");
104   if (gh_number_p (padding))
105     {
106       total_off += gh_scm2double (padding) * dir;
107     }
108
109   if (gh_number_p (minimum) 
110       && dir
111       && total_off * dir < gh_scm2double (minimum))
112     {
113       total_off = gh_scm2double (minimum) * dir;
114     }
115
116   if (fabs (total_off) > 100 CM)
117     programming_error ("Huh ? Improbable staff side dim.");
118
119   return gh_double2scm (total_off);
120 }
121
122 /*
123   Cut & paste (ugh.)
124  */
125 MAKE_SCHEME_CALLBACK(Side_position_interface,aligned_on_support_refpoints,2);
126 SCM
127 Side_position_interface::aligned_on_support_refpoints (SCM smob, SCM axis)
128 {
129   Grob *me = unsmob_grob (smob);
130   Axis a = (Axis) gh_scm2int (axis);
131
132   return  general_side_position (me, a, false); 
133 }
134
135
136 /**
137   callback that centers the element on itself
138  */
139 MAKE_SCHEME_CALLBACK(Side_position_interface,aligned_on_self,2);
140 SCM
141 Side_position_interface::aligned_on_self (SCM element_smob, SCM axis)
142 {
143   Grob *me = unsmob_grob (element_smob);
144   Axis a = (Axis) gh_scm2int (axis);
145   String s ("self-alignment-");
146
147   s +=  (a == X_AXIS) ? "X" : "Y";
148
149   SCM align (me->get_grob_property (s.ch_C()));
150   if (gh_number_p (align))
151     {
152       Interval ext(me->extent (me,a));
153
154       if (ext.empty_b ())
155         {
156           programming_error ("I'm empty. Can't align on self");
157           return gh_double2scm (0.0);
158         }
159       else
160         {
161           return gh_double2scm (- ext.linear_combination (gh_scm2double (align)));
162         }
163     }
164   else if (unsmob_grob (align))
165     {
166       return gh_double2scm (- unsmob_grob (align)->relative_coordinate (me,  a));
167     }
168     return gh_double2scm (0.0);
169 }
170
171
172
173 Real
174 directed_round (Real f, Direction d)
175 {
176   if (d < 0)
177     return floor (f);
178   else
179     return ceil (f);
180 }
181
182 /*
183   Callback that quantises in staff-spaces, rounding in the direction
184   of the elements "direction" elt property.
185
186   Only rounds when we're inside the staff, as determined by
187   Staff_symbol_referencer::staff_radius() */
188 MAKE_SCHEME_CALLBACK(Side_position_interface,quantised_position,2);
189 SCM
190 Side_position_interface::quantised_position (SCM element_smob, SCM )
191 {
192   Grob *me = unsmob_grob (element_smob);
193   
194   
195   Direction d = Side_position_interface::get_direction (me);
196
197   if (Staff_symbol_referencer::has_interface (me))
198     {
199       Real p = Staff_symbol_referencer::position_f (me);
200       Real rp = directed_round (p, d);
201       Real rad = Staff_symbol_referencer::staff_radius (me) *2 ;
202       int ip = int  (rp);
203
204       if (abs (ip) <= rad && Staff_symbol_referencer::on_staffline (me,ip))
205         {
206           ip += d;
207           rp += d;
208         }
209
210       return gh_double2scm ((rp - p) * Staff_symbol_referencer::staff_space (me) / 2.0);
211     }
212   return gh_double2scm (0.0);
213 }
214
215 /*
216   Position next to support, taking into account my own dimensions and padding.
217  */
218 MAKE_SCHEME_CALLBACK(Side_position_interface,aligned_side,2);
219 SCM
220 Side_position_interface::aligned_side (SCM element_smob, SCM axis)
221 {
222   Grob *me = unsmob_grob (element_smob);
223   Axis a = (Axis) gh_scm2int (axis);
224   
225   Direction d = Side_position_interface::get_direction (me);
226   Real o = gh_scm2double (aligned_on_support_extents (element_smob,axis));
227
228   Interval iv =  me->extent (me, a);
229
230   if (!iv.empty_b ())
231     {
232       o += - iv[-d];
233
234       SCM pad = me->get_grob_property ("padding");
235       if (gh_number_p (pad))
236         o += d *gh_scm2double (pad) ; 
237     }
238   return gh_double2scm (o);
239 }
240
241 /*
242   Position centered on parent.
243  */
244 MAKE_SCHEME_CALLBACK(Side_position_interface,centered_on_parent,2);
245 SCM
246 Side_position_interface::centered_on_parent (SCM element_smob, SCM axis)
247 {
248   Grob *me = unsmob_grob (element_smob);
249   Axis a = (Axis) gh_scm2int (axis);
250   Grob *him = me->parent_l (a);
251
252   return gh_double2scm (him->extent (him,a).center ());  
253 }
254
255
256 void
257 Side_position_interface::add_staff_support (Grob*me)
258 {
259   Grob* st = Staff_symbol_referencer::staff_symbol_l (me);
260   if (st)
261     {
262       add_support (me,st);
263     }
264 }
265
266 void
267 Side_position_interface::set_axis (Grob*me, Axis a)
268 {
269   me->add_offset_callback (Side_position_interface::aligned_side_proc, a);
270 }
271
272
273
274 // ugh. doesn't cactch all variants. 
275 Axis
276 Side_position_interface::get_axis (Grob*me)
277 {
278   if (me->has_offset_callback_b (Side_position_interface::aligned_side_proc, X_AXIS)
279       || me->has_offset_callback_b (Side_position_interface::aligned_side_proc , X_AXIS))
280     return X_AXIS;
281
282   
283   return Y_AXIS;
284 }
285
286 void
287 Side_position_interface::set_direction (Grob*me, Direction d)
288 {
289   me->set_grob_property ("direction", gh_int2scm (d));
290 }
291
292 void
293 Side_position_interface::set_minimum_space (Grob*me, Real m)
294 {
295   me->set_grob_property ("minimum-space", gh_double2scm (m));
296 }
297
298 void
299 Side_position_interface::set_padding (Grob*me, Real p)
300 {
301   me->set_grob_property ("padding", gh_double2scm (p));
302 }
303
304 bool
305 Side_position_interface::has_interface (Grob*me) 
306 {
307   return me->has_interface (ly_symbol2scm ("side-position-interface"));
308 }
309
310 bool
311 Side_position_interface::supported_b (Grob*me) 
312 {
313   SCM s = me->get_grob_property  ("side-support-elements"); 
314   return gh_pair_p(s);
315 }
316
317