]> git.donarmstrong.com Git - lilypond.git/blob - lily/break-align-item.cc
release: 1.3.92
[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--2000 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);
25
26 SCM
27 Break_align_interface::before_line_breaking (SCM smob)
28 {
29   Score_element* me = unsmob_element (smob);
30   do_alignment (me);
31   return SCM_UNSPECIFIED;
32 }
33
34 Real
35 Break_align_interface::alignment_callback (Score_element*c, Axis a)
36 {
37   assert (a == X_AXIS);
38   Score_element *par = c->parent_l (a);
39   if (par && !to_boolean (par->get_elt_property ("break-alignment-done")))\
40     {
41       par->set_elt_property ("break-alignment-done", SCM_BOOL_T);
42       Break_align_interface::do_alignment (par);
43     }
44     
45   return 0.0;
46 }
47
48 Real
49 Break_align_interface::self_align_callback (Score_element *me, Axis a)
50 {
51   assert (a == X_AXIS);
52   
53   Item* item = dynamic_cast<Item*> (me);
54   Direction bsd = item->break_status_dir();
55   if (bsd == LEFT)
56     {
57       me->set_elt_property ("self-alignment-X", gh_int2scm (RIGHT));
58     }
59
60   return Side_position::aligned_on_self (me, a);  
61 }
62
63 void
64 Break_align_interface::add_element (Score_element*me, Score_element *toadd)
65 {
66   toadd->add_offset_callback (alignment_callback, X_AXIS);
67   Axis_group_interface::add_element (me, toadd);
68 }
69
70 void
71 Break_align_interface::do_alignment (Score_element *me)
72 {
73   Item * item = dynamic_cast<Item*> (me);
74   Item *column = item->column_l ();
75
76   Real interline= me->paper_l ()->get_var ("staffspace");       
77   Link_array<Score_element> elems;
78   Link_array<Score_element> all_elems
79     = Pointer_group_interface__extract_elements (me, (Score_element*)0,
80                                                  "elements");
81   
82   for (int i=0; i < all_elems.size(); i++) 
83     {
84       Interval y = all_elems[i]->extent(X_AXIS);
85       if (!y.empty_b())
86         elems.push (dynamic_cast<Score_element*> (all_elems[i]));
87     }
88   
89   if (!elems.size ())
90     return;
91
92   SCM symbol_list = SCM_EOL;
93   Array<Real> dists;
94   SCM current_origin = ly_symbol2scm ("none");
95   for (int i=0; i <= elems.size (); i++)
96     {
97       Score_element *next_elt  = i < elems.size ()
98         ? elems[i]
99         : 0 ;
100       
101       SCM next_origin;
102
103       if (next_elt)
104         {
105           next_origin = next_elt->get_elt_property ("break-align-symbol");
106           next_origin =
107             gh_symbol_p (next_origin)? 
108             next_origin : ly_symbol2scm ("none")
109 ;
110         }
111       else
112         next_origin = ly_symbol2scm ("begin-of-note");
113       
114       SCM e = scm_assoc (scm_listify (current_origin,
115                                       next_origin,
116                                       SCM_UNDEFINED),
117                          scm_eval2 (ly_symbol2scm ("space-alist"), SCM_EOL));
118       
119       SCM extra_space;
120       if (e != SCM_BOOL_F)
121         {
122           extra_space = gh_cdr (e);
123         }
124       else
125         {
126           warning (_f ("unknown spacing pair `%s', `%s'",
127                        ly_symbol2string (current_origin),
128                        ly_symbol2string (next_origin)));
129           extra_space = scm_listify (ly_symbol2scm ("minimum-space"), gh_double2scm (0.0), SCM_UNDEFINED);
130         }
131
132       SCM symbol = gh_car  (extra_space);
133       Real spc = gh_scm2double (gh_cadr(extra_space));
134       spc *= interline;
135
136       dists.push(spc);
137       symbol_list = gh_cons (symbol, symbol_list);
138       current_origin = next_origin;
139     }
140
141
142   // skip the first sym.
143   symbol_list  = gh_cdr (scm_reverse (symbol_list));
144   for (int i=0; i <elems.size()-1; i++)
145     {
146       elems[i]->set_elt_property (gh_car  (symbol_list),
147                                   scm_cons (gh_double2scm (0),
148                                             gh_double2scm (dists[i+1])));
149
150       symbol_list = gh_cdr (symbol_list);
151     }
152
153
154   // urg
155   SCM first_pair = elems[0]->get_elt_property ("minimum-space");
156   if (gh_pair_p (first_pair))
157     first_pair = first_pair;
158   else
159     first_pair = gh_cons (gh_double2scm (0.0), gh_double2scm (0.0));
160   
161   scm_set_car_x (first_pair, gh_double2scm (-dists[0]));
162   elems[0]->set_elt_property ("minimum-space", first_pair);
163
164
165   /*
166     Force callbacks for alignment to be called   
167   */
168   Align_interface::do_side_processing (me, X_AXIS);
169
170   Real pre_space = elems[0]->relative_coordinate (column, X_AXIS);
171
172   Real xl = elems[0]->extent (X_AXIS)[LEFT];
173   if (!isinf (xl))
174     pre_space += xl;
175   else
176     programming_error ("Infinity reached. ");
177
178   Real xr = elems.top ()->extent (X_AXIS)[RIGHT];
179   Real spring_len = elems.top ()->relative_coordinate (column, X_AXIS);
180   if (!isinf (xr))
181     spring_len += xr;
182   else
183     programming_error ("Infinity reached.");
184   
185   Real stretch_distance =0.;
186   
187   if (gh_car  (symbol_list) == ly_symbol2scm ("extra-space"))
188     {
189       spring_len += dists.top ();
190       stretch_distance = dists.top ();
191     }
192   else if (gh_car  (symbol_list) == ly_symbol2scm ("minimum-space"))
193     {
194       spring_len = spring_len >? dists.top ();
195       stretch_distance = spring_len;
196     }
197
198   /*
199     Hint the spacing engine how much space to put in.
200
201     The pairs are in the format of an interval (ie. CAR <  CDR).
202   */
203   column->set_elt_property ("extra-space",
204                             scm_cons (gh_double2scm (pre_space),
205                                       gh_double2scm (spring_len)));
206
207   column->set_elt_property ("stretch-distance",
208                             gh_cons (gh_double2scm (-dists[0]),
209                                      gh_double2scm (stretch_distance)));
210
211
212 }
213
214
215 void
216 Break_align_interface::set_interface (Score_element*me)
217 {
218   Align_interface::set_interface (me); 
219   Align_interface::set_axis (me,X_AXIS);
220
221   me->add_offset_callback (Break_align_interface::self_align_callback, X_AXIS);
222 }