]> git.donarmstrong.com Git - lilypond.git/blob - lily/break-align-item.cc
release: 1.3.131
[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   return Side_position::aligned_on_self (element_smob, axis);  
68 }
69
70 void
71 Break_align_interface::add_element (Grob*me, Grob *toadd)
72 {
73   Axis_group_interface::add_element (me, toadd);
74 }
75
76 void
77 Break_align_interface::do_alignment (Grob *me)
78 {
79   Item * item = dynamic_cast<Item*> (me);
80   Item *column = item->column_l ();
81
82   Link_array<Grob> elems;
83   Link_array<Grob> all_elems
84     = Pointer_group_interface__extract_elements (me, (Grob*)0,
85                                                  "elements");
86   
87   for (int i=0; i < all_elems.size(); i++) 
88     {
89       Interval y = all_elems[i]->extent(all_elems[i], X_AXIS);
90       if (!y.empty_b())
91         elems.push (dynamic_cast<Grob*> (all_elems[i]));
92     }
93   
94   if (!elems.size ())
95     return;
96
97   SCM symbol_list = SCM_EOL;
98   Array<Real> dists;
99   SCM current_origin = ly_symbol2scm ("none");
100   for (int i=0; i <= elems.size (); i++)
101     {
102       Grob *next_elt  = i < elems.size ()
103         ? elems[i]
104         : 0 ;
105       
106       SCM next_origin;
107
108       if (next_elt)
109         {
110           next_origin = next_elt->get_grob_property ("break-align-symbol");
111           next_origin =
112             gh_symbol_p (next_origin)? 
113             next_origin : ly_symbol2scm ("none")
114 ;
115         }
116       else
117         next_origin = ly_symbol2scm ("begin-of-note");
118
119       SCM alist = me->get_grob_property ("space-alist");
120       SCM e = scm_assoc (scm_listify (current_origin,
121                                       next_origin,
122                                       SCM_UNDEFINED), alist);
123           
124       SCM extra_space;
125       if (e != SCM_BOOL_F)
126         {
127           extra_space = gh_cdr (e);
128         }
129       else
130         {
131           warning (_f ("unknown spacing pair `%s', `%s'",
132                        ly_symbol2string (current_origin),
133                        ly_symbol2string (next_origin)));
134           extra_space = scm_listify (ly_symbol2scm ("minimum-space"), gh_double2scm (0.0), SCM_UNDEFINED);
135         }
136
137       SCM symbol = gh_car  (extra_space);
138       Real spc = gh_scm2double (gh_cadr(extra_space));
139
140       dists.push(spc);
141       symbol_list = gh_cons (symbol, symbol_list);
142       current_origin = next_origin;
143     }
144
145
146   // skip the first sym.
147   symbol_list  = gh_cdr (scm_reverse (symbol_list));
148   for (int i=0; i <elems.size()-1; i++)
149     {
150       elems[i]->set_grob_property (gh_car  (symbol_list),
151                                   scm_cons (gh_double2scm (0),
152                                             gh_double2scm (dists[i+1])));
153
154       symbol_list = gh_cdr (symbol_list);
155     }
156
157
158   // urg
159   SCM first_pair = elems[0]->get_grob_property ("minimum-space");
160   if (gh_pair_p (first_pair))
161     first_pair = first_pair;
162   else
163     first_pair = gh_cons (gh_double2scm (0.0), gh_double2scm (0.0));
164   
165   scm_set_car_x (first_pair, gh_double2scm (-dists[0]));
166   elems[0]->set_grob_property ("minimum-space", first_pair);
167
168
169   /*
170     Force callbacks for alignment to be called   
171   */
172   Align_interface::align_to_extents (me, X_AXIS);
173
174   Real pre_space = elems[0]->relative_coordinate (column, X_AXIS);
175
176   Real xl = elems[0]->extent (elems[0],X_AXIS)[LEFT];
177   if (!isinf (xl))
178     pre_space += xl;
179   else
180     programming_error ("Infinity reached. ");
181
182   Real xr = elems.top ()->extent (elems.top (), X_AXIS)[RIGHT];
183   Real spring_len = elems.top ()->relative_coordinate (column, X_AXIS);
184   if (!isinf (xr))
185     spring_len += xr;
186   else
187     programming_error ("Infinity reached.");
188   
189   Real stretch_distance =0.;
190   
191   if (gh_car  (symbol_list) == ly_symbol2scm ("extra-space"))
192     {
193       spring_len += dists.top ();
194       stretch_distance = dists.top ();
195     }
196   else if (gh_car  (symbol_list) == ly_symbol2scm ("minimum-space"))
197     {
198       spring_len = spring_len >? dists.top ();
199       stretch_distance = spring_len;
200     }
201
202   /*
203     Hint the spacing engine how much space to put in.
204
205     The pairs are in the format of an interval (ie. CAR <  CDR).
206   */
207   column->set_grob_property ("extra-space",
208                             scm_cons (gh_double2scm (pre_space),
209                                       gh_double2scm (spring_len)));
210
211   column->set_grob_property ("stretch-distance",
212                             gh_cons (gh_double2scm (-dists[0]),
213                                      gh_double2scm (stretch_distance)));
214 }
215
216
217 void
218 Break_align_interface::set_interface (Grob*me)
219 {
220   Align_interface::set_interface (me); 
221   Align_interface::set_axis (me,X_AXIS);
222 }