2 system.cc -- implement System
4 source file of the GNU LilyPond music typesetter
6 (c) 1996--2005 Han-Wen Nienhuys <hanwen@cs.uu.nl>
12 #include "align-interface.hh"
13 #include "axis-group-interface.hh"
16 #include "paper-column.hh"
17 #include "output-def.hh"
18 #include "paper-score.hh"
20 #include "all-font-metrics.hh"
21 #include "spacing-interface.hh"
22 #include "staff-symbol-referencer.hh"
23 #include "paper-book.hh"
24 #include "paper-system.hh"
25 #include "tweak-registration.hh"
26 #include "grob-array.hh"
27 #include "pointer-group-interface.hh"
29 System::System (System const &src, int count)
30 : 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 set_object ("all-elements", scm_arr);
54 System::clone (int count) const
56 return new System (*this, count);
60 System::element_count () const
62 return all_elements_->size ();
66 System::spanner_count () const
69 for (int 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->pscore_ = pscore_;
83 all_elements_->add (elem);
89 System::derived_mark () const
91 if (!all_elements_->is_empty ())
93 Grob **ptr = &all_elements_->array_reference ().elem_ref (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 (Link_array<Grob> const &grobs)
111 for (int i = grobs.size (); i--;)
112 grobs[i]->fixup_refpoint ();
116 System::get_paper_systems ()
118 for (int 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_breakable (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 (int i = 0; i < broken_intos_.size (); i++)
144 Grob *se = broken_intos_[i];
146 extract_grob_set (se, "all-elements", all_elts);
147 for (int 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 (int i = 0; i < all_elements_->size (); i++)
162 all_elements_->grob (i)->handle_broken_dependencies ();
164 handle_broken_dependencies ();
166 #if 0 /* FIXME: strange side effects. */
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. ly_list_qsort_uniq_x ()
172 makes sure that no duplicates are in the list. */
173 for (int i = 0; i < line_count; i++)
175 SCM all = broken_intos_[i]->get_object ("all-elements");
176 all = ly_list_qsort_uniq_x (all);
180 if (be_verbose_global)
181 message (_f ("Element count %d.", count + element_count ()));
183 int line_count = broken_intos_.size ();
184 SCM lines = scm_c_make_vector (line_count, SCM_EOL);
186 for (int i = 0; i < line_count; i++)
188 if (be_verbose_global)
189 progress_indication ("[");
191 System *system = dynamic_cast<System *> (broken_intos_[i]);
192 system->post_processing ();
193 scm_vector_set_x (lines, scm_from_int (i), system->get_paper_system ());
195 if (be_verbose_global)
196 progress_indication (to_string (i) + "]");
202 System::break_into_pieces (Array<Column_x_positions> const &breaking)
204 for (int i = 0; i < breaking.size (); i++)
206 System *system = dynamic_cast<System *> (clone (i));
209 Link_array<Grob> c (breaking[i].cols_);
210 pscore_->typeset_system (system);
212 system->set_bound (LEFT, c[0]);
213 system->set_bound (RIGHT, c.top ());
214 for (int j = 0; j < c.size (); j++)
216 c[j]->translate_axis (breaking[i].config_[j], X_AXIS);
217 dynamic_cast<Paper_column *> (c[j])->system_ = system;
219 set_loose_columns (system, &breaking[i]);
220 broken_intos_.push (system);
225 System::add_column (Paper_column *p)
228 Grob_array *ga = unsmob_grob_array (me->get_object ("columns"));
231 SCM scm_ga = Grob_array::make_array ();
232 me->set_object ("columns", scm_ga);
233 ga = unsmob_grob_array (scm_ga);
238 ? Paper_column::get_rank (ga->array ().top ()) + 1
242 Axis_group_interface::add_element (this, p);
246 apply_tweaks (Grob *g, bool broken)
248 if (bool (g->original_) == broken)
250 SCM tweaks = global_registry_->get_tweaks (g);
251 for (SCM s = tweaks; scm_is_pair (s); s = scm_cdr (s))
253 SCM proc = scm_caar (s);
254 SCM rest = scm_cdar (s);
255 scm_apply_1 (proc, g->self_scm (), rest);
261 System::pre_processing ()
263 for (int i = 0; i < all_elements_->size (); i++)
264 all_elements_->grob (i)->discretionary_processing ();
266 if (be_verbose_global)
267 message (_f ("Grob count %d", element_count ()));
270 order is significant: broken grobs are added to the end of the
271 array, and should be processed before the original is potentially
274 for (int i = all_elements_->size (); i--;)
275 all_elements_->grob (i)->handle_prebroken_dependencies ();
277 fixup_refpoints (all_elements_->array ());
279 for (int i = 0; i < all_elements_->size (); i++)
280 apply_tweaks (all_elements_->grob (i), false);
282 for (int i = 0; i < all_elements_->size (); i++)
284 Grob *g = all_elements_->grob (i);
285 (void) g->get_property ("before-line-breaking");
288 message (_ ("Calculating line breaks..."));
289 progress_indication (" ");
291 for (int i = 0; i < all_elements_->size (); i++)
293 Grob *e = all_elements_->grob (i);
294 (void) e->get_property ("springs-and-rods");
299 System::post_processing ()
301 for (int i = 0; i < all_elements_->size (); i++)
303 Grob *g = all_elements_->grob (i);
305 apply_tweaks (g, true);
306 (void) g->get_property ("after-line-breaking");
309 Interval iv (extent (this, Y_AXIS));
311 programming_error ("system with empty extent");
313 translate_axis (-iv[MAX], Y_AXIS);
315 /* Generate all stencils to trigger font loads.
316 This might seem inefficient, but Stencils are cached per grob
319 Link_array<Grob> all_elts_sorted (all_elements_->array ());
320 all_elts_sorted.default_sort ();
321 all_elts_sorted.uniq ();
322 this->get_stencil ();
323 for (int i = all_elts_sorted.size (); i--;)
325 Grob *g = all_elts_sorted[i];
331 System::get_paper_system ()
333 static int const LAYER_COUNT = 3;
338 /* Output stencils in three layers: 0, 1, 2. Default layer: 1. */
339 for (int i = 0; i < LAYER_COUNT; i++)
340 for (int j = all_elements_->size (); j--;)
342 Grob *g = all_elements_->grob (j);
343 Stencil st = g->get_print_stencil ();
345 /* Skip empty stencils and grobs that are not in this layer. */
346 if (st.expr() == SCM_EOL
347 || robust_scm2int (g->get_property ("layer"), 1) != i)
351 for (int a = X_AXIS; a < NO_AXES; a++)
352 o[Axis (a)] = g->relative_coordinate (this, Axis (a));
354 Offset extra = robust_scm2offset (g->get_property ("extra-offset"),
356 * Staff_symbol_referencer::staff_space (g);
358 /* Must copy the stencil, for we cannot change the stencil
361 st.translate (o + extra);
363 *tail = scm_cons (st.expr (), SCM_EOL);
364 tail = SCM_CDRLOC (*tail);
367 if (Stencil *me = get_stencil ())
368 exprs = scm_cons (me->expr (), exprs);
370 Interval x (extent (this, X_AXIS));
371 Interval y (extent (this, Y_AXIS));
372 Stencil sys_stencil (Box (x, y),
373 scm_cons (ly_symbol2scm ("combine-stencil"),
378 Grob *left_bound = this->get_bound (LEFT);
379 SCM prop_init = left_bound->get_property ("line-break-system-details");
380 Paper_system *pl = new Paper_system (sys_stencil,
382 pl->set_property ("penalty",
383 left_bound->get_property ("page-penalty"));
385 if (!scm_is_pair (pl->get_property ("refpoint-Y-extent")))
387 Interval staff_refpoints;
388 staff_refpoints.set_empty ();
389 extract_grob_set (this, "spaceable-staves", staves);
390 for (int i = staves.size (); i--;)
393 staff_refpoints.add_point (g->relative_coordinate (this, Y_AXIS));
395 pl->set_property ("refpoint-Y-extent", ly_interval2scm (staff_refpoints));
398 return pl->unprotect ();
402 System::broken_col_range (Item const *left, Item const *right) const
404 Link_array<Item> ret;
406 left = left->get_column ();
407 right = right->get_column ();
409 extract_grob_set (this, "columns", cols);
411 while (i < cols.size ()
415 if (i < cols.size ())
418 while (i < cols.size ()
421 Paper_column *c = dynamic_cast<Paper_column *> (cols[i]);
422 if (Item::is_breakable (c) && !c->system_)
430 /** Return all columns, but filter out any unused columns , since they might
431 disrupt the spacing problem. */
433 System::columns () const
435 extract_grob_set (this, "columns", ro_columns);
437 int last_breakable = ro_columns.size ();
439 while (last_breakable--)
441 if (Item::is_breakable (ro_columns [last_breakable]))
445 Link_array<Grob> columns;
446 for (int i = 0; i <= last_breakable; i++)
448 if (Paper_column::is_used (ro_columns[i]))
449 columns.push (ro_columns[i]);
456 System::get_rank () const
461 ADD_INTERFACE (System, "system-interface",
462 "This is the toplevel object: each object in a score "
463 "ultimately has a System object as its X and Y parent. ",
464 "all-elements spaceable-staves columns")