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 System::System (System const &src, int count)
28 : Spanner (src, count)
36 System::System (SCM s, Object_key const *key)
45 System::init_elements ()
47 SCM scm_arr = Grob_array::make_array ();
48 all_elements_ = unsmob_grob_array (scm_arr);
49 all_elements_->set_ordered (false);
50 set_object ("all-elements", scm_arr);
54 System::clone (int index) const
56 return new System (*this, index);
60 System::element_count () const
62 return all_elements_->size ();
66 System::spanner_count () const
69 for (vsize i = all_elements_->size (); i--;)
70 if (dynamic_cast<Spanner *> (all_elements_->grob (i)))
76 System::typeset_grob (Grob *elem)
79 programming_error ("adding element twice");
82 elem->layout_ = pscore_->layout ();
83 all_elements_->add (elem);
89 System::derived_mark () const
91 if (!all_elements_->empty ())
93 Grob **ptr = &all_elements_->array_reference ()[0];
94 Grob **end = ptr + all_elements_->size ();
97 scm_gc_mark ((*ptr)->self_scm ());
103 scm_gc_mark (pscore_->self_scm ());
105 Spanner::derived_mark ();
109 fixup_refpoints (vector<Grob*> const &grobs)
111 for (vsize i = grobs.size (); i--;)
112 grobs[i]->fixup_refpoint ();
116 System::get_paper_systems ()
118 for (vsize i = 0; i < all_elements_->size (); i++)
120 Grob *g = all_elements_->grob (i);
121 if (g->internal_has_interface (ly_symbol2scm ("only-prebreak-interface")))
124 Kill no longer needed grobs.
126 Item *it = dynamic_cast<Item *> (g);
127 if (it && Item::is_non_musical (it))
129 it->find_prebroken_piece (LEFT)->suicide ();
130 it->find_prebroken_piece (RIGHT)->suicide ();
134 else if (g->is_live ())
135 g->do_break_processing ();
139 fixups must be done in broken line_of_scores, because new elements
140 are put over there. */
142 for (vsize i = 0; i < broken_intos_.size (); i++)
144 Grob *se = broken_intos_[i];
146 extract_grob_set (se, "all-elements", all_elts);
147 for (vsize j = 0; j < all_elts.size (); j++)
149 Grob *g = all_elts[j];
150 g->fixup_refpoint ();
153 count += all_elts.size ();
157 needed for doing items.
159 fixup_refpoints (all_elements_->array ());
161 for (vsize i = 0; i < all_elements_->size (); i++)
162 all_elements_->grob (i)->handle_broken_dependencies ();
164 handle_broken_dependencies ();
166 /* Because the this->get_property (all-elements) contains items in 3
167 versions, handle_broken_dependencies () will leave duplicated
168 items in all-elements. Strictly speaking this is harmless, but
169 it leads to duplicated symbols in the output. uniq makes sure
170 that no duplicates are in the list. */
171 for (vsize i = 0; i < broken_intos_.size (); i++)
173 System *child = dynamic_cast<System*> (broken_intos_[i]);
174 child->all_elements_->remove_duplicates ();
177 if (be_verbose_global)
178 message (_f ("Element count %d.", count + element_count ()));
180 SCM lines = scm_c_make_vector (broken_intos_.size (), SCM_EOL);
181 for (vsize i = 0; i < broken_intos_.size (); i++)
183 if (be_verbose_global)
184 progress_indication ("[");
186 System *system = dynamic_cast<System *> (broken_intos_[i]);
188 system->post_processing ();
189 system->build_skylines ();
192 System *prev = dynamic_cast<System*> (broken_intos_[i-1]);
193 Real r = prev->skylines_[DOWN].distance (system->skylines_[UP]);
194 system->set_property ("skyline-distance", scm_from_double (r));
196 scm_vector_set_x (lines, scm_from_int (i),
197 system->get_paper_system ());
199 if (be_verbose_global)
200 progress_indication (to_string (i) + "]");
206 System::break_into_pieces (vector<Column_x_positions> const &breaking)
208 for (vsize i = 0; i < breaking.size (); i++)
210 System *system = dynamic_cast<System *> (clone (broken_intos_.size ()));
211 system->rank_ = broken_intos_.size ();
213 vector<Grob*> c (breaking[i].cols_);
214 pscore_->typeset_system (system);
216 int st = Paper_column::get_rank (c[0]);
217 int end = Paper_column::get_rank (c.back ());
218 Interval iv (pure_height (this, st, end));
219 system->set_property ("pure-Y-extent", ly_interval2scm (iv));
221 system->set_bound (LEFT, c[0]);
222 system->set_bound (RIGHT, c.back ());
223 for (vsize j = 0; j < c.size (); j++)
225 c[j]->translate_axis (breaking[i].config_[j], X_AXIS);
226 dynamic_cast<Paper_column *> (c[j])->system_ = system;
229 set_loose_columns (system, &breaking[i]);
230 broken_intos_.push_back (system);
235 System::add_column (Paper_column *p)
238 Grob_array *ga = unsmob_grob_array (me->get_object ("columns"));
241 SCM scm_ga = Grob_array::make_array ();
242 me->set_object ("columns", scm_ga);
243 ga = unsmob_grob_array (scm_ga);
248 ? Paper_column::get_rank (ga->array ().back ()) + 1
252 Axis_group_interface::add_element (this, p);
256 apply_tweaks (Grob *g, bool broken)
258 if (bool (g->original ()) == broken)
260 SCM tweaks = global_registry_->get_tweaks (g);
261 for (SCM s = tweaks; scm_is_pair (s); s = scm_cdr (s))
263 SCM proc = scm_caar (s);
264 SCM rest = scm_cdar (s);
265 scm_apply_1 (proc, g->self_scm (), rest);
271 System::pre_processing ()
273 for (vsize i = 0; i < all_elements_->size (); i++)
274 all_elements_->grob (i)->discretionary_processing ();
276 if (be_verbose_global)
277 message (_f ("Grob count %d", element_count ()));
280 order is significant: broken grobs are added to the end of the
281 array, and should be processed before the original is potentially
284 for (vsize i = all_elements_->size (); i--;)
285 all_elements_->grob (i)->handle_prebroken_dependencies ();
287 fixup_refpoints (all_elements_->array ());
289 for (vsize i = 0; i < all_elements_->size (); i++)
290 apply_tweaks (all_elements_->grob (i), false);
292 for (vsize i = 0; i < all_elements_->size (); i++)
294 Grob *g = all_elements_->grob (i);
295 (void) g->get_property ("before-line-breaking");
298 for (vsize i = 0; i < all_elements_->size (); i++)
300 Grob *e = all_elements_->grob (i);
301 (void) e->get_property ("springs-and-rods");
306 System::post_processing ()
308 for (vsize i = 0; i < all_elements_->size (); i++)
310 Grob *g = all_elements_->grob (i);
312 apply_tweaks (g, true);
313 (void) g->get_property ("after-line-breaking");
316 Interval iv (extent (this, Y_AXIS));
318 programming_error ("system with empty extent");
320 translate_axis (-iv[MAX], Y_AXIS);
322 /* Generate all stencils to trigger font loads.
323 This might seem inefficient, but Stencils are cached per grob
326 vector<Grob*> all_elts_sorted (all_elements_->array ());
327 vector_sort (all_elts_sorted, std::less<Grob*> ());
328 uniq (all_elts_sorted);
329 this->get_stencil ();
330 for (vsize i = all_elts_sorted.size (); i--;)
332 Grob *g = all_elts_sorted[i];
344 operator< (Layer_entry const &a,
345 Layer_entry const &b)
347 return a.layer_ < b.layer_;
352 System::get_paper_system ()
357 vector<Layer_entry> entries;
358 for (vsize j = 0; j < all_elements_->size (); j++)
361 e.grob_ = all_elements_->grob (j);
362 e.layer_ = robust_scm2int (e.grob_->get_property ("layer"), 1);
364 entries.push_back (e);
367 vector_sort (entries, std::less<Layer_entry> ());
368 for (vsize j = 0; j < entries.size (); j++)
370 Grob *g = entries[j].grob_;
371 Stencil st = g->get_print_stencil ();
373 if (st.expr() == SCM_EOL)
377 for (int a = X_AXIS; a < NO_AXES; a++)
378 o[Axis (a)] = g->relative_coordinate (this, Axis (a));
380 Offset extra = robust_scm2offset (g->get_property ("extra-offset"),
382 * Staff_symbol_referencer::staff_space (g);
384 /* Must copy the stencil, for we cannot change the stencil
387 st.translate (o + extra);
389 *tail = scm_cons (st.expr (), SCM_EOL);
390 tail = SCM_CDRLOC (*tail);
393 if (Stencil *me = get_stencil ())
394 exprs = scm_cons (me->expr (), exprs);
396 Interval x (extent (this, X_AXIS));
397 Interval y (extent (this, Y_AXIS));
398 Stencil sys_stencil (Box (x, y),
399 scm_cons (ly_symbol2scm ("combine-stencil"),
402 Grob *left_bound = this->get_bound (LEFT);
403 SCM prop_init = left_bound->get_property ("line-break-system-details");
404 Prob *pl = make_paper_system (prop_init);
405 paper_system_set_stencil (pl, sys_stencil);
407 /* information that the page breaker might need */
408 Grob *right_bound = this->get_bound (RIGHT);
409 pl->set_property ("skyline-distance", get_property ("skyline-distance"));
410 pl->set_property ("page-break-permission", right_bound->get_property ("page-break-permission"));
411 pl->set_property ("page-turn-permission", right_bound->get_property ("page-turn-permission"));
412 pl->set_property ("page-break-penalty", right_bound->get_property ("page-break-penalty"));
413 pl->set_property ("page-turn-penalty", right_bound->get_property ("page-turn-penalty"));
415 if (!scm_is_pair (pl->get_property ("refpoint-Y-extent")))
417 Interval staff_refpoints;
418 staff_refpoints.set_empty ();
419 extract_grob_set (this, "spaceable-staves", staves);
420 for (vsize i = 0; i < staves.size (); i++)
423 staff_refpoints.add_point (g->relative_coordinate (this, Y_AXIS));
425 pl->set_property ("refpoint-Y-extent", ly_interval2scm (staff_refpoints));
428 pl->set_property ("system-grob", this->self_scm ());
430 return pl->unprotect ();
434 System::broken_col_range (Item const *left, Item const *right) const
438 left = left->get_column ();
439 right = right->get_column ();
441 extract_grob_set (this, "columns", cols);
443 while (i < cols.size ()
447 if (i < cols.size ())
450 while (i < cols.size ()
453 Paper_column *c = dynamic_cast<Paper_column *> (cols[i]);
454 if (Paper_column::is_breakable (c) && !c->system_)
462 /** Return all columns, but filter out any unused columns , since they might
463 disrupt the spacing problem. */
465 System::columns () const
467 extract_grob_set (this, "columns", ro_columns);
469 int last_breakable = ro_columns.size ();
471 while (last_breakable--)
473 if (Paper_column::is_breakable (ro_columns [last_breakable]))
477 vector<Grob*> columns;
478 for (int i = 0; i <= last_breakable; i++)
480 if (Paper_column::is_used (ro_columns[i]))
481 columns.push_back (ro_columns[i]);
488 System::paper_score () const
494 System::get_rank () const
500 get_root_system (Grob *me)
502 Grob *system_grob = me;
504 while (system_grob->get_parent (Y_AXIS))
505 system_grob = system_grob->get_parent (Y_AXIS);
507 return dynamic_cast<System*> (system_grob);
511 System::build_skylines ()
514 for (vsize i = 0; i < all_elements_->size (); i++)
516 Grob *g = all_elements_->grob (i);
517 if (!unsmob_stencil (g->get_property ("stencil")))
520 Interval xiv = g->extent (this, X_AXIS);
521 Interval yiv = g->extent (this, Y_AXIS);
522 if (!xiv.is_empty () && !yiv.is_empty ())
523 boxes.push_back (Box (xiv, yiv));
526 skylines_[UP] = Skyline (boxes, X_AXIS, UP);
527 skylines_[DOWN] = Skyline (boxes, X_AXIS, DOWN);
531 ADD_INTERFACE (System, "system-interface",
532 "This is the toplevel object: each object in a score "
533 "ultimately has a System object as its X and Y parent. ",