]> git.donarmstrong.com Git - lilypond.git/blob - lily/system.cc
Nitpick run.
[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 #include "pointer-group-interface.hh"
29
30 System::System (System const &src, int count)
31   : Spanner (src, count)
32 {
33   all_elements_ = 0;
34   rank_ = 0;
35   init_elements ();
36 }
37
38 System::System (SCM s, Object_key const *key)
39   : Spanner (s, key)
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   set_object ("all-elements", scm_arr);
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       elem->unprotect ();
86     }
87 }
88
89 void
90 System::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
103   if (pscore_)
104     scm_gc_mark (pscore_->self_scm ());
105
106   Spanner::derived_mark ();
107 }
108
109 static void
110 fixup_refpoints (Link_array<Grob> const &grobs)
111 {
112   for (int i = grobs.size (); i--;)
113     grobs[i]->fixup_refpoint ();
114 }
115
116 SCM
117 System::get_paper_systems ()
118 {
119   for (int 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_breakable (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 (int 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 (int 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 (int i = 0; i < all_elements_->size (); i++)
163     all_elements_->grob (i)->handle_broken_dependencies ();
164
165   handle_broken_dependencies ();
166
167 #if 0  /* FIXME: strange side effects.  */
168
169   /* Because the this->get_property (all-elements) contains items in 3
170      versions, handle_broken_dependencies () will leave duplicated
171      items in all-elements.  Strictly speaking this is harmless, but
172      it leads to duplicated symbols in the output.  ly_list_qsort_uniq_x ()
173      makes sure that no duplicates are in the list.  */
174   for (int i = 0; i < line_count; i++)
175     {
176       SCM all = broken_intos_[i]->get_object ("all-elements");
177       all = ly_list_qsort_uniq_x (all);
178     }
179 #endif
180
181   if (be_verbose_global)
182     message (_f ("Element count %d.", count + element_count ()));
183
184   int line_count = broken_intos_.size ();
185   SCM lines = scm_c_make_vector (line_count, SCM_EOL);
186
187   for (int i = 0; i < line_count; i++)
188     {
189       if (be_verbose_global)
190         progress_indication ("[");
191
192       System *system = dynamic_cast<System *> (broken_intos_[i]);
193       system->post_processing ();
194       scm_vector_set_x (lines, scm_from_int (i), 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     all_elements_->grob (i)->calculate_dependencies (PRECALCED, PRECALCING,
285                                                      ly_symbol2scm ("before-line-breaking-callback"));
286
287   message (_ ("Calculating line breaks..."));
288   progress_indication (" ");
289
290   for (int i = 0; i < all_elements_->size (); i++)
291     {
292       Grob *e = all_elements_->grob (i);
293       SCM proc = e->get_property ("spacing-procedure");
294       if (ly_is_procedure (proc))
295         scm_call_1 (proc, e->self_scm ());
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       g->calculate_dependencies (POSTCALCED, POSTCALCING,
308                                  ly_symbol2scm ("after-line-breaking-callback"));
309     }
310
311   Interval iv (extent (this, Y_AXIS));
312   if (iv.is_empty ())
313     programming_error ("system with empty extent");
314   else
315     translate_axis (-iv[MAX], Y_AXIS);
316
317   /* Generate all stencils to trigger font loads.
318      This might seem inefficient, but Stencils are cached per grob
319      anyway. */
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 pl->unprotect ();
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
432   int last_breakable = ro_columns.size ();
433
434   while (last_breakable--)
435     {
436       if (Item::is_breakable (ro_columns [last_breakable]))
437         break;
438     }
439
440   Link_array<Grob> columns;
441   for (int i = 0; i <= last_breakable; i++)
442     {
443       if (Paper_column::is_used (ro_columns[i]))
444         columns.push (ro_columns[i]);
445     }
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")