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