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