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