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