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