]> git.donarmstrong.com Git - lilypond.git/blob - lily/system.cc
(parse_symbol_list): Bugfix.
[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     {
114       grobs[i]->fixup_refpoint ();
115     }
116 }
117
118 SCM
119 System::get_paper_systems ()
120 {
121   for (int i = 0; i < all_elements_->size (); i++)
122     {
123       Grob *g = all_elements_->grob (i);
124       if (g->internal_has_interface (ly_symbol2scm ("only-prebreak-interface")))
125         {
126           /*
127             Kill no longer needed grobs.
128           */
129           Item *it = dynamic_cast<Item *> (g);
130           if (it && Item::is_breakable (it))
131             {
132               it->find_prebroken_piece (LEFT)->suicide ();
133               it->find_prebroken_piece (RIGHT)->suicide ();
134             }
135           g->suicide ();
136         }
137       else if (g->is_live ())
138         g->do_break_processing ();
139     }
140
141   /*
142     fixups must be done in broken line_of_scores, because new elements
143     are put over there.  */
144   int count = 0;
145   for (int i = 0; i < broken_intos_.size (); i++)
146     {
147       Grob *se = broken_intos_[i];
148
149       extract_grob_set (se, "all-elements", all_elts);
150       for (int j = 0; j < all_elts.size (); j++)
151         {
152           Grob *g = all_elts[j];
153           g->fixup_refpoint ();
154         }
155
156       count += all_elts.size ();
157     }
158
159   /*
160     needed for doing items.
161   */
162   fixup_refpoints (all_elements_->array ());
163
164   for (int i = 0; i < all_elements_->size (); i++)
165     all_elements_->grob (i)->handle_broken_dependencies ();
166
167   handle_broken_dependencies ();
168
169 #if 0  /* FIXME: strange side effects.  */
170
171   /* Because the this->get_property (all-elements) contains items in 3
172      versions, handle_broken_dependencies () will leave duplicated
173      items in all-elements.  Strictly speaking this is harmless, but
174      it leads to duplicated symbols in the output.  ly_list_qsort_uniq_x ()
175      makes sure that no duplicates are in the list.  */
176   for (int i = 0; i < line_count; i++)
177     {
178       SCM all = broken_intos_[i]->get_object ("all-elements");
179       all = ly_list_qsort_uniq_x (all);
180     }
181 #endif
182
183   if (be_verbose_global)
184     message (_f ("Element count %d.", count + element_count ()));
185
186   int line_count = broken_intos_.size ();
187   SCM lines = scm_c_make_vector (line_count, SCM_EOL);
188
189   for (int i = 0; i < line_count; i++)
190     {
191       if (be_verbose_global)
192         progress_indication ("[");
193
194       System *system = dynamic_cast<System *> (broken_intos_[i]);
195       system->post_processing ();
196       scm_vector_set_x (lines, scm_from_int (i), system->get_paper_system ());
197
198       if (be_verbose_global)
199         progress_indication (to_string (i) + "]");
200     }
201   return lines;
202 }
203
204 void
205 System::break_into_pieces (Array<Column_x_positions> const &breaking)
206 {
207   for (int i = 0; i < breaking.size (); i++)
208     {
209       System *system = dynamic_cast<System *> (clone (i));
210       system->rank_ = i;
211
212       Link_array<Grob> c (breaking[i].cols_);
213       pscore_->typeset_system (system);
214
215       system->set_bound (LEFT, c[0]);
216       system->set_bound (RIGHT, c.top ());
217       for (int j = 0; j < c.size (); j++)
218         {
219           c[j]->translate_axis (breaking[i].config_[j], X_AXIS);
220           dynamic_cast<Paper_column *> (c[j])->system_ = system;
221         }
222       set_loose_columns (system, &breaking[i]);
223       broken_intos_.push (system);
224     }
225 }
226
227 void
228 System::add_column (Paper_column *p)
229 {
230   Grob *me = this;
231   Grob_array *ga = unsmob_grob_array (me->get_object ("columns"));
232   if (!ga)
233     {
234       SCM scm_ga = Grob_array::make_array ();
235       me->set_object ("columns", scm_ga);
236       ga = unsmob_grob_array (scm_ga);
237     }
238
239   p->rank_
240     = ga->size ()
241     ? Paper_column::get_rank (ga->array ().top ()) + 1
242     : 0;
243
244   ga->add (p);
245   Axis_group_interface::add_element (this, p);
246 }
247
248 void
249 apply_tweaks (Grob *g, bool broken)
250 {
251   if (bool (g->original_) == broken)
252     {
253       SCM tweaks = global_registry_->get_tweaks (g);
254       for (SCM s = tweaks; scm_is_pair (s); s = scm_cdr (s))
255         {
256           SCM proc = scm_caar (s);
257           SCM rest = scm_cdar (s);
258           scm_apply_1 (proc, g->self_scm (), rest);
259         }
260     }
261 }
262
263 void
264 System::pre_processing ()
265 {
266   for (int i = 0; i < all_elements_->size (); i++)
267     all_elements_->grob (i)->discretionary_processing ();
268
269   if (be_verbose_global)
270     message (_f ("Grob count %d", element_count ()));
271
272   /*
273     order is significant: broken grobs are added to the end of the
274     array, and should be processed before the original is potentially
275     killed.
276   */
277   for (int i = all_elements_->size (); i--;)
278     all_elements_->grob (i)->handle_prebroken_dependencies ();
279
280   fixup_refpoints (all_elements_->array ());
281
282   for (int i = 0; i < all_elements_->size (); i++)
283     apply_tweaks (all_elements_->grob (i), false);
284
285   for (int i = 0; i < all_elements_->size (); i++)
286     all_elements_->grob (i)->calculate_dependencies (PRECALCED, PRECALCING,
287                                                      ly_symbol2scm ("before-line-breaking-callback"));
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       SCM proc = e->get_property ("spacing-procedure");
296       if (ly_is_procedure (proc))
297         scm_call_1 (proc, e->self_scm ());
298     }
299 }
300
301 void
302 System::post_processing ()
303 {
304   for (int i = 0; i < all_elements_->size (); i++)
305     {
306       Grob *g = all_elements_->grob (i);
307
308       apply_tweaks (g, true);
309       g->calculate_dependencies (POSTCALCED, POSTCALCING,
310                                  ly_symbol2scm ("after-line-breaking-callback"));
311     }
312
313   Interval iv (extent (this, Y_AXIS));
314   if (iv.is_empty ())
315     {
316       programming_error ("system with empty extent");
317     }
318   else
319     translate_axis (-iv[MAX], Y_AXIS);
320
321   /* Generate all stencils to trigger font loads.
322      This might seem inefficient, but Stencils are cached per grob
323      anyway. */
324
325   Link_array<Grob> all_elts_sorted (all_elements_->array ());
326   all_elts_sorted.default_sort ();
327   all_elts_sorted.uniq ();
328   this->get_stencil ();
329   for (int i = all_elts_sorted.size (); i--;)
330     {
331       Grob *g = all_elts_sorted[i];
332       g->get_stencil ();
333     }
334 }
335
336 SCM
337 System::get_paper_system ()
338 {
339   static int const LAYER_COUNT = 3;
340
341   SCM exprs = SCM_EOL;
342   SCM *tail = &exprs;
343
344   /* Output stencils in three layers: 0, 1, 2.  Default layer: 1. */
345   for (int i = 0; i < LAYER_COUNT; i++)
346     for (int j = all_elements_->size (); j--;)
347       {
348         Grob *g = all_elements_->grob (j);
349         Stencil *stil = g->get_stencil ();
350
351         /* Skip empty stencils and grobs that are not in this layer.  */
352         if (!stil
353             || robust_scm2int (g->get_property ("layer"), 1) != i)
354           continue;
355
356         Offset o (g->relative_coordinate (this, X_AXIS),
357                   g->relative_coordinate (this, Y_AXIS));
358
359         Offset extra = robust_scm2offset (g->get_property ("extra-offset"),
360                                           Offset (0, 0))
361           * Staff_symbol_referencer::staff_space (g);
362
363         /* Must copy the stencil, for we cannot change the stencil
364            cached in G.  */
365
366         Stencil st = *stil;
367         st.translate (o + extra);
368
369         *tail = scm_cons (st.expr (), SCM_EOL);
370         tail = SCM_CDRLOC (*tail);
371       }
372
373   if (Stencil *me = get_stencil ())
374     exprs = scm_cons (me->expr (), exprs);
375
376   Interval x (extent (this, X_AXIS));
377   Interval y (extent (this, Y_AXIS));
378   Stencil sys_stencil (Box (x, y),
379                        scm_cons (ly_symbol2scm ("combine-stencil"),
380                                  exprs));
381
382   Interval staff_refpoints;
383   staff_refpoints.set_empty ();
384   extract_grob_set (this, "spaceable-staves", staves);
385   for (int i = staves.size (); i--;)
386     {
387       Grob *g = staves[i];
388       staff_refpoints.add_point (g->relative_coordinate (this, Y_AXIS));
389     }
390
391   Paper_system *pl = new Paper_system (sys_stencil, false);
392   pl->staff_refpoints_ = staff_refpoints;
393   Item *break_point = this->get_bound (LEFT);
394   pl->break_before_penalty_
395     = robust_scm2double (break_point->get_property ("page-penalty"), 0.0);
396
397   return pl->unprotect ();
398 }
399
400 Link_array<Item>
401 System::broken_col_range (Item const *left, Item const *right) const
402 {
403   Link_array<Item> ret;
404
405   left = left->get_column ();
406   right = right->get_column ();
407
408   extract_grob_set (this, "columns", cols);
409   int i = 0;
410   while (i < cols.size ()
411          && cols[i] != left)
412     i++;
413
414   if (i < cols.size ())
415     i++;
416
417   while (i < cols.size ()
418          && cols[i] != right)
419     {
420       Paper_column *c = dynamic_cast<Paper_column *> (cols[i]);
421       if (Item::is_breakable (c) && !c->system_)
422         ret.push (c);
423       i++;
424     }
425
426   return ret;
427 }
428
429 /** Return all columns, but filter out any unused columns , since they might
430     disrupt the spacing problem. */
431 Link_array<Grob>
432 System::columns () const
433 {
434   extract_grob_set (this, "columns", ro_columns);
435
436   int last_breakable = ro_columns.size ();
437
438   while (last_breakable--)
439     {
440       if (Item::is_breakable (ro_columns [last_breakable]))
441         break;
442     }
443
444   Link_array<Grob> columns;
445   for (int i = 0; i <= last_breakable; i++)
446     {
447       if (Paper_column::is_used (ro_columns[i]))
448         columns.push (ro_columns[i]);
449     }
450
451   return columns;
452 }
453
454 int
455 System::get_rank () const
456 {
457   return rank_;
458 }
459
460 ADD_INTERFACE (System, "system-interface",
461                "This is the toplevel object: each object in a score "
462                "ultimately has a System object as its X and Y parent. ",
463                "all-elements spaceable-staves columns")