Use scalar instead of embedded_scm for context mod overrides.
[lilypond/mpolesky.git] / lily / hara-kiri-group-spanner.cc
blobacd65da2f12bde372440c3add767be353425a724
1 /*
2 hara-kiri-vertical-group-spanner.cc -- implement Hara_kiri_group_spanner
4 source file of the GNU LilyPond music typesetter
6 (c) 1998--2009 Jan Nieuwenhuizen <janneke@gnu.org>
7 Han-Wen Nienhuys <hanwen@xs4all.nl>
8 */
10 #include "hara-kiri-group-spanner.hh"
12 #include "paper-column.hh"
13 #include "pointer-group-interface.hh"
14 #include "axis-group-interface.hh"
15 #include "spanner.hh"
16 #include "warn.hh"
18 MAKE_SCHEME_CALLBACK (Hara_kiri_group_spanner, y_extent, 1);
19 SCM
20 Hara_kiri_group_spanner::y_extent (SCM smob)
22 Grob *me = unsmob_grob (smob);
23 consider_suicide (me);
24 return Axis_group_interface::generic_group_extent (me, Y_AXIS);
27 MAKE_SCHEME_CALLBACK (Hara_kiri_group_spanner, calc_skylines, 1);
28 SCM
29 Hara_kiri_group_spanner::calc_skylines (SCM smob)
31 Grob *me = unsmob_grob (smob);
32 consider_suicide (me);
33 return Axis_group_interface::calc_skylines (smob);
36 MAKE_SCHEME_CALLBACK (Hara_kiri_group_spanner, pure_height, 3);
37 SCM
38 Hara_kiri_group_spanner::pure_height (SCM smob, SCM start_scm, SCM end_scm)
40 Grob *me = unsmob_grob (smob);
41 int start = robust_scm2int (start_scm, 0);
42 int end = robust_scm2int (end_scm, INT_MAX);
44 if (request_suicide (me, start, end))
45 return ly_interval2scm (Interval ());
47 return ly_interval2scm (Axis_group_interface::pure_group_height (me, start, end));
50 /* there is probably a way that doesn't involve re-implementing a binary
51 search (I would love some proper closures right now) */
52 bool find_in_range (SCM vector, int low, int hi, int min, int max)
54 if (low >= hi)
55 return false;
57 int mid = low + (hi - low) / 2;
58 int val = scm_to_int (scm_c_vector_ref (vector, mid));
59 if (val >= min && val <= max)
60 return true;
61 else if (val < min)
62 return find_in_range (vector, mid+1, hi, min, max);
63 return find_in_range (vector, low, mid, min, max);
66 bool
67 Hara_kiri_group_spanner::request_suicide (Grob *me, int start, int end)
69 if (!to_boolean (me->get_property ("remove-empty")))
70 return false;
72 bool remove_first = to_boolean (me->get_property ("remove-first"));
73 if (!remove_first && start <= 0)
74 return false;
76 SCM important = me->get_property ("important-column-ranks");
77 if (scm_is_vector (important))
79 int len = scm_c_vector_length (important);
80 if (find_in_range (important, 0, len, start, end))
81 return false;
83 else /* build the important-columns-cache */
85 extract_grob_set (me, "items-worth-living", worth);
86 vector<int> ranks;
88 for (vsize i = 0; i < worth.size (); i++)
90 Interval_t<int> iv = worth[i]->spanned_rank_interval ();
91 for (int j = iv[LEFT]; j <= iv[RIGHT]; j++)
92 ranks.push_back (j);
94 vector_sort (ranks, less<int> ());
95 uniq (ranks);
97 SCM scm_vec = scm_c_make_vector (ranks.size (), SCM_EOL);
98 for (vsize i = 0; i < ranks.size (); i++)
99 scm_vector_set_x (scm_vec, scm_from_int (i), scm_from_int (ranks[i]));
100 me->set_property ("important-column-ranks", scm_vec);
102 return request_suicide (me, start, end);
105 return true;
108 void
109 Hara_kiri_group_spanner::consider_suicide (Grob *me)
111 Spanner *sp = dynamic_cast<Spanner*> (me);
112 int left = sp->get_bound (LEFT)->get_column ()->get_rank ();
113 int right = sp->get_bound (RIGHT)->get_column ()->get_rank ();
114 if (!request_suicide (me, left, right))
115 return;
117 vector<Grob*> childs;
118 Axis_group_interface::get_children (me, &childs);
119 for (vsize i = 0; i < childs.size (); i++)
120 childs[i]->suicide ();
123 very appropriate name here :-)
125 me->suicide ();
129 We can't rely on offsets and dimensions of elements in a hara-kiri
130 group. Use a callback to make sure that hara-kiri has been done
131 before asking for offsets. */
132 MAKE_SCHEME_CALLBACK (Hara_kiri_group_spanner, force_hara_kiri_callback, 1);
134 Hara_kiri_group_spanner::force_hara_kiri_callback (SCM smob)
136 Grob *me = unsmob_grob (smob);
137 consider_suicide (me);
138 return scm_from_double (0.0);
141 MAKE_SCHEME_CALLBACK (Hara_kiri_group_spanner, force_hara_kiri_in_y_parent_callback, 1);
143 Hara_kiri_group_spanner::force_hara_kiri_in_y_parent_callback (SCM smob)
145 Grob *daughter = unsmob_grob (smob);
146 force_hara_kiri_callback (daughter->get_parent (Y_AXIS)->self_scm ());
147 return scm_from_double (0.0);
150 void
151 Hara_kiri_group_spanner::add_interesting_item (Grob *me, Grob *n)
153 Pointer_group_interface::add_unordered_grob (me, ly_symbol2scm ("items-worth-living"), n);
156 ADD_INTERFACE (Hara_kiri_group_spanner,
157 "A group spanner that keeps track of interesting items. If it"
158 " doesn't contain any after line breaking, it removes itself"
159 " and all its children.",
161 /* properties */
162 "items-worth-living "
163 "important-column-ranks "
164 "remove-empty "
165 "remove-first "