]> git.donarmstrong.com Git - lilypond.git/blob - lily/break-align-interface.cc
release: 1.5.37
[lilypond.git] / lily / break-align-interface.cc
1 /*
2   break-align-interface.cc -- implement Break_align_interface
3
4   source file of the GNU LilyPond music typesetter
5
6   (c)  1997--2002 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8
9
10 #include <math.h>
11 #include <libc-extension.hh>    // isinf
12
13 #include "side-position-interface.hh"
14 #include "axis-group-interface.hh"
15 #include "warn.hh"
16 #include "lily-guile.hh"
17 #include "break-align-interface.hh"
18 #include "dimensions.hh"
19 #include "paper-def.hh"
20 #include "paper-column.hh"
21 #include "group-interface.hh"
22 #include "align-interface.hh"
23
24 MAKE_SCHEME_CALLBACK (Break_align_interface,alignment_callback,2);
25
26 SCM
27 Break_align_interface::alignment_callback (SCM element_smob, SCM axis)
28 {
29   Grob *me = unsmob_grob (element_smob);
30   Axis a = (Axis) gh_scm2int (axis);
31
32   assert (a == X_AXIS);
33   Grob *par = me->get_parent (a);
34   if (par && !to_boolean (par->get_grob_property ("break-alignment-done")))
35     {
36       par->set_grob_property ("break-alignment-done", SCM_BOOL_T);
37       Break_align_interface::do_alignment (par);
38     }
39     
40   return gh_double2scm (0);
41 }
42
43 MAKE_SCHEME_CALLBACK (Break_align_interface,self_align_callback,2);
44 SCM
45 Break_align_interface::self_align_callback (SCM element_smob, SCM axis)
46 {
47   Grob *me = unsmob_grob (element_smob);
48   Axis a = (Axis) gh_scm2int (axis);
49   assert (a == X_AXIS);
50   
51   Item* item = dynamic_cast<Item*> (me);
52   Direction bsd = item->break_status_dir ();
53   if (bsd == LEFT)
54     {
55       me->set_grob_property ("self-alignment-X", gh_int2scm (RIGHT));
56     }
57
58   /*
59     Force break alignment itself to be done first, in the case
60    */
61   return Side_position_interface::aligned_on_self (element_smob, axis);  
62 }
63
64 void
65 Break_align_interface::add_element (Grob*me, Grob *toadd)
66 {
67   Axis_group_interface::add_element (me, toadd);
68 }
69
70 void
71 Break_align_interface::set_interface (Grob*me)
72 {
73   Align_interface::set_interface (me); 
74   Align_interface::set_axis (me,X_AXIS);
75 }
76
77
78
79
80 void
81 Break_align_interface::do_alignment (Grob *me)
82 {
83   Item * item = dynamic_cast<Item*> (me);
84
85   Link_array<Grob> elems
86     = Pointer_group_interface__extract_grobs (me, (Grob*)0,
87                                                  "elements");
88   Array<Interval> extents;
89   
90   for (int i=0; i < elems.size (); i++) 
91     {
92       Interval y = elems[i]->extent (elems[i], X_AXIS);
93       extents.push (y);
94     }
95
96
97   int idx  = 0;
98   while (extents[idx].empty_b ())
99     idx++;
100   
101   Array<Real> offsets;
102   offsets.set_size (elems.size());
103   for (int i= 0; i < offsets.size();i ++)
104     offsets[i] = 0.0;
105
106
107   int edge_idx = -1;
108   while (idx < elems.size())
109     {
110       int next_idx = idx+1;
111       while ( next_idx < elems.size() && extents[next_idx].empty_b())
112         next_idx++;
113
114       if (next_idx == elems.size())
115         break;
116       
117       Grob *l = elems[idx];
118       Grob *r = elems[next_idx];
119
120       SCM alist = SCM_EOL;
121
122       for (SCM s= l->get_grob_property ("elements");
123            gh_pair_p (s) ; s = gh_cdr (s))
124           {
125             Grob *elt = unsmob_grob (gh_car (s));
126
127             if (edge_idx < 0
128                 && elt->get_grob_property ("break-align-symbol") == ly_symbol2scm( "left-edge"))
129               edge_idx = idx;
130             
131             SCM l =elt->get_grob_property ("space-alist");
132             if (gh_pair_p(l))
133               {
134                 alist= l;
135                 break;
136               }
137           }
138
139       SCM rsym = SCM_EOL;
140
141       /*
142         We used to use #'cause to find out the symbol and the spacing
143         table, but that gets icky when that grob is suicided for some
144         reason.
145       */
146       for (SCM s = r->get_grob_property ("elements");
147            gh_pair_p (s); s = gh_cdr (s))
148         {
149           Grob * elt =unsmob_grob(gh_car (s));
150
151           SCM sym = elt->get_grob_property ("break-align-symbol");
152           if (gh_symbol_p (sym))
153             {
154               rsym = sym;
155               break;
156             }
157         }
158       if (rsym  == ly_symbol2scm("left-edge"))
159         edge_idx = next_idx;
160
161       SCM entry = SCM_EOL;
162       if (gh_symbol_p (rsym))
163         entry = scm_assq (rsym, alist);
164
165       bool entry_found = gh_pair_p (entry);
166       if (!entry_found)
167         {
168           String sym_str;
169           if(gh_symbol_p(rsym))
170             sym_str = ly_symbol2string (rsym);
171
172           String orig_str ;
173           if (unsmob_grob (l->get_grob_property ("cause")))
174             orig_str = unsmob_grob (l->get_grob_property ("cause"))->name ();
175           
176           programming_error (_f("No spacing entry from %s to `%s'",
177                                 orig_str.ch_C (),
178                                 sym_str.ch_C()));
179         }
180
181       Real distance = 1.0;
182       SCM type = ly_symbol2scm ("extra-space");
183       
184       if (entry_found)
185         {
186           entry = gh_cdr (entry);
187           
188           distance = gh_scm2double (gh_cdr (entry));
189           type = gh_car (entry) ;
190         }
191
192       if (type == ly_symbol2scm ("extra-space"))
193         offsets[next_idx] = extents[idx][RIGHT] + distance;
194       else if (type == ly_symbol2scm("minimum-space"))
195         offsets[next_idx] = extents[idx][RIGHT] >? distance;
196
197       idx = next_idx;
198     }
199
200
201   Real here = 0.0;
202   Interval total_extent;
203
204   Real alignment_off =0.0;  
205   for (int i =0 ; i < offsets.size(); i++)
206     {
207       here += offsets[i];
208       if (i == edge_idx)
209         alignment_off = -here; 
210       total_extent.unite (extents[i] + here);
211     }
212
213
214   if (item->break_status_dir () == LEFT)
215     alignment_off = -total_extent[RIGHT];
216   else if (edge_idx < 0)
217     alignment_off = -total_extent[LEFT];
218
219   here = alignment_off;
220   for (int i =0 ; i < offsets.size(); i++)
221     {
222       here += offsets[i];
223       elems[i]->translate_axis (here, X_AXIS);
224     }
225 }
226