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