2 system.cc -- implement System
4 source file of the GNU LilyPond music typesetter
6 (c) 1996--2006 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 "spacing-interface.hh"
23 #include "staff-symbol-referencer.hh"
24 #include "tweak-registration.hh"
27 extern bool debug_skylines;
29 System::System (System const &src, int count)
30 : Spanner (src, count)
38 System::System (SCM s, Object_key const *key)
47 System::init_elements ()
49 SCM scm_arr = Grob_array::make_array ();
50 all_elements_ = unsmob_grob_array (scm_arr);
51 all_elements_->set_ordered (false);
52 set_object ("all-elements", scm_arr);
56 System::clone (int index) const
58 return new System (*this, index);
62 System::element_count () const
64 return all_elements_->size ();
68 System::spanner_count () const
71 for (vsize i = all_elements_->size (); i--;)
72 if (dynamic_cast<Spanner *> (all_elements_->grob (i)))
78 System::typeset_grob (Grob *elem)
81 programming_error ("adding element twice");
84 elem->layout_ = pscore_->layout ();
85 all_elements_->add (elem);
91 System::derived_mark () const
93 if (!all_elements_->empty ())
95 Grob **ptr = &all_elements_->array_reference ()[0];
96 Grob **end = ptr + all_elements_->size ();
99 scm_gc_mark ((*ptr)->self_scm ());
105 scm_gc_mark (pscore_->self_scm ());
107 Spanner::derived_mark ();
111 fixup_refpoints (vector<Grob*> const &grobs)
113 for (vsize i = grobs.size (); i--;)
114 grobs[i]->fixup_refpoint ();
118 System::get_paper_systems ()
120 for (vsize i = 0; i < all_elements_->size (); i++)
122 Grob *g = all_elements_->grob (i);
123 if (g->internal_has_interface (ly_symbol2scm ("only-prebreak-interface")))
126 Kill no longer needed grobs.
128 Item *it = dynamic_cast<Item *> (g);
129 if (it && Item::is_non_musical (it))
131 it->find_prebroken_piece (LEFT)->suicide ();
132 it->find_prebroken_piece (RIGHT)->suicide ();
136 else if (g->is_live ())
137 g->do_break_processing ();
141 fixups must be done in broken line_of_scores, because new elements
142 are put over there. */
144 for (vsize i = 0; i < broken_intos_.size (); i++)
146 Grob *se = broken_intos_[i];
148 extract_grob_set (se, "all-elements", all_elts);
149 for (vsize j = 0; j < all_elts.size (); j++)
151 Grob *g = all_elts[j];
152 g->fixup_refpoint ();
155 count += all_elts.size ();
159 needed for doing items.
161 fixup_refpoints (all_elements_->array ());
163 for (vsize i = 0; i < all_elements_->size (); i++)
164 all_elements_->grob (i)->handle_broken_dependencies ();
166 handle_broken_dependencies ();
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. uniq makes sure
172 that no duplicates are in the list. */
173 for (vsize i = 0; i < broken_intos_.size (); i++)
175 System *child = dynamic_cast<System*> (broken_intos_[i]);
176 child->all_elements_->remove_duplicates ();
179 if (be_verbose_global)
180 message (_f ("Element count %d.", count + element_count ()));
182 SCM lines = scm_c_make_vector (broken_intos_.size (), SCM_EOL);
183 for (vsize i = 0; i < broken_intos_.size (); i++)
185 if (be_verbose_global)
186 progress_indication ("[");
188 System *system = dynamic_cast<System *> (broken_intos_[i]);
190 system->post_processing ();
191 system->build_skylines ();
194 System *prev = dynamic_cast<System*> (broken_intos_[i-1]);
195 Real r = prev->skylines_[DOWN].distance (system->skylines_[UP]);
196 system->set_property ("skyline-distance", scm_from_double (r));
198 scm_vector_set_x (lines, scm_from_int (i),
199 system->get_paper_system ());
201 if (be_verbose_global)
202 progress_indication (to_string (i) + "]");
208 System::break_into_pieces (vector<Column_x_positions> const &breaking)
210 for (vsize i = 0; i < breaking.size (); i++)
212 System *system = dynamic_cast<System *> (clone (broken_intos_.size ()));
213 system->rank_ = broken_intos_.size ();
215 vector<Grob*> c (breaking[i].cols_);
216 pscore_->typeset_system (system);
218 int st = Paper_column::get_rank (c[0]);
219 int end = Paper_column::get_rank (c.back ());
220 Interval iv (pure_height (this, st, end));
221 system->set_property ("pure-Y-extent", ly_interval2scm (iv));
223 system->set_bound (LEFT, c[0]);
224 system->set_bound (RIGHT, c.back ());
225 for (vsize j = 0; j < c.size (); j++)
227 c[j]->translate_axis (breaking[i].config_[j], X_AXIS);
228 dynamic_cast<Paper_column *> (c[j])->system_ = system;
231 set_loose_columns (system, &breaking[i]);
232 broken_intos_.push_back (system);
237 System::add_column (Paper_column *p)
240 Grob_array *ga = unsmob_grob_array (me->get_object ("columns"));
243 SCM scm_ga = Grob_array::make_array ();
244 me->set_object ("columns", scm_ga);
245 ga = unsmob_grob_array (scm_ga);
248 p->rank_ = ga->size ();
251 Axis_group_interface::add_element (this, p);
255 apply_tweaks (Grob *g, bool broken)
257 if (bool (g->original ()) == broken)
259 SCM tweaks = global_registry_->get_tweaks (g);
260 for (SCM s = tweaks; scm_is_pair (s); s = scm_cdr (s))
262 SCM proc = scm_caar (s);
263 SCM rest = scm_cdar (s);
264 scm_apply_1 (proc, g->self_scm (), rest);
270 System::pre_processing ()
272 for (vsize i = 0; i < all_elements_->size (); i++)
273 all_elements_->grob (i)->discretionary_processing ();
275 if (be_verbose_global)
276 message (_f ("Grob count %d", element_count ()));
279 order is significant: broken grobs are added to the end of the
280 array, and should be processed before the original is potentially
283 for (vsize i = all_elements_->size (); i--;)
284 all_elements_->grob (i)->handle_prebroken_dependencies ();
286 fixup_refpoints (all_elements_->array ());
288 for (vsize i = 0; i < all_elements_->size (); i++)
289 apply_tweaks (all_elements_->grob (i), false);
291 for (vsize i = 0; i < all_elements_->size (); i++)
293 Grob *g = all_elements_->grob (i);
294 (void) g->get_property ("before-line-breaking");
297 for (vsize i = 0; i < all_elements_->size (); i++)
299 Grob *e = all_elements_->grob (i);
300 (void) e->get_property ("springs-and-rods");
305 System::post_processing ()
307 for (vsize i = 0; i < all_elements_->size (); i++)
309 Grob *g = all_elements_->grob (i);
311 apply_tweaks (g, true);
312 (void) g->get_property ("after-line-breaking");
315 Interval iv (extent (this, Y_AXIS));
317 programming_error ("system with empty extent");
319 translate_axis (-iv[MAX], Y_AXIS);
321 /* Generate all stencils to trigger font loads.
322 This might seem inefficient, but Stencils are cached per grob
325 vector<Grob*> all_elts_sorted (all_elements_->array ());
326 vector_sort (all_elts_sorted, std::less<Grob*> ());
327 uniq (all_elts_sorted);
328 this->get_stencil ();
329 for (vsize i = all_elts_sorted.size (); i--;)
331 Grob *g = all_elts_sorted[i];
343 operator< (Layer_entry const &a,
344 Layer_entry const &b)
346 return a.layer_ < b.layer_;
351 System::get_paper_system ()
356 vector<Layer_entry> entries;
357 for (vsize j = 0; j < all_elements_->size (); j++)
360 e.grob_ = all_elements_->grob (j);
361 e.layer_ = robust_scm2int (e.grob_->get_property ("layer"), 1);
363 entries.push_back (e);
366 vector_sort (entries, std::less<Layer_entry> ());
367 for (vsize j = 0; j < entries.size (); j++)
369 Grob *g = entries[j].grob_;
370 Stencil st = g->get_print_stencil ();
372 if (st.expr() == SCM_EOL)
376 for (int a = X_AXIS; a < NO_AXES; a++)
377 o[Axis (a)] = g->relative_coordinate (this, Axis (a));
379 Offset extra = robust_scm2offset (g->get_property ("extra-offset"),
381 * Staff_symbol_referencer::staff_space (g);
383 /* Must copy the stencil, for we cannot change the stencil
386 st.translate (o + extra);
388 *tail = scm_cons (st.expr (), SCM_EOL);
389 tail = SCM_CDRLOC (*tail);
392 if (Stencil *me = get_stencil ())
393 exprs = scm_cons (me->expr (), exprs);
395 Interval x (extent (this, X_AXIS));
396 Interval y (extent (this, Y_AXIS));
397 Stencil sys_stencil (Box (x, y),
398 scm_cons (ly_symbol2scm ("combine-stencil"),
402 sys_stencil.add_stencil (points_to_line_stencil (skylines_[UP].to_points ()).in_color (255, 0, 0));
403 sys_stencil.add_stencil (points_to_line_stencil (skylines_[DOWN].to_points ()).in_color (0, 255, 0));
406 Grob *left_bound = this->get_bound (LEFT);
407 SCM prop_init = left_bound->get_property ("line-break-system-details");
408 Prob *pl = make_paper_system (prop_init);
409 paper_system_set_stencil (pl, sys_stencil);
411 /* information that the page breaker might need */
412 Grob *right_bound = this->get_bound (RIGHT);
413 pl->set_property ("skyline-distance", get_property ("skyline-distance"));
414 pl->set_property ("page-break-permission", right_bound->get_property ("page-break-permission"));
415 pl->set_property ("page-turn-permission", right_bound->get_property ("page-turn-permission"));
416 pl->set_property ("page-break-penalty", right_bound->get_property ("page-break-penalty"));
417 pl->set_property ("page-turn-penalty", right_bound->get_property ("page-turn-penalty"));
419 if (!scm_is_pair (pl->get_property ("refpoint-Y-extent")))
421 Interval staff_refpoints;
422 staff_refpoints.set_empty ();
423 extract_grob_set (this, "spaceable-staves", staves);
424 for (vsize i = 0; i < staves.size (); i++)
427 staff_refpoints.add_point (g->relative_coordinate (this, Y_AXIS));
429 pl->set_property ("refpoint-Y-extent", ly_interval2scm (staff_refpoints));
432 pl->set_property ("system-grob", this->self_scm ());
434 return pl->unprotect ();
438 System::broken_col_range (Item const *left, Item const *right) const
442 left = left->get_column ();
443 right = right->get_column ();
446 extract_grob_set (this, "columns", cols);
448 vsize i = binary_search (cols, (Grob *) left,
449 Paper_column::less_than);
451 int end_rank = Paper_column::get_rank (right);
452 if (i < cols.size ())
455 while (i < cols.size ()
456 && Paper_column::get_rank (cols[i]) < end_rank)
458 Paper_column *c = dynamic_cast<Paper_column *> (cols[i]);
459 if (Paper_column::is_breakable (c) && !c->system_)
468 /** Return all columns, but filter out any unused columns , since they might
469 disrupt the spacing problem. */
471 System::columns () const
473 extract_grob_set (this, "columns", ro_columns);
475 int last_breakable = ro_columns.size ();
477 while (last_breakable--)
479 if (Paper_column::is_breakable (ro_columns [last_breakable]))
483 vector<Grob*> columns;
484 for (int i = 0; i <= last_breakable; i++)
486 if (Paper_column::is_used (ro_columns[i]))
487 columns.push_back (ro_columns[i]);
494 System::paper_score () const
500 System::get_rank () const
506 get_root_system (Grob *me)
508 Grob *system_grob = me;
510 while (system_grob->get_parent (Y_AXIS))
511 system_grob = system_grob->get_parent (Y_AXIS);
513 return dynamic_cast<System*> (system_grob);
517 System::build_skylines ()
520 for (vsize i = 0; i < all_elements_->size (); i++)
522 Grob *g = all_elements_->grob (i);
523 if (!unsmob_stencil (g->get_property ("stencil")))
526 Interval xiv = g->extent (this, X_AXIS);
527 Interval yiv = g->extent (this, Y_AXIS);
528 if (!xiv.is_empty () && !yiv.is_empty ())
529 boxes.push_back (Box (xiv, yiv));
532 SCM horizon_padding_scm = get_property ("skyline-horizontal-padding");
533 Real horizon_padding = robust_scm2double (horizon_padding_scm, 0);
534 skylines_[UP] = Skyline (boxes, horizon_padding, X_AXIS, UP);
535 skylines_[DOWN] = Skyline (boxes, horizon_padding, X_AXIS, DOWN);
539 ADD_INTERFACE (System,
540 "This is the toplevel object: each object in a score "
541 "ultimately has a System object as its X and Y parent. ",
549 "skyline-horizontal-padding "