]> git.donarmstrong.com Git - lilypond.git/blob - lily/system.cc
b804e43a3114704f5cc7203087f5cca5413cba0a
[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--2007 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
8
9 #include "system.hh"
10
11 #include "align-interface.hh"
12 #include "all-font-metrics.hh"
13 #include "axis-group-interface.hh"
14 #include "grob-array.hh"
15 #include "international.hh"
16 #include "main.hh"
17 #include "output-def.hh"
18 #include "paper-column.hh"
19 #include "paper-score.hh"
20 #include "paper-system.hh"
21 #include "pointer-group-interface.hh"
22 #include "spacing-interface.hh"
23 #include "staff-symbol-referencer.hh"
24 #include "tweak-registration.hh"
25 #include "warn.hh"
26 #include "lookup.hh"
27
28 extern bool debug_skylines;
29
30 System::System (System const &src, int count)
31   : Spanner (src, count)
32 {
33   all_elements_ = 0;
34   pscore_ = 0;
35   rank_ = 0;
36   init_elements ();
37 }
38
39 System::System (SCM s, Object_key const *key)
40   : Spanner (s, key)
41 {
42   all_elements_ = 0;
43   rank_ = 0;
44   init_elements ();
45 }
46
47 void
48 System::init_elements ()
49 {
50   SCM scm_arr = Grob_array::make_array ();
51   all_elements_ = unsmob_grob_array (scm_arr);
52   all_elements_->set_ordered (false);
53   set_object ("all-elements", scm_arr);
54 }
55
56 Grob *
57 System::clone (int index) const
58 {
59   return new System (*this, index);
60 }
61
62 int
63 System::element_count () const
64 {
65   return all_elements_->size ();
66 }
67
68 int
69 System::spanner_count () const
70 {
71   int k = 0;
72   for (vsize i = all_elements_->size (); i--;)
73     if (dynamic_cast<Spanner *> (all_elements_->grob (i)))
74       k++;
75   return k;
76 }
77
78 void
79 System::typeset_grob (Grob *elem)
80 {
81   if (elem->layout_)
82     programming_error ("adding element twice");
83   else
84     {
85       elem->layout_ = pscore_->layout ();
86       all_elements_->add (elem);
87       elem->unprotect ();
88     }
89 }
90
91 void
92 System::derived_mark () const
93 {
94   if (!all_elements_->empty ())
95     {
96       Grob **ptr = &all_elements_->array_reference ()[0];
97       Grob **end = ptr + all_elements_->size ();
98       while (ptr < end)
99         {
100           scm_gc_mark ((*ptr)->self_scm ());
101           ptr++;
102         }
103     }
104
105   if (pscore_)
106     scm_gc_mark (pscore_->self_scm ());
107
108   Spanner::derived_mark ();
109 }
110
111 static void
112 fixup_refpoints (vector<Grob*> const &grobs)
113 {
114   for (vsize i = grobs.size (); i--;)
115     grobs[i]->fixup_refpoint ();
116 }
117
118 SCM
119 System::get_paper_systems ()
120 {
121   for (vsize 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_non_musical (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 (vsize 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 (vsize 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 (vsize i = 0; i < all_elements_->size (); i++)
165     all_elements_->grob (i)->handle_broken_dependencies ();
166
167   handle_broken_dependencies ();
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.  uniq makes sure
173      that no duplicates are in the list.  */
174   for (vsize i = 0; i < broken_intos_.size (); i++)
175     {
176       System *child = dynamic_cast<System*> (broken_intos_[i]);
177       child->all_elements_->remove_duplicates ();
178     }
179
180   if (be_verbose_global)
181     message (_f ("Element count %d.", count + element_count ()));
182
183   SCM lines = scm_c_make_vector (broken_intos_.size (), SCM_EOL);
184   for (vsize i = 0; i < broken_intos_.size (); i++)
185     {
186       if (be_verbose_global)
187         progress_indication ("[");
188
189       System *system = dynamic_cast<System *> (broken_intos_[i]);
190
191       system->post_processing ();
192       system->build_skylines ();
193       if (i > 0)
194         {
195           System *prev = dynamic_cast<System*> (broken_intos_[i-1]);
196           Real r = prev->skylines_[DOWN].distance (system->skylines_[UP]);
197           system->set_property ("skyline-distance", scm_from_double (r));
198         }
199       scm_vector_set_x (lines, scm_from_int (i),
200                         system->get_paper_system ());
201
202       if (be_verbose_global)
203         progress_indication (to_string (i) + "]");
204     }
205   return lines;
206 }
207
208 void
209 System::break_into_pieces (vector<Column_x_positions> const &breaking)
210 {
211   for (vsize i = 0; i < breaking.size (); i++)
212     {
213       System *system = dynamic_cast<System *> (clone (broken_intos_.size ()));
214       system->rank_ = broken_intos_.size ();
215
216       vector<Grob*> c (breaking[i].cols_);
217       pscore_->typeset_system (system);
218
219       int st = Paper_column::get_rank (c[0]);
220       int end = Paper_column::get_rank (c.back ());
221       Interval iv (pure_height (this, st, end));
222       system->set_property ("pure-Y-extent", ly_interval2scm (iv));
223
224       system->set_bound (LEFT, c[0]);
225       system->set_bound (RIGHT, c.back ());
226       for (vsize j = 0; j < c.size (); j++)
227         {
228           c[j]->translate_axis (breaking[i].config_[j], X_AXIS);
229           dynamic_cast<Paper_column *> (c[j])->system_ = system;
230         }
231       
232       set_loose_columns (system, &breaking[i]);
233       broken_intos_.push_back (system);
234     }
235 }
236
237 void
238 System::add_column (Paper_column *p)
239 {
240   Grob *me = this;
241   Grob_array *ga = unsmob_grob_array (me->get_object ("columns"));
242   if (!ga)
243     {
244       SCM scm_ga = Grob_array::make_array ();
245       me->set_object ("columns", scm_ga);
246       ga = unsmob_grob_array (scm_ga);
247     }
248
249   p->rank_ = ga->size ();
250
251   ga->add (p);
252   Axis_group_interface::add_element (this, p);
253 }
254
255 void
256 System::pre_processing ()
257 {
258   for (vsize i = 0; i < all_elements_->size (); i++)
259     all_elements_->grob (i)->discretionary_processing ();
260
261   if (be_verbose_global)
262     message (_f ("Grob count %d", element_count ()));
263
264   /*
265     order is significant: broken grobs are added to the end of the
266     array, and should be processed before the original is potentially
267     killed.
268   */
269   for (vsize i = all_elements_->size (); i--;)
270     all_elements_->grob (i)->handle_prebroken_dependencies ();
271
272   fixup_refpoints (all_elements_->array ());
273
274   for (vsize i = 0; i < all_elements_->size (); i++)
275     {
276       Grob *g = all_elements_->grob (i);
277       (void) g->get_property ("before-line-breaking");
278     }
279
280   for (vsize i = 0; i < all_elements_->size (); i++)
281     {
282       Grob *e = all_elements_->grob (i);
283       (void) e->get_property ("springs-and-rods");
284     }
285 }
286
287 void
288 System::post_processing ()
289 {
290   for (vsize i = 0; i < all_elements_->size (); i++)
291     {
292       Grob *g = all_elements_->grob (i);
293
294       (void) g->get_property ("after-line-breaking");
295     }
296
297   Interval iv (extent (this, Y_AXIS));
298   if (iv.is_empty ())
299     programming_error ("system with empty extent");
300   else
301     translate_axis (-iv[MAX], Y_AXIS);
302
303   /* Generate all stencils to trigger font loads.
304      This might seem inefficient, but Stencils are cached per grob
305      anyway. */
306
307   vector<Grob*> all_elts_sorted (all_elements_->array ());
308   vector_sort (all_elts_sorted, std::less<Grob*> ());
309   uniq (all_elts_sorted);
310   this->get_stencil ();
311   for (vsize i = all_elts_sorted.size (); i--;)
312     {
313       Grob *g = all_elts_sorted[i];
314       g->get_stencil ();
315     }
316 }
317
318 struct Layer_entry
319 {
320   Grob *grob_;
321   int layer_;
322 };
323
324 bool
325 operator< (Layer_entry  const &a,
326            Layer_entry  const &b)
327 {
328   return a.layer_ < b.layer_;
329 }
330
331
332 SCM
333 System::get_paper_system ()
334 {
335   SCM exprs = SCM_EOL;
336   SCM *tail = &exprs;
337
338   vector<Layer_entry> entries;
339   for (vsize j = 0; j < all_elements_->size (); j++)
340     {
341       Layer_entry e;
342       e.grob_ = all_elements_->grob (j);
343       e.layer_ = robust_scm2int (e.grob_->get_property ("layer"), 1);
344       
345       entries.push_back (e); 
346     }
347
348   vector_sort (entries, std::less<Layer_entry> ());
349   for (vsize j = 0; j < entries.size (); j++)
350     {
351       Grob *g = entries[j].grob_;
352       Stencil st = g->get_print_stencil ();
353
354       if (st.expr() == SCM_EOL)
355         continue;
356       
357       Offset o;
358       for (int a = X_AXIS; a < NO_AXES; a++)
359         o[Axis (a)] = g->relative_coordinate (this, Axis (a));
360
361       Offset extra = robust_scm2offset (g->get_property ("extra-offset"),
362                                         Offset (0, 0))
363         * Staff_symbol_referencer::staff_space (g);
364
365       /* Must copy the stencil, for we cannot change the stencil
366          cached in G.  */
367
368       st.translate (o + extra);
369
370       *tail = scm_cons (st.expr (), SCM_EOL);
371       tail = SCM_CDRLOC (*tail);
372     }
373
374   if (Stencil *me = get_stencil ())
375     exprs = scm_cons (me->expr (), exprs);
376
377   Interval x (extent (this, X_AXIS));
378   Interval y (extent (this, Y_AXIS));
379   Stencil sys_stencil (Box (x, y),
380                        scm_cons (ly_symbol2scm ("combine-stencil"),
381                                  exprs));
382   if (debug_skylines)
383     {
384       sys_stencil.add_stencil (Lookup::points_to_line_stencil (0.1, skylines_[UP].to_points ()).in_color (255, 0, 0));
385       sys_stencil.add_stencil (Lookup::points_to_line_stencil (0.1, skylines_[DOWN].to_points ()).in_color (0, 255, 0));
386     }
387
388   Grob *left_bound = this->get_bound (LEFT);
389   SCM prop_init = left_bound->get_property ("line-break-system-details");
390   Prob *pl = make_paper_system (prop_init);
391   paper_system_set_stencil (pl, sys_stencil);
392
393   /* information that the page breaker might need */
394   Grob *right_bound = this->get_bound (RIGHT);
395   pl->set_property ("skyline-distance", get_property ("skyline-distance"));
396   pl->set_property ("page-break-permission", right_bound->get_property ("page-break-permission"));
397   pl->set_property ("page-turn-permission", right_bound->get_property ("page-turn-permission"));
398   pl->set_property ("page-break-penalty", right_bound->get_property ("page-break-penalty"));
399   pl->set_property ("page-turn-penalty", right_bound->get_property ("page-turn-penalty"));
400
401   if (!scm_is_pair (pl->get_property ("refpoint-Y-extent")))
402     {
403       Interval staff_refpoints;
404       staff_refpoints.set_empty ();
405       extract_grob_set (this, "spaceable-staves", staves);
406       for (vsize i = 0; i < staves.size (); i++)
407         {
408           Grob *g = staves[i];
409           staff_refpoints.add_point (g->relative_coordinate (this, Y_AXIS));
410         }
411       pl->set_property ("refpoint-Y-extent", ly_interval2scm (staff_refpoints));
412     }
413
414   pl->set_property ("system-grob", this->self_scm ()); 
415
416   return pl->unprotect ();
417 }
418
419 vector<Item*>
420 System::broken_col_range (Item const *left, Item const *right) const
421 {
422   vector<Item*> ret;
423
424   left = left->get_column ();
425   right = right->get_column ();
426
427   
428   extract_grob_set (this, "columns", cols);
429
430   vsize i = Paper_column::get_rank (left);
431   int end_rank = Paper_column::get_rank (right);
432   if (i < cols.size ())
433     i++;
434
435   while (i < cols.size ()
436          && Paper_column::get_rank (cols[i]) < end_rank)
437     {
438       Paper_column *c = dynamic_cast<Paper_column *> (cols[i]);
439       if (Paper_column::is_breakable (c) && !c->system_)
440         ret.push_back (c);
441       i++;
442     }
443
444   return ret;
445 }
446
447
448 /** Return all columns, but filter out any unused columns , since they might
449     disrupt the spacing problem. */
450 vector<Grob*>
451 System::used_columns () const
452 {
453   extract_grob_set (this, "columns", ro_columns);
454
455   int last_breakable = ro_columns.size ();
456
457   while (last_breakable--)
458     {
459       if (Paper_column::is_breakable (ro_columns [last_breakable]))
460         break;
461     }
462
463   vector<Grob*> columns;
464   for (int i = 0; i <= last_breakable; i++)
465     {
466       if (Paper_column::is_used (ro_columns[i]))
467         columns.push_back (ro_columns[i]);
468     }
469
470   return columns;
471 }
472
473 Paper_column *
474 System::column (vsize which) const
475 {
476   extract_grob_set (this, "columns", columns);
477   if (which >= columns.size ())
478     return 0;
479   
480   return dynamic_cast<Paper_column*> (columns[which]);
481 }
482
483 Paper_score*
484 System::paper_score () const
485 {
486   return pscore_;
487 }
488
489 int
490 System::get_rank () const
491 {
492   return rank_;
493 }
494
495 System *
496 get_root_system (Grob *me) 
497 {
498   Grob *system_grob = me;
499   
500   while (system_grob->get_parent (Y_AXIS))
501     system_grob = system_grob->get_parent (Y_AXIS);
502
503   return dynamic_cast<System*> (system_grob); 
504 }
505
506 void
507 System::build_skylines ()
508 {
509   vector<Box> boxes;
510   for (vsize i = 0; i < all_elements_->size (); i++)
511     {
512       Grob *g = all_elements_->grob (i);
513       if (!unsmob_stencil (g->get_property ("stencil")))
514         continue;
515
516       Interval xiv = g->extent (this, X_AXIS);
517       Interval yiv = g->extent (this, Y_AXIS);
518       if (!xiv.is_empty () && !yiv.is_empty ())
519         boxes.push_back (Box (xiv, yiv));
520     }
521
522   SCM horizon_padding_scm = get_property ("skyline-horizontal-padding");
523   Real horizon_padding = robust_scm2double (horizon_padding_scm, 0);
524   skylines_[UP] = Skyline (boxes, horizon_padding, X_AXIS, UP);
525   skylines_[DOWN] = Skyline (boxes, horizon_padding, X_AXIS, DOWN);
526 }
527
528
529 ADD_INTERFACE (System,
530                "This is the toplevel object: each object in a score "
531                "ultimately has a System object as its X and Y parent. ",
532
533                /* properties */
534                "all-elements "
535                "columns "
536                "pure-Y-extent "
537                "spaceable-staves "
538                "skyline-distance "
539                "skyline-horizontal-padding "
540                )