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