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"
28 System::System (System const &src, int count)
29 : Spanner (src, count)
37 System::System (SCM s, Object_key const *key)
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 (int index) const
57 return new System (*this, index);
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::get_paper_systems ()
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 ()));
181 SCM lines = scm_c_make_vector (broken_intos_.size (), SCM_EOL);
182 for (vsize i = 0; i < broken_intos_.size (); i++)
184 if (be_verbose_global)
185 progress_indication ("[");
187 System *system = dynamic_cast<System *> (broken_intos_[i]);
188 system->post_processing ();
189 scm_vector_set_x (lines, scm_from_int (i),
190 system->get_paper_system ());
192 if (be_verbose_global)
193 progress_indication (to_string (i) + "]");
199 System::break_into_pieces (vector<Column_x_positions> const &breaking)
201 for (vsize i = 0; i < breaking.size (); i++)
203 System *system = dynamic_cast<System *> (clone (broken_intos_.size ()));
204 system->rank_ = broken_intos_.size ();
206 vector<Grob*> c (breaking[i].cols_);
207 pscore_->typeset_system (system);
209 system->set_bound (LEFT, c[0]);
210 system->set_bound (RIGHT, c.back ());
211 for (vsize j = 0; j < c.size (); j++)
213 c[j]->translate_axis (breaking[i].config_[j], X_AXIS);
214 dynamic_cast<Paper_column *> (c[j])->system_ = system;
216 set_loose_columns (system, &breaking[i]);
217 broken_intos_.push_back (system);
222 System::add_column (Paper_column *p)
225 Grob_array *ga = unsmob_grob_array (me->get_object ("columns"));
228 SCM scm_ga = Grob_array::make_array ();
229 me->set_object ("columns", scm_ga);
230 ga = unsmob_grob_array (scm_ga);
235 ? Paper_column::get_rank (ga->array ().back ()) + 1
239 Axis_group_interface::add_element (this, p);
243 apply_tweaks (Grob *g, bool broken)
245 if (bool (g->original ()) == broken)
247 SCM tweaks = global_registry_->get_tweaks (g);
248 for (SCM s = tweaks; scm_is_pair (s); s = scm_cdr (s))
250 SCM proc = scm_caar (s);
251 SCM rest = scm_cdar (s);
252 scm_apply_1 (proc, g->self_scm (), rest);
258 System::pre_processing ()
260 for (vsize i = 0; i < all_elements_->size (); i++)
261 all_elements_->grob (i)->discretionary_processing ();
263 if (be_verbose_global)
264 message (_f ("Grob count %d", element_count ()));
267 order is significant: broken grobs are added to the end of the
268 array, and should be processed before the original is potentially
271 for (vsize i = all_elements_->size (); i--;)
272 all_elements_->grob (i)->handle_prebroken_dependencies ();
274 fixup_refpoints (all_elements_->array ());
276 for (vsize i = 0; i < all_elements_->size (); i++)
277 apply_tweaks (all_elements_->grob (i), false);
279 for (vsize i = 0; i < all_elements_->size (); i++)
281 Grob *g = all_elements_->grob (i);
282 (void) g->get_property ("before-line-breaking");
285 for (vsize i = 0; i < all_elements_->size (); i++)
287 Grob *e = all_elements_->grob (i);
288 (void) e->get_property ("springs-and-rods");
293 System::post_processing ()
295 for (vsize i = 0; i < all_elements_->size (); i++)
297 Grob *g = all_elements_->grob (i);
299 apply_tweaks (g, true);
300 (void) g->get_property ("after-line-breaking");
303 Interval iv (extent (this, Y_AXIS));
305 programming_error ("system with empty extent");
307 translate_axis (-iv[MAX], Y_AXIS);
309 /* Generate all stencils to trigger font loads.
310 This might seem inefficient, but Stencils are cached per grob
313 vector<Grob*> all_elts_sorted (all_elements_->array ());
314 vector_sort (all_elts_sorted, default_compare);
315 uniq (all_elts_sorted);
316 this->get_stencil ();
317 for (vsize i = all_elts_sorted.size (); i--;)
319 Grob *g = all_elts_sorted[i];
331 operator< (Layer_entry const &a,
332 Layer_entry const &b)
334 return a.layer_ < b.layer_;
339 System::get_paper_system ()
344 vector<Layer_entry> entries;
345 for (vsize j = 0; j < all_elements_->size (); j++)
348 e.grob_ = all_elements_->grob (j);
349 e.layer_ = robust_scm2int (e.grob_->get_property ("layer"), 1);
351 entries.push_back (e);
354 vector_sort (entries, default_compare);
355 for (vsize j = 0; j < entries.size (); j++)
357 Grob *g = entries[j].grob_;
358 Stencil st = g->get_print_stencil ();
360 if (st.expr() == SCM_EOL)
364 for (int a = X_AXIS; a < NO_AXES; a++)
365 o[Axis (a)] = g->relative_coordinate (this, Axis (a));
367 Offset extra = robust_scm2offset (g->get_property ("extra-offset"),
369 * Staff_symbol_referencer::staff_space (g);
371 /* Must copy the stencil, for we cannot change the stencil
374 st.translate (o + extra);
376 *tail = scm_cons (st.expr (), SCM_EOL);
377 tail = SCM_CDRLOC (*tail);
380 if (Stencil *me = get_stencil ())
381 exprs = scm_cons (me->expr (), exprs);
383 Interval x (extent (this, X_AXIS));
384 Interval y (extent (this, Y_AXIS));
385 Stencil sys_stencil (Box (x, y),
386 scm_cons (ly_symbol2scm ("combine-stencil"),
389 Grob *left_bound = this->get_bound (LEFT);
390 SCM prop_init = left_bound->get_property ("line-break-system-details");
391 Prob *pl = make_paper_system (prop_init);
392 paper_system_set_stencil (pl, sys_stencil);
394 /* backwards-compatibility hack for the old page-breaker */
395 SCM turn_perm = left_bound->get_property ("page-break-permission");
396 if (!scm_is_symbol (turn_perm))
397 pl->set_property ("penalty", scm_from_double (10001.0));
398 else if (turn_perm == ly_symbol2scm ("force"))
399 pl->set_property ("penalty", scm_from_double (-10001.0));
401 pl->set_property ("penalty", scm_from_double (0.0));
403 if (!scm_is_pair (pl->get_property ("refpoint-Y-extent")))
405 Interval staff_refpoints;
406 staff_refpoints.set_empty ();
407 extract_grob_set (this, "spaceable-staves", staves);
408 for (vsize i = 0; i < staves.size (); i++)
411 staff_refpoints.add_point (g->relative_coordinate (this, Y_AXIS));
413 pl->set_property ("refpoint-Y-extent", ly_interval2scm (staff_refpoints));
416 pl->set_property ("system-grob", this->self_scm ());
418 return pl->unprotect ();
422 System::broken_col_range (Item const *left, Item const *right) const
426 left = left->get_column ();
427 right = right->get_column ();
429 extract_grob_set (this, "columns", cols);
431 while (i < cols.size ()
435 if (i < cols.size ())
438 while (i < cols.size ()
441 Paper_column *c = dynamic_cast<Paper_column *> (cols[i]);
442 if (Paper_column::is_breakable (c) && !c->system_)
450 /** Return all columns, but filter out any unused columns , since they might
451 disrupt the spacing problem. */
453 System::columns () const
455 extract_grob_set (this, "columns", ro_columns);
457 int last_breakable = ro_columns.size ();
459 while (last_breakable--)
461 if (Paper_column::is_breakable (ro_columns [last_breakable]))
465 vector<Grob*> columns;
466 for (int i = 0; i <= last_breakable; i++)
468 if (Paper_column::is_used (ro_columns[i]))
469 columns.push_back (ro_columns[i]);
476 System::paper_score () const
482 System::get_rank () const
488 get_root_system (Grob *me)
490 Grob *system_grob = me;
492 while (system_grob->get_parent (Y_AXIS))
493 system_grob = system_grob->get_parent (Y_AXIS);
495 return dynamic_cast<System*> (system_grob);
500 ADD_INTERFACE (System, "system-interface",
501 "This is the toplevel object: each object in a score "
502 "ultimately has a System object as its X and Y parent. ",