]> git.donarmstrong.com Git - lilypond.git/blob - lily/axis-group-interface.cc
Add skyline-based collision resolving for grobs above and below the staff
[lilypond.git] / lily / axis-group-interface.cc
1 /*
2   axis-group-interface.cc -- implement Axis_group_interface
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 2000--2006 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
8
9 #include "axis-group-interface.hh"
10
11 #include "align-interface.hh"
12 #include "directional-element-interface.hh"
13 #include "pointer-group-interface.hh"
14 #include "grob.hh"
15 #include "grob-array.hh"
16 #include "hara-kiri-group-spanner.hh"
17 #include "international.hh"
18 #include "item.hh"
19 #include "paper-column.hh"
20 #include "paper-score.hh"
21 #include "std-vector.hh"
22 #include "system.hh"
23 #include "warn.hh"
24
25 void
26 Axis_group_interface::add_element (Grob *me, Grob *e)
27 {
28   SCM axes = me->get_property ("axes");
29   if (!scm_is_pair (axes))
30     programming_error ("axes should be nonempty");
31
32   for (SCM ax = axes; scm_is_pair (ax); ax = scm_cdr (ax))
33     {
34       Axis a = (Axis) scm_to_int (scm_car (ax));
35
36       if (!e->get_parent (a))
37         e->set_parent (me, a);
38
39       e->set_object ((a == X_AXIS)
40                      ? ly_symbol2scm ("axis-group-parent-X")
41                      : ly_symbol2scm ("axis-group-parent-Y"),
42                      me->self_scm ());
43     }
44
45   /* must be ordered, because Align_interface also uses
46      Axis_group_interface  */
47   Pointer_group_interface::add_grob (me, ly_symbol2scm ("elements"), e);
48 }
49
50 bool
51 Axis_group_interface::has_axis (Grob *me, Axis a)
52 {
53   SCM axes = me->get_property ("axes");
54
55   return (SCM_BOOL_F != scm_memq (scm_from_int (a), axes));
56 }
57
58 Interval
59 Axis_group_interface::relative_group_extent (vector<Grob*> const &elts,
60                                              Grob *common, Axis a)
61 {
62   Interval r;
63   for (vsize i = 0; i < elts.size (); i++)
64     {
65       Grob *se = elts[i];
66       Interval dims = se->extent (common, a);
67       if (!dims.is_empty ())
68         r.unite (dims);
69     }
70   return r;
71 }
72
73 Interval
74 Axis_group_interface::cached_pure_height (Grob *me,
75                                           vector<Grob*> const &elts,
76                                           Grob *common,
77                                           int start, int end)
78 {
79   Paper_score *ps = get_root_system (me)->paper_score ();
80   vector<vsize> breaks = ps->get_break_indices ();
81   vector<Grob*> cols = ps->get_columns ();
82   vsize start_index = VPOS;
83   vsize end_index = VPOS;
84
85   for (vsize i = 0; i < breaks.size (); i++)
86     {
87       int r = Paper_column::get_rank (cols[breaks[i]]);
88       if (start == r)
89         start_index = i;
90       if (end == r)
91         end_index = i;
92     }
93
94   if (start_index == VPOS || end_index == VPOS)
95     {
96       programming_error (_ ("tried to calculate pure-height at a non-breakpoint"));
97       return Interval (0, 0);
98     }
99
100   SCM extents = me->get_property ("cached-pure-extents");
101   if (!scm_is_vector (extents))
102     {
103       extents = scm_c_make_vector (breaks.size () - 1, SCM_EOL);
104       for (vsize i = 0; i < breaks.size () - 1; i++)
105         {
106           int st = Paper_column::get_rank (cols[breaks[i]]);
107           int ed = Paper_column::get_rank (cols[breaks[i+1]]);
108           Interval iv = relative_pure_height (me, elts, common, st, ed, false);
109           scm_vector_set_x (extents, scm_from_int (i), ly_interval2scm (iv));
110         }
111       me->set_property ("cached-pure-extents", extents);
112     }
113
114   Interval ext (0, 0);
115   for (vsize i = start_index; i < end_index; i++)
116     ext.unite (ly_scm2interval (scm_c_vector_ref (extents, i)));
117   return ext;
118 }
119
120 Interval
121 Axis_group_interface::relative_pure_height (Grob *me,
122                                             vector<Grob*> const &elts,
123                                             Grob *common,
124                                             int start, int end,
125                                             bool use_cache)
126 {
127   /* It saves a _lot_ of time if we assume a VerticalAxisGroup is additive
128      (ie. height (i, k) = height (i, j) + height (j, k) for all i <= j <= k).
129      Unfortunately, it isn't always true, particularly if there is a
130      VerticalAlignment somewhere in the descendants.
131
132      Apart from PianoStaff, which has a fixed VerticalAlignment so it doesn't
133      count, the only VerticalAlignment comes from Score. This makes it
134      reasonably safe to assume that if our parent is a VerticalAlignment,
135      we can assume additivity and cache things nicely. */
136   Grob *p = me->get_parent (Y_AXIS);
137   if (use_cache && p && Align_interface::has_interface (p))
138     return Axis_group_interface::cached_pure_height (me, elts, common, start, end);
139
140   Interval r;
141
142   for (vsize i = 0; i < elts.size (); i++)
143     {
144       Interval_t<int> rank_span = elts[i]->spanned_rank_iv ();
145       Item *it = dynamic_cast<Item*> (elts[i]);
146       if (rank_span[LEFT] <= end && rank_span[RIGHT] >= start && (!it || it->pure_is_visible (start, end)))
147         {
148           Interval dims = elts[i]->pure_height (common, start, end);
149           if (!dims.is_empty ())
150             r.unite (dims);
151         }
152     }
153   return r;
154 }
155
156 MAKE_SCHEME_CALLBACK (Axis_group_interface, width, 1);
157 SCM
158 Axis_group_interface::width (SCM smob)
159 {
160   Grob *me = unsmob_grob (smob);
161   return generic_group_extent (me, X_AXIS);
162 }
163
164 MAKE_SCHEME_CALLBACK (Axis_group_interface, height, 1);
165 SCM
166 Axis_group_interface::height (SCM smob)
167 {
168   Grob *me = unsmob_grob (smob);
169   return generic_group_extent (me, Y_AXIS);
170 }
171
172 MAKE_SCHEME_CALLBACK (Axis_group_interface, pure_height, 3);
173 SCM
174 Axis_group_interface::pure_height (SCM smob, SCM start_scm, SCM end_scm)
175 {
176   int start = robust_scm2int (start_scm, 0);
177   int end = robust_scm2int (end_scm, INT_MAX);
178   Grob *me = unsmob_grob (smob);
179
180   return pure_group_height (me, start, end);
181 }
182   
183 SCM
184 Axis_group_interface::generic_group_extent (Grob *me, Axis a)
185 {
186   extract_grob_set (me, "elements", elts);
187   Grob *common = common_refpoint_of_array (elts, me, a);
188
189   Real my_coord = me->relative_coordinate (common, a);
190   Interval r (relative_group_extent (elts, common, a));
191
192   return ly_interval2scm (r - my_coord);
193 }
194
195 SCM
196 Axis_group_interface::pure_group_height (Grob *me, int start, int end)
197 {
198   Grob *common = unsmob_grob (me->get_object ("common-refpoint-of-elements"));
199
200   if (!common)
201     {
202       extract_grob_set (me, "elements", elts);
203
204       vector<Grob*> relevant_elts;
205       SCM is_relevant = ly_lily_module_constant ("pure-relevant");
206
207       for (vsize i = 0; i < elts.size (); i++)
208         {
209           if (to_boolean (scm_apply_1 (is_relevant, elts[i]->self_scm (), SCM_EOL)))
210             relevant_elts.push_back (elts[i]);
211
212           Item *it = dynamic_cast<Item*> (elts[i]);
213           Direction d = LEFT;
214           if (it)
215             do
216               {
217                 Item *piece = it->find_prebroken_piece (d);
218                 if (piece && to_boolean (scm_apply_1 (is_relevant, piece->self_scm (), SCM_EOL)))
219                   relevant_elts.push_back (piece);
220               }
221             while (flip (&d) != LEFT);
222         }
223
224       common = common_refpoint_of_array (relevant_elts, me, Y_AXIS);
225       me->set_object ("common-refpoint-of-elements", common->self_scm ());
226
227       SCM ga_scm = Grob_array::make_array ();
228       Grob_array *ga = unsmob_grob_array (ga_scm);
229       ga->set_array (relevant_elts);
230       me->set_object ("pure-relevant-elements", ga_scm);
231     }
232
233   extract_grob_set (me, "pure-relevant-elements", elts);
234   Real my_coord = me->relative_coordinate (common, Y_AXIS);
235   Interval r (relative_pure_height (me, elts, common, start, end, true));
236
237   return ly_interval2scm (r - my_coord);
238 }
239
240 void
241 Axis_group_interface::get_children (Grob *me, vector<Grob*> *found)
242 {
243   found->push_back (me);
244
245   if (!has_interface (me))
246     return;
247
248   extract_grob_set (me, "elements", elements);
249   for (vsize i = 0; i < elements.size (); i++)
250     {
251       Grob *e = elements[i];
252       Axis_group_interface::get_children (e, found);
253     }
254 }
255
256 bool
257 staff_priority_less (Grob * const &g1, Grob * const &g2)
258 {
259   int priority_1 = robust_scm2int (g1->get_property ("outside-staff-priority"), INT_MIN);
260   int priority_2 = robust_scm2int (g2->get_property ("outside-staff-priority"), INT_MIN);
261
262   if (priority_1 < priority_2)
263     return true;
264   else if (priority_1 > priority_2)
265     return false;
266
267   /* if there is no preference in staff priority, choose the one with the lower rank */
268   int rank_1 = g1->spanned_rank_iv ()[LEFT];
269   int rank_2 = g2->spanned_rank_iv ()[LEFT];
270   return rank_1 < rank_2;
271 }
272
273 MAKE_SCHEME_CALLBACK (Axis_group_interface, skyline_spacing, 1)
274 SCM
275 Axis_group_interface::skyline_spacing (SCM smob)
276 {
277   Grob *me = unsmob_grob (smob);
278   extract_grob_set (me, "elements", ro_elements);
279   vector<Grob*> elements (ro_elements);
280   vector_sort (elements, staff_priority_less);
281   Grob *x_common = common_refpoint_of_array (elements, me, X_AXIS);
282
283   vsize i = 0;
284   vector<Box> boxes;
285   for (i = 0; i < elements.size ()
286          && !scm_is_number (elements[i]->get_property ("outside-staff-priority")); i++)
287     boxes.push_back (Box (elements[i]->extent (x_common, X_AXIS),
288                           elements[i]->extent (me, Y_AXIS)));
289
290   Drul_array<Skyline> skylines (Skyline (boxes, X_AXIS, DOWN),
291                                 Skyline (boxes, X_AXIS, UP));
292   for (; i < elements.size (); i++)
293     {
294       Direction dir = get_grob_direction (elements[i]);
295       if (dir == CENTER)
296         {
297           warning (_ ("an outside-staff object should have a direction"));
298           continue;
299         }
300
301       Box b (elements[i]->extent (x_common, X_AXIS),
302              elements[i]->extent (me, Y_AXIS));
303       boxes.clear ();
304       boxes.push_back (b);
305       Skyline other = Skyline (boxes, X_AXIS, -dir);
306       Real dist = skylines[dir].distance (other);
307
308       if (dist > 0)
309         {
310           b.translate (Offset (0, dir*dist));
311           elements[i]->translate_axis (dir*dist, Y_AXIS);
312         }
313       skylines[dir].insert (b, X_AXIS);
314     }
315   return SCM_UNSPECIFIED;
316 }
317
318 ADD_INTERFACE (Axis_group_interface, "axis-group-interface",
319
320                "An object that groups other layout objects.",
321
322                /* properties */
323                "axes "
324                "elements "
325                "common-refpoint-of-elements "
326                "pure-relevant-elements "
327                "cached-pure-extents "
328                );