]> git.donarmstrong.com Git - lilypond.git/blob - lily/align-element.cc
release: 1.2.4
[lilypond.git] / lily / align-element.cc
1 /*
2   align-elem.cc -- implement Align_elem
3
4   source file of the GNU LilyPond music typesetter
5
6   (c)  1997--1999 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8
9 #include "align-element.hh"
10 #include "interval.hh"
11 #include "direction.hh"
12 #include "debug.hh"
13 #include "hash-table-iter.hh"
14 #include "dimension-cache.hh"
15
16 struct Align_element_content {
17   Graphical_element * elem_l_;
18   int priority_i_;
19   
20   static int compare (Align_element_content const &h1, 
21                       Align_element_content const &h2) 
22     {
23       return h1.priority_i_ - h2.priority_i_;
24     }
25   Align_element_content (Graphical_element *elem_l, int p) 
26     {
27       priority_i_ = p;
28       elem_l_ = elem_l;
29     }
30   Align_element_content () {
31     elem_l_ = 0;
32     priority_i_ = 0;
33   }
34 };
35
36
37
38 void
39 Align_element::add_element (Score_element*el_l)
40 {
41   int p = elem_l_arr_.size ();
42   add_element_priority (el_l, p);
43 }
44
45 void
46 Align_element::add_element_priority (Score_element *el, int p)
47 {
48   assert (! contains_b (el));
49   Axis_group_element::add_element (el);
50   priority_i_hash_[el] = p;
51   add_dependency (el);
52 }
53
54 void
55 Align_element::do_substitute_element_pointer (Score_element*o,
56                                               Score_element*n)
57 {
58   Axis_group_element :: do_substitute_element_pointer (o,n);
59   if (o == center_l_)
60     {
61       center_l_ = n;
62     }
63   if (priority_i_hash_.elem_b (o))
64     {
65       priority_i_hash_[n] = priority_i_hash_[o];
66       /*
67         Huh? It seems the old pointers are still used.  Why?
68        */
69       // priority_i_hash_.remove (o);
70     }
71 }
72
73 void
74 Align_element::do_post_processing()
75 {
76   if (axis () == Y_AXIS)
77     do_side_processing ();
78 }
79
80 void
81 Align_element::do_pre_processing ()
82 {
83   if (axis () == X_AXIS)
84     do_side_processing ();
85 }
86
87 void
88 Align_element::do_side_processing ()
89 {
90   Array<Interval> dims;
91
92   Link_array<Score_element> elems;
93   for (int i=0; i < elem_l_arr_.size(); i++) 
94     {
95       Interval y = elem_l_arr_[i]->extent(axis ()) + elem_l_arr_[i]->relative_coordinate (this, axis ());
96       if (!y.empty_b())
97         {
98           Score_element *e =dynamic_cast<Score_element*>(elem_l_arr_[i]);
99
100           // todo: fucks up if item both in Halign & Valign. 
101           SCM min_dims = e->remove_elt_property (minimum_space_scm_sym);
102           if (min_dims != SCM_BOOL_F)
103             {
104               min_dims = SCM_CDR (min_dims);
105               y.unite (Interval (gh_scm2double (SCM_CAR (min_dims)),
106                                  gh_scm2double (SCM_CDR (min_dims))));
107             }
108           
109           SCM extra_dims = e->remove_elt_property (extra_space_scm_sym);
110           if (extra_dims != SCM_BOOL_F)
111             {
112               extra_dims = SCM_CDR (extra_dims);
113               y[LEFT] += gh_scm2double (SCM_CAR (extra_dims));
114               y[RIGHT] += gh_scm2double (SCM_CDR (extra_dims));
115             }
116
117           elems.push (e);
118           dims.push (y);          
119         }
120     }
121
122   Real where_f=0;
123   Real center_f = 0.0;
124   for (int i=0 ;  i < elems.size(); i++) 
125     {
126       Real dy = - stacking_dir_ * dims[i][-stacking_dir_];
127       if (i)
128         dy += stacking_dir_ * dims[i-1][stacking_dir_];
129
130       if (i)
131         {
132           dy = (dy >? threshold_interval_[SMALLER] )
133             <? threshold_interval_[BIGGER];
134         }
135
136       if (!i && align_dir_ == LEFT)
137         center_f = where_f;
138       else if (align_dir_ == CENTER && elems[i] == center_l_)
139         center_f = where_f;
140
141       where_f += stacking_dir_ * dy;
142       elems[i]->translate_axis (where_f, axis ());
143     }
144
145   if (dims.size ())
146     where_f += dims.top ()[stacking_dir_];
147   if (align_dir_ == RIGHT)
148     center_f = where_f;
149   else if (align_dir_ == CENTER && !center_l_)
150     center_f = where_f / 2;
151     
152   if (center_f)
153     translate_axis ( - center_f, axis ());
154
155   dim_cache_[axis ()]->invalidate ();
156 }
157
158 Align_element::Align_element()
159 {
160   ordered_b_ = true;
161   threshold_interval_ = Interval (0, Interval::infinity ());
162   stacking_dir_ = DOWN;
163   align_dir_ = CENTER;
164   center_l_ =0;
165   priority_i_hash_.hash_func_ = pointer_hash;
166 }
167
168 Axis
169 Align_element::axis () const
170 {
171   return axes_[0];
172 }
173
174 void
175 Align_element::set_axis (Axis a)
176 {
177   set_axes (a,a);
178 }
179
180
181 bool
182 Align_element::contains_b (Score_element const *e) const
183 {
184   return elem_l_arr_.find_l (e);
185 }
186
187 void
188 Align_element::sort_elements ()
189 {
190   Array<Align_element_content> content;
191   for  (int i =0; i < elem_l_arr_.size(); i++)
192     {
193       Score_element * e = dynamic_cast<Score_element*> (elem_l_arr_[i]);
194       assert (priority_i_hash_.elem_b (e));
195       int p = priority_i_hash_[e];
196       content.push (Align_element_content (e, p));
197     }
198   content.sort (Align_element_content::compare);
199   
200   elem_l_arr_.clear();
201   priority_i_hash_.clear();
202
203   for  (int i =0; i < content.size(); i++) 
204     {
205       elem_l_arr_.push (content[i].elem_l_);
206     }
207 }
208
209 void
210 Align_element::do_print () const
211 {
212 #ifndef NPRINT
213   DOUT << "contains: ";
214   for (int i=0 ;  i < elem_l_arr_.size(); i++) 
215     DOUT << classname (elem_l_arr_[i]) << ", ";
216 #endif
217 }
218
219 Score_element*
220 Align_element::get_elt_by_priority (int p) const
221 {
222   for (Hash_table_iter<Score_element*, int>  i(priority_i_hash_); i.ok (); i++)
223     {
224       if (i.val () == p)
225         return i.key();
226     }
227   return 0;
228 }
229
230 int
231 Align_element::get_priority (Score_element const * e) const
232 {
233   Score_element * nonconst = (Score_element*) e;
234   if ( priority_i_hash_.elem_b (nonconst))
235     return priority_i_hash_[nonconst];
236   else
237     return elem_l_arr_.find_i (nonconst);
238 }