]> git.donarmstrong.com Git - lilypond.git/blob - lily/spacing-determine-loose-columns.cc
00e6e125dd4bb0b37fda9539106b57e3064fdaa8
[lilypond.git] / lily / spacing-determine-loose-columns.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2005--2015 Han-Wen Nienhuys <hanwen@xs4all.nl>
5
6   LilyPond is free software: you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation, either version 3 of the License, or
9   (at your option) any later version.
10
11   LilyPond is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15
16   You should have received a copy of the GNU General Public License
17   along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "staff-spacing.hh"
21
22 #include "spacing-options.hh"
23 #include "system.hh"
24 #include "paper-column.hh"
25 #include "column-x-positions.hh"
26 #include "pointer-group-interface.hh"
27 #include "spacing-interface.hh"
28 #include "spacing-spanner.hh"
29 #include "note-spacing.hh"
30 #include "moment.hh"
31 #include "grob-array.hh"
32 #include "break-align-interface.hh"
33 #include "warn.hh"
34
35 using std::vector;
36
37 /*
38   Return whether COL is fixed to its neighbors by some kind of spacing
39   constraint.
40
41
42   If in doubt, then we're not loose; the spacing engine should space
43   for it, risking suboptimal spacing.
44
45   (Otherwise, we might risk core dumps, and other weird stuff.)
46 */
47 static bool
48 is_loose_column (Grob *l, Grob *col, Grob *r, Spacing_options const *options)
49 {
50   if (!to_boolean (col->get_property ("allow-loose-spacing")))
51     return false;
52
53   if ((options->float_nonmusical_columns_
54        || options->float_grace_columns_)
55       && Paper_column::when_mom (col).grace_part_)
56     {
57       return true;
58     }
59
60   if (Paper_column::is_musical (col))
61     return false;
62
63   /*
64     If this column doesn't have a proper neighbor, we should really
65     make it loose, but spacing it correctly is more than we can
66     currently can handle.
67
68     (this happens in the following situation:
69
70     |
71     |    clef G
72     *
73
74     |               |      ||
75     |               |      ||
76     O               O       ||
77
78
79     the column containing the clef is really loose, and should be
80     attached right to the first column, but that is a lot of work for
81     such a borderline case.)
82
83   */
84
85   Item *r_neighbor = unsmob<Item> (col->get_object ("right-neighbor"));
86   Item *l_neighbor = unsmob<Item> (col->get_object ("left-neighbor"));
87
88   if (!l_neighbor || !r_neighbor)
89     return false;
90
91   /* If a non-empty column (ie. not \bar "") is placed nicely in series with
92      its neighbor (ie. no funny polyphonic stuff), don't make it loose.
93   */
94   if (l == l_neighbor && r == r_neighbor && col->extent (col, X_AXIS).length () > 0)
95     return false;
96
97   /*
98     Only declare loose if the bounds make a little sense.  This means
99     some cases (two isolated, consecutive clef changes) won't be
100     nicely folded, but hey, then don't do that.
101   */
102   if (! ((Paper_column::is_musical (l_neighbor) || Paper_column::is_breakable (l_neighbor))
103          && (Paper_column::is_musical (r_neighbor) || Paper_column::is_breakable (r_neighbor))))
104     return false;
105
106   /*
107     in any case, we don't want to move bar lines.
108   */
109   extract_grob_set (col, "elements", elts);
110   for (vsize i = elts.size (); i--;)
111     {
112       Grob *g = elts[i];
113       if (has_interface<Break_alignment_interface> (g))
114         {
115           extract_grob_set (g, "elements", gelts);
116           for (vsize j = gelts.size (); j--;)
117             {
118               Grob *h = gelts[j];
119
120               if (h && scm_is_eq (h->get_property ("break-align-symbol"),
121                                   ly_symbol2scm ("staff-bar")))
122                 {
123                   extract_grob_set (h, "elements", helts);
124                   for (vsize k = helts.size (); k--;)
125                     if ("" != robust_scm2string (helts[k]->get_property ("glyph-name"), ""))
126                       return false;
127                 }
128             }
129         }
130     }
131
132   return true;
133 }
134
135 void
136 Spacing_spanner::set_distances_for_loose_col (Grob *me, Grob *c,
137                                               Drul_array<Item *> next_door,
138                                               Spacing_options const *options)
139 {
140   Drul_array<Real> dists (0, 0);
141
142   for (LEFT_and_RIGHT (d))
143     {
144       Item *lc = dynamic_cast<Item *> ((d == LEFT) ? next_door[LEFT] : c);
145       Item *rc = dynamic_cast<Item *> (d == LEFT ? c : next_door[RIGHT]);
146
147       extract_grob_set (lc, "spacing-wishes", wishes);
148       for (vsize k = wishes.size (); k--;)
149         {
150           Grob *sp = wishes[k];
151           if (Spacing_interface::left_column (sp) != lc
152               || Spacing_interface::right_column (sp) != rc)
153             continue;
154
155           if (has_interface<Note_spacing> (sp))
156             {
157               /*
158                 The note spacing should be taken from the musical
159                 columns.
160               */
161               Spring base = note_spacing (me, lc, rc, options);
162               Spring spring = Note_spacing::get_spacing (sp, rc, base, options->increment_);
163
164               dists[d] = std::max (dists[d], spring.min_distance ());
165             }
166           else if (has_interface<Staff_spacing> (sp))
167             {
168               Spring spring = Staff_spacing::get_spacing (sp, rc, 0.0);
169
170               dists[d] = std::max (dists[d], spring.min_distance ());
171             }
172           else
173             programming_error ("Subversive spacing wish");
174         }
175     }
176
177   Rod r;
178   r.distance_ = dists[LEFT] + dists[RIGHT];
179   r.item_drul_ = next_door;
180
181   r.add_to_cols ();
182 }
183
184 /*
185   Remove columns that are not tightly fitting from COLS. In the
186   removed columns, set 'between-cols to the columns where it is in
187   between.
188 */
189 void
190 Spacing_spanner::prune_loose_columns (Grob *me,
191                                       vector<Grob *> *cols,
192                                       Spacing_options *options)
193 {
194   vector<Grob *> newcols;
195   for (vsize i = 0; i < cols->size (); i++)
196     {
197       Grob *c = cols->at (i);
198
199       bool loose = (i > 0 && i + 1 < cols->size ())
200                    && is_loose_column (cols->at (i - 1), c, cols->at (i + 1), options);
201
202       /* Breakable columns never get pruned; even if they are loose,
203         their broken pieces are not.  However, we mark them so that
204         the spacing can take their mid-line looseness into account. */
205       if (loose && Paper_column::is_breakable (c))
206         {
207           loose = false;
208           c->set_property ("maybe-loose", SCM_BOOL_T);
209         }
210       /*
211         Unbreakable columns which only contain page-labels also
212         never get pruned, otherwise the labels are lost before they can
213         be collected by the System: so we mark these columns too.
214       */
215       if (!loose && !Paper_column::is_breakable (c)
216           && scm_is_pair (c->get_property ("labels")))
217         {
218           extract_grob_set (c, "elements", elts);
219           if (elts.empty ())
220             c->set_property ("maybe-loose", SCM_BOOL_T);
221         }
222
223       if (loose)
224         {
225           Grob *right_neighbor = unsmob<Grob> (c->get_object ("right-neighbor"));
226           Grob *left_neighbor = unsmob<Grob> (c->get_object ("left-neighbor"));
227
228           /*
229             Either object can be non existent, if the score ends
230             prematurely.
231           */
232           if (!right_neighbor || !left_neighbor)
233             {
234               c->programming_error ("Cannot determine neighbors for floating column.");
235               c->set_object ("between-cols", scm_cons (cols->at (i - 1)->self_scm (),
236                                                        cols->at (i + 1)->self_scm ()));
237             }
238           else
239             {
240               c->set_object ("between-cols", scm_cons (left_neighbor->self_scm (),
241                                                        right_neighbor->self_scm ()));
242
243               /*
244                 Set distance constraints for loose columns
245               */
246               Drul_array<Item *> next_door (dynamic_cast<Item *> (left_neighbor),
247                                             dynamic_cast<Item *> (right_neighbor));
248
249               set_distances_for_loose_col (me, c, next_door, options);
250             }
251         }
252
253       else
254         newcols.push_back (c);
255     }
256
257   *cols = newcols;
258 }
259
260 /*
261   Set neighboring columns determined by the spacing-wishes grob property.
262 */
263 void
264 Spacing_spanner::set_explicit_neighbor_columns (vector<Grob *> const &cols)
265 {
266   for (vsize i = 0; i < cols.size (); i++)
267     {
268       extract_grob_set (cols[i], "spacing-wishes", wishes);
269       for (vsize j = wishes.size (); j--;)
270         {
271           Item *wish = dynamic_cast<Item *> (wishes[j]);
272           Item *left_col = wish->get_column ();
273           int left_rank = Paper_column::get_rank (left_col);
274           int min_right_rank = INT_MAX;
275
276           extract_grob_set (wish, "right-items", right_items);
277           for (vsize k = right_items.size (); k--;)
278             {
279               Item *right_col = dynamic_cast<Item *> (right_items[k])->get_column ();
280               int right_rank = Paper_column::get_rank (right_col);
281
282               if (right_rank < min_right_rank)
283                 {
284                   left_col->set_object ("right-neighbor", right_col->self_scm ());
285                   min_right_rank = right_rank;
286                 }
287
288               Grob *old_left_neighbor = unsmob<Grob> (right_col->get_object ("left-neighbor"));
289               if (!old_left_neighbor || left_rank > Paper_column::get_rank (old_left_neighbor))
290                 right_col->set_object ("left-neighbor", left_col->self_scm ());
291             }
292         }
293     }
294 }
295
296 /*
297   Set neighboring columns that have no left/right-neighbor set
298   yet. Only do breakable non-musical columns, and musical columns.
299   Why only these? --jneem
300 */
301 void
302 Spacing_spanner::set_implicit_neighbor_columns (vector<Grob *> const &cols)
303 {
304   for (vsize i = 0; i < cols.size (); i++)
305     {
306       Item *it = dynamic_cast<Item *> (cols[i]);
307       if (!Paper_column::is_breakable (it) && !Paper_column::is_musical (it))
308         continue;
309
310       if (i && !unsmob<Grob> (cols[i]->get_object ("left-neighbor")))
311         cols[i]->set_object ("left-neighbor", cols[i - 1]->self_scm ());
312       if (i + 1 < cols.size () && !unsmob<Grob> (cols[i]->get_object ("right-neighbor")))
313         cols[i]->set_object ("right-neighbor", cols[i + 1]->self_scm ());
314     }
315 }