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