]> git.donarmstrong.com Git - lilypond.git/blob - lily/hara-kiri-group-spanner.cc
Web-ja: update introduction
[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 MAKE_SCHEME_CALLBACK (Hara_kiri_group_spanner, y_extent, 1);
30 SCM
31 Hara_kiri_group_spanner::y_extent (SCM smob)
32 {
33   Grob *me = unsmob<Grob> (smob);
34   consider_suicide (me);
35   return Axis_group_interface::generic_group_extent (me, Y_AXIS);
36 }
37
38 MAKE_SCHEME_CALLBACK (Hara_kiri_group_spanner, calc_skylines, 1);
39 SCM
40 Hara_kiri_group_spanner::calc_skylines (SCM smob)
41 {
42   Grob *me = unsmob<Grob> (smob);
43   consider_suicide (me);
44   return Axis_group_interface::calc_skylines (smob);
45 }
46
47 MAKE_SCHEME_CALLBACK (Hara_kiri_group_spanner, pure_height, 3);
48 SCM
49 Hara_kiri_group_spanner::pure_height (SCM smob, SCM start_scm, SCM end_scm)
50 {
51   Grob *me = unsmob<Grob> (smob);
52   int start = robust_scm2int (start_scm, 0);
53   int end = robust_scm2int (end_scm, INT_MAX);
54
55   if (request_suicide (me, start, end))
56     return ly_interval2scm (Interval ());
57
58   return ly_interval2scm (Axis_group_interface::pure_group_height (me, start, end));
59 }
60
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)
64 {
65   if (low >= hi)
66     return false;
67
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)
71     return true;
72   else if (val < min)
73     return find_in_range (vector, mid + 1, hi, min, max);
74   return find_in_range (vector, low, mid, min, max);
75 }
76
77 bool
78 Hara_kiri_group_spanner::request_suicide (Grob *me, int start, int end)
79 {
80   extract_grob_set (me, "make-dead-when", foes);
81   for (vsize i = 0; i < foes.size (); i++)
82     if (foes[i]->is_live () && !request_suicide_alone (foes[i], start, end))
83       return true;
84
85   if (!request_suicide_alone (me, start, end))
86     return false;
87
88   extract_grob_set (me, "keep-alive-with", friends);
89   for (vsize i = 0; i < friends.size (); ++i)
90     if (friends[i]->is_live () && !request_suicide_alone (friends[i], start, end))
91       return false;
92
93   return true;
94 }
95
96 bool
97 Hara_kiri_group_spanner::request_suicide_alone (Grob *me, int start, int end)
98 {
99   if (!to_boolean (me->get_property ("remove-empty")))
100     return false;
101
102   bool remove_first = to_boolean (me->get_property ("remove-first"));
103   if (!remove_first && start <= 0)
104     return false;
105
106   SCM important = me->get_property ("important-column-ranks");
107   if (scm_is_vector (important))
108     {
109       int len = scm_c_vector_length (important);
110       if (find_in_range (important, 0, len, start, end))
111         return false;
112     }
113   else /* build the important-columns-cache */
114     {
115       extract_grob_set (me, "items-worth-living", worth);
116       vector<int> ranks;
117
118       for (vsize i = 0; i < worth.size (); i++)
119         {
120           Interval_t<int> iv = worth[i]->spanned_rank_interval ();
121           for (int j = iv[LEFT]; j <= iv[RIGHT]; j++)
122             ranks.push_back (j);
123         }
124       vector_sort (ranks, less<int> ());
125       uniq (ranks);
126
127       SCM scm_vec = scm_c_make_vector (ranks.size (), SCM_EOL);
128       for (vsize i = 0; i < ranks.size (); i++)
129         scm_vector_set_x (scm_vec, scm_from_int (i), scm_from_int (ranks[i]));
130       me->set_property ("important-column-ranks", scm_vec);
131
132       return request_suicide (me, start, end);
133     }
134
135   return true;
136 }
137
138 void
139 Hara_kiri_group_spanner::consider_suicide (Grob *me)
140 {
141   Spanner *sp = dynamic_cast<Spanner *> (me);
142   int left = 0;
143   int right = INT_MAX;
144   if (Item *l = sp->get_bound (LEFT))
145     left = l->get_column ()->get_rank ();
146   if (Item *r = sp->get_bound (RIGHT))
147     right = r->get_column ()->get_rank ();
148   if (!request_suicide (me, left, right))
149     return;
150
151   vector<Grob *> childs;
152   Axis_group_interface::get_children (me, &childs);
153   for (vsize i = 0; i < childs.size (); i++)
154     childs[i]->suicide ();
155
156   /*
157     very appropriate name here :-)
158   */
159   me->suicide ();
160 }
161
162 /*
163   We can't rely on offsets and dimensions of elements in a hara-kiri
164   group. Use a callback to make sure that hara-kiri has been done
165   before asking for offsets.  */
166 MAKE_SCHEME_CALLBACK (Hara_kiri_group_spanner, force_hara_kiri_callback, 1);
167 SCM
168 Hara_kiri_group_spanner::force_hara_kiri_callback (SCM smob)
169 {
170   Grob *me = unsmob<Grob> (smob);
171   consider_suicide (me);
172   return scm_from_double (0.0);
173 }
174
175 MAKE_SCHEME_CALLBACK (Hara_kiri_group_spanner, force_hara_kiri_in_y_parent_callback, 1);
176 SCM
177 Hara_kiri_group_spanner::force_hara_kiri_in_y_parent_callback (SCM smob)
178 {
179   Grob *daughter = unsmob<Grob> (smob);
180   force_hara_kiri_callback (daughter->get_parent (Y_AXIS)->self_scm ());
181   return scm_from_double (0.0);
182 }
183
184 void
185 Hara_kiri_group_spanner::add_interesting_item (Grob *me, Grob *n)
186 {
187   Pointer_group_interface::add_unordered_grob (me, ly_symbol2scm ("items-worth-living"), n);
188 }
189
190 ADD_INTERFACE (Hara_kiri_group_spanner,
191                "A group spanner that keeps track of interesting items.  If it"
192                " doesn't contain any after line breaking, it removes itself"
193                " and all its children.  Greater control can be exercised via"
194                " @code{remove-layer} which can prioritize layers so only the"
195                " lowest-numbered non-empty layer is retained; make the layer"
196                " independent of the group; or make it dependent on any other"
197                " member of the group",
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               );