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