]> git.donarmstrong.com Git - lilypond.git/blob - lily/side-position-interface.cc
f19cbf83e38cf361ab2af6eb12818efb3c559b5d
[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--2000 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7   
8  */
9 #include <math.h>               // ceil.
10
11 #include "side-position-interface.hh"
12 #include "staff-symbol.hh"
13 #include "debug.hh"
14 #include "warn.hh"
15 #include "dimensions.hh"
16 #include "dimension-cache.hh"
17 #include "staff-symbol-referencer.hh"
18
19 Side_position_interface::Side_position_interface (Score_element const *e)
20 {
21   elt_l_ = (Score_element*)e;
22 }
23
24
25 void
26 Side_position_interface::add_support (Score_element*e)
27 {
28   SCM sup = elt_l_->get_elt_property ("side-support");
29   elt_l_->set_elt_property ("side-support",
30                             gh_cons (e->self_scm_,sup));
31 }
32
33
34
35 Direction
36 Side_position_interface::get_direction () const
37 {
38   SCM d = elt_l_->get_elt_property ("direction");
39   if (isdir_b (d))
40     return to_dir (d) ? to_dir (d) : DOWN;
41
42   Direction relative_dir = UP;
43   SCM reldir = elt_l_->get_elt_property ("side-relative-direction");    // should use a lambda.
44   if (isdir_b (reldir))
45     {
46       relative_dir = to_dir (reldir);
47     }
48   
49   SCM other_elt = elt_l_->get_elt_property ("direction-source");
50   Score_element * e = unsmob_element(other_elt);
51   if (e)
52     {
53       return (Direction)(relative_dir * Side_position_interface (e).get_direction ());
54     }
55   
56   return DOWN;
57 }
58   
59 /**
60    Callback that does the aligning.
61  */
62 Real
63 Side_position_interface::side_position (Dimension_cache const * c)
64 {
65   Score_element * me = dynamic_cast<Score_element*> (c->element_l ());
66
67   Interval dim;
68   Axis  axis = c->axis ();
69   Score_element *common = me->parent_l (axis);
70   SCM support = me->get_elt_property ("side-support");
71   for (SCM s = support; s != SCM_EOL; s = gh_cdr (s))
72     {
73       Score_element * e  = unsmob_element ( gh_car (s));
74       if (e)
75         common = common->common_refpoint (e, axis);
76     }
77   
78   for (SCM s = support; s != SCM_EOL; s = gh_cdr (s))
79     {
80
81       Score_element * e  = unsmob_element ( gh_car (s));
82       if (e)
83         {
84           Real coord = e->relative_coordinate (common, axis);
85
86           dim.unite (coord + e->extent (axis));
87         }
88     }
89
90   if (dim.empty_b ())
91     {
92       dim = Interval(0,0);
93     }
94
95   Real off =  me->parent_l (axis)->relative_coordinate (common, axis);
96
97
98   Direction dir = Side_position_interface (me).get_direction ();
99     
100   SCM pad = me->remove_elt_property ("padding");
101   if (gh_number_p (pad))
102     {
103       off += gh_scm2double (pad) * dir;
104     }
105   Real total_off = dim[dir] + off;
106
107   if (fabs (total_off) > 100 CM)
108     programming_error ("Huh ? Improbable staff side dim.");
109
110   return total_off;
111 }
112
113 Real
114 Side_position_interface::self_alignment (Dimension_cache const *c)
115 {
116   String s ("self-alignment-");
117   Axis ax = c->axis ();
118   s +=  (ax == X_AXIS) ? "X" : "Y";
119   Score_element *elm = dynamic_cast<Score_element*> (c->element_l ());
120   SCM align (elm->get_elt_property (s));
121   if (isdir_b (align))
122     {
123       Direction d = to_dir (align);
124       Interval ext(elm->extent (ax));
125       if (d)
126         {
127           return - ext[d];
128         }
129       return - ext.center ();
130     }
131   else
132     return 0.0;
133 }
134
135
136 Real
137 directed_round (Real f, Direction d)
138 {
139   if (d < 0)
140     return floor (f);
141   else
142     return ceil (f);
143 }
144
145 Real
146 Side_position_interface::quantised_position (Dimension_cache const *c)
147 {
148   Score_element * me = dynamic_cast<Score_element*> (c->element_l ());
149   Side_position_interface s(me);
150   Direction d = s.get_direction ();
151   Staff_symbol_referencer_interface si (me);
152
153   if (si.has_interface_b ())
154     {
155       Real p = si.position_f ();
156       Real rp = directed_round (p, d);
157
158       int ip = int  (rp);
159       if ((ip % 2) == 0)
160         {
161           ip += d;
162           rp += d;
163         }
164
165       return (rp - p) * si.staff_space () / 2.0;
166     }
167   return 0.0;
168 }
169
170 Real
171 Side_position_interface::aligned_side (Dimension_cache const *c)
172 {
173   Score_element * me = dynamic_cast<Score_element*> (c->element_l ());
174   Side_position_interface s(me);
175   Direction d = s.get_direction ();
176   Axis ax = c->axis ();
177   Real o = side_position (c);
178
179   Interval iv =  me->extent (ax);
180
181   if (!iv.empty_b ())
182     {
183       o += - iv[-d];
184
185       SCM pad = me->get_elt_property ("padding");
186       if (gh_number_p (pad))
187         o += d *gh_scm2double (pad) ; 
188     }
189   return o;
190 }
191
192
193
194
195 void
196 Side_position_interface::set_axis (Axis a)
197 {
198   // prop transparent ? 
199   if (elt_l_->get_elt_property ("side-support") == SCM_UNDEFINED)
200     elt_l_->set_elt_property ("side-support" ,SCM_EOL);
201
202   elt_l_->dim_cache_[a]->off_callbacks_.push (aligned_side);
203 }
204
205
206 void
207 Side_position_interface::set_quantised (Axis a)
208 {
209   Dimension_cache * c = elt_l_->dim_cache_[a];
210   
211   c->off_callbacks_.push (quantised_position);
212 }
213
214 Axis
215 Side_position_interface::get_axis () const
216 {
217   Dimension_cache * c =  elt_l_->dim_cache_[X_AXIS];
218   for (int i=0 ; i < c->off_callbacks_.size();i ++)
219     if (c->off_callbacks_[i] == side_position
220         ||c->off_callbacks_[i] == aligned_side)
221       return X_AXIS;
222
223   
224   return Y_AXIS;
225 }
226
227 void
228 Side_position_interface::set_direction (Direction d) 
229 {
230   elt_l_->set_elt_property ("direction", gh_int2scm (d));
231 }
232
233 bool
234 Side_position_interface::has_interface_b () const
235 {
236   return elt_l_->get_elt_property ("side-support") != SCM_UNDEFINED;
237 }
238
239 bool
240 Side_position_interface::supported_b () const
241 {
242   SCM s =elt_l_->get_elt_property  ("side-support"); 
243   return s != SCM_UNDEFINED && s != SCM_EOL;
244 }