]> git.donarmstrong.com Git - lilypond.git/blob - lily/align-interface.cc
release: 1.3.92
[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
12 #include "score-element.hh"
13 #include "group-interface.hh"
14 #include "axis-group-interface.hh"
15 #include "paper-def.hh"
16
17 /*
18   This callback is set in the children of the align element. It does
19   not compute anything, but a side effect of a->do_side_processing ()
20   is that the elements are placed correctly.  */
21 Real
22 Align_interface::alignment_callback (Score_element *sc, Axis ax)
23 {
24   Score_element * par = sc->parent_l (ax);
25   if (par && !to_boolean (par->get_elt_property ("alignment-done")))
26     {
27       Align_interface::do_side_processing (par, ax);
28     }
29   return 0.0;
30 }
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 (Score_element * me, Axis a)
39 {
40   me->set_elt_property ("alignment-done", SCM_BOOL_T);
41   
42   SCM d =   me->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     = Pointer_group_interface__extract_elements (  me, (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 (me, 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 = me->get_elt_property ("threshold");
87   if (gh_pair_p (thr))
88     {
89       Real ss = me->paper_l ()-> get_var ("staffspace");
90       threshold[SMALLER] = ss *gh_scm2double (gh_car (thr));
91       threshold[BIGGER] = ss * gh_scm2double (gh_cdr (thr));      
92     }
93
94   Real where_f=0;
95   for (int i=0 ;  i < elems.size(); i++) 
96     {
97       Real dy = - stacking_dir * dims[i][-stacking_dir];
98       if (i)
99         dy += stacking_dir * dims[i-1][stacking_dir];
100
101       if (i)
102         {
103           dy = (dy >? threshold[SMALLER] )
104             <? threshold[BIGGER];
105         }
106
107       where_f += stacking_dir * dy;
108       elems[i]->translate_axis (where_f, a);
109     }
110 }
111
112
113 Axis
114 Align_interface::axis (Score_element*me)
115 {
116   return  Axis (gh_scm2int (gh_car (me->get_elt_property ("axes"))));
117 }
118
119
120 /*
121   should  use generic Scm funcs.
122  */
123 int
124 Align_interface::get_count (Score_element*me,Score_element*s)
125 {
126   SCM e = me->get_elt_property ("elements");
127   int c =0;
128   while (gh_pair_p (e))
129     {
130       if (gh_car (e) == s->self_scm ())
131         break;
132       c++;
133       e = gh_cdr (e);
134     }
135   return c;
136 }
137
138 void
139 Align_interface::add_element (Score_element*me,Score_element* s)
140 {
141   s->add_offset_callback (alignment_callback, Align_interface::axis (me));
142   Axis_group_interface::add_element (me, s);
143 }
144
145
146 void
147 Align_interface::set_interface (Score_element*me)
148 {
149   me->set_interface (ly_symbol2scm ("align-interface"));
150
151   Axis_group_interface::set_interface (me);
152 }
153
154 void
155 Align_interface::set_axis (Score_element*me,Axis a)
156 {
157   Axis_group_interface::set_axes (me, a,a );
158 }
159
160 bool
161 Align_interface::has_interface (Score_element*me)
162 {
163   return me && me->has_interface (ly_symbol2scm ("align-interface"));
164 }
165