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