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