]> git.donarmstrong.com Git - lilypond.git/blob - lily/align-interface.cc
release: 1.3.54
[lilypond.git] / lily / align-interface.cc
1 /*   
2   align-interface.cc --  implement Align_interface
3   
4   source file of the GNU LilyPond music typesetter
5   
6   (c) 2000 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7   
8  */
9
10 #include "align-interface.hh"
11 #include "dimension-cache.hh"
12 #include "score-element.hh"
13 #include "group-interface.hh"
14 #include "axis-group-interface.hh"
15
16 /*
17   This callback is set in the children of the align element. It does
18   not compute anything, but a side effect of a->do_side_processing ()
19   is that the elements are placed correctly.  */
20 Real
21 Align_interface::alignment_callback (Score_element const *sc, Axis ax)
22 {
23   Score_element * par = sc->parent_l (ax);
24   if (par && par->get_elt_property ("alignment-done") == SCM_UNDEFINED) 
25     {
26       Align_interface (par).do_side_processing (ax);
27     }
28   return 0.0;
29 }
30
31
32 Real
33 Align_interface::center_on_element (Score_element const *me, Axis a)
34 {
35   Score_element *cent = unsmob_element (me->get_elt_property ("group-center-element"));
36
37   if (cent)
38     {
39       Real r = cent->relative_coordinate (me,  a);
40       return -r;
41     }
42   return 0;
43 }
44
45 /*
46   Hairy function to put elements where they should be. Can be tweaked
47   from the outside by setting minimum-space and extra-space in its
48   children */
49 void
50 Align_interface::do_side_processing (Axis a)
51 {
52   elt_l_->set_elt_property ("alignment-done", SCM_BOOL_T);
53   
54   SCM d =   elt_l_->get_elt_property ("stacking-dir");
55   Direction stacking_dir = gh_number_p(d) ? to_dir (d) : CENTER;
56   if (!stacking_dir)
57     stacking_dir = DOWN;
58
59   
60   Array<Interval> dims;
61
62   Link_array<Score_element> elems;
63   Link_array<Score_element> all_elts
64     = Group_interface__extract_elements (  elt_l_, (Score_element*) 0, "elements");
65   for (int i=0; i < all_elts.size(); i++) 
66     {
67       Interval y = all_elts[i]->extent(a) + all_elts[i]->relative_coordinate (elt_l_, a);
68       if (!y.empty_b())
69         {
70           Score_element *e =dynamic_cast<Score_element*>(all_elts[i]);
71
72           // todo: fucks up if item both in Halign & Valign. 
73           SCM min_dims = e->remove_elt_property ("minimum-space");
74           if (gh_pair_p (min_dims) &&
75               gh_number_p (gh_car (min_dims))
76               && gh_number_p (gh_cdr (min_dims)))
77             {
78               y.unite (Interval (gh_scm2double (gh_car  (min_dims)),
79                                  gh_scm2double (gh_cdr (min_dims))));
80             }
81           
82           SCM extra_dims = e->remove_elt_property ("extra-space");
83           if (gh_pair_p (extra_dims) &&
84               gh_number_p (gh_car (extra_dims))
85               && gh_number_p (gh_cdr (extra_dims)))
86             {
87               y[LEFT] += gh_scm2double (gh_car  (extra_dims));
88               y[RIGHT] += gh_scm2double (gh_cdr (extra_dims));
89             }
90
91           elems.push (e);
92           dims.push (y);          
93         }
94     }
95
96   
97   Interval threshold = Interval (0, Interval::infinity ());
98   SCM thr = elt_l_->get_elt_property ("threshold");
99   if (gh_pair_p (thr))
100     {
101       threshold[SMALLER] = gh_scm2double (gh_car (thr));
102       threshold[BIGGER] = gh_scm2double (gh_cdr (thr));      
103     }
104
105   Real where_f=0;
106   for (int i=0 ;  i < elems.size(); i++) 
107     {
108       Real dy = - stacking_dir * dims[i][-stacking_dir];
109       if (i)
110         dy += stacking_dir * dims[i-1][stacking_dir];
111
112       if (i)
113         {
114           dy = (dy >? threshold[SMALLER] )
115             <? threshold[BIGGER];
116         }
117
118       where_f += stacking_dir * dy;
119       elems[i]->translate_axis (where_f, a);
120     }
121 }
122
123
124 Axis
125 Align_interface::axis ()const
126 {
127   return  Axis (gh_scm2int (gh_car (elt_l_->get_elt_property ("axes"))));
128 }
129
130
131 /*
132   should  use generic Scm funcs.
133  */
134 int
135 Align_interface::get_count (Score_element*s)const
136 {
137   SCM e = elt_l_->get_elt_property ("elements");
138   int c =0;
139   while (gh_pair_p (e))
140     {
141       if (gh_car (e) == s->self_scm_)
142         break;
143       c++;
144       e = gh_cdr (e);
145     }
146   return c;
147 }
148
149 void
150 Align_interface::add_element (Score_element* s)
151 {
152   s->add_offset_callback (alignment_callback, axis ());
153   Axis_group_interface (elt_l_).add_element (s);
154 }
155
156 Align_interface::Align_interface (Score_element const*s)
157 {
158   elt_l_ = (Score_element*)s;
159 }
160
161 void
162 Align_interface::set_interface ()
163 {
164   Axis_group_interface (elt_l_).set_interface ();
165
166   Group_interface (elt_l_, "interfaces").add_thing (ly_symbol2scm ("Alignment"));
167 }
168
169 void
170 Align_interface::set_axis (Axis a)
171 {
172   Axis_group_interface (elt_l_).set_axes (a,a );
173 }
174
175 bool
176 Align_interface::has_interface_b ()
177 {
178   SCM ifs  = elt_l_->get_elt_property ("interfaces");
179   if (!gh_pair_p (ifs))
180     return false;
181   
182   return scm_memq (ly_symbol2scm ("Alignment"), ifs) != SCM_BOOL_F;
183 }
184