]> git.donarmstrong.com Git - lilypond.git/blob - lily/spacing-determine-loose-columns.cc
Merge branch 'lilypond/translation' of ssh://jomand@git.sv.gnu.org/srv/git/lilypond
[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--2009 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-interface.hh"
18 #include "spacing-spanner.hh"
19 #include "note-spacing.hh"
20 #include "moment.hh"
21 #include "grob-array.hh"
22 #include "break-align-interface.hh"
23 #include "warn.hh"
24
25 /*
26   Return whether COL is fixed to its neighbors by some kind of spacing
27   constraint.
28
29
30   If in doubt, then we're not loose; the spacing engine should space
31   for it, risking suboptimal spacing.
32
33   (Otherwise, we might risk core dumps, and other weird stuff.)
34 */
35 static bool
36 is_loose_column (Grob *l, Grob *col, Grob *r, Spacing_options const *options)
37 {
38   if (!to_boolean (col->get_property ("allow-loose-spacing")))
39     return false;
40   
41
42   if ((options->float_nonmusical_columns_
43        || options->float_grace_columns_)
44       && Paper_column::when_mom (col).grace_part_)
45     {
46       return true;
47     }
48
49   
50   if (Paper_column::is_musical (col))
51     return false;
52
53   /*
54     If this column doesn't have a proper neighbor, we should really
55     make it loose, but spacing it correctly is more than we can
56     currently can handle.
57
58     (this happens in the following situation:
59
60     |
61     |    clef G
62     *
63
64     |               |      ||
65     |               |      ||
66     O               O       ||
67
68
69     the column containing the clef is really loose, and should be
70     attached right to the first column, but that is a lot of work for
71     such a borderline case.)
72
73   */
74
75   Item *r_neighbor = unsmob_item (col->get_object ("right-neighbor"));
76   Item *l_neighbor = unsmob_item (col->get_object ("left-neighbor"));
77
78   if (!l_neighbor || !r_neighbor)
79     return false;
80
81   /* If a non-empty column (ie. not \bar "") is placed nicely in series with
82      its neighbor (ie. no funny polyphonic stuff), don't make it loose.
83   */
84   if (l == l_neighbor && r == r_neighbor && col->extent (col, X_AXIS).length () > 0)
85      return false;
86
87   /*
88     Only declare loose if the bounds make a little sense.  This means
89     some cases (two isolated, consecutive clef changes) won't be
90     nicely folded, but hey, then don't do that.
91   */
92   if (! ((Paper_column::is_musical (l_neighbor) || Paper_column::is_breakable (l_neighbor))
93          && (Paper_column::is_musical (r_neighbor) || Paper_column::is_breakable (r_neighbor))))
94     return false;
95
96   /*
97     in any case, we don't want to move bar lines.
98   */
99   extract_grob_set (col, "elements", elts);
100   for (vsize i = elts.size (); i--;)
101     {
102       Grob *g = elts[i];
103       if (g && Break_alignment_interface::has_interface (g))
104         {
105           extract_grob_set (g, "elements", gelts);
106           for (vsize j = gelts.size (); j--;)
107             {
108               Grob *h = gelts[j];
109
110               if (h && h->get_property ("break-align-symbol") == ly_symbol2scm ("staff-bar"))
111                 {
112                   extract_grob_set (h, "elements", helts);
113                   for (vsize k = helts.size (); k--;)
114                     if ("" != robust_scm2string (helts[k]->get_property ("glyph-name"), ""))
115                       return false;
116                 }
117             }
118         }
119     }
120
121   return true;
122 }
123
124 void
125 Spacing_spanner::set_distances_for_loose_col (Grob *me, Grob *c,
126                                               Drul_array<Item *> next_door,
127                                               Spacing_options const *options)
128 {
129   Direction d = LEFT;
130   Drul_array<Real> dists (0, 0);
131
132   do
133     {
134       Item *lc = dynamic_cast<Item *> ((d == LEFT) ? next_door[LEFT] : c);
135       Item *rc = dynamic_cast<Item *> (d == LEFT ? c : next_door[RIGHT]);
136
137       extract_grob_set (lc, "spacing-wishes", wishes);
138       for (vsize k = wishes.size (); k--;)
139         {
140           Grob *sp = wishes[k];
141           if (Spacing_interface::left_column (sp) != lc
142               || Spacing_interface::right_column (sp) != rc)
143             continue;
144
145           if (Note_spacing::has_interface (sp))
146             {
147               /*
148                 The note spacing should be taken from the musical
149                 columns.
150               */
151               Real base = note_spacing (me, lc, rc, options);
152               Spring spring = Note_spacing::get_spacing (sp, rc, base, options->increment_);
153
154               dists[d] = max (dists[d], spring.min_distance ());
155             }
156           else if (Staff_spacing::has_interface (sp))
157             {
158               Spring spring = Staff_spacing::get_spacing (sp, rc);
159
160               dists[d] = max (dists[d], spring.min_distance ());
161             }
162           else
163             programming_error ("Subversive spacing wish");
164         }
165     }
166   while (flip (&d) != LEFT);
167
168   Rod r;
169   r.distance_ = dists[LEFT] + dists[RIGHT];
170   r.item_drul_ = next_door;
171  
172   r.add_to_cols (); 
173 }
174
175
176 /*
177   Remove columns that are not tightly fitting from COLS. In the
178   removed columns, set 'between-cols to the columns where it is in
179   between.
180 */
181 void
182 Spacing_spanner::prune_loose_columns (Grob *me,
183                                       vector<Grob*> *cols,
184                                       Spacing_options *options)
185 {
186   vector<Grob*> newcols;
187
188   for (vsize i = 0; i < cols->size (); i++)
189     {
190       Grob *c = cols->at (i);
191
192       bool loose = (i > 0 && i + 1 < cols->size ())
193         && is_loose_column (cols->at (i - 1), c, cols->at (i + 1), options);
194
195       /* Breakable columns never get pruned; even if they are loose,
196         their broken pieces are not.  However, we mark them so that
197         the spacing can take their mid-line looseness into account. */
198       if (loose && Paper_column::is_breakable (c))
199         {
200           loose = false;
201           c->set_property ("maybe-loose", SCM_BOOL_T);
202         }
203
204       if (loose)
205         {
206           Grob *right_neighbor = unsmob_grob (c->get_object ("right-neighbor"));
207           Grob *left_neighbor = unsmob_grob (c->get_object ("left-neighbor"));
208
209           /*
210             Either object can be non existent, if the score ends
211             prematurely.
212           */
213           if (!right_neighbor || !left_neighbor)
214             {
215               c->programming_error ("Cannot determine neighbors for floating column. ");
216               c->set_object ("between-cols", scm_cons (cols->at (i-1)->self_scm (),
217                                                        cols->at (i+1)->self_scm ()));
218             }
219           else
220             {
221               c->set_object ("between-cols", scm_cons (left_neighbor->self_scm (),
222                                                        right_neighbor->self_scm ()));
223
224
225               /*
226                 Set distance constraints for loose columns
227               */
228               Drul_array<Item *> next_door (dynamic_cast<Item*> (cols->at (i - 1)),
229                                             dynamic_cast<Item*> (cols->at (i + 1)));
230
231               set_distances_for_loose_col (me, c, next_door, options);
232             }
233         }
234
235       if (!loose)
236         newcols.push_back (c);
237     }
238
239   *cols = newcols;
240 }
241
242 /*
243   Set neighboring columns determined by the spacing-wishes grob property.
244 */
245 void
246 Spacing_spanner::set_explicit_neighbor_columns (vector<Grob*> const &cols)
247 {
248   for (vsize i = 0; i < cols.size (); i++)
249     {
250       extract_grob_set (cols[i], "spacing-wishes", wishes);
251       for (vsize j = wishes.size (); j--;)
252         {
253           Item *wish = dynamic_cast<Item*> (wishes[j]);
254           Item *left_col = wish->get_column ();
255           int left_rank = Paper_column::get_rank (left_col);
256           int min_right_rank = INT_MAX;
257
258           extract_grob_set (wish, "right-items", right_items);
259           for (vsize k = right_items.size (); k--;)
260             {
261               Item *right_col = dynamic_cast<Item*> (right_items[k])->get_column ();
262               int right_rank = Paper_column::get_rank (right_col);
263
264               if (right_rank < min_right_rank)
265                 {
266                   left_col->set_object ("right-neighbor", right_col->self_scm ());
267                   min_right_rank = right_rank;
268                 }
269
270               Grob *old_left_neighbor = unsmob_grob (right_col->get_object ("left-neighbor"));
271               if (!old_left_neighbor || left_rank > Paper_column::get_rank (old_left_neighbor))
272                 right_col->set_object ("left-neighbor", left_col->self_scm ());
273             }
274         }
275     }
276 }
277
278 /*
279   Set neighboring columns that have no left/right-neighbor set
280   yet. Only do breakable non-musical columns, and musical columns.
281   Why only these? --jneem
282 */
283 void
284 Spacing_spanner::set_implicit_neighbor_columns (vector<Grob*> const &cols)
285 {
286   for (vsize i = 0; i < cols.size (); i++)
287     {
288       Item *it = dynamic_cast<Item *> (cols[i]);
289       if (!Paper_column::is_breakable (it) && !Paper_column::is_musical (it))
290         continue;
291
292       if (i && !unsmob_grob (cols[i]->get_object ("left-neighbor")))
293         cols[i]->set_object ("left-neighbor", cols[i-1]->self_scm ());
294       if (i + 1 < cols.size () && !unsmob_grob (cols[i]->get_object ("right-neighbor")))
295         cols[i]->set_object ("right-neighbor", cols[i+1]->self_scm ());
296     }
297 }