]> git.donarmstrong.com Git - lilypond.git/blob - lily/align-element.cc
release: 1.3.10
[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 void
17 Align_element::do_post_processing()
18 {
19   if (axis () == Y_AXIS)
20     do_side_processing ();
21 }
22
23 void
24 Align_element::do_pre_processing ()
25 {
26   if (axis () == X_AXIS)
27     do_side_processing ();
28 }
29
30 void
31 Align_element::do_side_processing ()
32 {
33   Array<Interval> dims;
34
35   Link_array<Score_element> elems;
36   Link_array<Score_element> all_elts (elem_l_arr ());
37   for (int i=0; i < elem_l_arr ().size(); i++) 
38     {
39       Interval y = all_elts[i]->extent(axis ()) + all_elts[i]->relative_coordinate (this, axis ());
40       if (!y.empty_b())
41         {
42           Score_element *e =dynamic_cast<Score_element*>(all_elts[i]);
43
44           // todo: fucks up if item both in Halign & Valign. 
45           SCM min_dims = e->remove_elt_property ("minimum-space");
46           if (min_dims != SCM_UNDEFINED)
47             {
48               y.unite (Interval (gh_scm2double (SCM_CAR (min_dims)),
49                                  gh_scm2double (SCM_CDR (min_dims))));
50             }
51           
52           SCM extra_dims = e->remove_elt_property ("extra-space");
53           if (extra_dims != SCM_UNDEFINED)
54             {
55               y[LEFT] += gh_scm2double (SCM_CAR (extra_dims));
56               y[RIGHT] += gh_scm2double (SCM_CDR (extra_dims));
57             }
58
59           elems.push (e);
60           dims.push (y);          
61         }
62     }
63
64   Real where_f=0;
65   Real center_f = 0.0;
66   SCM scenter = get_elt_property ("center-element");
67   Score_element *center_elt = unsmob_element (scenter);
68   
69   for (int i=0 ;  i < elems.size(); i++) 
70     {
71       Real dy = - stacking_dir_ * dims[i][-stacking_dir_];
72       if (i)
73         dy += stacking_dir_ * dims[i-1][stacking_dir_];
74
75       if (i)
76         {
77           dy = (dy >? threshold_interval_[SMALLER] )
78             <? threshold_interval_[BIGGER];
79         }
80
81       if (!i && align_dir_ == LEFT)
82         center_f = where_f;
83       else if (align_dir_ == CENTER && elems[i] == center_elt)
84         center_f = where_f;
85
86       where_f += stacking_dir_ * dy;
87       elems[i]->translate_axis (where_f, axis ());
88     }
89
90   if (dims.size ())
91     where_f += dims.top ()[stacking_dir_];
92   if (align_dir_ == RIGHT)
93     center_f = where_f;
94   else if (align_dir_ == CENTER && !center_elt)
95     center_f = where_f / 2;
96     
97   if (center_f)
98     translate_axis ( - center_f, axis ());
99
100   dim_cache_[axis ()]->invalidate ();
101 }
102
103 Align_element::Align_element()
104 {
105   ordered_b_ = true;
106   threshold_interval_ = Interval (0, Interval::infinity ());
107   stacking_dir_ = DOWN;
108   align_dir_ = CENTER;
109 }
110
111 int
112 Align_element::get_count (Score_element*s)const
113 {
114   SCM e = get_elt_property ("elements");
115   int c =0;
116   while (gh_pair_p (e))
117     {
118       if (gh_car (e) == s->self_scm_)
119         break;
120       c++;
121       e = gh_cdr (e);
122     }
123   return c;
124 }
125
126 Axis
127 Align_element::axis () const
128 {
129   return axes_[0];
130 }
131
132 void
133 Align_element::set_axis (Axis a)
134 {
135   set_axes (a,a);
136 }
137
138
139
140
141
142