]> git.donarmstrong.com Git - lilypond.git/blob - lily/system.cc
Fix off-by-one error in constrained-breaking.
[lilypond.git] / lily / system.cc
1 /*
2   system.cc -- implement System
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1996--2006 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
8
9 #include "system.hh"
10
11 #include "align-interface.hh"
12 #include "all-font-metrics.hh"
13 #include "axis-group-interface.hh"
14 #include "grob-array.hh"
15 #include "international.hh"
16 #include "main.hh"
17 #include "output-def.hh"
18 #include "paper-column.hh"
19 #include "paper-score.hh"
20 #include "paper-system.hh"
21 #include "pointer-group-interface.hh"
22 #include "spacing-interface.hh"
23 #include "staff-symbol-referencer.hh"
24 #include "tweak-registration.hh"
25 #include "warn.hh"
26
27 extern bool debug_skylines;
28
29 System::System (System const &src, int count)
30   : Spanner (src, count)
31 {
32   all_elements_ = 0;
33   pscore_ = 0;
34   rank_ = 0;
35   init_elements ();
36 }
37
38 System::System (SCM s, Object_key const *key)
39   : Spanner (s, key)
40 {
41   all_elements_ = 0;
42   rank_ = 0;
43   init_elements ();
44 }
45
46 void
47 System::init_elements ()
48 {
49   SCM scm_arr = Grob_array::make_array ();
50   all_elements_ = unsmob_grob_array (scm_arr);
51   all_elements_->set_ordered (false);
52   set_object ("all-elements", scm_arr);
53 }
54
55 Grob *
56 System::clone (int index) const
57 {
58   return new System (*this, index);
59 }
60
61 int
62 System::element_count () const
63 {
64   return all_elements_->size ();
65 }
66
67 int
68 System::spanner_count () const
69 {
70   int k = 0;
71   for (vsize i = all_elements_->size (); i--;)
72     if (dynamic_cast<Spanner *> (all_elements_->grob (i)))
73       k++;
74   return k;
75 }
76
77 void
78 System::typeset_grob (Grob *elem)
79 {
80   if (elem->layout_)
81     programming_error ("adding element twice");
82   else
83     {
84       elem->layout_ = pscore_->layout ();
85       all_elements_->add (elem);
86       elem->unprotect ();
87     }
88 }
89
90 void
91 System::derived_mark () const
92 {
93   if (!all_elements_->empty ())
94     {
95       Grob **ptr = &all_elements_->array_reference ()[0];
96       Grob **end = ptr + all_elements_->size ();
97       while (ptr < end)
98         {
99           scm_gc_mark ((*ptr)->self_scm ());
100           ptr++;
101         }
102     }
103
104   if (pscore_)
105     scm_gc_mark (pscore_->self_scm ());
106
107   Spanner::derived_mark ();
108 }
109
110 static void
111 fixup_refpoints (vector<Grob*> const &grobs)
112 {
113   for (vsize i = grobs.size (); i--;)
114     grobs[i]->fixup_refpoint ();
115 }
116
117 SCM
118 System::get_paper_systems ()
119 {
120   for (vsize i = 0; i < all_elements_->size (); i++)
121     {
122       Grob *g = all_elements_->grob (i);
123       if (g->internal_has_interface (ly_symbol2scm ("only-prebreak-interface")))
124         {
125           /*
126             Kill no longer needed grobs.
127           */
128           Item *it = dynamic_cast<Item *> (g);
129           if (it && Item::is_non_musical (it))
130             {
131               it->find_prebroken_piece (LEFT)->suicide ();
132               it->find_prebroken_piece (RIGHT)->suicide ();
133             }
134           g->suicide ();
135         }
136       else if (g->is_live ())
137         g->do_break_processing ();
138     }
139
140   /*
141     fixups must be done in broken line_of_scores, because new elements
142     are put over there.  */
143   int count = 0;
144   for (vsize i = 0; i < broken_intos_.size (); i++)
145     {
146       Grob *se = broken_intos_[i];
147
148       extract_grob_set (se, "all-elements", all_elts);
149       for (vsize j = 0; j < all_elts.size (); j++)
150         {
151           Grob *g = all_elts[j];
152           g->fixup_refpoint ();
153         }
154
155       count += all_elts.size ();
156     }
157
158   /*
159     needed for doing items.
160   */
161   fixup_refpoints (all_elements_->array ());
162
163   for (vsize i = 0; i < all_elements_->size (); i++)
164     all_elements_->grob (i)->handle_broken_dependencies ();
165
166   handle_broken_dependencies ();
167
168   /* Because the this->get_property (all-elements) contains items in 3
169      versions, handle_broken_dependencies () will leave duplicated
170      items in all-elements.  Strictly speaking this is harmless, but
171      it leads to duplicated symbols in the output.  uniq makes sure
172      that no duplicates are in the list.  */
173   for (vsize i = 0; i < broken_intos_.size (); i++)
174     {
175       System *child = dynamic_cast<System*> (broken_intos_[i]);
176       child->all_elements_->remove_duplicates ();
177     }
178
179   if (be_verbose_global)
180     message (_f ("Element count %d.", count + element_count ()));
181
182   SCM lines = scm_c_make_vector (broken_intos_.size (), SCM_EOL);
183   for (vsize i = 0; i < broken_intos_.size (); i++)
184     {
185       if (be_verbose_global)
186         progress_indication ("[");
187
188       System *system = dynamic_cast<System *> (broken_intos_[i]);
189
190       system->post_processing ();
191       system->build_skylines ();
192       if (i > 0)
193         {
194           System *prev = dynamic_cast<System*> (broken_intos_[i-1]);
195           Real r = prev->skylines_[DOWN].distance (system->skylines_[UP]);
196           system->set_property ("skyline-distance", scm_from_double (r));
197         }
198       scm_vector_set_x (lines, scm_from_int (i),
199                         system->get_paper_system ());
200
201       if (be_verbose_global)
202         progress_indication (to_string (i) + "]");
203     }
204   return lines;
205 }
206
207 void
208 System::break_into_pieces (vector<Column_x_positions> const &breaking)
209 {
210   for (vsize i = 0; i < breaking.size (); i++)
211     {
212       System *system = dynamic_cast<System *> (clone (broken_intos_.size ()));
213       system->rank_ = broken_intos_.size ();
214
215       vector<Grob*> c (breaking[i].cols_);
216       pscore_->typeset_system (system);
217
218       int st = Paper_column::get_rank (c[0]);
219       int end = Paper_column::get_rank (c.back ());
220       Interval iv (pure_height (this, st, end));
221       system->set_property ("pure-Y-extent", ly_interval2scm (iv));
222
223       system->set_bound (LEFT, c[0]);
224       system->set_bound (RIGHT, c.back ());
225       for (vsize j = 0; j < c.size (); j++)
226         {
227           c[j]->translate_axis (breaking[i].config_[j], X_AXIS);
228           dynamic_cast<Paper_column *> (c[j])->system_ = system;
229         }
230       
231       set_loose_columns (system, &breaking[i]);
232       broken_intos_.push_back (system);
233     }
234 }
235
236 void
237 System::add_column (Paper_column *p)
238 {
239   Grob *me = this;
240   Grob_array *ga = unsmob_grob_array (me->get_object ("columns"));
241   if (!ga)
242     {
243       SCM scm_ga = Grob_array::make_array ();
244       me->set_object ("columns", scm_ga);
245       ga = unsmob_grob_array (scm_ga);
246     }
247
248   p->rank_ = ga->size ();
249
250   ga->add (p);
251   Axis_group_interface::add_element (this, p);
252 }
253
254 void
255 apply_tweaks (Grob *g, bool broken)
256 {
257   if (bool (g->original ()) == broken)
258     {
259       SCM tweaks = global_registry_->get_tweaks (g);
260       for (SCM s = tweaks; scm_is_pair (s); s = scm_cdr (s))
261         {
262           SCM proc = scm_caar (s);
263           SCM rest = scm_cdar (s);
264           scm_apply_1 (proc, g->self_scm (), rest);
265         }
266     }
267 }
268
269 void
270 System::pre_processing ()
271 {
272   for (vsize i = 0; i < all_elements_->size (); i++)
273     all_elements_->grob (i)->discretionary_processing ();
274
275   if (be_verbose_global)
276     message (_f ("Grob count %d", element_count ()));
277
278   /*
279     order is significant: broken grobs are added to the end of the
280     array, and should be processed before the original is potentially
281     killed.
282   */
283   for (vsize i = all_elements_->size (); i--;)
284     all_elements_->grob (i)->handle_prebroken_dependencies ();
285
286   fixup_refpoints (all_elements_->array ());
287
288   for (vsize i = 0; i < all_elements_->size (); i++)
289     apply_tweaks (all_elements_->grob (i), false);
290
291   for (vsize i = 0; i < all_elements_->size (); i++)
292     {
293       Grob *g = all_elements_->grob (i);
294       (void) g->get_property ("before-line-breaking");
295     }
296
297   for (vsize i = 0; i < all_elements_->size (); i++)
298     {
299       Grob *e = all_elements_->grob (i);
300       (void) e->get_property ("springs-and-rods");
301     }
302 }
303
304 void
305 System::post_processing ()
306 {
307   for (vsize i = 0; i < all_elements_->size (); i++)
308     {
309       Grob *g = all_elements_->grob (i);
310
311       apply_tweaks (g, true);
312       (void) g->get_property ("after-line-breaking");
313     }
314
315   Interval iv (extent (this, Y_AXIS));
316   if (iv.is_empty ())
317     programming_error ("system with empty extent");
318   else
319     translate_axis (-iv[MAX], Y_AXIS);
320
321   /* Generate all stencils to trigger font loads.
322      This might seem inefficient, but Stencils are cached per grob
323      anyway. */
324
325   vector<Grob*> all_elts_sorted (all_elements_->array ());
326   vector_sort (all_elts_sorted, std::less<Grob*> ());
327   uniq (all_elts_sorted);
328   this->get_stencil ();
329   for (vsize i = all_elts_sorted.size (); i--;)
330     {
331       Grob *g = all_elts_sorted[i];
332       g->get_stencil ();
333     }
334 }
335
336 struct Layer_entry
337 {
338   Grob *grob_;
339   int layer_;
340 };
341
342 bool
343 operator< (Layer_entry  const &a,
344            Layer_entry  const &b)
345 {
346   return a.layer_ < b.layer_;
347 }
348
349
350 SCM
351 System::get_paper_system ()
352 {
353   SCM exprs = SCM_EOL;
354   SCM *tail = &exprs;
355
356   vector<Layer_entry> entries;
357   for (vsize j = 0; j < all_elements_->size (); j++)
358     {
359       Layer_entry e;
360       e.grob_ = all_elements_->grob (j);
361       e.layer_ = robust_scm2int (e.grob_->get_property ("layer"), 1);
362       
363       entries.push_back (e); 
364     }
365
366   vector_sort (entries, std::less<Layer_entry> ());
367   for (vsize j = 0; j < entries.size (); j++)
368     {
369       Grob *g = entries[j].grob_;
370       Stencil st = g->get_print_stencil ();
371
372       if (st.expr() == SCM_EOL)
373         continue;
374       
375       Offset o;
376       for (int a = X_AXIS; a < NO_AXES; a++)
377         o[Axis (a)] = g->relative_coordinate (this, Axis (a));
378
379       Offset extra = robust_scm2offset (g->get_property ("extra-offset"),
380                                         Offset (0, 0))
381         * Staff_symbol_referencer::staff_space (g);
382
383       /* Must copy the stencil, for we cannot change the stencil
384          cached in G.  */
385
386       st.translate (o + extra);
387
388       *tail = scm_cons (st.expr (), SCM_EOL);
389       tail = SCM_CDRLOC (*tail);
390     }
391
392   if (Stencil *me = get_stencil ())
393     exprs = scm_cons (me->expr (), exprs);
394
395   Interval x (extent (this, X_AXIS));
396   Interval y (extent (this, Y_AXIS));
397   Stencil sys_stencil (Box (x, y),
398                        scm_cons (ly_symbol2scm ("combine-stencil"),
399                                  exprs));
400   if (debug_skylines)
401     {
402       sys_stencil.add_stencil (points_to_line_stencil (skylines_[UP].to_points ()).in_color (255, 0, 0));
403       sys_stencil.add_stencil (points_to_line_stencil (skylines_[DOWN].to_points ()).in_color (0, 255, 0));
404     }
405
406   Grob *left_bound = this->get_bound (LEFT);
407   SCM prop_init = left_bound->get_property ("line-break-system-details");
408   Prob *pl = make_paper_system (prop_init);
409   paper_system_set_stencil (pl, sys_stencil);
410
411   /* information that the page breaker might need */
412   Grob *right_bound = this->get_bound (RIGHT);
413   pl->set_property ("skyline-distance", get_property ("skyline-distance"));
414   pl->set_property ("page-break-permission", right_bound->get_property ("page-break-permission"));
415   pl->set_property ("page-turn-permission", right_bound->get_property ("page-turn-permission"));
416   pl->set_property ("page-break-penalty", right_bound->get_property ("page-break-penalty"));
417   pl->set_property ("page-turn-penalty", right_bound->get_property ("page-turn-penalty"));
418
419   if (!scm_is_pair (pl->get_property ("refpoint-Y-extent")))
420     {
421       Interval staff_refpoints;
422       staff_refpoints.set_empty ();
423       extract_grob_set (this, "spaceable-staves", staves);
424       for (vsize i = 0; i < staves.size (); i++)
425         {
426           Grob *g = staves[i];
427           staff_refpoints.add_point (g->relative_coordinate (this, Y_AXIS));
428         }
429       pl->set_property ("refpoint-Y-extent", ly_interval2scm (staff_refpoints));
430     }
431
432   pl->set_property ("system-grob", this->self_scm ()); 
433
434   return pl->unprotect ();
435 }
436
437 vector<Item*>
438 System::broken_col_range (Item const *left, Item const *right) const
439 {
440   vector<Item*> ret;
441
442   left = left->get_column ();
443   right = right->get_column ();
444
445   
446   extract_grob_set (this, "columns", cols);
447
448   vsize i = binary_search (cols, (Grob *) left,
449                            Paper_column::less_than);
450
451   int end_rank = Paper_column::get_rank (right);
452   if (i < cols.size ())
453     i++;
454
455   while (i < cols.size ()
456          && Paper_column::get_rank (cols[i]) < end_rank)
457     {
458       Paper_column *c = dynamic_cast<Paper_column *> (cols[i]);
459       if (Paper_column::is_breakable (c) && !c->system_)
460         ret.push_back (c);
461       i++;
462     }
463
464   return ret;
465 }
466
467
468 /** Return all columns, but filter out any unused columns , since they might
469     disrupt the spacing problem. */
470 vector<Grob*>
471 System::columns () const
472 {
473   extract_grob_set (this, "columns", ro_columns);
474
475   int last_breakable = ro_columns.size ();
476
477   while (last_breakable--)
478     {
479       if (Paper_column::is_breakable (ro_columns [last_breakable]))
480         break;
481     }
482
483   vector<Grob*> columns;
484   for (int i = 0; i <= last_breakable; i++)
485     {
486       if (Paper_column::is_used (ro_columns[i]))
487         columns.push_back (ro_columns[i]);
488     }
489
490   return columns;
491 }
492
493 Paper_score*
494 System::paper_score () const
495 {
496   return pscore_;
497 }
498
499 int
500 System::get_rank () const
501 {
502   return rank_;
503 }
504
505 System *
506 get_root_system (Grob *me) 
507 {
508   Grob *system_grob = me;
509   
510   while (system_grob->get_parent (Y_AXIS))
511     system_grob = system_grob->get_parent (Y_AXIS);
512
513   return dynamic_cast<System*> (system_grob); 
514 }
515
516 void
517 System::build_skylines ()
518 {
519   vector<Box> boxes;
520   for (vsize i = 0; i < all_elements_->size (); i++)
521     {
522       Grob *g = all_elements_->grob (i);
523       if (!unsmob_stencil (g->get_property ("stencil")))
524         continue;
525
526       Interval xiv = g->extent (this, X_AXIS);
527       Interval yiv = g->extent (this, Y_AXIS);
528       if (!xiv.is_empty () && !yiv.is_empty ())
529         boxes.push_back (Box (xiv, yiv));
530     }
531
532   SCM horizon_padding_scm = get_property ("skyline-horizontal-padding");
533   Real horizon_padding = robust_scm2double (horizon_padding_scm, 0);
534   skylines_[UP] = Skyline (boxes, horizon_padding, X_AXIS, UP);
535   skylines_[DOWN] = Skyline (boxes, horizon_padding, X_AXIS, DOWN);
536 }
537
538
539 ADD_INTERFACE (System,
540                "This is the toplevel object: each object in a score "
541                "ultimately has a System object as its X and Y parent. ",
542
543                /* properties */
544                "all-elements "
545                "columns "
546                "pure-Y-extent "
547                "spaceable-staves "
548                "skyline-distance "
549                "skyline-horizontal-padding "
550                )