]> git.donarmstrong.com Git - lilypond.git/blob - lily/break-align-interface.cc
07a99d272f10fc6c6bf551cc3d03720fdfb78d99
[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--2003 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8
9
10 #include <math.h>
11 #include <libc-extension.hh>    // isinf
12
13 #include "self-alignment-interface.hh"
14 #include "side-position-interface.hh"
15 #include "axis-group-interface.hh"
16 #include "warn.hh"
17 #include "lily-guile.hh"
18 #include "break-align-interface.hh"
19 #include "dimensions.hh"
20 #include "paper-def.hh"
21 #include "paper-column.hh"
22 #include "group-interface.hh"
23 #include "align-interface.hh"
24
25 MAKE_SCHEME_CALLBACK (Break_align_interface,alignment_callback,2);
26
27 SCM
28 Break_align_interface::alignment_callback (SCM element_smob, SCM axis)
29 {
30   Grob *me = unsmob_grob (element_smob);
31   Axis a = (Axis) gh_scm2int (axis);
32
33   assert (a == X_AXIS);
34   Grob *par = me->get_parent (a);
35   if (par && !to_boolean (par->get_grob_property ("positioning-done")))
36     {
37       par->set_grob_property ("positioning-done", SCM_BOOL_T);
38       Break_align_interface::do_alignment (par);
39     }
40     
41   return gh_double2scm (0);
42 }
43
44 MAKE_SCHEME_CALLBACK (Break_align_interface,self_align_callback,2);
45 SCM
46 Break_align_interface::self_align_callback (SCM element_smob, SCM axis)
47 {
48   Grob *me = unsmob_grob (element_smob);
49   Axis a = (Axis) gh_scm2int (axis);
50   assert (a == X_AXIS);
51   
52   Item* item = dynamic_cast<Item*> (me);
53   Direction bsd = item->break_status_dir ();
54   if (bsd == LEFT)
55     {
56       me->set_grob_property ("self-alignment-X", scm_int2num (RIGHT));
57     }
58
59   /*
60     Force break alignment itself to be done first, in the case
61    */
62   return Self_alignment_interface::aligned_on_self (element_smob, axis);  
63 }
64
65 void
66 Break_align_interface::add_element (Grob*me, Grob *toadd)
67 {
68   Axis_group_interface::add_element (me, toadd);
69 }
70
71 void
72 Break_align_interface::do_alignment (Grob *me)
73 {
74   Item * item = dynamic_cast<Item*> (me);
75   Link_array<Grob> elems
76     = Pointer_group_interface__extract_grobs (me, (Grob*)0,
77                                                  "elements");
78   Array<Interval> extents;
79
80   int last_nonempty = -1; 
81   for (int i=0; i < elems.size (); i++) 
82     {
83       Interval y = elems[i]->extent (elems[i], X_AXIS);
84       extents.push (y);
85       if (!y.is_empty ())
86         last_nonempty = i; 
87     }
88
89   int idx  = 0;
90   while (idx < extents.size  () && extents[idx].is_empty ())
91     idx++;
92   
93   Array<Real> offsets;
94   offsets.set_size (elems.size());
95   for (int i= 0; i < offsets.size();i ++)
96     offsets[i] = 0.0;
97
98
99   Real extra_right_space = 0.0;
100   int edge_idx = -1;
101   while (idx < elems.size())
102     {
103       int next_idx = idx+1;
104       while (next_idx < elems.size() &&
105              extents[next_idx].is_empty () )
106         next_idx++;
107       
108       Grob *l = elems[idx];
109       Grob *r = 0;
110
111       if (next_idx < elems.size())
112         r = elems[next_idx];
113
114       SCM alist = SCM_EOL;
115
116
117       /*
118         Find the first grob with a space-alist entry.
119        */
120       for (SCM s= l->get_grob_property ("elements");
121            gh_pair_p (s) ; s = gh_cdr (s))
122           {
123             Grob *elt = unsmob_grob (gh_car (s));
124
125             if (edge_idx < 0
126                 && elt->get_grob_property ("break-align-symbol")
127                 == ly_symbol2scm( "left-edge"))
128               edge_idx = idx;
129             
130             SCM l =elt->get_grob_property ("space-alist");
131             if (gh_pair_p(l))
132               {
133                 alist= l;
134                 break;
135               }
136           }
137
138       SCM rsym = r ? SCM_EOL : ly_symbol2scm ("right-edge");
139
140       /*
141         We used to use #'cause to find out the symbol and the spacing
142         table, but that gets icky when that grob is suicided for some
143         reason.
144       */
145       for (SCM s = r ? r->get_grob_property ("elements") : SCM_EOL;
146            !gh_symbol_p (rsym) && gh_pair_p (s); s = gh_cdr (s))
147         {
148           Grob * elt =unsmob_grob(gh_car (s));
149
150           rsym = elt->get_grob_property ("break-align-symbol");
151         }
152         
153       if (rsym  == ly_symbol2scm("left-edge"))
154         edge_idx = next_idx;
155
156       SCM entry = SCM_EOL;
157       if (gh_symbol_p (rsym))
158         entry = scm_assq (rsym, alist);
159
160       bool entry_found = gh_pair_p (entry);
161       if (!entry_found)
162         {
163           String sym_string;
164           if(gh_symbol_p (rsym))
165             sym_string = ly_symbol2string (rsym);
166
167           String orig_string ;
168           if (unsmob_grob (l->get_grob_property ("cause")))
169             orig_string = unsmob_grob (l->get_grob_property ("cause"))->name ();
170           
171           programming_error (_f("No spacing entry from %s to `%s'",
172                                 orig_string.to_str0 (),
173                                 sym_string.to_str0 ()));
174         }
175
176       Real distance = 1.0;
177       SCM type = ly_symbol2scm ("extra-space");
178       
179       if (entry_found)
180         {
181           entry = gh_cdr (entry);
182           
183           distance = gh_scm2double (gh_cdr (entry));
184           type = gh_car (entry) ;
185         }
186
187       if (r)
188         {
189           if (type == ly_symbol2scm ("extra-space"))
190             offsets[next_idx] = extents[idx][RIGHT] + distance
191               - extents[next_idx][LEFT];
192           /* should probably junk minimum-space */
193           else if (type == ly_symbol2scm("minimum-space"))
194             offsets[next_idx] = extents[idx][RIGHT] >? distance;
195         }
196       else
197         {
198           extra_right_space = distance;   
199         }
200       
201       idx = next_idx;
202     }
203
204   Real here = 0.0;
205   Interval total_extent;
206
207   Real alignment_off =0.0;  
208   for (int i =0 ; i < offsets.size(); i++)
209     {
210       here += offsets[i];
211       if (i == edge_idx)
212         alignment_off = -here; 
213       total_extent.unite (extents[i] + here);
214     }
215
216
217   if (item->break_status_dir () == LEFT)
218     {
219       alignment_off = - total_extent[RIGHT] - extra_right_space;
220     }
221   else if (edge_idx < 0)
222     alignment_off = -total_extent[LEFT];
223
224   here = alignment_off;
225   for (int i =0 ; i < offsets.size(); i++)
226     {
227       here += offsets[i];
228       elems[i]->translate_axis (here, X_AXIS);
229     }
230 }
231
232
233 ADD_INTERFACE (Break_aligned_interface, "break-aligned-interface",
234                "Items that are aligned in prefatory matter.\n"
235                "\n"
236                "The spacing of these items is controlled by the space-alist\n"
237                "property. It contains a list break-align-symbols with a specification\n"
238                "of the associated space. The space specification can be "
239                "@table @code\n"
240                "@item (minimum-space . @var{spc}))\n"
241                "  Pad space until the distance is @var{spc}\n"
242                "@item (fixed-space . @var{spc})\n"
243                "  Set a fixed space\n" 
244                "@item (semi-fixed-space . @var{spc})\n"
245                "  Set a space. Half of it is fixed and half is stretchable. \n"
246                "(does not work at start of line. fixme)\n"
247                "@item (extra-space . @var{spc})\n"
248                "  Add @var{spc} amount of space.\n"
249                "@end table\n"
250                "\n"
251                "Special keys for the alist are 'first-note and 'next-note, signifying\n"
252                "the first note on a line, and the next note halfway a line.\n"
253                "\n"
254                "Rules for this spacing are much more complicated than this. \n"
255                "See [Wanske] page 126 -- 134, [Ross] pg 143 -- 147\n",
256                "break-align-symbol space-alist");
257
258 ADD_INTERFACE (Break_align_interface, "break-alignment-interface",
259   "See @ref{break-aligned-interface}.",
260   "positioning-done");
261
262
263