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