]> git.donarmstrong.com Git - lilypond.git/blob - lily/system.cc
* scm/output-lib.scm (begin-of-line-invisible): use vectors for
[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
55 Grob *
56 System::clone (int count) const
57 {
58   return new System (*this, count);
59 }
60
61 int
62 System::element_count () const
63 {
64   return all_elements_->size ();
65 }
66
67 int
68 System::spanner_count () const
69 {
70   int k = 0;
71   for (int i = all_elements_->size(); i--;)
72     if (dynamic_cast<Spanner *> (all_elements_->grob (i)))
73       k++;
74   return k;
75 }
76
77 void
78 System::typeset_grob (Grob *elem)
79 {
80   if (elem->pscore_)
81     programming_error ("adding element twice");
82   else
83     {
84       elem->pscore_ = pscore_;
85       all_elements_->add (elem);
86       scm_gc_unprotect_object (elem->self_scm ());
87     }
88 }
89
90 SCM
91 System::do_derived_mark () const
92 {
93   if (!all_elements_->is_empty ())
94     {
95       Grob **ptr = &all_elements_->array_reference ().elem_ref (0);
96       Grob **end = ptr + all_elements_->size ();
97       while (ptr < end)
98         {
99           scm_gc_mark ((*ptr)->self_scm ());
100           ptr ++;
101         }
102     }
103   return Spanner::do_derived_mark ();
104 }
105
106 static void
107 fixup_refpoints (Link_array<Grob> const &grobs)
108 {
109   for (int i = grobs.size (); i--; )
110     {
111       grobs[i]->fixup_refpoint ();
112     }
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  /* don't do this: 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_int2num (i), system->get_paper_system ());
194
195       if (be_verbose_global)
196         progress_indication (to_string (i) + "]");
197     }
198   return lines;
199 }
200
201 void
202 System::break_into_pieces (Array<Column_x_positions> const &breaking)
203 {
204   for (int i = 0; i < breaking.size (); i++)
205     {
206       System *system = dynamic_cast<System *> (clone (i));
207       system->rank_ = i;
208
209       Link_array<Grob> c (breaking[i].cols_);
210       pscore_->typeset_system (system);
211
212       system->set_bound (LEFT, c[0]);
213       system->set_bound (RIGHT, c.top ());
214       for (int j = 0; j < c.size (); j++)
215         {
216           c[j]->translate_axis (breaking[i].config_[j], X_AXIS);
217           dynamic_cast<Paper_column *> (c[j])->system_ = system;
218         }
219       set_loose_columns (system, &breaking[i]);
220       broken_intos_.push (system);
221     }
222 }
223
224 void
225 System::add_column (Paper_column *p)
226 {
227   Grob *me = this;
228   Grob_array *ga = unsmob_grob_array (me->get_object ("columns"));
229   if (!ga)
230     {
231       SCM scm_ga = Grob_array::make_array ();
232       me->set_object ("columns", scm_ga);
233       ga = unsmob_grob_array (scm_ga);
234     }
235
236   p->rank_
237     = ga->size()
238     ? Paper_column::get_rank (ga->array ().top ()) + 1
239     : 0;
240     
241   ga->add (p);
242   Axis_group_interface::add_element (this, p);
243 }
244
245 void
246 apply_tweaks (Grob *g, bool broken)
247 {
248   if (bool (g->original_) == broken)
249     {
250       SCM tweaks = global_registry_->get_tweaks (g);
251       for (SCM s = tweaks; scm_is_pair (s); s = scm_cdr (s))
252         {
253           SCM proc = scm_caar (s);
254           SCM rest = scm_cdar (s);
255           scm_apply_1 (proc, g->self_scm (), rest);
256         }
257     }
258 }
259
260 void
261 System::pre_processing ()
262 {
263   for (int i = 0 ;  i < all_elements_->size(); i ++)
264     all_elements_->grob (i)->discretionary_processing ();
265   
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 zero 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
322   Link_array<Grob> all_elts_sorted (all_elements_->array ());
323   all_elts_sorted.default_sort ();
324   all_elts_sorted.uniq ();
325   this->get_stencil ();
326   for (int i = all_elts_sorted.size (); i--;)
327     {
328       Grob *g = all_elts_sorted[i];
329       g->get_stencil ();
330     }
331 }
332
333 SCM
334 System::get_paper_system ()
335 {
336   static int const LAYER_COUNT = 3;
337
338   SCM exprs = SCM_EOL;
339   SCM *tail = &exprs;
340
341   /* Output stencils in three layers: 0, 1, 2.  Default layer: 1. */
342   for (int i = 0; i < LAYER_COUNT; i++)
343     for (int j = all_elements_->size (); j --;)
344       {
345         Grob *g = all_elements_->grob (j);
346         Stencil *stil = g->get_stencil ();
347
348         /* Skip empty stencils and grobs that are not in this layer.  */
349         if (!stil
350             || robust_scm2int (g->get_property ("layer"), 1) != i)
351           continue;
352
353         Offset o (g->relative_coordinate (this, X_AXIS),
354                   g->relative_coordinate (this, Y_AXIS));
355
356         Offset extra = robust_scm2offset (g->get_property ("extra-offset"),
357                                           Offset (0, 0))
358           * Staff_symbol_referencer::staff_space (g);
359
360         /* Must copy the stencil, for we cannot change the stencil
361            cached in G.  */
362
363         Stencil st = *stil;
364         st.translate (o + extra);
365
366         *tail = scm_cons (st.expr (), SCM_EOL);
367         tail = SCM_CDRLOC (*tail);
368       }
369
370   if (Stencil *me = get_stencil ())
371     exprs = scm_cons (me->expr (), exprs);
372
373   Interval x (extent (this, X_AXIS));
374   Interval y (extent (this, Y_AXIS));
375   Stencil sys_stencil (Box (x, y),
376                        scm_cons (ly_symbol2scm ("combine-stencil"),
377                                  exprs));
378
379   Interval staff_refpoints;
380   staff_refpoints.set_empty ();
381   extract_grob_set (this, "spaceable-staves", staves);
382   for (int i = staves.size (); i--; )
383     {
384       Grob *g = staves[i];
385       staff_refpoints.add_point (g->relative_coordinate (this, Y_AXIS));
386     }
387
388   Paper_system *pl = new Paper_system (sys_stencil, false);
389   pl->staff_refpoints_ = staff_refpoints;
390   Item *break_point = this->get_bound (LEFT);
391   pl->break_before_penalty_
392     = robust_scm2double (break_point->get_property ("page-penalty"), 0.0);
393
394   return scm_gc_unprotect_object (pl->self_scm ());
395 }
396
397 Link_array<Item>
398 System::broken_col_range (Item const *left, Item const *right) const
399 {
400   Link_array<Item> ret;
401
402   left = left->get_column ();
403   right = right->get_column ();
404
405   extract_grob_set (this, "columns", cols);
406   int i = 0;
407   while (i < cols.size()
408          && cols[i] != left)
409     i++;
410
411   if (i < cols.size())
412     i ++;
413   
414   while (i < cols.size()
415          && cols[i] != right)
416     {
417       Paper_column *c = dynamic_cast<Paper_column *> (cols[i]);
418       if (Item::is_breakable (c) && !c->system_)
419         ret.push (c);
420       i++;      
421     }
422
423   return ret;
424 }
425
426 /** Return all columns, but filter out any unused columns , since they might
427     disrupt the spacing problem. */
428 Link_array<Grob>
429 System::columns () const
430 {
431   extract_grob_set (this, "columns", ro_columns);
432
433   int last_breakable = ro_columns.size ();
434
435   while  (last_breakable --)
436     {
437       if (Item::is_breakable (ro_columns [last_breakable]))
438         break;
439     }
440
441   Link_array<Grob> columns;
442   for (int i = 0; i <= last_breakable; i++)
443     {
444       if (Paper_column::is_used (ro_columns[i]))
445         columns.push (ro_columns[i]);
446     }
447
448   return columns;
449 }
450
451 int
452 System::get_rank () const
453 {
454   return rank_;
455 }
456
457 ADD_INTERFACE (System, "system-interface",
458                "This is the toplevel object: each object in a score "
459                "ultimately has a System object as its X and Y parent. ",
460                "all-elements spaceable-staves columns")