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