]> git.donarmstrong.com Git - lilypond.git/blob - lily/break-align-item.cc
c3f9b05ca5ba7ba28415bc5bc2d15b97c767bf82
[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--1999 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8
9 #include "dimension-cache.hh"
10 #include "lily-guile.hh"
11 #include "break-align-item.hh"
12 #include "dimensions.hh"
13 #include "paper-score.hh"
14 #include "paper-def.hh"
15 #include "paper-column.hh"
16
17 /*
18   Handle spacing for prefatory matter. 
19
20
21
22   TODO: rewrite this.  It is kludgy
23 */
24
25 void
26 Break_align_item::do_pre_processing()
27 {
28   if (break_status_dir() == LEFT)
29     align_dir_ = LEFT;
30   else
31     align_dir_ = RIGHT;
32   
33   flip (&align_dir_);
34   sort_elements ();
35   Real interline= paper_l ()->get_realvar (interline_scm_sym);  
36   
37   Link_array<Score_element> elems;
38   for (int i=0; i < elem_l_arr_.size(); i++) 
39     {
40       Interval y = elem_l_arr_[i]->extent(axis ());
41       if (!y.empty_b())
42         elems.push (dynamic_cast<Score_element*> (elem_l_arr_[i]));
43     }
44   
45   if (!elems.size ())
46     return;
47
48   SCM symbol_list = SCM_EOL;
49   Array<Real> dists;
50   SCM current_origin = ly_ch_C_to_scm ("");
51   for (int i=0; i <= elems.size (); i++)
52     {
53       Score_element *next_elt  = i < elems.size ()
54         ? elems[i]
55         : 0 ;
56       
57       SCM next_origin;
58
59       if (next_elt)
60         {
61           next_origin = next_elt->get_elt_property (origin_scm_sym);
62           next_origin =
63             (next_origin == SCM_BOOL_F)
64             ? ly_ch_C_to_scm ("")
65             : SCM_CDR (next_origin);
66         }
67       else
68         next_origin = ly_ch_C_to_scm ("begin-of-note");
69       
70       SCM extra_space
71         = scm_eval (scm_listify (ly_symbol ("break-align-spacer"),
72                                  current_origin, next_origin, SCM_UNDEFINED)); 
73       SCM symbol = SCM_CAR (extra_space);
74       Real spc = gh_scm2double (SCM_CADR(extra_space));
75       spc *= interline;
76
77       dists.push(spc);
78       symbol_list = gh_cons (symbol, symbol_list);
79       current_origin = next_origin;
80     }
81
82
83   // skip the first sym.
84   symbol_list  = SCM_CDR (scm_reverse (symbol_list));
85   for (int i=0; i <elems.size()-1; i++)
86     {
87       elems[i]->set_elt_property (SCM_CAR (symbol_list),
88                                   scm_cons (gh_double2scm (0),
89                                             gh_double2scm (dists[i+1])));
90
91       symbol_list = SCM_CDR (symbol_list);
92     }
93
94
95   // urg
96   SCM first_pair = elems[0]->get_elt_property (minimum_space_scm_sym);
97   if (first_pair == SCM_BOOL_F)
98     first_pair = gh_cons (gh_double2scm (0.0), gh_double2scm (0.0));
99   else
100     first_pair = SCM_CDR (first_pair);
101   
102   scm_set_car_x (first_pair, gh_double2scm (-dists[0]));
103   elems[0]->set_elt_property (minimum_space_scm_sym, first_pair);
104   
105   Axis_align_item::do_pre_processing();
106
107
108   Real pre_space = elems[0]->extent (X_AXIS)[LEFT]
109     + elems[0]->relative_coordinate (column_l ()->dim_cache_[X_AXIS], X_AXIS);
110   Real spring_len = elems.top ()->extent (X_AXIS)[RIGHT]
111     + elems.top ()->relative_coordinate (column_l ()->dim_cache_[X_AXIS], X_AXIS);
112   
113   Real stretch_distance =0.;
114   
115   if (SCM_CAR (symbol_list) == extra_space_scm_sym)
116     {
117       spring_len += dists.top ();
118       stretch_distance = dists.top ();
119     }
120   else if (SCM_CAR (symbol_list) == minimum_space_scm_sym)
121     {
122       spring_len = spring_len >? dists.top ();
123       stretch_distance = spring_len;
124     }
125
126   /*
127     Hint the spacing engine how much space to put in.
128
129
130     The pairs are in the format of an interval (ie. CAR <  CDR).
131   */
132   column_l ()->set_elt_property (extra_space_scm_sym,
133                                  scm_cons (gh_double2scm (pre_space),
134                                            gh_double2scm (spring_len)));
135
136   column_l ()->set_elt_property (stretch_distance_scm_sym,
137                                  gh_cons (gh_double2scm (-dists[0]),
138                                           gh_double2scm (stretch_distance)));
139                                  
140 }
141
142
143
144 Break_align_item::Break_align_item ()
145 {
146   stacking_dir_ = RIGHT;
147   set_axis (X_AXIS);
148 }
149
150 void
151 Break_align_item::add_breakable_item (Item *it)
152 {
153   SCM pr = it->remove_elt_property (break_priority_scm_sym); 
154
155   if (pr == SCM_BOOL_F)
156     return;
157
158   int priority = gh_scm2int (SCM_CDR (pr));
159
160   Score_element * column_l = get_elt_by_priority (priority);
161   Axis_group_item * hg=0;
162   if (column_l)
163     {
164       hg = dynamic_cast<Axis_group_item*> (column_l);
165     }
166   else
167     {
168       hg = new Axis_group_item;
169       hg->set_axes (X_AXIS,X_AXIS);
170
171       /*
172         this is quite ridiculous, but we do this anyway, to ensure that no
173         warning bells about missing Y refpoints go off later on.
174       */
175       hg->dim_cache_[Y_AXIS]->parent_l_ = dim_cache_[Y_AXIS];
176       hg->set_elt_property (ly_symbol ("origin"), ly_ch_C_to_scm (it->name ()));
177
178       pscore_l_->typeset_element (hg);
179       add_element_priority (hg, priority);
180
181       if (priority == 0)
182         center_l_ = hg;
183     }
184   
185   hg->add_element (it);
186 }