]> git.donarmstrong.com Git - lilypond.git/blob - lily/system.cc
Revert "Fix 1062."
[lilypond.git] / lily / system.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1996--2010 Han-Wen Nienhuys <hanwen@xs4all.nl>
5
6   LilyPond is free software: you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation, either version 3 of the License, or
9   (at your option) any later version.
10
11   LilyPond is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15
16   You should have received a copy of the GNU General Public License
17   along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "system.hh"
21
22 #include "align-interface.hh"
23 #include "all-font-metrics.hh"
24 #include "axis-group-interface.hh"
25 #include "grob-array.hh"
26 #include "hara-kiri-group-spanner.hh"
27 #include "international.hh"
28 #include "lookup.hh"
29 #include "main.hh"
30 #include "output-def.hh"
31 #include "page-layout-problem.hh"
32 #include "paper-column.hh"
33 #include "paper-score.hh"
34 #include "paper-system.hh"
35 #include "pointer-group-interface.hh"
36 #include "skyline-pair.hh"
37 #include "staff-symbol-referencer.hh"
38 #include "warn.hh"
39
40 System::System (System const &src)
41   : Spanner (src)
42 {
43   all_elements_ = 0;
44   pscore_ = 0;
45   rank_ = 0;
46   init_elements ();
47 }
48
49 System::System (SCM s)
50   : Spanner (s)
51 {
52   all_elements_ = 0;
53   rank_ = 0;
54   init_elements ();
55 }
56
57 void
58 System::init_elements ()
59 {
60   SCM scm_arr = Grob_array::make_array ();
61   all_elements_ = unsmob_grob_array (scm_arr);
62   all_elements_->set_ordered (false);
63   set_object ("all-elements", scm_arr);
64 }
65
66 Grob *
67 System::clone () const
68 {
69   return new System (*this);
70 }
71
72 int
73 System::element_count () const
74 {
75   return all_elements_->size ();
76 }
77
78 int
79 System::spanner_count () const
80 {
81   int k = 0;
82   for (vsize i = all_elements_->size (); i--;)
83     if (dynamic_cast<Spanner *> (all_elements_->grob (i)))
84       k++;
85   return k;
86 }
87
88 void
89 System::typeset_grob (Grob *elem)
90 {
91   if (elem->layout_)
92     programming_error ("adding element twice");
93   else
94     {
95       elem->layout_ = pscore_->layout ();
96       all_elements_->add (elem);
97       elem->unprotect ();
98     }
99 }
100
101 void
102 System::derived_mark () const
103 {
104   if (!all_elements_->empty ())
105     {
106       Grob **ptr = &all_elements_->array_reference ()[0];
107       Grob **end = ptr + all_elements_->size ();
108       while (ptr < end)
109         {
110           scm_gc_mark ((*ptr)->self_scm ());
111           ptr++;
112         }
113     }
114
115   if (pscore_)
116     scm_gc_mark (pscore_->self_scm ());
117
118   Spanner::derived_mark ();
119 }
120
121 static void
122 fixup_refpoints (vector<Grob*> const &grobs)
123 {
124   for (vsize i = grobs.size (); i--;)
125     grobs[i]->fixup_refpoint ();
126 }
127
128 void
129 System::do_break_substitution_and_fixup_refpoints ()
130 {
131   for (vsize i = 0; i < all_elements_->size (); i++)
132     {
133       Grob *g = all_elements_->grob (i);
134       if (g->internal_has_interface (ly_symbol2scm ("only-prebreak-interface")))
135         {
136           /*
137             Kill no longer needed grobs.
138           */
139           Item *it = dynamic_cast<Item *> (g);
140           if (it && Item::is_non_musical (it))
141             {
142               it->find_prebroken_piece (LEFT)->suicide ();
143               it->find_prebroken_piece (RIGHT)->suicide ();
144             }
145           g->suicide ();
146         }
147       else if (g->is_live ())
148         g->do_break_processing ();
149     }
150
151   /*
152     fixups must be done in broken line_of_scores, because new elements
153     are put over there.  */
154   int count = 0;
155   for (vsize i = 0; i < broken_intos_.size (); i++)
156     {
157       Grob *se = broken_intos_[i];
158
159       extract_grob_set (se, "all-elements", all_elts);
160       for (vsize j = 0; j < all_elts.size (); j++)
161         {
162           Grob *g = all_elts[j];
163           g->fixup_refpoint ();
164         }
165
166       count += all_elts.size ();
167     }
168
169   /*
170     needed for doing items.
171   */
172   fixup_refpoints (all_elements_->array ());
173
174   for (vsize i = 0; i < all_elements_->size (); i++)
175     all_elements_->grob (i)->handle_broken_dependencies ();
176
177   handle_broken_dependencies ();
178
179   /* Because the this->get_property (all-elements) contains items in 3
180      versions, handle_broken_dependencies () will leave duplicated
181      items in all-elements.  Strictly speaking this is harmless, but
182      it leads to duplicated symbols in the output.  uniq makes sure
183      that no duplicates are in the list.  */
184   for (vsize i = 0; i < broken_intos_.size (); i++)
185     {
186       System *child = dynamic_cast<System*> (broken_intos_[i]);
187       child->all_elements_->remove_duplicates ();
188       for (vsize j = 0; j < child->all_elements_->size (); j++)
189         {
190           Grob *g = child->all_elements_->grob (j);
191
192           (void) g->get_property ("after-line-breaking");
193         }
194     }
195
196   if (be_verbose_global)
197     message (_f ("Element count %d", count + element_count ()) + "\n");
198 }
199
200 SCM
201 System::get_broken_system_grobs ()
202 {
203   SCM ret = SCM_EOL;
204   for (vsize i = 0; i < broken_intos_.size (); i++)
205     ret = scm_cons (broken_intos_[i]->self_scm (), ret);
206   return scm_reverse (ret);
207 }
208
209 SCM
210 System::get_paper_systems ()
211 {
212   SCM lines = scm_c_make_vector (broken_intos_.size (), SCM_EOL);
213   for (vsize i = 0; i < broken_intos_.size (); i++)
214     {
215       if (be_verbose_global)
216         progress_indication ("[");
217
218       System *system = dynamic_cast<System *> (broken_intos_[i]);
219
220       scm_vector_set_x (lines, scm_from_int (i),
221                         system->get_paper_system ());
222
223       if (be_verbose_global)
224         progress_indication (to_string (i) + "]");
225     }
226   return lines;
227 }
228
229 void
230 System::break_into_pieces (vector<Column_x_positions> const &breaking)
231 {
232   for (vsize i = 0; i < breaking.size (); i++)
233     {
234       System *system = dynamic_cast<System *> (clone ());
235       system->rank_ = broken_intos_.size ();
236
237       vector<Grob*> c (breaking[i].cols_);
238       pscore_->typeset_system (system);
239
240       int st = Paper_column::get_rank (c[0]);
241       int end = Paper_column::get_rank (c.back ());
242       Interval iv (pure_height (this, st, end));
243       system->set_property ("pure-Y-extent", ly_interval2scm (iv));
244
245       system->set_bound (LEFT, c[0]);
246       system->set_bound (RIGHT, c.back ());
247       SCM system_labels = SCM_EOL;
248       for (vsize j = 0; j < c.size (); j++)
249         {
250           c[j]->translate_axis (breaking[i].config_[j], X_AXIS);
251           dynamic_cast<Paper_column *> (c[j])->set_system (system);
252           /* collect the column labels */
253           SCM col_labels = c[j]->get_property ("labels");
254           if (scm_is_pair (col_labels))
255             system_labels = scm_append (scm_list_2 (col_labels, system_labels));
256         }
257       system->set_property ("labels", system_labels);
258       
259       set_loose_columns (system, &breaking[i]);
260       broken_intos_.push_back (system);
261     }
262 }
263
264 void
265 System::add_column (Paper_column *p)
266 {
267   Grob *me = this;
268   Grob_array *ga = unsmob_grob_array (me->get_object ("columns"));
269   if (!ga)
270     {
271       SCM scm_ga = Grob_array::make_array ();
272       me->set_object ("columns", scm_ga);
273       ga = unsmob_grob_array (scm_ga);
274     }
275
276   p->set_rank (ga->size ());
277
278   ga->add (p);
279   Axis_group_interface::add_element (this, p);
280 }
281
282 void
283 System::pre_processing ()
284 {
285   for (vsize i = 0; i < all_elements_->size (); i++)
286     all_elements_->grob (i)->discretionary_processing ();
287
288   if (be_verbose_global)
289     message (_f ("Grob count %d", element_count ()));
290
291   /*
292     order is significant: broken grobs are added to the end of the
293     array, and should be processed before the original is potentially
294     killed.
295   */
296   for (vsize i = all_elements_->size (); i--;)
297     all_elements_->grob (i)->handle_prebroken_dependencies ();
298
299   fixup_refpoints (all_elements_->array ());
300
301   for (vsize i = 0; i < all_elements_->size (); i++)
302     {
303       Grob *g = all_elements_->grob (i);
304       (void) g->get_property ("before-line-breaking");
305     }
306
307   for (vsize i = 0; i < all_elements_->size (); i++)
308     {
309       Grob *e = all_elements_->grob (i);
310       (void) e->get_property ("springs-and-rods");
311     }
312 }
313
314 void
315 System::post_processing ()
316 {
317   Interval iv (extent (this, Y_AXIS));
318   if (iv.is_empty ())
319     programming_error ("system with empty extent");
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   vector<Grob*> all_elts_sorted (all_elements_->array ());
328   vector_sort (all_elts_sorted, std::less<Grob*> ());
329   uniq (all_elts_sorted);
330   this->get_stencil ();
331   for (vsize i = all_elts_sorted.size (); i--;)
332     {
333       Grob *g = all_elts_sorted[i];
334       g->get_stencil ();
335     }
336 }
337
338 struct Layer_entry
339 {
340   Grob *grob_;
341   int layer_;
342 };
343
344 bool
345 operator< (Layer_entry  const &a,
346            Layer_entry  const &b)
347 {
348   return a.layer_ < b.layer_;
349 }
350
351
352 SCM
353 System::get_paper_system ()
354 {
355   SCM exprs = SCM_EOL;
356   SCM *tail = &exprs;
357
358   post_processing ();
359
360   vector<Layer_entry> entries;
361   for (vsize j = 0; j < all_elements_->size (); j++)
362     {
363       Layer_entry e;
364       e.grob_ = all_elements_->grob (j);
365       e.layer_ = robust_scm2int (e.grob_->get_property ("layer"), 1);
366       
367       entries.push_back (e); 
368     }
369
370   vector_sort (entries, std::less<Layer_entry> ());
371   for (vsize j = 0; j < entries.size (); j++)
372     {
373       Grob *g = entries[j].grob_;
374       Stencil st = g->get_print_stencil ();
375
376       if (st.expr () == SCM_EOL)
377         continue;
378       
379       Offset o;
380       for (int a = X_AXIS; a < NO_AXES; a++)
381         o[Axis (a)] = g->relative_coordinate (this, Axis (a));
382
383       Offset extra = robust_scm2offset (g->get_property ("extra-offset"),
384                                         Offset (0, 0))
385         * Staff_symbol_referencer::staff_space (g);
386
387       /* Must copy the stencil, for we cannot change the stencil
388          cached in G.  */
389
390       st.translate (o + extra);
391
392       *tail = scm_cons (st.expr (), SCM_EOL);
393       tail = SCM_CDRLOC (*tail);
394     }
395
396   if (Stencil *me = get_stencil ())
397     exprs = scm_cons (me->expr (), exprs);
398
399   Interval x (extent (this, X_AXIS));
400   Interval y (extent (this, Y_AXIS));
401   Stencil sys_stencil (Box (x, y),
402                        scm_cons (ly_symbol2scm ("combine-stencil"),
403                                  exprs));
404   if (debug_skylines)
405     {
406       Skyline_pair *skylines = Skyline_pair::unsmob (get_property ("vertical-skylines"));
407       if (skylines)
408         {
409           Stencil up
410             = Lookup::points_to_line_stencil (0.1, (*skylines)[UP].to_points (X_AXIS));
411           Stencil down
412             = Lookup::points_to_line_stencil (0.1, (*skylines)[DOWN].to_points (X_AXIS));
413           sys_stencil.add_stencil (up.in_color (255, 0, 0));
414           sys_stencil.add_stencil (down.in_color (0, 255, 0));
415         }
416     }
417
418   Grob *left_bound = this->get_bound (LEFT);
419   SCM prop_init = left_bound->get_property ("line-break-system-details");
420   Prob *pl = make_paper_system (prop_init);
421   paper_system_set_stencil (pl, sys_stencil);
422
423   /* information that the page breaker might need */
424   Grob *right_bound = this->get_bound (RIGHT);
425   pl->set_property ("vertical-skylines", this->get_property ("vertical-skylines"));
426   pl->set_property ("page-break-permission", right_bound->get_property ("page-break-permission"));
427   pl->set_property ("page-turn-permission", right_bound->get_property ("page-turn-permission"));
428   pl->set_property ("page-break-penalty", right_bound->get_property ("page-break-penalty"));
429   pl->set_property ("page-turn-penalty", right_bound->get_property ("page-turn-penalty"));
430
431   Interval staff_refpoints;
432   extract_grob_set (this, "spaceable-staves", staves);
433   for (vsize i = 0; i < staves.size (); i++)
434     if (staves[i]->is_live ())
435       staff_refpoints.add_point (staves[i]->relative_coordinate (this, Y_AXIS));
436
437   pl->set_property ("staff-refpoint-extent", ly_interval2scm (staff_refpoints));
438   pl->set_property ("system-grob", this->self_scm ()); 
439
440   return pl->unprotect ();
441 }
442
443 vector<Item*>
444 System::broken_col_range (Item const *left, Item const *right) const
445 {
446   vector<Item*> ret;
447
448   left = left->get_column ();
449   right = right->get_column ();
450
451   
452   extract_grob_set (this, "columns", cols);
453
454   vsize i = Paper_column::get_rank (left);
455   int end_rank = Paper_column::get_rank (right);
456   if (i < cols.size ())
457     i++;
458
459   while (i < cols.size ()
460          && Paper_column::get_rank (cols[i]) < end_rank)
461     {
462       Paper_column *c = dynamic_cast<Paper_column *> (cols[i]);
463       if (Paper_column::is_breakable (c) && !c->get_system ())
464         ret.push_back (c);
465       i++;
466     }
467
468   return ret;
469 }
470
471
472 /** Return all columns, but filter out any unused columns , since they might
473     disrupt the spacing problem. */
474 vector<Grob*>
475 System::used_columns () const
476 {
477   extract_grob_set (this, "columns", ro_columns);
478
479   int last_breakable = ro_columns.size ();
480
481   while (last_breakable--)
482     {
483       if (Paper_column::is_breakable (ro_columns [last_breakable]))
484         break;
485     }
486
487   vector<Grob*> columns;
488   for (int i = 0; i <= last_breakable; i++)
489     {
490       if (Paper_column::is_used (ro_columns[i]))
491         columns.push_back (ro_columns[i]);
492     }
493
494   return columns;
495 }
496
497 Paper_column *
498 System::column (vsize which) const
499 {
500   extract_grob_set (this, "columns", columns);
501   if (which >= columns.size ())
502     return 0;
503   
504   return dynamic_cast<Paper_column*> (columns[which]);
505 }
506
507 Paper_score*
508 System::paper_score () const
509 {
510   return pscore_;
511 }
512
513 int
514 System::get_rank () const
515 {
516   return rank_;
517 }
518
519 System *
520 get_root_system (Grob *me) 
521 {
522   Grob *system_grob = me;
523   
524   while (system_grob->get_parent (Y_AXIS))
525     system_grob = system_grob->get_parent (Y_AXIS);
526
527   return dynamic_cast<System*> (system_grob); 
528 }
529
530 Grob *
531 System::get_vertical_alignment ()
532 {
533   extract_grob_set (this, "elements", elts);
534   Grob *ret = 0;
535   for (vsize i = 0; i < elts.size (); i++)
536     if (Align_interface::has_interface (elts[i]))
537       {
538         if (ret)
539           programming_error ("found multiple vertical alignments in this system");
540         ret = elts[i];
541       }
542
543   if (!ret)
544     programming_error ("didn't find a vertical alignment in this system");
545   return ret;
546 }
547
548 // Finds the furthest staff in the given direction whose x-extent
549 // overlaps with the given interval.
550 Grob *
551 System::get_extremal_staff (Direction dir, Interval const &iv)
552 {
553   Grob *align = get_vertical_alignment ();
554   if (!align)
555     return 0;
556
557   extract_grob_set (align, "elements", elts);
558   vsize start = (dir == UP) ? 0 : elts.size () - 1;
559   vsize end = (dir == UP) ? elts.size () : VPOS;
560   for (vsize i = start; i != end; i += dir)
561     {
562       if (Hara_kiri_group_spanner::has_interface (elts[i]))
563         Hara_kiri_group_spanner::consider_suicide (elts[i]);
564
565       Interval intersection = elts[i]->extent (this, X_AXIS);
566       intersection.intersect (iv);
567       if (elts[i]->is_live () && !intersection.is_empty ())
568         return elts[i];
569     }
570   return 0;
571 }
572
573 Interval
574 System::part_of_line_pure_height (vsize start, vsize end, bool begin)
575 {
576   Grob *alignment = get_vertical_alignment ();
577   if (!alignment)
578     {
579       programming_error("system does not have a vertical alignment");
580       return Interval();
581     }
582   extract_grob_set (alignment, "elements", staves);
583   vector<Real> offsets = Align_interface::get_minimum_translations (alignment, staves, Y_AXIS, true, start, end);
584
585   Interval ret;
586   for (vsize i = 0; i < staves.size(); ++i)
587     {
588       Interval iv = begin?
589         Axis_group_interface::begin_of_line_pure_height (staves[i], start) :
590         Axis_group_interface::rest_of_line_pure_height (staves[i], start, end);
591       if (i<offsets.size())
592         iv.translate (offsets[i]);
593       ret.unite (iv);
594     }
595   return ret;
596 }
597
598 Interval
599 System::begin_of_line_pure_height (vsize start, vsize end)
600 {
601   return part_of_line_pure_height (start, end, true);
602 }
603
604 Interval
605 System::rest_of_line_pure_height (vsize start, vsize end)
606 {
607   return part_of_line_pure_height (start, end, false);
608 }
609
610 ADD_INTERFACE (System,
611                "This is the top-level object: Each object in a score"
612                " ultimately has a @code{System} object as its X and"
613                " Y@tie{}parent.",
614
615                /* properties */
616                "all-elements "
617                "columns "
618                "labels "
619                "pure-Y-extent "
620                "spaceable-staves "
621                "skyline-distance "
622                "skyline-horizontal-padding "
623                )