]> git.donarmstrong.com Git - lilypond.git/blob - lily/break-align-item.cc
bbea713e2bb8d1f711fad9d141242db4371b5c9a
[lilypond.git] / lily / break-align-item.cc
1 /*
2   break-align-item.cc -- implement Break_align_item
3
4   source file of the GNU LilyPond music typesetter
5
6   (c)  1997--2000 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8 #include <math.h>
9 #include <libc-extension.hh>
10
11 #include "side-position-interface.hh"
12 #include "warn.hh"
13 #include "dimension-cache.hh"
14 #include "lily-guile.hh"
15 #include "break-align-item.hh"
16 #include "dimensions.hh"
17 #include "paper-score.hh"
18 #include "paper-def.hh"
19 #include "paper-column.hh"
20 #include "group-interface.hh"
21 #include "align-interface.hh"
22
23 GLUE_SCORE_ELEMENT(Break_align_item,before_line_breaking);
24 SCM
25 Break_align_item::member_before_line_breaking ()
26 {
27   if (break_status_dir() == LEFT)
28     {
29       set_elt_property ("self-alignment-X", gh_int2scm (RIGHT));
30     }
31   else
32     {
33       add_offset_callback (Align_interface::center_on_element, X_AXIS);
34     }
35   
36
37
38   Real interline= paper_l ()->get_var ("interline");    
39   Link_array<Score_element> elems;
40   Link_array<Score_element> all_elems
41     = Pointer_group_interface__extract_elements (this, (Score_element*)0,
42                                          "elements");
43   
44   for (int i=0; i < all_elems.size(); i++) 
45     {
46       Interval y = all_elems[i]->extent(X_AXIS);
47       if (!y.empty_b())
48         elems.push (dynamic_cast<Score_element*> (all_elems[i]));
49     }
50   
51   if (!elems.size ())
52     return SCM_UNDEFINED;
53
54   SCM symbol_list = SCM_EOL;
55   Array<Real> dists;
56   SCM current_origin = ly_symbol2scm ("none");
57   for (int i=0; i <= elems.size (); i++)
58     {
59       Score_element *next_elt  = i < elems.size ()
60         ? elems[i]
61         : 0 ;
62       
63       SCM next_origin;
64
65       if (next_elt)
66         {
67           next_origin = next_elt->get_elt_property ("break-align-symbol");
68           next_origin =
69             (next_origin == SCM_UNDEFINED)
70             ? ly_symbol2scm ("none")
71             : next_origin;
72         }
73       else
74         next_origin = ly_symbol2scm ("begin-of-note");
75       
76       SCM e = scm_assoc (scm_listify (current_origin,
77                                       next_origin,
78                                       SCM_UNDEFINED),
79                          scm_eval (ly_symbol2scm ("space-alist")));
80       
81       SCM extra_space;
82       if (e != SCM_BOOL_F)
83         {
84           extra_space = gh_cdr (e);
85         }
86       else
87         {
88           warning (_f ("unknown spacing pair `%s', `%s'",
89                        ly_symbol2string (current_origin),
90                        ly_symbol2string (next_origin)));
91           extra_space = scm_listify (ly_symbol2scm ("minimum-space"), gh_double2scm (0.0), SCM_UNDEFINED);
92         }
93
94       SCM symbol = gh_car  (extra_space);
95       Real spc = gh_scm2double (gh_cadr(extra_space));
96       spc *= interline;
97
98       dists.push(spc);
99       symbol_list = gh_cons (symbol, symbol_list);
100       current_origin = next_origin;
101     }
102
103
104   // skip the first sym.
105   symbol_list  = gh_cdr (scm_reverse (symbol_list));
106   for (int i=0; i <elems.size()-1; i++)
107     {
108       String sym_str = ly_symbol2string (gh_car  (symbol_list));
109       elems[i]->set_elt_property (sym_str,
110                                   scm_cons (gh_double2scm (0),
111                                             gh_double2scm (dists[i+1])));
112
113       symbol_list = gh_cdr (symbol_list);
114     }
115
116
117   // urg
118   SCM first_pair = elems[0]->get_elt_property ("minimum-space");
119   if (gh_pair_p (first_pair))
120     first_pair = first_pair;
121   else
122     first_pair = gh_cons (gh_double2scm (0.0), gh_double2scm (0.0));
123   
124   scm_set_car_x (first_pair, gh_double2scm (-dists[0]));
125   elems[0]->set_elt_property ("minimum-space", first_pair);
126
127
128   /*
129     Force callbacks for alignment to be called   
130    */
131   Real unused =  elems[0]->relative_coordinate (this, X_AXIS);
132   Real pre_space = elems[0]->relative_coordinate (column_l (), X_AXIS);
133
134   Real xl = elems[0]->extent (X_AXIS)[LEFT];
135   if (!isinf (xl))
136     pre_space += xl;
137   else
138     programming_error ("Infinity reached. ");
139
140   Real xr = elems.top ()->extent (X_AXIS)[RIGHT];
141   Real spring_len = elems.top ()->relative_coordinate (column_l (), X_AXIS);
142   if (!isinf (xr))
143     spring_len += xr;
144   else
145     programming_error ("Infinity reached.");
146   
147   Real stretch_distance =0.;
148   
149   if (gh_car  (symbol_list) == ly_symbol2scm ("extra-space"))
150     {
151       spring_len += dists.top ();
152       stretch_distance = dists.top ();
153     }
154   else if (gh_car  (symbol_list) == ly_symbol2scm ("minimum-space"))
155     {
156       spring_len = spring_len >? dists.top ();
157       stretch_distance = spring_len;
158     }
159
160   /*
161     Hint the spacing engine how much space to put in.
162
163     The pairs are in the format of an interval (ie. CAR <  CDR).
164   */
165   column_l ()->set_elt_property ("extra-space",
166                                  scm_cons (gh_double2scm (pre_space),
167                                            gh_double2scm (spring_len)));
168
169   column_l ()->set_elt_property ("stretch-distance",
170                                  gh_cons (gh_double2scm (-dists[0]),
171                                           gh_double2scm (stretch_distance)));
172
173
174   return SCM_UNDEFINED;
175 }
176
177 Break_align_item::Break_align_item (SCM s)
178   : Item (s)
179 {
180   set_elt_property ("stacking-dir" , gh_int2scm (RIGHT));
181
182   Align_interface (this).set_interface (); 
183   Align_interface (this).set_axis (X_AXIS);
184
185   add_offset_callback (Side_position_interface::aligned_on_self, X_AXIS);
186 }