2 system.cc -- implement System
4 source file of the GNU LilyPond music typesetter
6 (c) 1996--2007 Han-Wen Nienhuys <hanwen@xs4all.nl>
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"
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 "staff-symbol-referencer.hh"
26 extern bool debug_skylines;
28 System::System (System const &src)
37 System::System (SCM s)
46 System::init_elements ()
48 SCM scm_arr = Grob_array::make_array ();
49 all_elements_ = unsmob_grob_array (scm_arr);
50 all_elements_->set_ordered (false);
51 set_object ("all-elements", scm_arr);
55 System::clone () const
57 return new System (*this);
61 System::element_count () const
63 return all_elements_->size ();
67 System::spanner_count () const
70 for (vsize i = all_elements_->size (); i--;)
71 if (dynamic_cast<Spanner *> (all_elements_->grob (i)))
77 System::typeset_grob (Grob *elem)
80 programming_error ("adding element twice");
83 elem->layout_ = pscore_->layout ();
84 all_elements_->add (elem);
90 System::derived_mark () const
92 if (!all_elements_->empty ())
94 Grob **ptr = &all_elements_->array_reference ()[0];
95 Grob **end = ptr + all_elements_->size ();
98 scm_gc_mark ((*ptr)->self_scm ());
104 scm_gc_mark (pscore_->self_scm ());
106 Spanner::derived_mark ();
110 fixup_refpoints (vector<Grob*> const &grobs)
112 for (vsize i = grobs.size (); i--;)
113 grobs[i]->fixup_refpoint ();
117 System::do_break_substitution_and_fixup_refpoints ()
119 for (vsize i = 0; i < all_elements_->size (); i++)
121 Grob *g = all_elements_->grob (i);
122 if (g->internal_has_interface (ly_symbol2scm ("only-prebreak-interface")))
125 Kill no longer needed grobs.
127 Item *it = dynamic_cast<Item *> (g);
128 if (it && Item::is_non_musical (it))
130 it->find_prebroken_piece (LEFT)->suicide ();
131 it->find_prebroken_piece (RIGHT)->suicide ();
135 else if (g->is_live ())
136 g->do_break_processing ();
140 fixups must be done in broken line_of_scores, because new elements
141 are put over there. */
143 for (vsize i = 0; i < broken_intos_.size (); i++)
145 Grob *se = broken_intos_[i];
147 extract_grob_set (se, "all-elements", all_elts);
148 for (vsize j = 0; j < all_elts.size (); j++)
150 Grob *g = all_elts[j];
151 g->fixup_refpoint ();
154 count += all_elts.size ();
158 needed for doing items.
160 fixup_refpoints (all_elements_->array ());
162 for (vsize i = 0; i < all_elements_->size (); i++)
163 all_elements_->grob (i)->handle_broken_dependencies ();
165 handle_broken_dependencies ();
167 /* Because the this->get_property (all-elements) contains items in 3
168 versions, handle_broken_dependencies () will leave duplicated
169 items in all-elements. Strictly speaking this is harmless, but
170 it leads to duplicated symbols in the output. uniq makes sure
171 that no duplicates are in the list. */
172 for (vsize i = 0; i < broken_intos_.size (); i++)
174 System *child = dynamic_cast<System*> (broken_intos_[i]);
175 child->all_elements_->remove_duplicates ();
178 if (be_verbose_global)
179 message (_f ("Element count %d.", count + element_count ()));
183 System::get_broken_system_grobs ()
186 for (vsize i = 0; i < broken_intos_.size (); i++)
187 ret = scm_cons (broken_intos_[i]->self_scm (), ret);
188 return scm_reverse (ret);
192 System::get_paper_systems ()
194 SCM lines = scm_c_make_vector (broken_intos_.size (), SCM_EOL);
195 for (vsize i = 0; i < broken_intos_.size (); i++)
197 if (be_verbose_global)
198 progress_indication ("[");
200 System *system = dynamic_cast<System *> (broken_intos_[i]);
202 scm_vector_set_x (lines, scm_from_int (i),
203 system->get_paper_system ());
205 if (be_verbose_global)
206 progress_indication (to_string (i) + "]");
212 System::break_into_pieces (vector<Column_x_positions> const &breaking)
214 for (vsize i = 0; i < breaking.size (); i++)
216 System *system = dynamic_cast<System *> (clone ());
217 system->rank_ = broken_intos_.size ();
219 vector<Grob*> c (breaking[i].cols_);
220 pscore_->typeset_system (system);
222 int st = Paper_column::get_rank (c[0]);
223 int end = Paper_column::get_rank (c.back ());
224 Interval iv (pure_height (this, st, end));
225 system->set_property ("pure-Y-extent", ly_interval2scm (iv));
227 system->set_bound (LEFT, c[0]);
228 system->set_bound (RIGHT, c.back ());
229 SCM system_labels = SCM_EOL;
230 for (vsize j = 0; j < c.size (); j++)
232 c[j]->translate_axis (breaking[i].config_[j], X_AXIS);
233 dynamic_cast<Paper_column *> (c[j])->system_ = system;
234 /* collect the column labels */
235 SCM col_labels = c[j]->get_property ("labels");
236 if (scm_is_pair (col_labels))
237 system_labels = scm_append (scm_list_2 (col_labels, system_labels));
239 system->set_property ("labels", system_labels);
241 set_loose_columns (system, &breaking[i]);
242 broken_intos_.push_back (system);
247 System::add_column (Paper_column *p)
250 Grob_array *ga = unsmob_grob_array (me->get_object ("columns"));
253 SCM scm_ga = Grob_array::make_array ();
254 me->set_object ("columns", scm_ga);
255 ga = unsmob_grob_array (scm_ga);
258 p->rank_ = ga->size ();
261 Axis_group_interface::add_element (this, p);
265 System::pre_processing ()
267 for (vsize i = 0; i < all_elements_->size (); i++)
268 all_elements_->grob (i)->discretionary_processing ();
270 if (be_verbose_global)
271 message (_f ("Grob count %d", element_count ()));
274 order is significant: broken grobs are added to the end of the
275 array, and should be processed before the original is potentially
278 for (vsize i = all_elements_->size (); i--;)
279 all_elements_->grob (i)->handle_prebroken_dependencies ();
281 fixup_refpoints (all_elements_->array ());
283 for (vsize i = 0; i < all_elements_->size (); i++)
285 Grob *g = all_elements_->grob (i);
286 (void) g->get_property ("before-line-breaking");
289 for (vsize i = 0; i < all_elements_->size (); i++)
291 Grob *e = all_elements_->grob (i);
292 (void) e->get_property ("springs-and-rods");
297 System::post_processing ()
299 for (vsize i = 0; i < all_elements_->size (); i++)
301 Grob *g = all_elements_->grob (i);
303 (void) g->get_property ("after-line-breaking");
306 Interval iv (extent (this, Y_AXIS));
308 programming_error ("system with empty extent");
310 translate_axis (-iv[MAX], Y_AXIS);
312 /* Generate all stencils to trigger font loads.
313 This might seem inefficient, but Stencils are cached per grob
316 vector<Grob*> all_elts_sorted (all_elements_->array ());
317 vector_sort (all_elts_sorted, std::less<Grob*> ());
318 uniq (all_elts_sorted);
319 this->get_stencil ();
320 for (vsize i = all_elts_sorted.size (); i--;)
322 Grob *g = all_elts_sorted[i];
334 operator< (Layer_entry const &a,
335 Layer_entry const &b)
337 return a.layer_ < b.layer_;
342 System::get_paper_system ()
349 vector<Layer_entry> entries;
350 for (vsize j = 0; j < all_elements_->size (); j++)
353 e.grob_ = all_elements_->grob (j);
354 e.layer_ = robust_scm2int (e.grob_->get_property ("layer"), 1);
356 entries.push_back (e);
359 vector_sort (entries, std::less<Layer_entry> ());
360 for (vsize j = 0; j < entries.size (); j++)
362 Grob *g = entries[j].grob_;
363 Stencil st = g->get_print_stencil ();
365 if (st.expr () == SCM_EOL)
369 for (int a = X_AXIS; a < NO_AXES; a++)
370 o[Axis (a)] = g->relative_coordinate (this, Axis (a));
372 Offset extra = robust_scm2offset (g->get_property ("extra-offset"),
374 * Staff_symbol_referencer::staff_space (g);
376 /* Must copy the stencil, for we cannot change the stencil
379 st.translate (o + extra);
381 *tail = scm_cons (st.expr (), SCM_EOL);
382 tail = SCM_CDRLOC (*tail);
385 if (Stencil *me = get_stencil ())
386 exprs = scm_cons (me->expr (), exprs);
388 Interval x (extent (this, X_AXIS));
389 Interval y (extent (this, Y_AXIS));
390 Stencil sys_stencil (Box (x, y),
391 scm_cons (ly_symbol2scm ("combine-stencil"),
395 Skyline_pair *skylines = Skyline_pair::unsmob (get_property ("vertical-skylines"));
398 sys_stencil.add_stencil (Lookup::points_to_line_stencil (0.1, (*skylines)[UP].to_points (X_AXIS)).in_color (255, 0, 0));
399 sys_stencil.add_stencil (Lookup::points_to_line_stencil (0.1, (*skylines)[DOWN].to_points (X_AXIS)).in_color (0, 255, 0));
403 Grob *left_bound = this->get_bound (LEFT);
404 SCM prop_init = left_bound->get_property ("line-break-system-details");
405 Prob *pl = make_paper_system (prop_init);
406 paper_system_set_stencil (pl, sys_stencil);
408 /* information that the page breaker might need */
409 Grob *right_bound = this->get_bound (RIGHT);
410 pl->set_property ("vertical-skylines", this->get_property ("vertical-skylines"));
411 pl->set_property ("page-break-permission", right_bound->get_property ("page-break-permission"));
412 pl->set_property ("page-turn-permission", right_bound->get_property ("page-turn-permission"));
413 pl->set_property ("page-break-penalty", right_bound->get_property ("page-break-penalty"));
414 pl->set_property ("page-turn-penalty", right_bound->get_property ("page-turn-penalty"));
416 Interval staff_refpoints;
417 extract_grob_set (this, "spaceable-staves", staves);
418 for (vsize i = 0; i < staves.size (); i++)
419 staff_refpoints.add_point (staves[i]->relative_coordinate (this, Y_AXIS));
421 pl->set_property ("staff-refpoint-extent", ly_interval2scm (staff_refpoints));
422 pl->set_property ("system-grob", this->self_scm ());
424 return pl->unprotect ();
428 System::broken_col_range (Item const *left, Item const *right) const
432 left = left->get_column ();
433 right = right->get_column ();
436 extract_grob_set (this, "columns", cols);
438 vsize i = Paper_column::get_rank (left);
439 int end_rank = Paper_column::get_rank (right);
440 if (i < cols.size ())
443 while (i < cols.size ()
444 && Paper_column::get_rank (cols[i]) < end_rank)
446 Paper_column *c = dynamic_cast<Paper_column *> (cols[i]);
447 if (Paper_column::is_breakable (c) && !c->system_)
456 /** Return all columns, but filter out any unused columns , since they might
457 disrupt the spacing problem. */
459 System::used_columns () const
461 extract_grob_set (this, "columns", ro_columns);
463 int last_breakable = ro_columns.size ();
465 while (last_breakable--)
467 if (Paper_column::is_breakable (ro_columns [last_breakable]))
471 vector<Grob*> columns;
472 for (int i = 0; i <= last_breakable; i++)
474 if (Paper_column::is_used (ro_columns[i]))
475 columns.push_back (ro_columns[i]);
482 System::column (vsize which) const
484 extract_grob_set (this, "columns", columns);
485 if (which >= columns.size ())
488 return dynamic_cast<Paper_column*> (columns[which]);
492 System::paper_score () const
498 System::get_rank () const
504 get_root_system (Grob *me)
506 Grob *system_grob = me;
508 while (system_grob->get_parent (Y_AXIS))
509 system_grob = system_grob->get_parent (Y_AXIS);
511 return dynamic_cast<System*> (system_grob);
514 ADD_INTERFACE (System,
515 "This is the top-level object: Each object in a score"
516 " ultimately has a @code{System} object as its X and"
526 "skyline-horizontal-padding "