]> git.donarmstrong.com Git - lilypond.git/blob - lily/hara-kiri-group-spanner.cc
Issue 4550 (1/2) Avoid "using namespace std;" in included files
[lilypond.git] / lily / hara-kiri-group-spanner.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1998--2015 Jan Nieuwenhuizen <janneke@gnu.org>
5   Han-Wen Nienhuys <hanwen@xs4all.nl>
6
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.
11
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.
16
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/>.
19 */
20
21 #include "hara-kiri-group-spanner.hh"
22
23 #include "paper-column.hh"
24 #include "pointer-group-interface.hh"
25 #include "axis-group-interface.hh"
26 #include "spanner.hh"
27 #include "warn.hh"
28
29 using std::vector;
30
31 MAKE_SCHEME_CALLBACK (Hara_kiri_group_spanner, y_extent, 1);
32 SCM
33 Hara_kiri_group_spanner::y_extent (SCM smob)
34 {
35   Grob *me = unsmob<Grob> (smob);
36   consider_suicide (me);
37   return Axis_group_interface::generic_group_extent (me, Y_AXIS);
38 }
39
40 MAKE_SCHEME_CALLBACK (Hara_kiri_group_spanner, calc_skylines, 1);
41 SCM
42 Hara_kiri_group_spanner::calc_skylines (SCM smob)
43 {
44   Grob *me = unsmob<Grob> (smob);
45   consider_suicide (me);
46   return Axis_group_interface::calc_skylines (smob);
47 }
48
49 MAKE_SCHEME_CALLBACK (Hara_kiri_group_spanner, pure_height, 3);
50 SCM
51 Hara_kiri_group_spanner::pure_height (SCM smob, SCM start_scm, SCM end_scm)
52 {
53   Grob *me = unsmob<Grob> (smob);
54   int start = robust_scm2int (start_scm, 0);
55   int end = robust_scm2int (end_scm, INT_MAX);
56
57   if (request_suicide (me, start, end))
58     return ly_interval2scm (Interval ());
59
60   return ly_interval2scm (Axis_group_interface::pure_group_height (me, start, end));
61 }
62
63 /* there is probably a way that doesn't involve re-implementing a binary
64    search (I would love some proper closures right now) */
65 bool find_in_range (SCM vector, int low, int hi, int min, int max)
66 {
67   if (low >= hi)
68     return false;
69
70   int mid = low + (hi - low) / 2;
71   int val = scm_to_int (scm_c_vector_ref (vector, mid));
72   if (val >= min && val <= max)
73     return true;
74   else if (val < min)
75     return find_in_range (vector, mid + 1, hi, min, max);
76   return find_in_range (vector, low, mid, min, max);
77 }
78
79 bool
80 Hara_kiri_group_spanner::request_suicide (Grob *me, int start, int end)
81 {
82   extract_grob_set (me, "make-dead-when", foes);
83   for (vsize i = 0; i < foes.size (); i++)
84     if (foes[i]->is_live () && !request_suicide_alone (foes[i], start, end))
85       return true;
86
87   if (!request_suicide_alone (me, start, end))
88     return false;
89
90   extract_grob_set (me, "keep-alive-with", friends);
91   for (vsize i = 0; i < friends.size (); ++i)
92     if (friends[i]->is_live () && !request_suicide_alone (friends[i], start, end))
93       return false;
94
95   return true;
96 }
97
98 bool
99 Hara_kiri_group_spanner::request_suicide_alone (Grob *me, int start, int end)
100 {
101   if (!to_boolean (me->get_property ("remove-empty")))
102     return false;
103
104   bool remove_first = to_boolean (me->get_property ("remove-first"));
105   if (!remove_first && start <= 0)
106     return false;
107
108   SCM important = me->get_property ("important-column-ranks");
109   if (scm_is_vector (important))
110     {
111       int len = scm_c_vector_length (important);
112       if (find_in_range (important, 0, len, start, end))
113         return false;
114     }
115   else /* build the important-columns-cache */
116     {
117       extract_grob_set (me, "items-worth-living", worth);
118       vector<int> ranks;
119
120       for (vsize i = 0; i < worth.size (); i++)
121         {
122           Interval_t<int> iv = worth[i]->spanned_rank_interval ();
123           for (int j = iv[LEFT]; j <= iv[RIGHT]; j++)
124             ranks.push_back (j);
125         }
126       vector_sort (ranks, less<int> ());
127       uniq (ranks);
128
129       SCM scm_vec = scm_c_make_vector (ranks.size (), SCM_EOL);
130       for (vsize i = 0; i < ranks.size (); i++)
131         scm_vector_set_x (scm_vec, scm_from_int (i), scm_from_int (ranks[i]));
132       me->set_property ("important-column-ranks", scm_vec);
133
134       return request_suicide (me, start, end);
135     }
136
137   return true;
138 }
139
140 void
141 Hara_kiri_group_spanner::consider_suicide (Grob *me)
142 {
143   Spanner *sp = dynamic_cast<Spanner *> (me);
144   int left = 0;
145   int right = INT_MAX;
146   if (Item *l = sp->get_bound (LEFT))
147     left = l->get_column ()->get_rank ();
148   if (Item *r = sp->get_bound (RIGHT))
149     right = r->get_column ()->get_rank ();
150   if (!request_suicide (me, left, right))
151     return;
152
153   vector<Grob *> childs;
154   Axis_group_interface::get_children (me, &childs);
155   for (vsize i = 0; i < childs.size (); i++)
156     childs[i]->suicide ();
157
158   /*
159     very appropriate name here :-)
160   */
161   me->suicide ();
162 }
163
164 /*
165   We can't rely on offsets and dimensions of elements in a hara-kiri
166   group. Use a callback to make sure that hara-kiri has been done
167   before asking for offsets.  */
168 MAKE_SCHEME_CALLBACK (Hara_kiri_group_spanner, force_hara_kiri_callback, 1);
169 SCM
170 Hara_kiri_group_spanner::force_hara_kiri_callback (SCM smob)
171 {
172   Grob *me = unsmob<Grob> (smob);
173   consider_suicide (me);
174   return scm_from_double (0.0);
175 }
176
177 MAKE_SCHEME_CALLBACK (Hara_kiri_group_spanner, force_hara_kiri_in_y_parent_callback, 1);
178 SCM
179 Hara_kiri_group_spanner::force_hara_kiri_in_y_parent_callback (SCM smob)
180 {
181   Grob *daughter = unsmob<Grob> (smob);
182   force_hara_kiri_callback (daughter->get_parent (Y_AXIS)->self_scm ());
183   return scm_from_double (0.0);
184 }
185
186 void
187 Hara_kiri_group_spanner::add_interesting_item (Grob *me, Grob *n)
188 {
189   Pointer_group_interface::add_unordered_grob (me, ly_symbol2scm ("items-worth-living"), n);
190 }
191
192 ADD_INTERFACE (Hara_kiri_group_spanner,
193                "A group spanner that keeps track of interesting items.  If it"
194                " doesn't contain any after line breaking, it removes itself"
195                " and all its children.  Children may be prioritized in layers"
196                " via @code{remove-layer}, in which case only the"
197                " lowest-numbered non-empty layer is retained.",
198
199                /* properties */
200                "items-worth-living "
201                "important-column-ranks "
202                "keep-alive-with "
203                "make-dead-when "
204                "remove-empty "
205                "remove-first "
206                "remove-layer "
207               );