]> git.donarmstrong.com Git - lilypond.git/blob - lily/spacing-determine-loose-columns.cc
c0eecf93280e6bf9631e3daca7112e40b24dc135
[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     A rather hairy check, but we really only want to move around
108     clefs. (anything else?)
109
110     in any case, we don't want to move bar lines.
111   */
112   extract_grob_set (col, "elements", elts);
113   for (vsize i = elts.size (); i--;)
114     {
115       Grob *g = elts[i];
116       if (g && Break_alignment_interface::has_interface (g))
117         {
118           extract_grob_set (g, "elements", gelts);
119           for (vsize j = gelts.size (); j--;)
120             {
121               Grob *h = gelts[j];
122
123               /*
124                 ugh. -- fix staff-bar name?
125               */
126               if (h && h->get_property ("break-align-symbol") == ly_symbol2scm ("staff-bar"))
127                 return false;
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   Direction d = LEFT;
141   Drul_array<Real> dists (0, 0);
142
143   do
144     {
145       Item *lc = dynamic_cast<Item *> ((d == LEFT) ? next_door[LEFT] : c);
146       Item *rc = dynamic_cast<Item *> (d == LEFT ? c : next_door[RIGHT]);
147
148       extract_grob_set (lc, "spacing-wishes", wishes);
149       for (vsize k = wishes.size (); k--;)
150         {
151           Grob *sp = wishes[k];
152           if (Note_spacing::left_column (sp) != lc
153               || Note_spacing::right_column (sp) != rc)
154             continue;
155
156           if (Note_spacing::has_interface (sp))
157             {
158               /*
159                 The note spacing should be taken from the musical
160                 columns.
161               */
162               Real space = 0.0;
163               Real fixed = 0.0;
164               bool dummy = false;
165                   
166               Real base = note_spacing (me, lc, rc, options, &dummy);
167               Note_spacing::get_spacing (sp, rc, base, options->increment_,
168                                          &space, &fixed);
169
170               space -= options->increment_;
171
172               dists[d] = max (dists[d], space);
173             }
174           else if (Staff_spacing::has_interface (sp))
175             {
176               Real space = 0;
177               Real fixed_space = 0;
178               Staff_spacing::get_spacing_params (sp,
179                                                  &space, &fixed_space);
180
181               dists[d] = max (dists[d], fixed_space);
182             }
183           else
184             programming_error ("Subversive spacing wish");
185         }
186     }
187   while (flip (&d) != LEFT);
188
189   Rod r;
190   r.distance_ = dists[LEFT] + dists[RIGHT];
191   r.item_drul_ = next_door;
192  
193   r.add_to_cols (); 
194 }
195
196
197 /*
198   Remove columns that are not tightly fitting from COLS. In the
199   removed columns, set 'between-cols to the columns where it is in
200   between.
201 */
202 void
203 Spacing_spanner::prune_loose_columns (Grob *me, vector<Grob*> *cols,
204                                       Spacing_options const *options)
205 {
206   vector<Grob*> newcols;
207
208   for (vsize i = 0; i < cols->size (); i++)
209     {
210       Grob *c = cols->at (i);
211
212       bool loose = (i > 0 && i < cols->size () - 1)
213         && is_loose_column (cols->at (i - 1), c, cols->at (i + 1), options);
214
215       if (loose)
216         {
217           extract_grob_set (c, "right-neighbors", rns_arr);
218           extract_grob_set (c, "left-neighbors", lns_arr);
219
220           SCM lns = lns_arr.size () ? lns_arr.back ()->self_scm () : SCM_BOOL_F;
221           SCM rns = rns_arr.size () ? rns_arr.back ()->self_scm () : SCM_BOOL_F;
222
223           /*
224             Either object can be non existent, if the score ends
225             prematurely.
226           */
227
228           extract_grob_set (unsmob_grob (rns), "right-items", right_items);
229           if (right_items.size () == 0 || !unsmob_grob (lns))
230             {
231               c->programming_error ("Can't determine neighbors for floating column. ");
232               c->set_object ("between-cols", scm_cons (cols->at (i-1)->self_scm (),
233                                                        cols->at (i+1)->self_scm ()));
234             }
235           else
236             {
237               c->set_object ("between-cols", scm_cons (lns,
238                                                        right_items[0]->self_scm ()));
239
240               /*
241                 Set distance constraints for loose columns
242               */
243               Drul_array<Item *> next_door (dynamic_cast<Item*> (cols->at (i - 1)),
244                                             dynamic_cast<Item*> (cols->at (i + 1)));
245
246               set_distances_for_loose_col (me, c, next_door, options);
247             }
248         }
249
250       if (!loose)
251         newcols.push_back (c);
252     }
253
254   *cols = newcols;
255 }
256
257 /*
258   Set neighboring columns determined by the spacing-wishes grob property.
259 */
260 void
261 Spacing_spanner::set_explicit_neighbor_columns (vector<Grob*> const &cols)
262 {
263   for (vsize i = 0; i < cols.size (); i++)
264     {
265       SCM right_neighbors = Grob_array::make_array ();
266       Grob_array *rn_arr = unsmob_grob_array (right_neighbors);
267       int min_rank = 100000;    // inf.
268
269       extract_grob_set (cols[i], "spacing-wishes", wishes);
270       for (vsize k = wishes.size (); k--;)
271         {
272           Item *wish = dynamic_cast<Item *> (wishes[k]);
273
274           Item *lc = wish->get_column ();
275           Grob *right = Note_spacing::right_column (wish);
276
277           if (!right)
278             continue;
279
280           Item *rc = dynamic_cast<Item *> (right);
281
282           int right_rank = Paper_column::get_rank (rc);
283           int left_rank = Paper_column::get_rank (lc);
284
285           /*
286             update the left column.
287           */
288           if (right_rank <= min_rank)
289             {
290               if (right_rank < min_rank)
291                 rn_arr->clear ();
292
293               min_rank = right_rank;
294               rn_arr->add (wish);
295             }
296
297           /*
298             update the right column of the wish.
299           */
300           int maxrank = 0;
301
302           extract_grob_set (rc, "left-neighbors", lns_arr);
303           if (lns_arr.size ())
304             {
305               Item *it = dynamic_cast<Item *> (lns_arr.back ());
306               maxrank = Paper_column::get_rank (it->get_column ());
307             }
308
309           if (left_rank >= maxrank)
310             {
311
312               if (left_rank > maxrank)
313                 {
314                   Grob_array *ga = unsmob_grob_array (rc->get_object ("left-neighbors"));
315                   if (ga)
316                     ga->clear ();
317                 }
318
319               Pointer_group_interface::add_grob (rc, ly_symbol2scm ("left-neighbors"), wish);
320             }
321         }
322
323       if (rn_arr->size ())
324         cols[i]->set_object ("right-neighbors", right_neighbors);
325     }
326 }
327
328 /*
329   Set neighboring columns that have no left/right-neighbor set
330   yet. Only do breakable non-musical columns, and musical columns.
331 */
332 void
333 Spacing_spanner::set_implicit_neighbor_columns (vector<Grob*> const &cols)
334 {
335   for (vsize i = 0; i < cols.size (); i++)
336     {
337       Item *it = dynamic_cast<Item *> (cols[i]);
338       if (!Paper_column::is_breakable (it) && !Paper_column::is_musical (it))
339         continue;
340
341       // it->breakable || it->musical
342
343       /*
344         sloppy with typing left/right-neighbors should take list, but paper-column found instead.
345       */
346       extract_grob_set (cols[i], "left-neighbors", lns);
347       if (lns.empty () && i)
348         {
349           SCM ga_scm = Grob_array::make_array ();
350           Grob_array *ga = unsmob_grob_array (ga_scm);
351           ga->add (cols[i - 1]);
352           cols[i]->set_object ("left-neighbors", ga_scm);
353         }
354       extract_grob_set (cols[i], "right-neighbors", rns);
355       if (rns.empty () && i < cols.size () - 1)
356         {
357           SCM ga_scm = Grob_array::make_array ();
358           Grob_array *ga = unsmob_grob_array (ga_scm);
359           ga->add (cols[i + 1]);
360           cols[i]->set_object ("right-neighbors", ga_scm);
361         }
362     }
363 }