]> git.donarmstrong.com Git - lilypond.git/blob - lily/side-position-interface.cc
release: 1.5.24
[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 #include "directional-element-interface.hh"
18
19 void
20 Side_position_interface::add_support (Grob*me, Grob*e)
21 {
22   Pointer_group_interface::add_element (me, ly_symbol2scm ("side-support-elements"), e);
23 }
24
25
26
27 Direction
28 Side_position_interface::get_direction (Grob*me)
29 {
30   SCM d = me->get_grob_property ("direction");
31   if (isdir_b (d) && to_dir (d))
32     return to_dir (d);
33
34   Direction relative_dir = Direction (1);
35   SCM reldir = me->get_grob_property ("side-relative-direction");       // should use a lambda.
36   if (isdir_b (reldir))
37     {
38       relative_dir = to_dir (reldir);
39     }
40   
41   SCM other_elt = me->get_grob_property ("direction-source");
42   Grob * e = unsmob_grob (other_elt);
43   if (e)
44     {
45       return (Direction) (relative_dir * Directional_element_interface::get (e));
46     }
47   
48   return CENTER;
49 }
50   
51
52 MAKE_SCHEME_CALLBACK (Side_position_interface,aligned_on_support_extents, 2);
53 SCM
54 Side_position_interface::aligned_on_support_extents (SCM element_smob, SCM axis)
55 {
56   Grob *me = unsmob_grob (element_smob);
57   Axis a = (Axis) gh_scm2int (axis);
58
59   return general_side_position (me, a, true);
60 }
61
62
63 /*
64  Puts the element next to the support, optionally taking in
65  account the extent of the support.
66 */
67 SCM
68 Side_position_interface::general_side_position (Grob * me, Axis a, bool use_extents)
69 {
70   Grob *common = me->get_parent (a);
71
72   /*
73     As this is only used as a callback, this is called only once. We
74     could wipe SIDE-SUPPORT-ELEMENTS after we retrieve it to conserve
75     memory; however -- we should look more into benefits of such actions?
76
77     The benefit is small, it seems: total GC times taken don't
78     differ. Would this also hamper Generational GC ?
79     
80   */
81   SCM support = me->get_grob_property ("side-support-elements");
82     // me->remove_grob_property ("side-support-elements");
83   for (SCM s = support; s != SCM_EOL; s = ly_cdr (s))
84     {
85       Grob * e  = unsmob_grob (ly_car (s));
86       if (e)
87         common = common->common_refpoint (e, a);
88     }
89   
90   Interval dim;
91   for (SCM s = support; s != SCM_EOL; s = ly_cdr (s))
92     {
93       Grob * e  = unsmob_grob (ly_car (s));
94       if (e)
95         if (use_extents)
96           dim.unite (e->extent (common, a));
97         else
98           {
99             Real x = e->relative_coordinate (common, a);
100             dim.unite (Interval (x,x));
101           }
102     }
103
104   if (dim.empty_b ())
105     {
106       dim = Interval (0,0);
107     }
108
109   Direction dir = Side_position_interface::get_direction (me);
110     
111   Real off =  me->get_parent (a)->relative_coordinate (common, a);
112   SCM minimum = me->remove_grob_property ("minimum-space");
113
114   Real total_off = dim.linear_combination (dir) + off;
115   SCM padding = me->remove_grob_property ("padding");
116   if (gh_number_p (padding))
117     {
118       total_off += gh_scm2double (padding) * dir;
119     }
120
121   if (gh_number_p (minimum) 
122       && dir
123       && total_off * dir < gh_scm2double (minimum))
124     {
125       total_off = gh_scm2double (minimum) * dir;
126     }
127
128   if (fabs (total_off) > 100 CM)
129     programming_error ("Huh ? Improbable staff side dim.");
130
131   return gh_double2scm (total_off);
132 }
133
134 /*
135   Cut & paste (ugh.)
136  */
137 MAKE_SCHEME_CALLBACK (Side_position_interface,aligned_on_support_refpoints,2);
138 SCM
139 Side_position_interface::aligned_on_support_refpoints (SCM smob, SCM axis)
140 {
141   Grob *me = unsmob_grob (smob);
142   Axis a = (Axis) gh_scm2int (axis);
143
144   return  general_side_position (me, a, false); 
145 }
146
147
148 /**
149   callback that centers the element on itself
150
151   Requires that self-alignment-{X,Y} be set.
152  */
153 MAKE_SCHEME_CALLBACK (Side_position_interface,aligned_on_self,2);
154 SCM
155 Side_position_interface::aligned_on_self (SCM element_smob, SCM axis)
156 {
157   Grob *me = unsmob_grob (element_smob);
158   Axis a = (Axis) gh_scm2int (axis);
159   String s ("self-alignment-");
160
161   s += (a == X_AXIS) ? "X" : "Y";
162
163   SCM align (me->get_grob_property (s.ch_C ()));
164   if (gh_number_p (align))
165     {
166       Interval ext (me->extent (me,a));
167
168       if (ext.empty_b ())
169         {
170           programming_error ("I'm empty. Can't align on self");
171           return gh_double2scm (0.0);
172         }
173       else
174         {
175           return gh_double2scm (- ext.linear_combination (gh_scm2double (align)));
176         }
177     }
178   else if (unsmob_grob (align))
179     {
180       return gh_double2scm (- unsmob_grob (align)->relative_coordinate (me,  a));
181     }
182   return gh_double2scm (0.0);
183 }
184
185
186
187 Real
188 directed_round (Real f, Direction d)
189 {
190   if (d < 0)
191     return floor (f);
192   else
193     return ceil (f);
194 }
195
196 /*
197   Callback that quantises in staff-spaces, rounding in the direction
198   of the elements "direction" elt property.
199
200   Only rounds when we're inside the staff, as determined by
201   Staff_symbol_referencer::staff_radius () */
202 MAKE_SCHEME_CALLBACK (Side_position_interface,quantised_position,2);
203 SCM
204 Side_position_interface::quantised_position (SCM element_smob, SCM)
205 {
206   Grob *me = unsmob_grob (element_smob);
207   
208   
209   Direction d = Side_position_interface::get_direction (me);
210
211   if (Staff_symbol_referencer::has_interface (me))
212     {
213       Real p = Staff_symbol_referencer::position_f (me);
214       Real rp = directed_round (p, d);
215       Real rad = Staff_symbol_referencer::staff_radius (me) *2 ;
216       int ip = int (rp);
217
218       if (abs (ip) <= rad && Staff_symbol_referencer::on_staffline (me,ip))
219         {
220           ip += d;
221           rp += d;
222         }
223
224       return gh_double2scm ((rp - p) * Staff_symbol_referencer::staff_space (me) / 2.0);
225     }
226   return gh_double2scm (0.0);
227 }
228
229 /*
230   Position next to support, taking into account my own dimensions and padding.
231  */
232 MAKE_SCHEME_CALLBACK (Side_position_interface,aligned_side,2);
233 SCM
234 Side_position_interface::aligned_side (SCM element_smob, SCM axis)
235 {
236   Grob *me = unsmob_grob (element_smob);
237   Axis a = (Axis) gh_scm2int (axis);
238   
239   Direction d = Side_position_interface::get_direction (me);
240   
241   Real o = gh_scm2double (aligned_on_support_extents (element_smob,axis));
242
243   Interval iv =  me->extent (me, a);
244
245   if (!iv.empty_b ())
246     {
247       if (!d)
248         {
249           programming_error ("Direction unknown, but aligned-side wanted.");
250           d = DOWN;
251         }
252       o += - iv[-d];
253
254       SCM pad = me->get_grob_property ("padding");
255       if (gh_number_p (pad))
256         o += d *gh_scm2double (pad) ; 
257     }
258   return gh_double2scm (o);
259 }
260
261 /*
262   Position centered on parent.
263  */
264 MAKE_SCHEME_CALLBACK (Side_position_interface,centered_on_parent,2);
265 SCM
266 Side_position_interface::centered_on_parent (SCM element_smob, SCM axis)
267 {
268   Grob *me = unsmob_grob (element_smob);
269   Axis a = (Axis) gh_scm2int (axis);
270   Grob *him = me->get_parent (a);
271
272   return gh_double2scm (him->extent (him,a).center ());  
273 }
274
275
276 void
277 Side_position_interface::add_staff_support (Grob*me)
278 {
279   Grob* st = Staff_symbol_referencer::staff_symbol_l (me);
280   if (st && get_axis (me) == Y_AXIS)
281     {
282       add_support (me,st);
283     }
284 }
285
286 void
287 Side_position_interface::set_axis (Grob*me, Axis a)
288 {
289   me->add_offset_callback (Side_position_interface::aligned_side_proc, a);
290 }
291
292
293
294 // ugh. doesn't cactch all variants. 
295 Axis
296 Side_position_interface::get_axis (Grob*me)
297 {
298   if (me->has_offset_callback_b (Side_position_interface::aligned_side_proc, X_AXIS)
299       || me->has_offset_callback_b (Side_position_interface::aligned_side_proc , X_AXIS))
300     return X_AXIS;
301
302   
303   return Y_AXIS;
304 }
305
306 void
307 Side_position_interface::set_direction (Grob*me, Direction d)
308 {
309   me->set_grob_property ("direction", gh_int2scm (d));
310 }
311
312 void
313 Side_position_interface::set_minimum_space (Grob*me, Real m)
314 {
315   me->set_grob_property ("minimum-space", gh_double2scm (m));
316 }
317
318 void
319 Side_position_interface::set_padding (Grob*me, Real p)
320 {
321   me->set_grob_property ("padding", gh_double2scm (p));
322 }
323
324 bool
325 Side_position_interface::has_interface (Grob*me) 
326 {
327   return me->has_interface (ly_symbol2scm ("side-position-interface"));
328 }
329
330 bool
331 Side_position_interface::supported_b (Grob*me) 
332 {
333   SCM s = me->get_grob_property ("side-support-elements"); 
334   return gh_pair_p (s);
335 }
336
337