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