]> git.donarmstrong.com Git - lilypond.git/blob - lily/system.cc
* scm/paper-system.scm (paper-system-annotate): also annotate the
[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 System::System (System const &src, int count)
28   : Spanner (src, count)
29 {
30   all_elements_ = 0;
31   pscore_ = 0;
32   rank_ = 0;
33   init_elements ();
34 }
35
36 System::System (SCM s, Object_key const *key)
37   : Spanner (s, key)
38 {
39   all_elements_ = 0;
40   rank_ = 0;
41   init_elements ();
42 }
43
44 void
45 System::init_elements ()
46 {
47   SCM scm_arr = Grob_array::make_array ();
48   all_elements_ = unsmob_grob_array (scm_arr);
49   all_elements_->set_ordered (false);
50   set_object ("all-elements", scm_arr);
51 }
52
53 Grob *
54 System::clone (int index) const
55 {
56   return new System (*this, index);
57 }
58
59 int
60 System::element_count () const
61 {
62   return all_elements_->size ();
63 }
64
65 int
66 System::spanner_count () const
67 {
68   int k = 0;
69   for (vsize i = all_elements_->size (); i--;)
70     if (dynamic_cast<Spanner *> (all_elements_->grob (i)))
71       k++;
72   return k;
73 }
74
75 void
76 System::typeset_grob (Grob *elem)
77 {
78   if (elem->layout_)
79     programming_error ("adding element twice");
80   else
81     {
82       elem->layout_ = pscore_->layout ();
83       all_elements_->add (elem);
84       elem->unprotect ();
85     }
86 }
87
88 void
89 System::derived_mark () const
90 {
91   if (!all_elements_->empty ())
92     {
93       Grob **ptr = &all_elements_->array_reference ()[0];
94       Grob **end = ptr + all_elements_->size ();
95       while (ptr < end)
96         {
97           scm_gc_mark ((*ptr)->self_scm ());
98           ptr++;
99         }
100     }
101
102   if (pscore_)
103     scm_gc_mark (pscore_->self_scm ());
104
105   Spanner::derived_mark ();
106 }
107
108 static void
109 fixup_refpoints (vector<Grob*> const &grobs)
110 {
111   for (vsize i = grobs.size (); i--;)
112     grobs[i]->fixup_refpoint ();
113 }
114
115 SCM
116 System::get_paper_systems ()
117 {
118   for (vsize i = 0; i < all_elements_->size (); i++)
119     {
120       Grob *g = all_elements_->grob (i);
121       if (g->internal_has_interface (ly_symbol2scm ("only-prebreak-interface")))
122         {
123           /*
124             Kill no longer needed grobs.
125           */
126           Item *it = dynamic_cast<Item *> (g);
127           if (it && Item::is_non_musical (it))
128             {
129               it->find_prebroken_piece (LEFT)->suicide ();
130               it->find_prebroken_piece (RIGHT)->suicide ();
131             }
132           g->suicide ();
133         }
134       else if (g->is_live ())
135         g->do_break_processing ();
136     }
137
138   /*
139     fixups must be done in broken line_of_scores, because new elements
140     are put over there.  */
141   int count = 0;
142   for (vsize i = 0; i < broken_intos_.size (); i++)
143     {
144       Grob *se = broken_intos_[i];
145
146       extract_grob_set (se, "all-elements", all_elts);
147       for (vsize j = 0; j < all_elts.size (); j++)
148         {
149           Grob *g = all_elts[j];
150           g->fixup_refpoint ();
151         }
152
153       count += all_elts.size ();
154     }
155
156   /*
157     needed for doing items.
158   */
159   fixup_refpoints (all_elements_->array ());
160
161   for (vsize i = 0; i < all_elements_->size (); i++)
162     all_elements_->grob (i)->handle_broken_dependencies ();
163
164   handle_broken_dependencies ();
165
166   /* Because the this->get_property (all-elements) contains items in 3
167      versions, handle_broken_dependencies () will leave duplicated
168      items in all-elements.  Strictly speaking this is harmless, but
169      it leads to duplicated symbols in the output.  uniq makes sure
170      that no duplicates are in the list.  */
171   for (vsize i = 0; i < broken_intos_.size (); i++)
172     {
173       System *child = dynamic_cast<System*> (broken_intos_[i]);
174       child->all_elements_->remove_duplicates ();
175     }
176
177   if (be_verbose_global)
178     message (_f ("Element count %d.", count + element_count ()));
179
180   SCM lines = scm_c_make_vector (broken_intos_.size (), SCM_EOL);
181   for (vsize i = 0; i < broken_intos_.size (); i++)
182     {
183       if (be_verbose_global)
184         progress_indication ("[");
185
186       System *system = dynamic_cast<System *> (broken_intos_[i]);
187
188       system->post_processing ();
189       scm_vector_set_x (lines, scm_from_int (i),
190                         system->get_paper_system ());
191
192       if (be_verbose_global)
193         progress_indication (to_string (i) + "]");
194     }
195   return lines;
196 }
197
198 void
199 System::break_into_pieces (vector<Column_x_positions> const &breaking)
200 {
201   for (vsize i = 0; i < breaking.size (); i++)
202     {
203       System *system = dynamic_cast<System *> (clone (broken_intos_.size ()));
204       system->rank_ = broken_intos_.size ();
205
206       vector<Grob*> c (breaking[i].cols_);
207       pscore_->typeset_system (system);
208
209       int st = Paper_column::get_rank (c[0]);
210       int end = Paper_column::get_rank (c.back ());
211       Interval iv (pure_height (this, st, end));
212       system->set_property ("pure-Y-extent", ly_interval2scm (iv));
213
214       system->set_bound (LEFT, c[0]);
215       system->set_bound (RIGHT, c.back ());
216       for (vsize j = 0; j < c.size (); j++)
217         {
218           c[j]->translate_axis (breaking[i].config_[j], X_AXIS);
219           dynamic_cast<Paper_column *> (c[j])->system_ = system;
220         }
221       set_loose_columns (system, &breaking[i]);
222       broken_intos_.push_back (system);
223     }
224 }
225
226 void
227 System::add_column (Paper_column *p)
228 {
229   Grob *me = this;
230   Grob_array *ga = unsmob_grob_array (me->get_object ("columns"));
231   if (!ga)
232     {
233       SCM scm_ga = Grob_array::make_array ();
234       me->set_object ("columns", scm_ga);
235       ga = unsmob_grob_array (scm_ga);
236     }
237
238   p->rank_
239     = ga->size ()
240     ? Paper_column::get_rank (ga->array ().back ()) + 1
241     : 0;
242
243   ga->add (p);
244   Axis_group_interface::add_element (this, p);
245 }
246
247 void
248 apply_tweaks (Grob *g, bool broken)
249 {
250   if (bool (g->original ()) == broken)
251     {
252       SCM tweaks = global_registry_->get_tweaks (g);
253       for (SCM s = tweaks; scm_is_pair (s); s = scm_cdr (s))
254         {
255           SCM proc = scm_caar (s);
256           SCM rest = scm_cdar (s);
257           scm_apply_1 (proc, g->self_scm (), rest);
258         }
259     }
260 }
261
262 void
263 System::pre_processing ()
264 {
265   for (vsize i = 0; i < all_elements_->size (); i++)
266     all_elements_->grob (i)->discretionary_processing ();
267
268   if (be_verbose_global)
269     message (_f ("Grob count %d", element_count ()));
270
271   /*
272     order is significant: broken grobs are added to the end of the
273     array, and should be processed before the original is potentially
274     killed.
275   */
276   for (vsize i = all_elements_->size (); i--;)
277     all_elements_->grob (i)->handle_prebroken_dependencies ();
278
279   fixup_refpoints (all_elements_->array ());
280
281   for (vsize i = 0; i < all_elements_->size (); i++)
282     apply_tweaks (all_elements_->grob (i), false);
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       apply_tweaks (g, true);
305       (void) g->get_property ("after-line-breaking");
306     }
307
308   Interval iv (extent (this, Y_AXIS));
309   if (iv.is_empty ())
310     programming_error ("system with empty extent");
311   else
312     translate_axis (-iv[MAX], Y_AXIS);
313
314   /* Generate all stencils to trigger font loads.
315      This might seem inefficient, but Stencils are cached per grob
316      anyway. */
317
318   vector<Grob*> all_elts_sorted (all_elements_->array ());
319   vector_sort (all_elts_sorted, default_compare);
320   uniq (all_elts_sorted);
321   this->get_stencil ();
322   for (vsize i = all_elts_sorted.size (); i--;)
323     {
324       Grob *g = all_elts_sorted[i];
325       g->get_stencil ();
326     }
327 }
328
329 struct Layer_entry
330 {
331   Grob *grob_;
332   int layer_;
333 };
334
335 bool
336 operator< (Layer_entry  const &a,
337            Layer_entry  const &b)
338 {
339   return a.layer_ < b.layer_;
340 }
341
342
343 SCM
344 System::get_paper_system ()
345 {
346   SCM exprs = SCM_EOL;
347   SCM *tail = &exprs;
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, default_compare);
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
394   Grob *left_bound = this->get_bound (LEFT);
395   SCM prop_init = left_bound->get_property ("line-break-system-details");
396   Prob *pl = make_paper_system (prop_init);
397   paper_system_set_stencil (pl, sys_stencil);
398
399   /* backwards-compatibility hack for the old page-breaker */
400   SCM turn_perm = left_bound->get_property ("page-break-permission");
401   if (!scm_is_symbol (turn_perm))
402     pl->set_property ("penalty", scm_from_double (10001.0));
403   else if (turn_perm == ly_symbol2scm ("force"))
404     pl->set_property ("penalty", scm_from_double (-10001.0));
405   else
406     pl->set_property ("penalty", scm_from_double (0.0));
407   
408   if (!scm_is_pair (pl->get_property ("refpoint-Y-extent")))
409     {
410       Interval staff_refpoints;
411       staff_refpoints.set_empty ();
412       extract_grob_set (this, "spaceable-staves", staves);
413       for (vsize i = 0; i < staves.size (); i++)
414         {
415           Grob *g = staves[i];
416           staff_refpoints.add_point (g->relative_coordinate (this, Y_AXIS));
417         }
418       pl->set_property ("refpoint-Y-extent", ly_interval2scm (staff_refpoints));
419     }
420
421   pl->set_property ("system-grob", this->self_scm ()); 
422
423   return pl->unprotect ();
424 }
425
426 vector<Item*>
427 System::broken_col_range (Item const *left, Item const *right) const
428 {
429   vector<Item*> ret;
430
431   left = left->get_column ();
432   right = right->get_column ();
433
434   extract_grob_set (this, "columns", cols);
435   vsize i = 0;
436   while (i < cols.size ()
437          && cols[i] != left)
438     i++;
439
440   if (i < cols.size ())
441     i++;
442
443   while (i < cols.size ()
444          && cols[i] != right)
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 /** Return all columns, but filter out any unused columns , since they might
456     disrupt the spacing problem. */
457 vector<Grob*>
458 System::columns () const
459 {
460   extract_grob_set (this, "columns", ro_columns);
461
462   int last_breakable = ro_columns.size ();
463
464   while (last_breakable--)
465     {
466       if (Paper_column::is_breakable (ro_columns [last_breakable]))
467         break;
468     }
469
470   vector<Grob*> columns;
471   for (int i = 0; i <= last_breakable; i++)
472     {
473       if (Paper_column::is_used (ro_columns[i]))
474         columns.push_back (ro_columns[i]);
475     }
476
477   return columns;
478 }
479
480 Paper_score*
481 System::paper_score () const
482 {
483   return pscore_;
484 }
485
486 int
487 System::get_rank () const
488 {
489   return rank_;
490 }
491
492 System *
493 get_root_system (Grob *me) 
494 {
495   Grob *system_grob = me;
496   
497   while (system_grob->get_parent (Y_AXIS))
498     system_grob = system_grob->get_parent (Y_AXIS);
499
500   return dynamic_cast<System*> (system_grob); 
501 }
502
503
504
505 ADD_INTERFACE (System, "system-interface",
506                "This is the toplevel object: each object in a score "
507                "ultimately has a System object as its X and Y parent. ",
508
509                /* properties */
510                "all-elements "
511                "columns "
512                "pure-Y-extent "
513                "spaceable-staves "
514                )