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