]> git.donarmstrong.com Git - lilypond.git/blob - lily/break-align-interface.cc
* VERSION (MY_PATCH_LEVEL): make 1.7.0
[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", scm_int2num (RIGHT));
56     }
57
58   /*
59     Force break alignment itself to be done first, in the case
60    */
61   return Self_alignment_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::do_alignment (Grob *me)
72 {
73   Item * item = dynamic_cast<Item*> (me);
74   int rank = Paper_column::get_rank (item->get_column ());
75   Link_array<Grob> elems
76     = Pointer_group_interface__extract_grobs (me, (Grob*)0,
77                                                  "elements");
78   Array<Interval> extents;
79   
80   for (int i=0; i < elems.size (); i++) 
81     {
82       Interval y = elems[i]->extent (elems[i], X_AXIS);
83       extents.push (y);
84     }
85
86
87   int idx  = 0;
88   while (idx < extents.size  () && extents[idx].empty_b ())
89     idx++;
90   
91   Array<Real> offsets;
92   offsets.set_size (elems.size());
93   for (int i= 0; i < offsets.size();i ++)
94     offsets[i] = 0.0;
95
96
97   int edge_idx = -1;
98   while (idx < elems.size() - 1)
99     {
100       int next_idx = idx+1;
101       while (next_idx < elems.size() &&
102              extents[next_idx].empty_b()
103              && next_idx != elems.size() -1 )
104         next_idx++;
105       
106       Grob *l = elems[idx];
107       Grob *r = elems[next_idx];
108
109       SCM alist = SCM_EOL;
110
111       for (SCM s= l->get_grob_property ("elements");
112            gh_pair_p (s) ; s = gh_cdr (s))
113           {
114             Grob *elt = unsmob_grob (gh_car (s));
115
116             if (edge_idx < 0
117                 && elt->get_grob_property ("break-align-symbol") == ly_symbol2scm( "left-edge"))
118               edge_idx = idx;
119             
120             SCM l =elt->get_grob_property ("space-alist");
121             if (gh_pair_p(l))
122               {
123                 alist= l;
124                 break;
125               }
126           }
127
128       SCM rsym = SCM_EOL;
129
130       /*
131         We used to use #'cause to find out the symbol and the spacing
132         table, but that gets icky when that grob is suicided for some
133         reason.
134       */
135         for (SCM s = r->get_grob_property ("elements");
136              gh_pair_p (s); s = gh_cdr (s))
137           {
138             Grob * elt =unsmob_grob(gh_car (s));
139
140             SCM sym = elt->get_grob_property ("break-align-symbol");
141             if (gh_symbol_p (sym))
142               {
143                 rsym = sym;
144                 break;
145               }
146           }
147       if (rsym  == ly_symbol2scm("left-edge"))
148         edge_idx = next_idx;
149
150       SCM entry = SCM_EOL;
151       if (gh_symbol_p (rsym))
152         entry = scm_assq (rsym, alist);
153
154       bool entry_found = gh_pair_p (entry);
155       if (!entry_found)
156         {
157           String sym_string;
158           if(gh_symbol_p (rsym))
159             sym_string = ly_symbol2string (rsym);
160
161           String orig_string ;
162           if (unsmob_grob (l->get_grob_property ("cause")))
163             orig_string = unsmob_grob (l->get_grob_property ("cause"))->name ();
164           
165           programming_error (_f("No spacing entry from %s to `%s'",
166                                 orig_string.to_str0 (),
167                                 sym_string.to_str0 ()));
168         }
169
170       Real distance = 1.0;
171       SCM type = ly_symbol2scm ("extra-space");
172       
173       if (entry_found)
174         {
175           entry = gh_cdr (entry);
176           
177           distance = gh_scm2double (gh_cdr (entry));
178           type = gh_car (entry) ;
179         }
180
181       if (type == ly_symbol2scm ("extra-space"))
182         offsets[next_idx] = extents[idx][RIGHT] + distance;
183       else if (type == ly_symbol2scm("minimum-space"))
184         offsets[next_idx] = extents[idx][RIGHT] >? distance;
185       
186       idx = next_idx;
187     }
188
189
190   Real here = 0.0;
191   Interval total_extent;
192
193   Real alignment_off =0.0;  
194   for (int i =0 ; i < offsets.size(); i++)
195     {
196       here += offsets[i];
197       if (i == edge_idx)
198         alignment_off = -here; 
199       total_extent.unite (extents[i] + here);
200     }
201
202
203   if (item->break_status_dir () == LEFT)
204     alignment_off = -total_extent[RIGHT];
205   else if (edge_idx < 0)
206     alignment_off = -total_extent[LEFT];
207
208   here = alignment_off;
209   for (int i =0 ; i < offsets.size(); i++)
210     {
211       here += offsets[i];
212       elems[i]->translate_axis (here, X_AXIS);
213     }
214 }
215
216
217 ADD_INTERFACE (Break_aligned_interface, "break-aligned-interface",
218   "Items that are aligned in prefatory matter.
219
220 The spacing of these items is controlled by the space-alist
221 property. It contains a list break-align-symbols with a specification
222 of the associated space. The space definition is either (extra-space
223 . @var{number}), which adds space after the symbol, (minimum-space
224 . @var{ms}), which pads the space until it it is @var{ms}.
225
226
227 Special keys for the alist are 'first-note and 'next-note, signifyign
228 the first note on a line, and the next note halfway a line.
229
230 Rules for this spacing are much more complicated than this. 
231 See [Wanske] page 126 -- 134, [Ross] pg 143 -- 147
232
233
234 ",
235   "break-align-symbol break-alignment-done space-alist");
236
237 ADD_INTERFACE (Break_align_interface, "break-alignment-interface",
238   "See @ref{break-aligned-interface}.",
239   "break-alignment-done");
240
241
242