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