]> git.donarmstrong.com Git - lilypond.git/blob - lily/axis-group-interface.cc
*** empty log message ***
[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 "pointer-group-interface.hh"
13 #include "grob.hh"
14 #include "grob-array.hh"
15 #include "hara-kiri-group-spanner.hh"
16 #include "international.hh"
17 #include "item.hh"
18 #include "paper-column.hh"
19 #include "paper-score.hh"
20 #include "system.hh"
21 #include "warn.hh"
22
23 void
24 Axis_group_interface::add_element (Grob *me, Grob *e)
25 {
26   SCM axes = me->get_property ("axes");
27   if (!scm_is_pair (axes))
28     programming_error ("axes should be nonempty");
29
30   for (SCM ax = axes; ax != SCM_EOL; ax = scm_cdr (ax))
31     {
32       Axis a = (Axis) scm_to_int (scm_car (ax));
33
34       if (!e->get_parent (a))
35         e->set_parent (me, a);
36
37       e->internal_set_object ((a == X_AXIS)
38                               ? ly_symbol2scm ("axis-group-parent-X")
39                               : ly_symbol2scm ("axis-group-parent-Y"),
40                               me->self_scm ());
41     }
42
43   /* must be ordered, because Align_interface also uses
44      Axis_group_interface  */
45   Pointer_group_interface::add_grob (me, ly_symbol2scm ("elements"), e);
46 }
47
48 bool
49 Axis_group_interface::has_axis (Grob *me, Axis a)
50 {
51   SCM axes = me->get_property ("axes");
52
53   return (SCM_BOOL_F != scm_memq (scm_from_int (a), axes));
54 }
55
56 Interval
57 Axis_group_interface::relative_group_extent (vector<Grob*> const &elts,
58                                              Grob *common, Axis a)
59 {
60   Interval r;
61   for (vsize i = 0; i < elts.size (); i++)
62     {
63       Grob *se = elts[i];
64       Interval dims = se->extent (common, a);
65       if (!dims.is_empty ())
66         r.unite (dims);
67     }
68   return r;
69 }
70
71 Interval
72 Axis_group_interface::cached_pure_height (Grob *me,
73                                           vector<Grob*> const &elts,
74                                           Grob *common,
75                                           int start, int end)
76 {
77   Paper_score *ps = get_root_system (me)->paper_score ();
78   vector<vsize> breaks = ps->get_break_indices ();
79   vector<Grob*> cols = ps->get_columns ();
80   vsize start_index = VPOS;
81   vsize end_index = VPOS;
82
83   for (vsize i = 0; i < breaks.size (); i++)
84     {
85       int r = Paper_column::get_rank (cols[breaks[i]]);
86       if (start == r)
87         start_index = i;
88       if (end == r)
89         end_index = i;
90     }
91
92   if (start_index == VPOS || end_index == VPOS)
93     {
94       programming_error (_ ("tried to calculate pure-height at a non-breakpoint"));
95       return Interval (0, 0);
96     }
97
98   SCM extents = me->get_property ("cached-pure-extents");
99   if (!scm_is_vector (extents))
100     {
101       extents = scm_c_make_vector (breaks.size () - 1, SCM_EOL);
102       for (vsize i = 0; i < breaks.size () - 1; i++)
103         {
104           int st = Paper_column::get_rank (cols[breaks[i]]);
105           int ed = Paper_column::get_rank (cols[breaks[i+1]]);
106           Interval iv = relative_pure_height (me, elts, common, st, ed, false);
107           scm_vector_set_x (extents, scm_from_int (i), ly_interval2scm (iv));
108         }
109       me->set_property ("cached-pure-extents", extents);
110     }
111
112   Interval ext (0, 0);
113   for (vsize i = start_index; i < end_index; i++)
114     ext.unite (ly_scm2interval (scm_c_vector_ref (extents, i)));
115   return ext;
116 }
117
118 Interval
119 Axis_group_interface::relative_pure_height (Grob *me,
120                                             vector<Grob*> const &elts,
121                                             Grob *common,
122                                             int start, int end,
123                                             bool use_cache)
124 {
125   /* It saves a _lot_ of time if we assume a VerticalAxisGroup is additive
126      (ie. height (i, k) = height (i, j) + height (j, k) for all i <= j <= k).
127      Unfortunately, it isn't always true, particularly if there is a
128      VerticalAlignment somewhere in the descendants.
129
130      Apart from PianoStaff, which has a fixed VerticalAlignment so it doesn't
131      count, the only VerticalAlignment comes from Score. This makes it
132      reasonably safe to assume that if our parent is a VerticalAlignment,
133      we can assume additivity and cache things nicely. */
134   Grob *p = me->get_parent (Y_AXIS);
135   if (use_cache && p && Align_interface::has_interface (p))
136     return Axis_group_interface::cached_pure_height (me, elts, common, start, end);
137
138   Interval r;
139
140   for (vsize i = 0; i < elts.size (); i++)
141     {
142       Interval_t<int> rank_span = elts[i]->spanned_rank_iv ();
143       Item *it = dynamic_cast<Item*> (elts[i]);
144       if (rank_span[LEFT] <= end && rank_span[RIGHT] >= start && (!it || it->pure_is_visible (start, end)))
145         {
146           Interval dims = elts[i]->pure_height (common, start, end);
147           if (!dims.is_empty ())
148             {
149               r.unite (dims);
150               /*
151               message (_f ("axis-group (%d-%d) %s has child %s with height (%.2f,%.2f), my height is now (%.2f,%.2f)",
152                            start, end,
153                            me->name ().c_str (), elts[i]->name ().c_str (), dims[DOWN], dims[UP], r[DOWN], r[UP]));
154               */
155             }
156         }
157     }
158   return r;
159 }
160
161 MAKE_SCHEME_CALLBACK (Axis_group_interface, width, 1);
162 SCM
163 Axis_group_interface::width (SCM smob)
164 {
165   Grob *me = unsmob_grob (smob);
166   return generic_group_extent (me, X_AXIS);
167 }
168
169 MAKE_SCHEME_CALLBACK (Axis_group_interface, height, 1);
170 SCM
171 Axis_group_interface::height (SCM smob)
172 {
173   Grob *me = unsmob_grob (smob);
174   return generic_group_extent (me, Y_AXIS);
175 }
176
177 MAKE_SCHEME_CALLBACK (Axis_group_interface, pure_height, 3);
178 SCM
179 Axis_group_interface::pure_height (SCM smob, SCM start_scm, SCM end_scm)
180 {
181   int start = robust_scm2int (start_scm, 0);
182   int end = robust_scm2int (end_scm, INT_MAX);
183   Grob *me = unsmob_grob (smob);
184
185   return pure_group_height (me, start, end);
186 }
187   
188 SCM
189 Axis_group_interface::generic_group_extent (Grob *me, Axis a)
190 {
191   extract_grob_set (me, "elements", elts);
192   Grob *common = common_refpoint_of_array (elts, me, a);
193
194   Real my_coord = me->relative_coordinate (common, a);
195   Interval r (relative_group_extent (elts, common, a));
196
197   return ly_interval2scm (r - my_coord);
198 }
199
200 SCM
201 Axis_group_interface::pure_group_height (Grob *me, int start, int end)
202 {
203   Grob *common = unsmob_grob (me->get_object ("common-refpoint-of-elements"));
204
205   if (!common)
206     {
207       extract_grob_set (me, "elements", elts);
208
209       vector<Grob*> relevant_elts;
210       SCM is_relevant = ly_lily_module_constant ("pure-relevant");
211
212       for (vsize i = 0; i < elts.size (); i++)
213         {
214           if (to_boolean (scm_apply_1 (is_relevant, elts[i]->self_scm (), SCM_EOL)))
215             relevant_elts.push_back (elts[i]);
216
217           Item *it = dynamic_cast<Item*> (elts[i]);
218           Direction d = LEFT;
219           if (it)
220             do
221               {
222                 Item *piece = it->find_prebroken_piece (d);
223                 if (piece && to_boolean (scm_apply_1 (is_relevant, piece->self_scm (), SCM_EOL)))
224                   relevant_elts.push_back (piece);
225               }
226             while (flip (&d) != LEFT);
227         }
228
229       common = common_refpoint_of_array (relevant_elts, me, Y_AXIS);
230       me->set_object ("common-refpoint-of-elements", common->self_scm ());
231
232       SCM ga_scm = Grob_array::make_array ();
233       Grob_array *ga = unsmob_grob_array (ga_scm);
234       ga->set_array (relevant_elts);
235       me->set_object ("pure-relevant-elements", ga_scm);
236     }
237
238   extract_grob_set (me, "pure-relevant-elements", elts);
239   Real my_coord = me->relative_coordinate (common, Y_AXIS);
240   Interval r (relative_pure_height (me, elts, common, start, end, true));
241
242   return ly_interval2scm (r - my_coord);
243 }
244
245 void
246 Axis_group_interface::get_children (Grob *me, vector<Grob*> *found)
247 {
248   found->push_back (me);
249
250   if (!has_interface (me))
251     return;
252
253   extract_grob_set (me, "elements", elements);
254   for (vsize i = 0; i < elements.size (); i++)
255     {
256       Grob *e = elements[i];
257       Axis_group_interface::get_children (e, found);
258     }
259 }
260
261 ADD_INTERFACE (Axis_group_interface, "axis-group-interface",
262
263                "An object that groups other layout objects.",
264
265                /* properties */
266                "axes "
267                "elements "
268                "common-refpoint-of-elements "
269                "pure-relevant-elements "
270                "cached-pure-extents "
271                );