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