2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 1998--2010 Jan Nieuwenhuizen <janneke@gnu.org>
5 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 LilyPond is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 LilyPond is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with LilyPond. If not, see <http://www.gnu.org/licenses/>.
21 #include "hara-kiri-group-spanner.hh"
23 #include "paper-column.hh"
24 #include "pointer-group-interface.hh"
25 #include "axis-group-interface.hh"
29 MAKE_SCHEME_CALLBACK (Hara_kiri_group_spanner
, y_extent
, 1);
31 Hara_kiri_group_spanner::y_extent (SCM smob
)
33 Grob
*me
= unsmob_grob (smob
);
34 consider_suicide (me
);
35 return Axis_group_interface::generic_group_extent (me
, Y_AXIS
);
38 MAKE_SCHEME_CALLBACK (Hara_kiri_group_spanner
, calc_skylines
, 1);
40 Hara_kiri_group_spanner::calc_skylines (SCM smob
)
42 Grob
*me
= unsmob_grob (smob
);
43 consider_suicide (me
);
44 return Axis_group_interface::calc_skylines (smob
);
47 MAKE_SCHEME_CALLBACK (Hara_kiri_group_spanner
, pure_height
, 3);
49 Hara_kiri_group_spanner::pure_height (SCM smob
, SCM start_scm
, SCM end_scm
)
51 Grob
*me
= unsmob_grob (smob
);
52 int start
= robust_scm2int (start_scm
, 0);
53 int end
= robust_scm2int (end_scm
, INT_MAX
);
55 if (request_suicide (me
, start
, end
))
56 return ly_interval2scm (Interval ());
58 return ly_interval2scm (Axis_group_interface::pure_group_height (me
, start
, end
));
61 /* there is probably a way that doesn't involve re-implementing a binary
62 search (I would love some proper closures right now) */
63 bool find_in_range (SCM vector
, int low
, int hi
, int min
, int max
)
68 int mid
= low
+ (hi
- low
) / 2;
69 int val
= scm_to_int (scm_c_vector_ref (vector
, mid
));
70 if (val
>= min
&& val
<= max
)
73 return find_in_range (vector
, mid
+1, hi
, min
, max
);
74 return find_in_range (vector
, low
, mid
, min
, max
);
78 Hara_kiri_group_spanner::request_suicide (Grob
*me
, int start
, int end
)
80 if (!to_boolean (me
->get_property ("remove-empty")))
83 bool remove_first
= to_boolean (me
->get_property ("remove-first"));
84 if (!remove_first
&& start
<= 0)
87 SCM important
= me
->get_property ("important-column-ranks");
88 if (scm_is_vector (important
))
90 int len
= scm_c_vector_length (important
);
91 if (find_in_range (important
, 0, len
, start
, end
))
94 else /* build the important-columns-cache */
96 extract_grob_set (me
, "items-worth-living", worth
);
99 for (vsize i
= 0; i
< worth
.size (); i
++)
101 Interval_t
<int> iv
= worth
[i
]->spanned_rank_interval ();
102 for (int j
= iv
[LEFT
]; j
<= iv
[RIGHT
]; j
++)
105 vector_sort (ranks
, less
<int> ());
108 SCM scm_vec
= scm_c_make_vector (ranks
.size (), SCM_EOL
);
109 for (vsize i
= 0; i
< ranks
.size (); i
++)
110 scm_vector_set_x (scm_vec
, scm_from_int (i
), scm_from_int (ranks
[i
]));
111 me
->set_property ("important-column-ranks", scm_vec
);
113 return request_suicide (me
, start
, end
);
120 Hara_kiri_group_spanner::consider_suicide (Grob
*me
)
122 Spanner
*sp
= dynamic_cast<Spanner
*> (me
);
123 int left
= sp
->get_bound (LEFT
)->get_column ()->get_rank ();
124 int right
= sp
->get_bound (RIGHT
)->get_column ()->get_rank ();
125 if (!request_suicide (me
, left
, right
))
128 vector
<Grob
*> childs
;
129 Axis_group_interface::get_children (me
, &childs
);
130 for (vsize i
= 0; i
< childs
.size (); i
++)
131 childs
[i
]->suicide ();
134 very appropriate name here :-)
140 We can't rely on offsets and dimensions of elements in a hara-kiri
141 group. Use a callback to make sure that hara-kiri has been done
142 before asking for offsets. */
143 MAKE_SCHEME_CALLBACK (Hara_kiri_group_spanner
, force_hara_kiri_callback
, 1);
145 Hara_kiri_group_spanner::force_hara_kiri_callback (SCM smob
)
147 Grob
*me
= unsmob_grob (smob
);
148 consider_suicide (me
);
149 return scm_from_double (0.0);
152 MAKE_SCHEME_CALLBACK (Hara_kiri_group_spanner
, force_hara_kiri_in_y_parent_callback
, 1);
154 Hara_kiri_group_spanner::force_hara_kiri_in_y_parent_callback (SCM smob
)
156 Grob
*daughter
= unsmob_grob (smob
);
157 force_hara_kiri_callback (daughter
->get_parent (Y_AXIS
)->self_scm ());
158 return scm_from_double (0.0);
162 Hara_kiri_group_spanner::add_interesting_item (Grob
*me
, Grob
*n
)
164 Pointer_group_interface::add_unordered_grob (me
, ly_symbol2scm ("items-worth-living"), n
);
167 ADD_INTERFACE (Hara_kiri_group_spanner
,
168 "A group spanner that keeps track of interesting items. If it"
169 " doesn't contain any after line breaking, it removes itself"
170 " and all its children.",
173 "items-worth-living "
174 "important-column-ranks "