]> git.donarmstrong.com Git - lilypond.git/blob - lily/align-element.cc
release: 1.1.37
[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
15 struct Align_element_content {
16   Graphical_element * elem_l_;
17   int priority_i_;
18   
19   static int compare (Align_element_content const &h1, 
20                       Align_element_content const &h2) 
21     {
22       return h1.priority_i_ - h2.priority_i_;
23     }
24   Align_element_content (Graphical_element *elem_l, int p) 
25     {
26       priority_i_ = p;
27       elem_l_ = elem_l;
28     }
29   Align_element_content () {
30     elem_l_ = 0;
31     priority_i_ = 0;
32   }
33 };
34
35
36
37 void
38 Align_element::add_element (Score_element*el_l)
39 {
40   int p = elem_l_arr_.size ();
41   add_element_priority (el_l, p);
42 }
43
44 void
45 Align_element::add_element_priority (Score_element *el, int p)
46 {
47   assert (! contains_b (el));
48   Axis_group_element::add_element (el);
49   priority_i_hash_[el] = p;
50   add_dependency (el);
51 }
52
53 void
54 Align_element::do_substitute_element_pointer (Score_element*o,
55                                               Score_element*n)
56 {
57   Axis_group_element :: do_substitute_element_pointer (o,n);
58   if (o == center_l_)
59     {
60       center_l_ = n;
61     }
62   if (priority_i_hash_.elem_b (o))
63     {
64       priority_i_hash_[n] = priority_i_hash_[o];
65       /*
66         Huh? It seems the old pointers are still used.  Why?
67        */
68       // priority_i_hash_.remove (o);
69     }
70 }
71
72 void
73 Align_element::do_post_processing()
74 {
75   if (axis () == Y_AXIS)
76     do_side_processing ();
77 }
78
79 void
80 Align_element::do_pre_processing ()
81 {
82   if (axis () == X_AXIS)
83     do_side_processing ();
84 }
85
86 void
87 Align_element::do_side_processing ()
88 {
89   sort_elements ();
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 ());
96       if (!y.empty_b())
97         {
98           dims.push (y);
99           Score_element *e =dynamic_cast<Score_element*>(elem_l_arr_[i]);
100           elems.push (e);
101         }
102     }
103
104   Real where_f=0;
105   Real center_f = 0.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_interval_[SMALLER] )
115             <? threshold_interval_[BIGGER];
116         }
117
118       if (!i && align_dir_ == LEFT)
119         center_f = where_f;
120       else if (align_dir_ == CENTER && elems[i] == center_l_)
121         center_f = where_f;
122
123       where_f += stacking_dir_ * dy;
124       elems[i]->translate_axis (where_f, axis ());
125     }
126
127   if (dims.size ())
128     where_f += dims.top ()[stacking_dir_];
129   if (align_dir_ == RIGHT)
130     center_f = where_f;
131   else if (align_dir_ == CENTER && !center_l_)
132     center_f = where_f / 2;
133     
134   if (center_f)
135     translate_axis ( - center_f, axis ());
136
137   dim_cache_[axis ()].invalidate ();
138 }
139
140 Align_element::Align_element()
141 {
142   ordered_b_ = true;
143   threshold_interval_ = Interval (0, Interval::infinity ());
144   stacking_dir_ = DOWN;
145   align_dir_ = CENTER;
146   center_l_ =0;
147   priority_i_hash_.hash_func_ = pointer_hash;
148 }
149
150 Axis
151 Align_element::axis () const
152 {
153   return axes_[0];
154 }
155
156 void
157 Align_element::set_axis (Axis a)
158 {
159   set_axes (a,a);
160 }
161
162
163 bool
164 Align_element::contains_b (Score_element const *e) const
165 {
166   return elem_l_arr_.find_l (e);
167 }
168
169 void
170 Align_element::sort_elements ()
171 {
172   Array<Align_element_content> content;
173   for  (int i =0; i < elem_l_arr_.size(); i++)
174     {
175       Score_element * e = dynamic_cast<Score_element*> (elem_l_arr_[i]);
176       assert (priority_i_hash_.elem_b (e));
177       int p = priority_i_hash_[e];
178       content.push (Align_element_content (e, p));
179     }
180   content.sort (Align_element_content::compare);
181   
182   elem_l_arr_.clear();
183   priority_i_hash_.clear();
184
185   for  (int i =0; i < content.size(); i++) 
186     {
187       elem_l_arr_.push (content[i].elem_l_);
188     }
189 }
190
191 void
192 Align_element::do_print () const
193 {
194 #ifndef NPRINT
195   DOUT << "contains: ";
196   for (int i=0 ;  i < elem_l_arr_.size(); i++) 
197     DOUT << classname (elem_l_arr_[i]) << ", ";
198 #endif
199 }
200
201 Score_element*
202 Align_element::get_elt_by_priority (int p) const
203 {
204   for (Hash_table_iter<Score_element*, int>  i(priority_i_hash_); i.ok (); i++)
205     {
206       if (i.val () == p)
207         return i.key();
208     }
209   return 0;
210 }
211
212 int
213 Align_element::get_priority (Score_element* e) const
214 {
215   if ( priority_i_hash_.elem_b (e))
216     return priority_i_hash_[e];
217   else
218     return elem_l_arr_.find_i (e);
219 }