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