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