]> git.donarmstrong.com Git - lilypond.git/blob - lily/spacing-determine-loose-columns.cc
Merge with git+ssh://jneem@git.sv.gnu.org/srv/git/lilypond.git
[lilypond.git] / lily / spacing-determine-loose-columns.cc
1 /*
2   spacing-determine-loose-columns.cc -- implement Spacing_spanner
3   methods that decide which columns to turn loose.
4
5   source file of the GNU LilyPond music typesetter
6
7   (c) 2005--2006 Han-Wen Nienhuys <hanwen@xs4all.nl>
8 */
9
10 #include "staff-spacing.hh"
11
12 #include "system.hh"
13 #include "paper-column.hh"
14 #include "column-x-positions.hh"
15 #include "pointer-group-interface.hh"
16 #include "spacing-spanner.hh"
17 #include "note-spacing.hh"
18 #include "moment.hh"
19 #include "break-align-interface.hh"
20 #include "warn.hh"
21
22 /*
23   Return whether COL is fixed to its neighbors by some kind of spacing
24   constraint.
25
26
27   If in doubt, then we're not loose; the spacing engine should space
28   for it, risking suboptimal spacing.
29
30   (Otherwise, we might risk core dumps, and other weird stuff.)
31 */
32 static bool
33 is_loose_column (Grob *l, Grob *col, Grob *r, Spacing_options const *options)
34 {
35   if (!to_boolean (col->get_property ("allow-loose-spacing")))
36     return false;
37   
38   if ((options->float_nonmusical_columns_
39        ||options->float_grace_columns_)
40       && Paper_column::when_mom (col).grace_part_)
41     return true;
42
43   if (Paper_column::is_musical (col)
44       || Paper_column::is_breakable (col))
45     return false;
46
47   extract_grob_set (col, "right-neighbors", rns);
48   extract_grob_set (col, "left-neighbors", lns);
49
50   /*
51     If this column doesn't have a proper neighbor, we should really
52     make it loose, but spacing it correctly is more than we can
53     currently can handle.
54
55     (this happens in the following situation:
56
57     |
58     |    clef G
59     *
60
61     |               |      ||
62     |               |      ||
63     O               O       ||
64
65
66     the column containing the clef is really loose, and should be
67     attached right to the first column, but that is a lot of work for
68     such a borderline case.)
69
70   */
71   if (lns.empty () || rns.empty ())
72     return false;
73
74   Item *l_neighbor = dynamic_cast<Item *> (lns[0]);
75   Item *r_neighbor = dynamic_cast<Item *> (rns[0]);
76
77   if (!l_neighbor || !r_neighbor)
78     return false;
79
80   l_neighbor = l_neighbor->get_column ();
81   r_neighbor = dynamic_cast<Item *> (Note_spacing::right_column (r_neighbor));
82
83   if (l == l_neighbor && r == r_neighbor)
84     return false;
85
86   if (!l_neighbor || !r_neighbor)
87     return false;
88
89   /*
90     Only declare loose if the bounds make a little sense.  This means
91     some cases (two isolated, consecutive clef changes) won't be
92     nicely folded, but hey, then don't do that.
93   */
94   if (! ((Paper_column::is_musical (l_neighbor) || Paper_column::is_breakable (l_neighbor))
95          && (Paper_column::is_musical (r_neighbor) || Paper_column::is_breakable (r_neighbor))))
96     return false;
97
98   /*
99     A rather hairy check, but we really only want to move around
100     clefs. (anything else?)
101
102     in any case, we don't want to move bar lines.
103   */
104   extract_grob_set (col, "elements", elts);
105   for (vsize i = elts.size (); i--;)
106     {
107       Grob *g = elts[i];
108       if (g && Break_align_interface::has_interface (g))
109         {
110           extract_grob_set (g, "elements", gelts);
111           for (vsize j = gelts.size (); j--;)
112             {
113               Grob *h = gelts[j];
114
115               /*
116                 ugh. -- fix staff-bar name?
117               */
118               if (h && h->get_property ("break-align-symbol") == ly_symbol2scm ("staff-bar"))
119                 return false;
120             }
121         }
122     }
123
124   return true;
125 }
126
127 /*
128   Remove columns that are not tightly fitting from COLS. In the
129   removed columns, set 'between-cols to the columns where it is in
130   between.
131 */
132 void
133 Spacing_spanner::prune_loose_columns (Grob *me, vector<Grob*> *cols,
134                                       Spacing_options const *options)
135 {
136   vector<Grob*> newcols;
137
138   for (vsize i = 0; i < cols->size (); i++)
139     {
140       Grob *c = cols->at (i);
141
142       bool loose = (i > 0 && i < cols->size () - 1)
143         && is_loose_column (cols->at (i - 1), c, cols->at (i + 1), options);
144
145       if (loose)
146         {
147           extract_grob_set (c, "right-neighbors", rns_arr);
148           extract_grob_set (c, "left-neighbors", lns_arr);
149
150           SCM lns = lns_arr.size () ? lns_arr.back ()->self_scm () : SCM_BOOL_F;
151           SCM rns = rns_arr.size () ? rns_arr.back ()->self_scm () : SCM_BOOL_F;
152
153           /*
154             Either object can be non existent, if the score ends
155             prematurely.
156           */
157
158           extract_grob_set (unsmob_grob (rns), "right-items", right_items);
159           c->set_object ("between-cols", scm_cons (lns,
160                                                    right_items[0]->self_scm ()));
161
162           /*
163             Set distance constraints for loose columns
164           */
165           Drul_array<Grob *> next_door (cols->at (i - 1),
166                                         cols->at (i + 1));
167           Direction d = LEFT;
168           Drul_array<Real> dists (0, 0);
169
170           do
171             {
172               Item *lc = dynamic_cast<Item *> ((d == LEFT) ? next_door[LEFT] : c);
173               Item *rc = dynamic_cast<Item *> (d == LEFT ? c : next_door[RIGHT]);
174
175               extract_grob_set (lc, "spacing-wishes", wishes);
176               for (vsize k = wishes.size (); k--;)
177                 {
178                   Grob *sp = wishes[k];
179                   if (Note_spacing::left_column (sp) != lc
180                       || Note_spacing::right_column (sp) != rc)
181                     continue;
182
183                   if (Note_spacing::has_interface (sp))
184                     {
185                       /*
186                         The note spacing should be taken from the musical
187                         columns.
188                       */
189                       Real space = 0.0;
190                       Real fixed = 0.0;
191                       bool dummy = false;
192                   
193                       Real base = note_spacing (me, lc, rc, options, &dummy);
194                       Note_spacing::get_spacing (sp, rc, base, options->increment_,
195                                                  &space, &fixed);
196
197                       space -= options->increment_;
198
199                       dists[d] = max (dists[d], space);
200                     }
201                   else if (Staff_spacing::has_interface (sp))
202                     {
203                       Real space = 0;
204                       Real fixed_space = 0;
205                       Staff_spacing::get_spacing_params (sp,
206                                                          &space, &fixed_space);
207
208                       dists[d] = max (dists[d], fixed_space);
209                     }
210                   else
211                     programming_error ("Subversive spacing wish");
212                 }
213             }
214           while (flip (&d) != LEFT);
215
216           Rod r;
217           r.distance_ = dists[LEFT] + dists[RIGHT];
218           r.item_drul_[LEFT] = dynamic_cast<Item *> (cols->at (i - 1));
219           r.item_drul_[RIGHT] = dynamic_cast<Item *> (cols->at (i + 1));
220
221           r.add_to_cols ();
222         }
223       else
224         newcols.push_back (c);
225     }
226
227   *cols = newcols;
228 }
229
230 /*
231   Set neighboring columns determined by the spacing-wishes grob property.
232 */
233 void
234 Spacing_spanner::set_explicit_neighbor_columns (vector<Grob*> const &cols)
235 {
236   for (vsize i = 0; i < cols.size (); i++)
237     {
238       SCM right_neighbors = Grob_array::make_array ();
239       Grob_array *rn_arr = unsmob_grob_array (right_neighbors);
240       int min_rank = 100000;    // inf.
241
242       extract_grob_set (cols[i], "spacing-wishes", wishes);
243       for (vsize k = wishes.size (); k--;)
244         {
245           Item *wish = dynamic_cast<Item *> (wishes[k]);
246
247           Item *lc = wish->get_column ();
248           Grob *right = Note_spacing::right_column (wish);
249
250           if (!right)
251             continue;
252
253           Item *rc = dynamic_cast<Item *> (right);
254
255           int right_rank = Paper_column::get_rank (rc);
256           int left_rank = Paper_column::get_rank (lc);
257
258           /*
259             update the left column.
260           */
261           if (right_rank <= min_rank)
262             {
263               if (right_rank < min_rank)
264                 rn_arr->clear ();
265
266               min_rank = right_rank;
267               rn_arr->add (wish);
268             }
269
270           /*
271             update the right column of the wish.
272           */
273           int maxrank = 0;
274
275           extract_grob_set (rc, "left-neighbors", lns_arr);
276           if (lns_arr.size ())
277             {
278               Item *it = dynamic_cast<Item *> (lns_arr.back ());
279               maxrank = Paper_column::get_rank (it->get_column ());
280             }
281
282           if (left_rank >= maxrank)
283             {
284
285               if (left_rank > maxrank)
286                 {
287                   Grob_array *ga = unsmob_grob_array (rc->get_object ("left-neighbors"));
288                   if (ga)
289                     ga->clear ();
290                 }
291
292               Pointer_group_interface::add_grob (rc, ly_symbol2scm ("left-neighbors"), wish);
293             }
294         }
295
296       if (rn_arr->size ())
297         cols[i]->set_object ("right-neighbors", right_neighbors);
298     }
299 }
300
301 /*
302   Set neighboring columns that have no left/right-neighbor set
303   yet. Only do breakable non-musical columns, and musical columns.
304 */
305 void
306 Spacing_spanner::set_implicit_neighbor_columns (vector<Grob*> const &cols)
307 {
308   for (vsize i = 0; i < cols.size (); i++)
309     {
310       Item *it = dynamic_cast<Item *> (cols[i]);
311       if (!Paper_column::is_breakable (it) && !Paper_column::is_musical (it))
312         continue;
313
314       // it->breakable || it->musical
315
316       /*
317         sloppy with typing left/right-neighbors should take list, but paper-column found instead.
318       */
319       extract_grob_set (cols[i], "left-neighbors", lns);
320       if (lns.empty () && i)
321         {
322           SCM ga_scm = Grob_array::make_array ();
323           Grob_array *ga = unsmob_grob_array (ga_scm);
324           ga->add (cols[i - 1]);
325           cols[i]->set_object ("left-neighbors", ga_scm);
326         }
327       extract_grob_set (cols[i], "right-neighbors", rns);
328       if (rns.empty () && i < cols.size () - 1)
329         {
330           SCM ga_scm = Grob_array::make_array ();
331           Grob_array *ga = unsmob_grob_array (ga_scm);
332           ga->add (cols[i + 1]);
333           cols[i]->set_object ("right-neighbors", ga_scm);
334         }
335     }
336 }