]> git.donarmstrong.com Git - lilypond.git/blob - lily/system.cc
fix tuplet example
[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--2007 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 "warn.hh"
25 #include "lookup.hh"
26
27 extern bool debug_skylines;
28
29 System::System (System const &src)
30   : Spanner (src)
31 {
32   all_elements_ = 0;
33   pscore_ = 0;
34   rank_ = 0;
35   init_elements ();
36 }
37
38 System::System (SCM s)
39   : Spanner (s)
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 () const
57 {
58   return new System (*this);
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 ());
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 System::pre_processing ()
256 {
257   for (vsize i = 0; i < all_elements_->size (); i++)
258     all_elements_->grob (i)->discretionary_processing ();
259
260   if (be_verbose_global)
261     message (_f ("Grob count %d", element_count ()));
262
263   /*
264     order is significant: broken grobs are added to the end of the
265     array, and should be processed before the original is potentially
266     killed.
267   */
268   for (vsize i = all_elements_->size (); i--;)
269     all_elements_->grob (i)->handle_prebroken_dependencies ();
270
271   fixup_refpoints (all_elements_->array ());
272
273   for (vsize i = 0; i < all_elements_->size (); i++)
274     {
275       Grob *g = all_elements_->grob (i);
276       (void) g->get_property ("before-line-breaking");
277     }
278
279   for (vsize i = 0; i < all_elements_->size (); i++)
280     {
281       Grob *e = all_elements_->grob (i);
282       (void) e->get_property ("springs-and-rods");
283     }
284 }
285
286 void
287 System::post_processing ()
288 {
289   for (vsize i = 0; i < all_elements_->size (); i++)
290     {
291       Grob *g = all_elements_->grob (i);
292
293       (void) g->get_property ("after-line-breaking");
294     }
295
296   Interval iv (extent (this, Y_AXIS));
297   if (iv.is_empty ())
298     programming_error ("system with empty extent");
299   else
300     translate_axis (-iv[MAX], Y_AXIS);
301
302   /* Generate all stencils to trigger font loads.
303      This might seem inefficient, but Stencils are cached per grob
304      anyway. */
305
306   vector<Grob*> all_elts_sorted (all_elements_->array ());
307   vector_sort (all_elts_sorted, std::less<Grob*> ());
308   uniq (all_elts_sorted);
309   this->get_stencil ();
310   for (vsize i = all_elts_sorted.size (); i--;)
311     {
312       Grob *g = all_elts_sorted[i];
313       g->get_stencil ();
314     }
315 }
316
317 struct Layer_entry
318 {
319   Grob *grob_;
320   int layer_;
321 };
322
323 bool
324 operator< (Layer_entry  const &a,
325            Layer_entry  const &b)
326 {
327   return a.layer_ < b.layer_;
328 }
329
330
331 SCM
332 System::get_paper_system ()
333 {
334   SCM exprs = SCM_EOL;
335   SCM *tail = &exprs;
336
337   vector<Layer_entry> entries;
338   for (vsize j = 0; j < all_elements_->size (); j++)
339     {
340       Layer_entry e;
341       e.grob_ = all_elements_->grob (j);
342       e.layer_ = robust_scm2int (e.grob_->get_property ("layer"), 1);
343       
344       entries.push_back (e); 
345     }
346
347   vector_sort (entries, std::less<Layer_entry> ());
348   for (vsize j = 0; j < entries.size (); j++)
349     {
350       Grob *g = entries[j].grob_;
351       Stencil st = g->get_print_stencil ();
352
353       if (st.expr() == SCM_EOL)
354         continue;
355       
356       Offset o;
357       for (int a = X_AXIS; a < NO_AXES; a++)
358         o[Axis (a)] = g->relative_coordinate (this, Axis (a));
359
360       Offset extra = robust_scm2offset (g->get_property ("extra-offset"),
361                                         Offset (0, 0))
362         * Staff_symbol_referencer::staff_space (g);
363
364       /* Must copy the stencil, for we cannot change the stencil
365          cached in G.  */
366
367       st.translate (o + extra);
368
369       *tail = scm_cons (st.expr (), SCM_EOL);
370       tail = SCM_CDRLOC (*tail);
371     }
372
373   if (Stencil *me = get_stencil ())
374     exprs = scm_cons (me->expr (), exprs);
375
376   Interval x (extent (this, X_AXIS));
377   Interval y (extent (this, Y_AXIS));
378   Stencil sys_stencil (Box (x, y),
379                        scm_cons (ly_symbol2scm ("combine-stencil"),
380                                  exprs));
381   if (debug_skylines)
382     {
383       sys_stencil.add_stencil (Lookup::points_to_line_stencil (0.1, skylines_[UP].to_points ()).in_color (255, 0, 0));
384       sys_stencil.add_stencil (Lookup::points_to_line_stencil (0.1, skylines_[DOWN].to_points ()).in_color (0, 255, 0));
385     }
386
387   Grob *left_bound = this->get_bound (LEFT);
388   SCM prop_init = left_bound->get_property ("line-break-system-details");
389   Prob *pl = make_paper_system (prop_init);
390   paper_system_set_stencil (pl, sys_stencil);
391
392   /* information that the page breaker might need */
393   Grob *right_bound = this->get_bound (RIGHT);
394   pl->set_property ("skyline-distance", get_property ("skyline-distance"));
395   pl->set_property ("page-break-permission", right_bound->get_property ("page-break-permission"));
396   pl->set_property ("page-turn-permission", right_bound->get_property ("page-turn-permission"));
397   pl->set_property ("page-break-penalty", right_bound->get_property ("page-break-penalty"));
398   pl->set_property ("page-turn-penalty", right_bound->get_property ("page-turn-penalty"));
399
400   if (!scm_is_pair (pl->get_property ("refpoint-Y-extent")))
401     {
402       Interval staff_refpoints;
403       staff_refpoints.set_empty ();
404       extract_grob_set (this, "spaceable-staves", staves);
405       for (vsize i = 0; i < staves.size (); i++)
406         {
407           Grob *g = staves[i];
408           staff_refpoints.add_point (g->relative_coordinate (this, Y_AXIS));
409         }
410       pl->set_property ("refpoint-Y-extent", ly_interval2scm (staff_refpoints));
411     }
412
413   pl->set_property ("system-grob", this->self_scm ()); 
414
415   return pl->unprotect ();
416 }
417
418 vector<Item*>
419 System::broken_col_range (Item const *left, Item const *right) const
420 {
421   vector<Item*> ret;
422
423   left = left->get_column ();
424   right = right->get_column ();
425
426   
427   extract_grob_set (this, "columns", cols);
428
429   vsize i = Paper_column::get_rank (left);
430   int end_rank = Paper_column::get_rank (right);
431   if (i < cols.size ())
432     i++;
433
434   while (i < cols.size ()
435          && Paper_column::get_rank (cols[i]) < end_rank)
436     {
437       Paper_column *c = dynamic_cast<Paper_column *> (cols[i]);
438       if (Paper_column::is_breakable (c) && !c->system_)
439         ret.push_back (c);
440       i++;
441     }
442
443   return ret;
444 }
445
446
447 /** Return all columns, but filter out any unused columns , since they might
448     disrupt the spacing problem. */
449 vector<Grob*>
450 System::used_columns () const
451 {
452   extract_grob_set (this, "columns", ro_columns);
453
454   int last_breakable = ro_columns.size ();
455
456   while (last_breakable--)
457     {
458       if (Paper_column::is_breakable (ro_columns [last_breakable]))
459         break;
460     }
461
462   vector<Grob*> columns;
463   for (int i = 0; i <= last_breakable; i++)
464     {
465       if (Paper_column::is_used (ro_columns[i]))
466         columns.push_back (ro_columns[i]);
467     }
468
469   return columns;
470 }
471
472 Paper_column *
473 System::column (vsize which) const
474 {
475   extract_grob_set (this, "columns", columns);
476   if (which >= columns.size ())
477     return 0;
478   
479   return dynamic_cast<Paper_column*> (columns[which]);
480 }
481
482 Paper_score*
483 System::paper_score () const
484 {
485   return pscore_;
486 }
487
488 int
489 System::get_rank () const
490 {
491   return rank_;
492 }
493
494 System *
495 get_root_system (Grob *me) 
496 {
497   Grob *system_grob = me;
498   
499   while (system_grob->get_parent (Y_AXIS))
500     system_grob = system_grob->get_parent (Y_AXIS);
501
502   return dynamic_cast<System*> (system_grob); 
503 }
504
505 void
506 System::build_skylines ()
507 {
508   vector<Box> boxes;
509   for (vsize i = 0; i < all_elements_->size (); i++)
510     {
511       Grob *g = all_elements_->grob (i);
512       if (!unsmob_stencil (g->get_property ("stencil")))
513         continue;
514
515       Interval xiv = g->extent (this, X_AXIS);
516       Interval yiv = g->extent (this, Y_AXIS);
517       if (!xiv.is_empty () && !yiv.is_empty ())
518         boxes.push_back (Box (xiv, yiv));
519     }
520
521   SCM horizon_padding_scm = get_property ("skyline-horizontal-padding");
522   Real horizon_padding = robust_scm2double (horizon_padding_scm, 0);
523   skylines_[UP] = Skyline (boxes, horizon_padding, X_AXIS, UP);
524   skylines_[DOWN] = Skyline (boxes, horizon_padding, X_AXIS, DOWN);
525 }
526
527
528 ADD_INTERFACE (System,
529                "This is the toplevel object: each object in a score "
530                "ultimately has a System object as its X and Y parent. ",
531
532                /* properties */
533                "all-elements "
534                "columns "
535                "pure-Y-extent "
536                "spaceable-staves "
537                "skyline-distance "
538                "skyline-horizontal-padding "
539                )