]> git.donarmstrong.com Git - lilypond.git/blob - lily/break-align-item.cc
release: 1.3.132
[lilypond.git] / lily / break-align-item.cc
1 /*
2   break-align-item.cc -- implement Break_align_interface
3
4   source file of the GNU LilyPond music typesetter
5
6   (c)  1997--2001 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8
9
10 #include <math.h>
11 #include <libc-extension.hh>
12
13 #include "side-position-interface.hh"
14 #include "axis-group-interface.hh"
15 #include "warn.hh"
16 #include "lily-guile.hh"
17 #include "break-align-item.hh"
18 #include "dimensions.hh"
19 #include "paper-def.hh"
20 #include "paper-column.hh"
21 #include "group-interface.hh"
22 #include "align-interface.hh"
23
24 MAKE_SCHEME_CALLBACK(Break_align_interface,before_line_breaking,1);
25
26 SCM
27 Break_align_interface::before_line_breaking (SCM smob)
28 {
29   Grob* me = unsmob_grob (smob);
30   do_alignment (me);
31   return SCM_UNSPECIFIED;
32 }
33 MAKE_SCHEME_CALLBACK(Break_align_interface,alignment_callback,2);
34
35 SCM
36 Break_align_interface::alignment_callback (SCM element_smob, SCM axis)
37 {
38   Grob *me = unsmob_grob (element_smob);
39   Axis a = (Axis) gh_scm2int (axis);
40
41   assert (a == X_AXIS);
42   Grob *par = me->parent_l (a);
43   if (par && !to_boolean (par->get_grob_property ("break-alignment-done")))\
44     {
45       par->set_grob_property ("break-alignment-done", SCM_BOOL_T);
46       Break_align_interface::do_alignment (par);
47     }
48     
49   return gh_double2scm (0);
50 }
51
52 MAKE_SCHEME_CALLBACK(Break_align_interface,self_align_callback,2);
53 SCM
54 Break_align_interface::self_align_callback (SCM element_smob, SCM axis)
55 {
56   Grob *me = unsmob_grob (element_smob);
57   Axis a = (Axis) gh_scm2int (axis);
58   assert (a == X_AXIS);
59   
60   Item* item = dynamic_cast<Item*> (me);
61   Direction bsd = item->break_status_dir();
62   if (bsd == LEFT)
63     {
64       me->set_grob_property ("self-alignment-X", gh_int2scm (RIGHT));
65     }
66
67   /*
68     Force break alignment itself to be done first, in the case
69    */
70   
71   
72   return Side_position::aligned_on_self (element_smob, axis);  
73 }
74
75 void
76 Break_align_interface::add_element (Grob*me, Grob *toadd)
77 {
78   Axis_group_interface::add_element (me, toadd);
79 }
80
81 void
82 Break_align_interface::do_alignment (Grob *me)
83 {
84   Item * item = dynamic_cast<Item*> (me);
85   Item *column = item->column_l ();
86
87   Link_array<Grob> elems;
88   Link_array<Grob> all_elems
89     = Pointer_group_interface__extract_elements (me, (Grob*)0,
90                                                  "elements");
91   
92   for (int i=0; i < all_elems.size(); i++) 
93     {
94       Interval y = all_elems[i]->extent(all_elems[i], X_AXIS);
95       if (!y.empty_b())
96         elems.push (dynamic_cast<Grob*> (all_elems[i]));
97     }
98   
99   if (!elems.size ())
100     return;
101
102   SCM symbol_list = SCM_EOL;
103   Array<Real> dists;
104   SCM current_origin = ly_symbol2scm ("none");
105   for (int i=0; i <= elems.size (); i++)
106     {
107       Grob *next_elt  = i < elems.size ()
108         ? elems[i]
109         : 0 ;
110       
111       SCM next_origin;
112
113       if (next_elt)
114         {
115           next_origin = next_elt->get_grob_property ("break-align-symbol");
116           next_origin =
117             gh_symbol_p (next_origin)? 
118             next_origin : ly_symbol2scm ("none")
119 ;
120         }
121       else
122         next_origin = ly_symbol2scm ("begin-of-note");
123
124       SCM alist = me->get_grob_property ("space-alist");
125       SCM e = scm_assoc (scm_listify (current_origin,
126                                       next_origin,
127                                       SCM_UNDEFINED), alist);
128           
129       SCM extra_space;
130       if (e != SCM_BOOL_F)
131         {
132           extra_space = gh_cdr (e);
133         }
134       else
135         {
136           warning (_f ("unknown spacing pair `%s', `%s'",
137                        ly_symbol2string (current_origin),
138                        ly_symbol2string (next_origin)));
139           extra_space = scm_listify (ly_symbol2scm ("minimum-space"), gh_double2scm (0.0), SCM_UNDEFINED);
140         }
141
142       SCM symbol = gh_car  (extra_space);
143       Real spc = gh_scm2double (gh_cadr(extra_space));
144
145       dists.push(spc);
146       symbol_list = gh_cons (symbol, symbol_list);
147       current_origin = next_origin;
148     }
149
150
151   // skip the first sym.
152   symbol_list  = gh_cdr (scm_reverse (symbol_list));
153   for (int i=0; i <elems.size()-1; i++)
154     {
155       elems[i]->set_grob_property (gh_car  (symbol_list),
156                                   scm_cons (gh_double2scm (0),
157                                             gh_double2scm (dists[i+1])));
158
159       symbol_list = gh_cdr (symbol_list);
160     }
161
162
163   // urg
164   SCM first_pair = elems[0]->get_grob_property ("minimum-space");
165   if (gh_pair_p (first_pair))
166     first_pair = first_pair;
167   else
168     first_pair = gh_cons (gh_double2scm (0.0), gh_double2scm (0.0));
169   
170   scm_set_car_x (first_pair, gh_double2scm (-dists[0]));
171   elems[0]->set_grob_property ("minimum-space", first_pair);
172
173
174   /*
175     Force callbacks for alignment to be called   
176   */
177   Align_interface::align_elements_to_extents (me, X_AXIS);
178
179   Real pre_space = elems[0]->relative_coordinate (column, X_AXIS);
180
181   Real xl = elems[0]->extent (elems[0],X_AXIS)[LEFT];
182   if (!isinf (xl))
183     pre_space += xl;
184   else
185     programming_error ("Infinity reached. ");
186
187   Real xr = elems.top ()->extent (elems.top (), X_AXIS)[RIGHT];
188   Real spring_len = elems.top ()->relative_coordinate (column, X_AXIS);
189   if (!isinf (xr))
190     spring_len += xr;
191   else
192     programming_error ("Infinity reached.");
193   
194   Real stretch_distance =0.;
195   
196   if (gh_car  (symbol_list) == ly_symbol2scm ("extra-space"))
197     {
198       spring_len += dists.top ();
199       stretch_distance = dists.top ();
200     }
201   else if (gh_car  (symbol_list) == ly_symbol2scm ("minimum-space"))
202     {
203       spring_len = spring_len >? dists.top ();
204       stretch_distance = spring_len;
205     }
206
207   
208   /*
209     Hint the spacing engine how much space to put in.
210
211     The pairs are in the format of an interval (ie. CAR <  CDR).
212   */
213   /*
214     UGH UGH UGH
215
216     This is a side effect, and there is no guarantee that this info is
217     computed at a "sane" moment.
218
219     (just spent some time tracking a bug that was caused by this info
220     being written halfway:
221
222     self_alignment_callback (*)
223     -> child->relative_coordinate (self)
224     -> break_alignment
225     -> child->relative_coordinate (column)
226
227     the last call incorporates the value that should've been computed
228     in (*), but--of course-- is not yet.
229
230     The result is that an offsets of align_elements_to_extents () are
231     not compensated for, and spring_len is completely off.
232
233     
234   */
235   column->set_grob_property ("extra-space",
236                             scm_cons (gh_double2scm (pre_space),
237                                       gh_double2scm (spring_len)));
238
239   column->set_grob_property ("stretch-distance",
240                             gh_cons (gh_double2scm (-dists[0]),
241                                      gh_double2scm (stretch_distance)));
242 }
243
244
245 void
246 Break_align_interface::set_interface (Grob*me)
247 {
248   Align_interface::set_interface (me); 
249   Align_interface::set_axis (me,X_AXIS);
250 }