]> git.donarmstrong.com Git - lilypond.git/blob - lily/system.cc
147d85c8636567805fa8198c3b6e45c93393ebdc
[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
12 #include "align-interface.hh"
13 #include "axis-group-interface.hh"
14 #include "warn.hh"
15 #include "main.hh"
16 #include "paper-column.hh"
17 #include "output-def.hh"
18 #include "paper-score.hh"
19 #include "warn.hh"
20 #include "all-font-metrics.hh"
21 #include "spacing-interface.hh"
22 #include "staff-symbol-referencer.hh"
23 #include "paper-system.hh"
24 #include "tweak-registration.hh"
25 #include "grob-array.hh"
26 #include "pointer-group-interface.hh"
27
28 System::System (System const &src, int count)
29   : Spanner (src, count)
30 {
31   all_elements_ = 0;
32   pscore_ = 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 Grob *
54 System::clone (int count) const
55 {
56   return new System (*this, count);
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 (int 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_->is_empty ())
92     {
93       Grob **ptr = &all_elements_->array_reference ().elem_ref (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 (Link_array<Grob> const &grobs)
110 {
111   for (int i = grobs.size (); i--;)
112     grobs[i]->fixup_refpoint ();
113 }
114
115 SCM
116 System::get_paper_systems ()
117 {
118   for (int 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_breakable (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 (int 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 (int 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 (int i = 0; i < all_elements_->size (); i++)
162     all_elements_->grob (i)->handle_broken_dependencies ();
163
164   handle_broken_dependencies ();
165
166 #if 0  /* FIXME: strange side effects.  */
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.  ly_list_qsort_uniq_x ()
172      makes sure that no duplicates are in the list.  */
173   for (int i = 0; i < line_count; i++)
174     {
175       SCM all = broken_intos_[i]->get_object ("all-elements");
176       all = ly_list_qsort_uniq_x (all);
177     }
178 #endif
179
180   if (be_verbose_global)
181     message (_f ("Element count %d.", count + element_count ()));
182
183   int line_count = broken_intos_.size ();
184   SCM lines = scm_c_make_vector (line_count, SCM_EOL);
185
186   for (int i = 0; i < line_count; i++)
187     {
188       if (be_verbose_global)
189         progress_indication ("[");
190
191       System *system = dynamic_cast<System *> (broken_intos_[i]);
192       system->post_processing ();
193       scm_vector_set_x (lines, scm_from_int (i),
194                         system->get_paper_system ());
195
196       if (be_verbose_global)
197         progress_indication (to_string (i) + "]");
198     }
199   return lines;
200 }
201
202 void
203 System::break_into_pieces (Array<Column_x_positions> const &breaking)
204 {
205   for (int i = 0; i < breaking.size (); i++)
206     {
207       System *system = dynamic_cast<System *> (clone (i));
208       system->rank_ = i;
209
210       Link_array<Grob> c (breaking[i].cols_);
211       pscore_->typeset_system (system);
212
213       system->set_bound (LEFT, c[0]);
214       system->set_bound (RIGHT, c.top ());
215       for (int j = 0; j < c.size (); j++)
216         {
217           c[j]->translate_axis (breaking[i].config_[j], X_AXIS);
218           dynamic_cast<Paper_column *> (c[j])->system_ = system;
219         }
220       set_loose_columns (system, &breaking[i]);
221       broken_intos_.push (system);
222     }
223 }
224
225 void
226 System::add_column (Paper_column *p)
227 {
228   Grob *me = this;
229   Grob_array *ga = unsmob_grob_array (me->get_object ("columns"));
230   if (!ga)
231     {
232       SCM scm_ga = Grob_array::make_array ();
233       me->set_object ("columns", scm_ga);
234       ga = unsmob_grob_array (scm_ga);
235     }
236
237   p->rank_
238     = ga->size ()
239     ? Paper_column::get_rank (ga->array ().top ()) + 1
240     : 0;
241
242   ga->add (p);
243   Axis_group_interface::add_element (this, p);
244 }
245
246 void
247 apply_tweaks (Grob *g, bool broken)
248 {
249   if (bool (g->original ()) == broken)
250     {
251       SCM tweaks = global_registry_->get_tweaks (g);
252       for (SCM s = tweaks; scm_is_pair (s); s = scm_cdr (s))
253         {
254           SCM proc = scm_caar (s);
255           SCM rest = scm_cdar (s);
256           scm_apply_1 (proc, g->self_scm (), rest);
257         }
258     }
259 }
260
261 void
262 System::pre_processing ()
263 {
264   for (int i = 0; i < all_elements_->size (); i++)
265     all_elements_->grob (i)->discretionary_processing ();
266
267   if (be_verbose_global)
268     message (_f ("Grob count %d", element_count ()));
269
270   /*
271     order is significant: broken grobs are added to the end of the
272     array, and should be processed before the original is potentially
273     killed.
274   */
275   for (int i = all_elements_->size (); i--;)
276     all_elements_->grob (i)->handle_prebroken_dependencies ();
277
278   fixup_refpoints (all_elements_->array ());
279
280   for (int i = 0; i < all_elements_->size (); i++)
281     apply_tweaks (all_elements_->grob (i), false);
282
283   for (int 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   message (_ ("Calculating line breaks..."));
290   progress_indication (" ");
291
292   for (int i = 0; i < all_elements_->size (); i++)
293     {
294       Grob *e = all_elements_->grob (i);
295       (void) e->get_property ("springs-and-rods");
296     }
297 }
298
299 void
300 System::post_processing ()
301 {
302   for (int i = 0; i < all_elements_->size (); i++)
303     {
304       Grob *g = all_elements_->grob (i);
305
306       apply_tweaks (g, true);
307       (void) g->get_property ("after-line-breaking");
308     }
309
310   Interval iv (extent (this, Y_AXIS));
311   if (iv.is_empty ())
312     programming_error ("system with empty 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   Link_array<Grob> all_elts_sorted (all_elements_->array ());
321   all_elts_sorted.default_sort ();
322   all_elts_sorted.uniq ();
323   this->get_stencil ();
324   for (int i = all_elts_sorted.size (); i--;)
325     {
326       Grob *g = all_elts_sorted[i];
327       g->get_stencil ();
328     }
329 }
330
331 SCM
332 System::get_paper_system ()
333 {
334   static int const LAYER_COUNT = 3;
335
336   SCM exprs = SCM_EOL;
337   SCM *tail = &exprs;
338
339   /* Output stencils in three layers: 0, 1, 2.  Default layer: 1. */
340   for (int i = 0; i < LAYER_COUNT; i++)
341     for (int j = all_elements_->size (); j--;)
342       {
343         Grob *g = all_elements_->grob (j);
344         Stencil st = g->get_print_stencil ();
345
346         /* Skip empty stencils and grobs that are not in this layer.  */
347         if (st.expr() == SCM_EOL
348             || robust_scm2int (g->get_property ("layer"), 1) != i)
349           continue;
350
351         Offset o;
352         for (int a = X_AXIS; a < NO_AXES; a++)
353           o[Axis (a)] = g->relative_coordinate (this, Axis (a));
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         st.translate (o + extra);
363
364         *tail = scm_cons (st.expr (), SCM_EOL);
365         tail = SCM_CDRLOC (*tail);
366       }
367
368   if (Stencil *me = get_stencil ())
369     exprs = scm_cons (me->expr (), exprs);
370
371   Interval x (extent (this, X_AXIS));
372   Interval y (extent (this, Y_AXIS));
373   Stencil sys_stencil (Box (x, y),
374                        scm_cons (ly_symbol2scm ("combine-stencil"),
375                                  exprs));
376
377   Grob *left_bound = this->get_bound (LEFT);
378   SCM prop_init = left_bound->get_property ("line-break-system-details");
379   Prob *pl = make_paper_system (prop_init);
380   paper_system_set_stencil (pl, sys_stencil);
381   pl->set_property ("penalty",
382                     left_bound->get_property ("page-penalty"));
383   
384   if (!scm_is_pair (pl->get_property ("refpoint-Y-extent")))
385     {
386       Interval staff_refpoints;
387       staff_refpoints.set_empty ();
388       extract_grob_set (this, "spaceable-staves", staves);
389       for (int i = staves.size (); i--;)
390         {
391           Grob *g = staves[i];
392           staff_refpoints.add_point (g->relative_coordinate (this, Y_AXIS));
393         }
394       pl->set_property ("refpoint-Y-extent", ly_interval2scm (staff_refpoints));
395     }
396
397   pl->set_property ("system-grob", this->self_scm ()); 
398
399   return pl->unprotect ();
400 }
401
402 Link_array<Item>
403 System::broken_col_range (Item const *left, Item const *right) const
404 {
405   Link_array<Item> ret;
406
407   left = left->get_column ();
408   right = right->get_column ();
409
410   extract_grob_set (this, "columns", cols);
411   int i = 0;
412   while (i < cols.size ()
413          && cols[i] != left)
414     i++;
415
416   if (i < cols.size ())
417     i++;
418
419   while (i < cols.size ()
420          && cols[i] != right)
421     {
422       Paper_column *c = dynamic_cast<Paper_column *> (cols[i]);
423       if (Item::is_breakable (c) && !c->system_)
424         ret.push (c);
425       i++;
426     }
427
428   return ret;
429 }
430
431 /** Return all columns, but filter out any unused columns , since they might
432     disrupt the spacing problem. */
433 Link_array<Grob>
434 System::columns () const
435 {
436   extract_grob_set (this, "columns", ro_columns);
437
438   int last_breakable = ro_columns.size ();
439
440   while (last_breakable--)
441     {
442       if (Item::is_breakable (ro_columns [last_breakable]))
443         break;
444     }
445
446   Link_array<Grob> columns;
447   for (int i = 0; i <= last_breakable; i++)
448     {
449       if (Paper_column::is_used (ro_columns[i]))
450         columns.push (ro_columns[i]);
451     }
452
453   return columns;
454 }
455
456 Paper_score*
457 System::paper_score () const
458 {
459   return pscore_;
460 }
461
462 int
463 System::get_rank () const
464 {
465   return rank_;
466 }
467
468 System *
469 get_root_system (Grob *me) 
470 {
471   Grob *system_grob = me;
472   
473   while (system_grob->get_parent (Y_AXIS))
474     system_grob = system_grob->get_parent (Y_AXIS);
475
476   return dynamic_cast<System*> (system_grob); 
477 }
478
479
480
481 ADD_INTERFACE (System, "system-interface",
482                "This is the toplevel object: each object in a score "
483                "ultimately has a System object as its X and Y parent. ",
484
485                /* properties */
486                "all-elements "
487                "spaceable-staves "
488                "columns"
489                )