]> git.donarmstrong.com Git - lilypond.git/blob - lily/align-interface.cc
lilypond-1.3.37
[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 (Dimension_cache const *c)
22 {
23   Axis ax = c->axis ();
24   Score_element * sc = c->element_l ()->parent_l (ax);
25
26   if (sc && sc->get_elt_property ("alignment-done") == SCM_UNDEFINED) 
27     {
28       Align_interface (sc).do_side_processing (ax);
29     }
30   return 0.0;
31 }
32
33 /*
34   Hairy function to put elements where they should be. Can be tweaked
35   from the outside by setting minimum-space and extra-space in its
36   children */
37 void
38 Align_interface::do_side_processing (Axis a)
39 {
40   elt_l_->set_elt_property ("alignment-done", SCM_BOOL_T);
41   
42   SCM d =   elt_l_->get_elt_property ("stacking-dir");
43   Direction stacking_dir = gh_number_p(d) ? to_dir (d) : CENTER;
44   if (!stacking_dir)
45     stacking_dir = DOWN;
46
47   
48   Array<Interval> dims;
49
50   Link_array<Score_element> elems;
51   Link_array<Score_element> all_elts
52     = Group_interface__extract_elements (  elt_l_, (Score_element*) 0, "elements");
53   for (int i=0; i < all_elts.size(); i++) 
54     {
55       Interval y = all_elts[i]->extent(a) + all_elts[i]->relative_coordinate (elt_l_, a);
56       if (!y.empty_b())
57         {
58           Score_element *e =dynamic_cast<Score_element*>(all_elts[i]);
59
60           // todo: fucks up if item both in Halign & Valign. 
61           SCM min_dims = e->remove_elt_property ("minimum-space");
62           if (gh_pair_p (min_dims) &&
63               gh_number_p (gh_car (min_dims))
64               && gh_number_p (gh_cdr (min_dims)))
65             {
66               y.unite (Interval (gh_scm2double (gh_car  (min_dims)),
67                                  gh_scm2double (gh_cdr (min_dims))));
68             }
69           
70           SCM extra_dims = e->remove_elt_property ("extra-space");
71           if (gh_pair_p (extra_dims) &&
72               gh_number_p (gh_car (extra_dims))
73               && gh_number_p (gh_cdr (extra_dims)))
74             {
75               y[LEFT] += gh_scm2double (gh_car  (extra_dims));
76               y[RIGHT] += gh_scm2double (gh_cdr (extra_dims));
77             }
78
79           elems.push (e);
80           dims.push (y);          
81         }
82     }
83
84   
85   Interval threshold = Interval (0, Interval::infinity ());
86   SCM thr = elt_l_->get_elt_property ("threshold");
87   if (gh_pair_p (thr))
88     {
89       threshold[SMALLER] = gh_scm2double (gh_car (thr));
90       threshold[BIGGER] = gh_scm2double (gh_cdr (thr));      
91     }
92
93   Real where_f=0;
94   for (int i=0 ;  i < elems.size(); i++) 
95     {
96       Real dy = - stacking_dir * dims[i][-stacking_dir];
97       if (i)
98         dy += stacking_dir * dims[i-1][stacking_dir];
99
100       if (i)
101         {
102           dy = (dy >? threshold[SMALLER] )
103             <? threshold[BIGGER];
104         }
105
106       where_f += stacking_dir * dy;
107       elems[i]->translate_axis (where_f, a);
108     }
109 }
110
111
112 Axis
113 Align_interface::axis ()const
114 {
115   return  Axis (gh_scm2int (gh_car (elt_l_->get_elt_property ("axes"))));
116 }
117
118
119 /*
120   should  use generic Scm funcs.
121  */
122 int
123 Align_interface::get_count (Score_element*s)const
124 {
125   SCM e = elt_l_->get_elt_property ("elements");
126   int c =0;
127   while (gh_pair_p (e))
128     {
129       if (gh_car (e) == s->self_scm_)
130         break;
131       c++;
132       e = gh_cdr (e);
133     }
134   return c;
135 }
136
137 void
138 Align_interface::add_element (Score_element* s)
139 {
140   s->add_offset_callback (alignment_callback, axis ());
141   Axis_group_interface (elt_l_).add_element (s);
142   
143 }
144
145 Align_interface::Align_interface (Score_element const*s)
146 {
147   elt_l_ = (Score_element*)s;
148 }
149
150 void
151 Align_interface::set_interface ()
152 {
153   Axis_group_interface (elt_l_).set_interface ();
154
155   Group_interface (elt_l_, "interfaces").add_thing (ly_symbol2scm ("Alignment"));
156 }
157
158 void
159 Align_interface::set_axis (Axis a)
160 {
161   Axis_group_interface (elt_l_).set_axes (a,a );
162 }
163
164 bool
165 Align_interface::has_interface_b ()
166 {
167   SCM memq = scm_memq (ly_symbol2scm ("Alignment"),
168               elt_l_->get_elt_property ("interfaces"));
169   
170   return (memq != SCM_BOOL_F);
171 }
172