lilypond-1.3.145
[lilypond.git] / lily / align-interface.cc
blobaf9d3763e67837d00cd59ca86bdb3e67440f09ae
1 /*
2 align-interface.cc -- implement Align_interface
4 source file of the GNU LilyPond music typesetter
6 (c) 2000--2001 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_elements_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_elements_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));
127 SCM extra_dims = e->remove_grob_property ("extra-space");
128 if (gh_pair_p (extra_dims) &&
129 gh_number_p (gh_car (extra_dims))
130 && gh_number_p (gh_cdr (extra_dims)))
132 y[LEFT] += gh_scm2double (gh_car (extra_dims));
133 y[RIGHT] += gh_scm2double (gh_cdr (extra_dims));
136 elems.push (e);
137 dims.push (y);
143 Read self-alignment-X and self-alignment-Y. This may seem like
144 code duplication. (and really: it is), but this is necessary to
145 prevent ugly cyclic dependencies that arise when you combine
146 self-alignment on a child with alignment of children.
149 String s ("self-alignment-");
151 s += (a == X_AXIS) ? "X" : "Y";
153 SCM align (me->get_grob_property (s.ch_C ()));
155 Array<Real> translates ;
156 Interval total;
157 Real where_f=0;
159 for (int j=0 ; j < elems.size (); j++)
161 Real dy = 0.0;
162 dy = - stacking_dir * dims[j][-stacking_dir];
163 if (j)
164 dy += stacking_dir * dims[j-1][stacking_dir];
166 if (j)
168 dy = (dy >? threshold[SMALLER])
169 <? threshold[BIGGER];
172 where_f += stacking_dir * dy;
173 total.unite (dims[j] + where_f);
174 translates.push (where_f);
179 Grob * align_center = unsmob_grob (align);
180 Real center_offset = 0.0;
183 also move the grobs that were empty, to maintain spatial order.
185 Array<Real> all_translates;
186 if (translates.size ())
188 int i =0;
189 int j =0;
190 Real w = translates[0];
191 while (j < all_grobs.size ())
193 if (i < elems.size () && all_grobs[j] == elems[i])
195 w = translates[i++];
197 if (all_grobs[j] == align_center)
198 center_offset = w;
199 all_translates .push (w);
200 j++;
203 if (isdir_b (align))
205 center_offset = total.linear_combination (gh_scm2double (align));
208 for (int j = 0 ; j < all_grobs.size (); j++)
209 all_grobs[j]->translate_axis (all_translates[j] - center_offset, a);
212 Axis
213 Align_interface::axis (Grob*me)
215 return Axis (gh_scm2int (gh_car (me->get_grob_property ("axes"))));
220 should use generic Scm funcs.
223 Align_interface::get_count (Grob*me,Grob*s)
225 SCM e = me->get_grob_property ("elements");
226 int c =0;
227 while (gh_pair_p (e))
229 if (gh_car (e) == s->self_scm ())
230 break;
231 c++;
232 e = gh_cdr (e);
234 return c;
237 void
238 Align_interface::add_element (Grob*me,Grob* s, SCM cb)
240 s->add_offset_callback (cb, Align_interface::axis (me));
241 Axis_group_interface::add_element (me, s);
245 void
246 Align_interface::set_interface (Grob*me)
248 me->set_interface (ly_symbol2scm ("align-interface"));
250 Axis_group_interface::set_interface (me);
253 void
254 Align_interface::set_axis (Grob*me,Axis a)
256 Axis_group_interface::set_axes (me, a,a);
259 bool
260 Align_interface::has_interface (Grob*me)
262 return me && me->has_interface (ly_symbol2scm ("align-interface"));