]> git.donarmstrong.com Git - lilypond.git/blob - lily/break-align-interface.cc
*** empty log message ***
[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--2004 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 "output-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) scm_to_int (axis);
32
33   assert (a == X_AXIS);
34   Grob *par = me->get_parent (a);
35   if (par && !to_boolean (par->get_property ("positioning-done")))
36     {
37       par->set_property ("positioning-done", SCM_BOOL_T);
38       Break_align_interface::do_alignment (par);
39     }
40     
41   return scm_make_real (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) scm_to_int (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_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
66 /*
67   This is tricky: we cannot modify 'elements, since callers are
68   iterating the same list. Reordering the list in-place, or resetting
69   'elements will skip elements in the loops of callers.
70   
71   So we return the correct order as an array.
72  */
73 Link_array<Grob>
74 Break_align_interface::ordered_elements (Grob *grob)
75 {
76   Item *me  = dynamic_cast<Item*> (grob);
77   SCM elts = me->get_property ("elements");
78   SCM order_vec = me->get_property ("break-align-orders");
79   if (!ly_c_vector_p (order_vec)
80       || ly_vector_length (order_vec) < 3)
81     return  Pointer_group_interface__extract_grobs (me, (Grob*)0,
82                                                     "elements");
83   SCM order = scm_vector_ref (order_vec,
84                               scm_int2num (me->break_status_dir() + 1));
85
86
87   /*
88     Copy in order specified in BREAK-ALIGN-ORDER.
89   */
90   Link_array<Grob> new_elts;
91   for (; scm_is_pair (order); order = scm_cdr (order))
92     {
93       SCM sym = scm_car (order);
94       
95       for (SCM s =elts; scm_is_pair (s); s = scm_cdr (s))
96         {
97           Grob *g = unsmob_grob (scm_car (s));
98           if (g && sym == g->get_property ("break-align-symbol"))
99             {
100               new_elts.push (g);
101               elts = scm_delq (g->self_scm (), elts); 
102             }
103         }
104     }
105   return new_elts;
106 }
107
108 void
109 Break_align_interface::add_element (Grob*me, Grob *toadd)
110 {
111   Axis_group_interface::add_element (me, toadd);
112 }
113
114 void
115 Break_align_interface::do_alignment (Grob *grob)
116 {
117   Item * me = dynamic_cast<Item*> (grob);
118
119   
120   Link_array<Grob> elems = ordered_elements (me);
121   Array<Interval> extents;
122
123   int last_nonempty = -1; 
124   for (int i=0; i < elems.size (); i++) 
125     {
126       Interval y = elems[i]->extent (elems[i], X_AXIS);
127       extents.push (y);
128       if (!y.is_empty ())
129         last_nonempty = i; 
130     }
131
132   int idx  = 0;
133   while (idx < extents.size  () && extents[idx].is_empty ())
134     idx++;
135   
136   Array<Real> offsets;
137   offsets.set_size (elems.size ());
138   for (int i= 0; i < offsets.size ();i ++)
139     offsets[i] = 0.0;
140
141
142   Real extra_right_space = 0.0;
143   int edge_idx = -1;
144   while (idx < elems.size ())
145     {
146       int next_idx = idx+1;
147       while (next_idx < elems.size () &&
148              extents[next_idx].is_empty () )
149         next_idx++;
150       
151       Grob *l = elems[idx];
152       Grob *r = 0;
153
154       if (next_idx < elems.size ())
155         r = elems[next_idx];
156
157       SCM alist = SCM_EOL;
158
159
160       /*
161         Find the first grob with a space-alist entry.
162        */
163       for (SCM s = l->get_property ("elements");
164            scm_is_pair (s) ; s = scm_cdr (s))
165           {
166             Grob *elt = unsmob_grob (scm_car (s));
167
168             if (edge_idx < 0
169                 && elt->get_property ("break-align-symbol")
170                 == ly_symbol2scm ( "left-edge"))
171               edge_idx = idx;
172             
173             SCM l =elt->get_property ("space-alist");
174             if (scm_is_pair (l))
175               {
176                 alist= l;
177                 break;
178               }
179           }
180
181       SCM rsym = r ? SCM_EOL : ly_symbol2scm ("right-edge");
182
183       /*
184         We used to use #'cause to find out the symbol and the spacing
185         table, but that gets icky when that grob is suicided for some
186         reason.
187       */
188       for (SCM s = r ? r->get_property ("elements") : SCM_EOL;
189            !scm_is_symbol (rsym) && scm_is_pair (s); s = scm_cdr (s))
190         {
191           Grob * elt =unsmob_grob (scm_car (s));
192
193           rsym = elt->get_property ("break-align-symbol");
194         }
195         
196       if (rsym  == ly_symbol2scm ("left-edge"))
197         edge_idx = next_idx;
198
199       SCM entry = SCM_EOL;
200       if (scm_is_symbol (rsym))
201         entry = scm_assq (rsym, alist);
202
203       bool entry_found = scm_is_pair (entry);
204       if (!entry_found)
205         {
206           String sym_string;
207           if (scm_is_symbol (rsym))
208             sym_string = ly_symbol2string (rsym);
209
210           String orig_string ;
211           if (unsmob_grob (l->get_property ("cause")))
212             orig_string = unsmob_grob (l->get_property ("cause"))->name ();
213           
214           programming_error (_f ("No spacing entry from %s to `%s'",
215                                 orig_string.to_str0 (),
216                                 sym_string.to_str0 ()));
217         }
218
219       Real distance = 1.0;
220       SCM type = ly_symbol2scm ("extra-space");
221       
222       if (entry_found)
223         {
224           entry = scm_cdr (entry);
225           
226           distance = scm_to_double (scm_cdr (entry));
227           type = scm_car (entry) ;
228         }
229
230       if (r)
231         {
232           if (type == ly_symbol2scm ("extra-space"))
233             offsets[next_idx] = extents[idx][RIGHT] + distance
234               - extents[next_idx][LEFT];
235           /* should probably junk minimum-space */
236           else if (type == ly_symbol2scm ("minimum-space"))
237             offsets[next_idx] = extents[idx][RIGHT] >? distance;
238         }
239       else
240         {
241           extra_right_space = distance;   
242         }
243       
244       idx = next_idx;
245     }
246
247   Real here = 0.0;
248   Interval total_extent;
249
250   Real alignment_off =0.0;  
251   for (int i =0 ; i < offsets.size (); i++)
252     {
253       here += offsets[i];
254       if (i == edge_idx)
255         alignment_off = -here; 
256       total_extent.unite (extents[i] + here);
257     }
258
259
260   if (me->break_status_dir () == LEFT)
261     {
262       alignment_off = - total_extent[RIGHT] - extra_right_space;
263     }
264   else if (edge_idx < 0)
265     alignment_off = -total_extent[LEFT];
266
267   here = alignment_off;
268   for (int i =0 ; i < offsets.size (); i++)
269     {
270       here += offsets[i];
271       elems[i]->translate_axis (here, X_AXIS);
272     }
273 }
274
275
276 ADD_INTERFACE (Break_aligned_interface, "break-aligned-interface",
277                "Items that are aligned in prefatory matter.\n"
278                "\n"
279                "The spacing of these items is controlled by the @code{space-alist}\n"
280                "property. It contains a list @code{break-align-symbol}s with a specification\n"
281                "of the associated space. The space specification can be "
282                "@table @code\n"
283                "@item (minimum-space . @var{spc}))\n"
284                "  Pad space until the distance is @var{spc}\n"
285                "@item (fixed-space . @var{spc})\n"
286                "  Set a fixed space\n" 
287                "@item (semi-fixed-space . @var{spc})\n"
288                "  Set a space. Half of it is fixed and half is stretchable. \n"
289                "(does not work at start of line. fixme)\n"
290                "@item (extra-space . @var{spc})\n"
291                "  Add @var{spc} amount of space.\n"
292                "@end table\n"
293                "\n"
294                "Special keys for the alist are @code{first-note} and @code{next-note}, signifying\n"
295                "the first note on a line, and the next note halfway a line.\n"
296                "\n"
297                "Rules for this spacing are much more complicated than this. \n"
298                "See [Wanske] page 126 -- 134, [Ross] pg 143 -- 147\n",
299                "break-align-symbol space-alist");
300
301 ADD_INTERFACE (Break_align_interface, "break-alignment-interface",
302                "The object that performs break aligment. See @ref{break-aligned-interface}.",
303                "positioning-done break-align-orders");
304
305
306