]> git.donarmstrong.com Git - lilypond.git/blob - lily/system.cc
Bookmarking labels and page referencing:
[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 void
118 System::do_break_substitution_and_fixup_refpoints ()
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
183 SCM
184 System::get_broken_system_grobs ()
185 {
186   SCM ret = SCM_EOL;
187   for (vsize i = 0; i < broken_intos_.size (); i++)
188     ret = scm_cons (broken_intos_[i]->self_scm (), ret);
189   return scm_reverse (ret);
190 }
191
192 SCM
193 System::get_paper_systems ()
194 {
195   SCM lines = scm_c_make_vector (broken_intos_.size (), SCM_EOL);
196   for (vsize i = 0; i < broken_intos_.size (); i++)
197     {
198       if (be_verbose_global)
199         progress_indication ("[");
200
201       System *system = dynamic_cast<System *> (broken_intos_[i]);
202
203       scm_vector_set_x (lines, scm_from_int (i),
204                         system->get_paper_system ());
205
206       if (be_verbose_global)
207         progress_indication (to_string (i) + "]");
208     }
209   return lines;
210 }
211
212 void
213 System::break_into_pieces (vector<Column_x_positions> const &breaking)
214 {
215   for (vsize i = 0; i < breaking.size (); i++)
216     {
217       System *system = dynamic_cast<System *> (clone ());
218       system->rank_ = broken_intos_.size ();
219
220       vector<Grob*> c (breaking[i].cols_);
221       pscore_->typeset_system (system);
222
223       int st = Paper_column::get_rank (c[0]);
224       int end = Paper_column::get_rank (c.back ());
225       Interval iv (pure_height (this, st, end));
226       system->set_property ("pure-Y-extent", ly_interval2scm (iv));
227
228       system->set_bound (LEFT, c[0]);
229       system->set_bound (RIGHT, c.back ());
230       SCM system_labels = SCM_EOL;
231       for (vsize j = 0; j < c.size (); j++)
232         {
233           c[j]->translate_axis (breaking[i].config_[j], X_AXIS);
234           dynamic_cast<Paper_column *> (c[j])->system_ = system;
235           /* collect the column labels */
236           SCM col_labels = c[j]->get_property ("labels");
237           if (scm_is_pair (col_labels))
238             system_labels = scm_append (scm_list_2 (col_labels, system_labels));
239         }
240       system->set_property ("labels", system_labels);
241       
242       set_loose_columns (system, &breaking[i]);
243       broken_intos_.push_back (system);
244     }
245 }
246
247 void
248 System::add_column (Paper_column *p)
249 {
250   Grob *me = this;
251   Grob_array *ga = unsmob_grob_array (me->get_object ("columns"));
252   if (!ga)
253     {
254       SCM scm_ga = Grob_array::make_array ();
255       me->set_object ("columns", scm_ga);
256       ga = unsmob_grob_array (scm_ga);
257     }
258
259   p->rank_ = ga->size ();
260
261   ga->add (p);
262   Axis_group_interface::add_element (this, p);
263 }
264
265 void
266 System::pre_processing ()
267 {
268   for (vsize i = 0; i < all_elements_->size (); i++)
269     all_elements_->grob (i)->discretionary_processing ();
270
271   if (be_verbose_global)
272     message (_f ("Grob count %d", element_count ()));
273
274   /*
275     order is significant: broken grobs are added to the end of the
276     array, and should be processed before the original is potentially
277     killed.
278   */
279   for (vsize i = all_elements_->size (); i--;)
280     all_elements_->grob (i)->handle_prebroken_dependencies ();
281
282   fixup_refpoints (all_elements_->array ());
283
284   for (vsize i = 0; i < all_elements_->size (); i++)
285     {
286       Grob *g = all_elements_->grob (i);
287       (void) g->get_property ("before-line-breaking");
288     }
289
290   for (vsize i = 0; i < all_elements_->size (); i++)
291     {
292       Grob *e = all_elements_->grob (i);
293       (void) e->get_property ("springs-and-rods");
294     }
295 }
296
297 void
298 System::post_processing ()
299 {
300   for (vsize i = 0; i < all_elements_->size (); i++)
301     {
302       Grob *g = all_elements_->grob (i);
303
304       (void) g->get_property ("after-line-breaking");
305     }
306
307   Interval iv (extent (this, Y_AXIS));
308   if (iv.is_empty ())
309     programming_error ("system with empty extent");
310   else
311     translate_axis (-iv[MAX], Y_AXIS);
312
313   /* Generate all stencils to trigger font loads.
314      This might seem inefficient, but Stencils are cached per grob
315      anyway. */
316
317   vector<Grob*> all_elts_sorted (all_elements_->array ());
318   vector_sort (all_elts_sorted, std::less<Grob*> ());
319   uniq (all_elts_sorted);
320   this->get_stencil ();
321   for (vsize i = all_elts_sorted.size (); i--;)
322     {
323       Grob *g = all_elts_sorted[i];
324       g->get_stencil ();
325     }
326 }
327
328 struct Layer_entry
329 {
330   Grob *grob_;
331   int layer_;
332 };
333
334 bool
335 operator< (Layer_entry  const &a,
336            Layer_entry  const &b)
337 {
338   return a.layer_ < b.layer_;
339 }
340
341
342 SCM
343 System::get_paper_system ()
344 {
345   SCM exprs = SCM_EOL;
346   SCM *tail = &exprs;
347
348   post_processing ();
349
350   vector<Layer_entry> entries;
351   for (vsize j = 0; j < all_elements_->size (); j++)
352     {
353       Layer_entry e;
354       e.grob_ = all_elements_->grob (j);
355       e.layer_ = robust_scm2int (e.grob_->get_property ("layer"), 1);
356       
357       entries.push_back (e); 
358     }
359
360   vector_sort (entries, std::less<Layer_entry> ());
361   for (vsize j = 0; j < entries.size (); j++)
362     {
363       Grob *g = entries[j].grob_;
364       Stencil st = g->get_print_stencil ();
365
366       if (st.expr () == SCM_EOL)
367         continue;
368       
369       Offset o;
370       for (int a = X_AXIS; a < NO_AXES; a++)
371         o[Axis (a)] = g->relative_coordinate (this, Axis (a));
372
373       Offset extra = robust_scm2offset (g->get_property ("extra-offset"),
374                                         Offset (0, 0))
375         * Staff_symbol_referencer::staff_space (g);
376
377       /* Must copy the stencil, for we cannot change the stencil
378          cached in G.  */
379
380       st.translate (o + extra);
381
382       *tail = scm_cons (st.expr (), SCM_EOL);
383       tail = SCM_CDRLOC (*tail);
384     }
385
386   if (Stencil *me = get_stencil ())
387     exprs = scm_cons (me->expr (), exprs);
388
389   Interval x (extent (this, X_AXIS));
390   Interval y (extent (this, Y_AXIS));
391   Stencil sys_stencil (Box (x, y),
392                        scm_cons (ly_symbol2scm ("combine-stencil"),
393                                  exprs));
394   if (debug_skylines)
395     {
396       Skyline_pair *skylines = Skyline_pair::unsmob (get_property ("vertical-skylines"));
397       if (skylines)
398         {
399           sys_stencil.add_stencil (Lookup::points_to_line_stencil (0.1, (*skylines)[UP].to_points (X_AXIS)).in_color (255, 0, 0));
400           sys_stencil.add_stencil (Lookup::points_to_line_stencil (0.1, (*skylines)[DOWN].to_points (X_AXIS)).in_color (0, 255, 0));
401         }
402     }
403
404   Grob *left_bound = this->get_bound (LEFT);
405   SCM prop_init = left_bound->get_property ("line-break-system-details");
406   Prob *pl = make_paper_system (prop_init);
407   paper_system_set_stencil (pl, sys_stencil);
408
409   /* information that the page breaker might need */
410   Grob *right_bound = this->get_bound (RIGHT);
411   pl->set_property ("vertical-skylines", this->get_property ("vertical-skylines"));
412   pl->set_property ("page-break-permission", right_bound->get_property ("page-break-permission"));
413   pl->set_property ("page-turn-permission", right_bound->get_property ("page-turn-permission"));
414   pl->set_property ("page-break-penalty", right_bound->get_property ("page-break-penalty"));
415   pl->set_property ("page-turn-penalty", right_bound->get_property ("page-turn-penalty"));
416
417   Interval staff_refpoints;
418   extract_grob_set (this, "spaceable-staves", staves);
419   for (vsize i = 0; i < staves.size (); i++)
420     staff_refpoints.add_point (staves[i]->relative_coordinate (this, Y_AXIS));
421
422   pl->set_property ("staff-refpoint-extent", ly_interval2scm (staff_refpoints));
423   pl->set_property ("system-grob", this->self_scm ()); 
424
425   return pl->unprotect ();
426 }
427
428 vector<Item*>
429 System::broken_col_range (Item const *left, Item const *right) const
430 {
431   vector<Item*> ret;
432
433   left = left->get_column ();
434   right = right->get_column ();
435
436   
437   extract_grob_set (this, "columns", cols);
438
439   vsize i = Paper_column::get_rank (left);
440   int end_rank = Paper_column::get_rank (right);
441   if (i < cols.size ())
442     i++;
443
444   while (i < cols.size ()
445          && Paper_column::get_rank (cols[i]) < end_rank)
446     {
447       Paper_column *c = dynamic_cast<Paper_column *> (cols[i]);
448       if (Paper_column::is_breakable (c) && !c->system_)
449         ret.push_back (c);
450       i++;
451     }
452
453   return ret;
454 }
455
456
457 /** Return all columns, but filter out any unused columns , since they might
458     disrupt the spacing problem. */
459 vector<Grob*>
460 System::used_columns () const
461 {
462   extract_grob_set (this, "columns", ro_columns);
463
464   int last_breakable = ro_columns.size ();
465
466   while (last_breakable--)
467     {
468       if (Paper_column::is_breakable (ro_columns [last_breakable]))
469         break;
470     }
471
472   vector<Grob*> columns;
473   for (int i = 0; i <= last_breakable; i++)
474     {
475       if (Paper_column::is_used (ro_columns[i]))
476         columns.push_back (ro_columns[i]);
477     }
478
479   return columns;
480 }
481
482 Paper_column *
483 System::column (vsize which) const
484 {
485   extract_grob_set (this, "columns", columns);
486   if (which >= columns.size ())
487     return 0;
488   
489   return dynamic_cast<Paper_column*> (columns[which]);
490 }
491
492 Paper_score*
493 System::paper_score () const
494 {
495   return pscore_;
496 }
497
498 int
499 System::get_rank () const
500 {
501   return rank_;
502 }
503
504 System *
505 get_root_system (Grob *me) 
506 {
507   Grob *system_grob = me;
508   
509   while (system_grob->get_parent (Y_AXIS))
510     system_grob = system_grob->get_parent (Y_AXIS);
511
512   return dynamic_cast<System*> (system_grob); 
513 }
514
515 ADD_INTERFACE (System,
516                "This is the toplevel object: each object in a score "
517                "ultimately has a System object as its X and Y parent. ",
518
519                /* properties */
520                "all-elements "
521                "columns "
522                "pure-Y-extent "
523                "spaceable-staves "
524                "skyline-distance "
525                "skyline-horizontal-padding "
526                )