]> git.donarmstrong.com Git - lilypond.git/blob - lily/system.cc
* lily/system.cc (do_derived_mark): don't mark from object_alist_
[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--2005 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8
9 #include "system.hh"
10
11 #include <math.h>
12
13 #include "align-interface.hh"
14 #include "axis-group-interface.hh"
15 #include "warn.hh"
16 #include "main.hh"
17 #include "paper-column.hh"
18 #include "output-def.hh"
19 #include "paper-score.hh"
20 #include "warn.hh"
21 #include "all-font-metrics.hh"
22 #include "spacing-interface.hh"
23 #include "staff-symbol-referencer.hh"
24 #include "paper-book.hh"
25 #include "paper-system.hh"
26 #include "tweak-registration.hh"
27 #include "grob-array.hh"
28
29 System::System (System const &src, int count)
30   : Spanner (src, count)
31 {
32   all_elements_ = 0;
33   rank_ = 0;
34   init_elements (); 
35 }
36
37 System::System (SCM s, Object_key const *key)
38   : Spanner (s, key)
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   set_object ("all-elements", scm_arr);
51 }
52
53
54 Grob *
55 System::clone (int count) const
56 {
57   return new System (*this, count);
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 (int 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->pscore_)
80     programming_error ("adding element twice");
81   else
82     {
83       elem->pscore_ = pscore_;
84       all_elements_->add (elem);
85       scm_gc_unprotect_object (elem->self_scm ());
86     }
87 }
88
89 SCM
90 System::do_derived_mark () const
91 {
92   if (!all_elements_->is_empty ())
93     {
94       Grob **ptr = &all_elements_->array_reference ().elem_ref (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   return Spanner::do_derived_mark ();
103 }
104
105 static void
106 fixup_refpoints (Link_array<Grob> const &grobs)
107 {
108   for (int i = grobs.size (); i--; )
109     {
110       grobs[i]->fixup_refpoint ();
111     }
112 }
113
114 SCM
115 System::get_paper_systems ()
116 {
117   for (int i = 0; i < all_elements_->size(); i++)
118     {
119       Grob *g = all_elements_->grob (i);
120       if (g->internal_has_interface (ly_symbol2scm ("only-prebreak-interface")))
121         {
122           /*
123             Kill no longer needed grobs.
124           */
125           Item *it = dynamic_cast<Item *> (g);
126           if (it && Item::is_breakable (it))
127             {
128               it->find_prebroken_piece (LEFT)->suicide ();
129               it->find_prebroken_piece (RIGHT)->suicide ();
130             }
131           g->suicide ();
132         }
133       else if (g->is_live ())
134         g->do_break_processing ();
135     }
136
137   /*
138     fixups must be done in broken line_of_scores, because new elements
139     are put over there.  */
140   int count = 0;
141   for (int i = 0; i < broken_intos_.size (); i++)
142     {
143       Grob *se = broken_intos_[i];
144       
145       extract_grob_set (se, "all-elements", all_elts);
146       for (int j = 0; j < all_elts.size(); j++)
147         {
148           Grob *g = all_elts[j];
149           g->fixup_refpoint ();
150         }
151       
152       count += all_elts.size ();
153     }
154
155   /*
156     needed for doing items.
157   */
158   fixup_refpoints (all_elements_->array ());
159   
160   for (int i = 0 ; i < all_elements_->size(); i++)
161     all_elements_->grob (i)->handle_broken_dependencies ();
162
163   handle_broken_dependencies ();
164   
165 #if 0  /* don't do this: strange side effects.  */
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.  ly_list_qsort_uniq_x ()
171      makes sure that no duplicates are in the list.  */
172   for (int i = 0; i < line_count; i++)
173     {
174       SCM all = broken_intos_[i]->get_object ("all-elements");
175       all = ly_list_qsort_uniq_x (all);
176     }
177 #endif
178
179   if (be_verbose_global)
180     message (_f ("Element count %d.", count + element_count ()));
181
182   int line_count = broken_intos_.size ();
183   SCM lines = scm_c_make_vector (line_count, SCM_EOL);
184
185   for (int i = 0; i < line_count; i++)
186     {
187       if (be_verbose_global)
188         progress_indication ("[");
189
190       System *system = dynamic_cast<System *> (broken_intos_[i]);
191       system->post_processing ();
192       scm_vector_set_x (lines, scm_int2num (i), system->get_paper_system ());
193
194       if (be_verbose_global)
195         progress_indication (to_string (i) + "]");
196     }
197   return lines;
198 }
199
200 void
201 System::break_into_pieces (Array<Column_x_positions> const &breaking)
202 {
203   for (int i = 0; i < breaking.size (); i++)
204     {
205       System *system = dynamic_cast<System *> (clone (i));
206       system->rank_ = i;
207
208       Link_array<Grob> c (breaking[i].cols_);
209       pscore_->typeset_system (system);
210
211       system->set_bound (LEFT, c[0]);
212       system->set_bound (RIGHT, c.top ());
213       for (int j = 0; j < c.size (); j++)
214         {
215           c[j]->translate_axis (breaking[i].config_[j], X_AXIS);
216           dynamic_cast<Paper_column *> (c[j])->system_ = system;
217         }
218       set_loose_columns (system, &breaking[i]);
219       broken_intos_.push (system);
220     }
221 }
222
223 void
224 System::add_column (Paper_column *p)
225 {
226   Grob *me = this;
227   Grob_array *ga = unsmob_grob_array (me->get_object ("columns"));
228   if (!ga)
229     {
230       SCM scm_ga = Grob_array::make_array ();
231       me->set_object ("columns", scm_ga);
232       ga = unsmob_grob_array (scm_ga);
233     }
234
235   p->rank_
236     = ga->size()
237     ? Paper_column::get_rank (ga->array ().top ()) + 1
238     : 0;
239     
240   ga->add (p);
241   Axis_group_interface::add_element (this, p);
242 }
243
244 void
245 apply_tweaks (Grob *g, bool broken)
246 {
247   if (bool (g->original_) == broken)
248     {
249       SCM tweaks = global_registry_->get_tweaks (g);
250       for (SCM s = tweaks; scm_is_pair (s); s = scm_cdr (s))
251         {
252           SCM proc = scm_caar (s);
253           SCM rest = scm_cdar (s);
254           scm_apply_1 (proc, g->self_scm (), rest);
255         }
256     }
257 }
258
259 void
260 System::pre_processing ()
261 {
262   for (int i = 0 ;  i < all_elements_->size(); i ++)
263     all_elements_->grob (i)->discretionary_processing ();
264   
265
266   if (be_verbose_global)
267     message (_f ("Grob count %d", element_count ()));
268
269   /*
270     order is significant: broken grobs are added to the end of the
271     array, and should be processed before the original is potentially
272     killed.
273   */
274   for (int i = all_elements_->size(); i --; )
275     all_elements_->grob (i)->handle_prebroken_dependencies ();
276
277   fixup_refpoints (all_elements_->array ());
278
279   for (int i = 0 ;  i < all_elements_->size(); i ++)
280     apply_tweaks (all_elements_->grob (i), false);
281
282   for (int i = 0 ;  i < all_elements_->size(); i ++)
283     all_elements_->grob (i)->calculate_dependencies (PRECALCED, PRECALCING,
284                                                      ly_symbol2scm ("before-line-breaking-callback"));
285
286   message (_ ("Calculating line breaks..."));
287   progress_indication (" ");
288   
289   for (int i = 0 ;  i < all_elements_->size(); i ++)
290     {
291       Grob *e =    all_elements_->grob (i);
292       SCM proc = e->get_property ("spacing-procedure");
293       if (ly_is_procedure (proc))
294         scm_call_1 (proc, e->self_scm ());
295     }
296 }
297
298 void
299 System::post_processing ()
300 {
301   for (int i = 0 ;  i < all_elements_->size(); i ++)
302     {
303       Grob *g = all_elements_->grob (i);
304
305       apply_tweaks (g, true);
306       g->calculate_dependencies (POSTCALCED, POSTCALCING,
307                                  ly_symbol2scm ("after-line-breaking-callback"));
308     }
309
310   Interval iv (extent (this, Y_AXIS));
311   if (iv.is_empty ())
312     programming_error ("system with zero extent");
313   else
314     translate_axis (-iv[MAX], Y_AXIS);
315
316   /* Generate all stencils to trigger font loads.
317      This might seem inefficient, but Stencils are cached per grob
318      anyway. */
319
320
321   Link_array<Grob> all_elts_sorted (all_elements_->array ());
322   all_elts_sorted.default_sort ();
323   all_elts_sorted.uniq ();
324   this->get_stencil ();
325   for (int i = all_elts_sorted.size (); i--;)
326     {
327       Grob *g = all_elts_sorted[i];
328       g->get_stencil ();
329     }
330 }
331
332 SCM
333 System::get_paper_system ()
334 {
335   static int const LAYER_COUNT = 3;
336
337   SCM exprs = SCM_EOL;
338   SCM *tail = &exprs;
339
340   /* Output stencils in three layers: 0, 1, 2.  Default layer: 1. */
341   for (int i = 0; i < LAYER_COUNT; i++)
342     for (int j = all_elements_->size (); j --;)
343       {
344         Grob *g = all_elements_->grob (j);
345         Stencil *stil = g->get_stencil ();
346
347         /* Skip empty stencils and grobs that are not in this layer.  */
348         if (!stil
349             || robust_scm2int (g->get_property ("layer"), 1) != i)
350           continue;
351
352         Offset o (g->relative_coordinate (this, X_AXIS),
353                   g->relative_coordinate (this, Y_AXIS));
354
355         Offset extra = robust_scm2offset (g->get_property ("extra-offset"),
356                                           Offset (0, 0))
357           * Staff_symbol_referencer::staff_space (g);
358
359         /* Must copy the stencil, for we cannot change the stencil
360            cached in G.  */
361
362         Stencil st = *stil;
363         st.translate (o + extra);
364
365         *tail = scm_cons (st.expr (), SCM_EOL);
366         tail = SCM_CDRLOC (*tail);
367       }
368
369   if (Stencil *me = get_stencil ())
370     exprs = scm_cons (me->expr (), exprs);
371
372   Interval x (extent (this, X_AXIS));
373   Interval y (extent (this, Y_AXIS));
374   Stencil sys_stencil (Box (x, y),
375                        scm_cons (ly_symbol2scm ("combine-stencil"),
376                                  exprs));
377
378   Interval staff_refpoints;
379   staff_refpoints.set_empty ();
380   extract_grob_set (this, "spaceable-staves", staves);
381   for (int i = staves.size (); i--; )
382     {
383       Grob *g = staves[i];
384       staff_refpoints.add_point (g->relative_coordinate (this, Y_AXIS));
385     }
386
387   Paper_system *pl = new Paper_system (sys_stencil, false);
388   pl->staff_refpoints_ = staff_refpoints;
389   Item *break_point = this->get_bound (LEFT);
390   pl->break_before_penalty_
391     = robust_scm2double (break_point->get_property ("page-penalty"), 0.0);
392
393   return scm_gc_unprotect_object (pl->self_scm ());
394 }
395
396 Link_array<Item>
397 System::broken_col_range (Item const *left, Item const *right) const
398 {
399   Link_array<Item> ret;
400
401   left = left->get_column ();
402   right = right->get_column ();
403
404   extract_grob_set (this, "columns", cols);
405   int i = 0;
406   while (i < cols.size()
407          && cols[i] != left)
408     i++;
409
410   if (i < cols.size())
411     i ++;
412   
413   while (i < cols.size()
414          && cols[i] != right)
415     {
416       Paper_column *c = dynamic_cast<Paper_column *> (cols[i]);
417       if (Item::is_breakable (c) && !c->system_)
418         ret.push (c);
419       i++;      
420     }
421
422   return ret;
423 }
424
425 /** Return all columns, but filter out any unused columns , since they might
426     disrupt the spacing problem. */
427 Link_array<Grob>
428 System::columns () const
429 {
430   extract_grob_set (this, "columns", ro_columns);
431   Link_array<Grob> columns (ro_columns);
432   
433   bool found = false;
434   for (int i = columns.size (); i--;)
435     {
436       bool brb = Item::is_breakable (columns[i]);
437       found = found || brb;
438
439       /*
440         the last column should be breakable. Weed out any columns that
441         seem empty. We need to retain breakable columns, in case
442         someone forced a breakpoint.
443       */
444       if (!found || !Paper_column::is_used (columns[i]))
445         columns.del (i);
446     }
447   return columns;
448 }
449
450 int
451 System::get_rank () const
452 {
453   return rank_;
454 }
455
456 ADD_INTERFACE (System, "system-interface",
457                "This is the toplevel object: each object in a score "
458                "ultimately has a System object as its X and Y parent. ",
459                "all-elements spaceable-staves columns")