]> git.donarmstrong.com Git - lilypond.git/blob - lily/spacing-determine-loose-columns.cc
trim duplicate headers.
[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 "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 "grob-array.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 *col, Grob *r, Spacing_options const *options)
35 {
36   if (!to_boolean (col->get_property ("allow-loose-spacing")))
37     return false;
38   
39   if ((options->float_nonmusical_columns_
40        ||options->float_grace_columns_)
41       && Paper_column::when_mom (col).grace_part_)
42     return true;
43
44   if (Paper_column::is_musical (col)
45       || Paper_column::is_breakable (col))
46     return false;
47
48   extract_grob_set (col, "right-neighbors", rns);
49   extract_grob_set (col, "left-neighbors", lns);
50
51   /*
52     If this column doesn't have a proper neighbor, we should really
53     make it loose, but spacing it correctly is more than we can
54     currently can handle.
55
56     (this happens in the following situation:
57
58     |
59     |    clef G
60     *
61
62     |               |      ||
63     |               |      ||
64     O               O       ||
65
66
67     the column containing the clef is really loose, and should be
68     attached right to the first column, but that is a lot of work for
69     such a borderline case.)
70
71   */
72   if (lns.empty () || rns.empty ())
73     return false;
74
75   Item *l_neighbor = dynamic_cast<Item *> (lns[0]);
76   Item *r_neighbor = dynamic_cast<Item *> (rns[0]);
77
78   if (!l_neighbor || !r_neighbor)
79     return false;
80
81   l_neighbor = l_neighbor->get_column ();
82   r_neighbor = dynamic_cast<Item *> (Note_spacing::right_column (r_neighbor));
83
84   if (l == l_neighbor && r == r_neighbor)
85     return false;
86
87   if (!l_neighbor || !r_neighbor)
88     return false;
89
90   /*
91     Only declare loose if the bounds make a little sense.  This means
92     some cases (two isolated, consecutive clef changes) won't be
93     nicely folded, but hey, then don't do that.
94   */
95   if (! ((Paper_column::is_musical (l_neighbor) || Paper_column::is_breakable (l_neighbor))
96          && (Paper_column::is_musical (r_neighbor) || Paper_column::is_breakable (r_neighbor))))
97     return false;
98
99   /*
100     A rather hairy check, but we really only want to move around
101     clefs. (anything else?)
102
103     in any case, we don't want to move bar lines.
104   */
105   extract_grob_set (col, "elements", elts);
106   for (vsize i = elts.size (); i--;)
107     {
108       Grob *g = elts[i];
109       if (g && Break_alignment_interface::has_interface (g))
110         {
111           extract_grob_set (g, "elements", gelts);
112           for (vsize j = gelts.size (); j--;)
113             {
114               Grob *h = gelts[j];
115
116               /*
117                 ugh. -- fix staff-bar name?
118               */
119               if (h && h->get_property ("break-align-symbol") == ly_symbol2scm ("staff-bar"))
120                 return false;
121             }
122         }
123     }
124
125   return true;
126 }
127
128 /*
129   Remove columns that are not tightly fitting from COLS. In the
130   removed columns, set 'between-cols to the columns where it is in
131   between.
132 */
133 void
134 Spacing_spanner::prune_loose_columns (Grob *me, vector<Grob*> *cols,
135                                       Spacing_options const *options)
136 {
137   vector<Grob*> newcols;
138
139   for (vsize i = 0; i < cols->size (); i++)
140     {
141       Grob *c = cols->at (i);
142
143       bool loose = (i > 0 && i < cols->size () - 1)
144         && is_loose_column (cols->at (i - 1), c, cols->at (i + 1), options);
145
146       if (loose)
147         {
148           extract_grob_set (c, "right-neighbors", rns_arr);
149           extract_grob_set (c, "left-neighbors", lns_arr);
150
151           SCM lns = lns_arr.size () ? lns_arr.back ()->self_scm () : SCM_BOOL_F;
152           SCM rns = rns_arr.size () ? rns_arr.back ()->self_scm () : SCM_BOOL_F;
153
154           /*
155             Either object can be non existent, if the score ends
156             prematurely.
157           */
158
159           extract_grob_set (unsmob_grob (rns), "right-items", right_items);
160           c->set_object ("between-cols", scm_cons (lns,
161                                                    right_items[0]->self_scm ()));
162
163           /*
164             Set distance constraints for loose columns
165           */
166           Drul_array<Grob *> next_door (cols->at (i - 1),
167                                         cols->at (i + 1));
168           Direction d = LEFT;
169           Drul_array<Real> dists (0, 0);
170
171           do
172             {
173               Item *lc = dynamic_cast<Item *> ((d == LEFT) ? next_door[LEFT] : c);
174               Item *rc = dynamic_cast<Item *> (d == LEFT ? c : next_door[RIGHT]);
175
176               extract_grob_set (lc, "spacing-wishes", wishes);
177               for (vsize k = wishes.size (); k--;)
178                 {
179                   Grob *sp = wishes[k];
180                   if (Note_spacing::left_column (sp) != lc
181                       || Note_spacing::right_column (sp) != rc)
182                     continue;
183
184                   if (Note_spacing::has_interface (sp))
185                     {
186                       /*
187                         The note spacing should be taken from the musical
188                         columns.
189                       */
190                       Real space = 0.0;
191                       Real fixed = 0.0;
192                       bool dummy = false;
193                   
194                       Real base = note_spacing (me, lc, rc, options, &dummy);
195                       Note_spacing::get_spacing (sp, rc, base, options->increment_,
196                                                  &space, &fixed);
197
198                       space -= options->increment_;
199
200                       dists[d] = max (dists[d], space);
201                     }
202                   else if (Staff_spacing::has_interface (sp))
203                     {
204                       Real space = 0;
205                       Real fixed_space = 0;
206                       Staff_spacing::get_spacing_params (sp,
207                                                          &space, &fixed_space);
208
209                       dists[d] = max (dists[d], fixed_space);
210                     }
211                   else
212                     programming_error ("Subversive spacing wish");
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->at (i - 1));
220           r.item_drul_[RIGHT] = dynamic_cast<Item *> (cols->at (i + 1));
221
222           r.add_to_cols ();
223         }
224       else
225         newcols.push_back (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 (vector<Grob*> const &cols)
236 {
237   for (vsize 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 (vsize 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.back ());
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         cols[i]->set_object ("right-neighbors", right_neighbors);
299     }
300 }
301
302 /*
303   Set neighboring columns that have no left/right-neighbor set
304   yet. Only do breakable non-musical columns, and musical columns.
305 */
306 void
307 Spacing_spanner::set_implicit_neighbor_columns (vector<Grob*> const &cols)
308 {
309   for (vsize i = 0; i < cols.size (); i++)
310     {
311       Item *it = dynamic_cast<Item *> (cols[i]);
312       if (!Paper_column::is_breakable (it) && !Paper_column::is_musical (it))
313         continue;
314
315       // it->breakable || it->musical
316
317       /*
318         sloppy with typing left/right-neighbors should take list, but paper-column found instead.
319       */
320       extract_grob_set (cols[i], "left-neighbors", lns);
321       if (lns.empty () && i)
322         {
323           SCM ga_scm = Grob_array::make_array ();
324           Grob_array *ga = unsmob_grob_array (ga_scm);
325           ga->add (cols[i - 1]);
326           cols[i]->set_object ("left-neighbors", ga_scm);
327         }
328       extract_grob_set (cols[i], "right-neighbors", rns);
329       if (rns.empty () && i < cols.size () - 1)
330         {
331           SCM ga_scm = Grob_array::make_array ();
332           Grob_array *ga = unsmob_grob_array (ga_scm);
333           ga->add (cols[i + 1]);
334           cols[i]->set_object ("right-neighbors", ga_scm);
335         }
336     }
337 }