lilypond-1.3.118
[lilypond.git] / lily / align-interface.cc
blobda303bd33e2b2233a8205933ad53cf2896cf0fae
1 /*
2 align-interface.cc -- implement Align_interface
4 source file of the GNU LilyPond music typesetter
6 (c) 2000 Han-Wen Nienhuys <hanwen@cs.uu.nl>
8 */
10 #include "align-interface.hh"
11 #include "grob.hh"
12 #include "group-interface.hh"
13 #include "axis-group-interface.hh"
14 #include "paper-def.hh"
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)
24 Grob * me = unsmob_grob (element_smob);
25 Axis ax = (Axis )gh_scm2int (axis);
26 Grob * par = me->parent_l (ax);
27 if (par && !to_boolean (par->get_grob_property ("alignment-done")))
29 Align_interface::align_to_extents (par, ax);
31 return gh_double2scm (0.0);
34 MAKE_SCHEME_CALLBACK(Align_interface,fixed_distance_alignment_callback,2);
35 SCM
36 Align_interface::fixed_distance_alignment_callback (SCM element_smob, SCM axis)
38 Grob * me = unsmob_grob (element_smob);
39 Axis ax = (Axis )gh_scm2int (axis);
40 Grob * par = me->parent_l (ax);
41 if (par && !to_boolean (par->get_grob_property ("alignment-done")))
43 Align_interface::align_to_fixed_distance (par, ax);
45 return gh_double2scm (0.0);
48 void
49 Align_interface::align_to_fixed_distance (Grob *me , Axis a)
51 me->set_grob_property ("alignment-done", SCM_BOOL_T);
53 SCM d = me->get_grob_property ("stacking-dir");
56 Direction stacking_dir = gh_number_p(d) ? to_dir (d) : CENTER;
57 if (!stacking_dir)
58 stacking_dir = DOWN;
61 SCM force = me->get_grob_property ("forced-distance");
63 Real dy = 0.0;
64 if (gh_number_p (force))
66 dy = gh_scm2double (force);
69 Link_array<Grob> elems
70 = Pointer_group_interface__extract_elements ( me, (Grob*) 0, "elements");
71 Real where_f=0;
72 for (int j=0 ; j < elems.size(); j++)
74 where_f += stacking_dir * dy;
75 elems[j]->translate_axis (where_f, a);
80 Hairy function to put elements where they should be. Can be tweaked
81 from the outside by setting minimum-space and extra-space in its
82 children */
83 void
84 Align_interface::align_to_extents (Grob * me, Axis a)
86 me->set_grob_property ("alignment-done", SCM_BOOL_T);
88 SCM d = me->get_grob_property ("stacking-dir");
91 Direction stacking_dir = gh_number_p(d) ? to_dir (d) : CENTER;
92 if (!stacking_dir)
93 stacking_dir = DOWN;
97 Interval threshold = Interval (0, Interval::infinity ());
98 SCM thr = me->get_grob_property ("threshold");
99 if (gh_pair_p (thr))
101 threshold[SMALLER] = gh_scm2double (gh_car (thr));
102 threshold[BIGGER] = gh_scm2double (gh_cdr (thr));
106 Array<Interval> dims;
108 Link_array<Grob> elems;
109 Link_array<Grob> all_grobs
110 = Pointer_group_interface__extract_elements ( me, (Grob*) 0, "elements");
111 for (int i=0; i < all_grobs.size(); i++)
113 Interval y = all_grobs[i]->extent(me, a);
114 if (!y.empty_b())
116 Grob *e =dynamic_cast<Grob*>(all_grobs[i]);
118 // todo: fucks up if item both in Halign & Valign.
119 SCM min_dims = e->remove_grob_property ("minimum-space");
120 if (gh_pair_p (min_dims) &&
121 gh_number_p (gh_car (min_dims))
122 && gh_number_p (gh_cdr (min_dims)))
124 y.unite (ly_scm2interval (min_dims));
128 SCM extra_dims = e->remove_grob_property ("extra-space");
129 if (gh_pair_p (extra_dims) &&
130 gh_number_p (gh_car (extra_dims))
131 && gh_number_p (gh_cdr (extra_dims)))
133 y[LEFT] += gh_scm2double (gh_car (extra_dims));
134 y[RIGHT] += gh_scm2double (gh_cdr (extra_dims));
137 elems.push (e);
138 dims.push (y);
143 Real where_f=0;
144 Array<Real> translates ;
145 for (int j=0 ; j < elems.size(); j++)
147 Real dy = 0.0;
148 dy = - stacking_dir * dims[j][-stacking_dir];
149 if (j)
150 dy += stacking_dir * dims[j-1][stacking_dir];
152 if (j)
154 dy = (dy >? threshold[SMALLER] )
155 <? threshold[BIGGER];
158 where_f += stacking_dir * dy;
159 translates.push (where_f);
163 also move the grobs that were empty, to maintain spatial order.
165 if (translates.size ())
167 int i =0;
168 int j =0;
169 Real w = translates[0];
170 while (j < all_grobs.size ())
172 if (i < elems.size () && all_grobs[j] == elems[i])
174 w = translates[i++];
176 all_grobs[j]->translate_axis (w, a);
178 j++;
183 Axis
184 Align_interface::axis (Grob*me)
186 return Axis (gh_scm2int (gh_car (me->get_grob_property ("axes"))));
191 should use generic Scm funcs.
194 Align_interface::get_count (Grob*me,Grob*s)
196 SCM e = me->get_grob_property ("elements");
197 int c =0;
198 while (gh_pair_p (e))
200 if (gh_car (e) == s->self_scm ())
201 break;
202 c++;
203 e = gh_cdr (e);
205 return c;
208 void
209 Align_interface::add_element (Grob*me,Grob* s, SCM cb)
211 s->add_offset_callback (cb, Align_interface::axis (me));
212 Axis_group_interface::add_element (me, s);
216 void
217 Align_interface::set_interface (Grob*me)
219 me->set_interface (ly_symbol2scm ("align-interface"));
221 Axis_group_interface::set_interface (me);
224 void
225 Align_interface::set_axis (Grob*me,Axis a)
227 Axis_group_interface::set_axes (me, a,a );
230 bool
231 Align_interface::has_interface (Grob*me)
233 return me && me->has_interface (ly_symbol2scm ("align-interface"));