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