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>
14 #include "align-interface.hh"
15 #include "axis-group-interface.hh"
18 #include "paper-column.hh"
19 #include "output-def.hh"
20 #include "paper-score.hh"
22 #include "all-font-metrics.hh"
23 #include "spacing-interface.hh"
24 #include "staff-symbol-referencer.hh"
25 #include "paper-book.hh"
26 #include "paper-system.hh"
27 #include "tweak-registration.hh"
28 #include "grob-array.hh"
29 #include "pointer-group-interface.hh"
31 System::System (System const &src, int count)
32 : Spanner (src, count)
39 System::System (SCM s, Object_key const *key)
48 System::init_elements ()
50 SCM scm_arr = Grob_array::make_array ();
51 all_elements_ = unsmob_grob_array (scm_arr);
52 set_object ("all-elements", scm_arr);
56 System::clone (int count) const
58 return new System (*this, count);
62 System::element_count () const
64 return all_elements_->size ();
68 System::spanner_count () const
71 for (int 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->pscore_ = pscore_;
85 all_elements_->add (elem);
91 System::derived_mark () const
93 if (!all_elements_->is_empty ())
95 Grob **ptr = &all_elements_->array_reference ().elem_ref (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 (Link_array<Grob> const &grobs)
113 for (int i = grobs.size (); i--;)
114 grobs[i]->fixup_refpoint ();
118 System::get_paper_systems ()
120 for (int 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_breakable (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 (int i = 0; i < broken_intos_.size (); i++)
146 Grob *se = broken_intos_[i];
148 extract_grob_set (se, "all-elements", all_elts);
149 for (int 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 (int i = 0; i < all_elements_->size (); i++)
164 all_elements_->grob (i)->handle_broken_dependencies ();
166 handle_broken_dependencies ();
168 #if 0 /* FIXME: strange side effects. */
170 /* Because the this->get_property (all-elements) contains items in 3
171 versions, handle_broken_dependencies () will leave duplicated
172 items in all-elements. Strictly speaking this is harmless, but
173 it leads to duplicated symbols in the output. ly_list_qsort_uniq_x ()
174 makes sure that no duplicates are in the list. */
175 for (int i = 0; i < line_count; i++)
177 SCM all = broken_intos_[i]->get_object ("all-elements");
178 all = ly_list_qsort_uniq_x (all);
182 if (be_verbose_global)
183 message (_f ("Element count %d.", count + element_count ()));
185 int line_count = broken_intos_.size ();
186 SCM lines = scm_c_make_vector (line_count, SCM_EOL);
188 for (int i = 0; i < line_count; i++)
190 if (be_verbose_global)
191 progress_indication ("[");
193 System *system = dynamic_cast<System *> (broken_intos_[i]);
194 system->post_processing ();
195 scm_vector_set_x (lines, scm_from_int (i), system->get_paper_system ());
197 if (be_verbose_global)
198 progress_indication (to_string (i) + "]");
204 System::break_into_pieces (Array<Column_x_positions> const &breaking)
206 for (int i = 0; i < breaking.size (); i++)
208 System *system = dynamic_cast<System *> (clone (i));
211 Link_array<Grob> c (breaking[i].cols_);
212 pscore_->typeset_system (system);
214 system->set_bound (LEFT, c[0]);
215 system->set_bound (RIGHT, c.top ());
216 for (int j = 0; j < c.size (); j++)
218 c[j]->translate_axis (breaking[i].config_[j], X_AXIS);
219 dynamic_cast<Paper_column *> (c[j])->system_ = system;
221 set_loose_columns (system, &breaking[i]);
222 broken_intos_.push (system);
227 System::add_column (Paper_column *p)
230 Grob_array *ga = unsmob_grob_array (me->get_object ("columns"));
233 SCM scm_ga = Grob_array::make_array ();
234 me->set_object ("columns", scm_ga);
235 ga = unsmob_grob_array (scm_ga);
240 ? Paper_column::get_rank (ga->array ().top ()) + 1
244 Axis_group_interface::add_element (this, p);
248 apply_tweaks (Grob *g, bool broken)
250 if (bool (g->original_) == broken)
252 SCM tweaks = global_registry_->get_tweaks (g);
253 for (SCM s = tweaks; scm_is_pair (s); s = scm_cdr (s))
255 SCM proc = scm_caar (s);
256 SCM rest = scm_cdar (s);
257 scm_apply_1 (proc, g->self_scm (), rest);
263 System::pre_processing ()
265 for (int i = 0; i < all_elements_->size (); i++)
266 all_elements_->grob (i)->discretionary_processing ();
268 if (be_verbose_global)
269 message (_f ("Grob count %d", element_count ()));
272 order is significant: broken grobs are added to the end of the
273 array, and should be processed before the original is potentially
276 for (int i = all_elements_->size (); i--;)
277 all_elements_->grob (i)->handle_prebroken_dependencies ();
279 fixup_refpoints (all_elements_->array ());
281 for (int i = 0; i < all_elements_->size (); i++)
282 apply_tweaks (all_elements_->grob (i), false);
284 for (int i = 0; i < all_elements_->size (); i++)
285 all_elements_->grob (i)->calculate_dependencies (PRECALCED, PRECALCING,
286 ly_symbol2scm ("before-line-breaking-callback"));
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 SCM proc = e->get_property ("spacing-procedure");
295 if (ly_is_procedure (proc))
296 scm_call_1 (proc, e->self_scm ());
301 System::post_processing ()
303 for (int i = 0; i < all_elements_->size (); i++)
305 Grob *g = all_elements_->grob (i);
307 apply_tweaks (g, true);
308 g->calculate_dependencies (POSTCALCED, POSTCALCING,
309 ly_symbol2scm ("after-line-breaking-callback"));
312 Interval iv (extent (this, Y_AXIS));
314 programming_error ("system with empty extent");
316 translate_axis (-iv[MAX], Y_AXIS);
318 /* Generate all stencils to trigger font loads.
319 This might seem inefficient, but Stencils are cached per grob
322 Link_array<Grob> all_elts_sorted (all_elements_->array ());
323 all_elts_sorted.default_sort ();
324 all_elts_sorted.uniq ();
325 this->get_stencil ();
326 for (int i = all_elts_sorted.size (); i--;)
328 Grob *g = all_elts_sorted[i];
334 System::get_paper_system ()
336 static int const LAYER_COUNT = 3;
341 /* Output stencils in three layers: 0, 1, 2. Default layer: 1. */
342 for (int i = 0; i < LAYER_COUNT; i++)
343 for (int j = all_elements_->size (); j--;)
345 Grob *g = all_elements_->grob (j);
346 Stencil *stil = g->get_stencil ();
348 /* Skip empty stencils and grobs that are not in this layer. */
350 || robust_scm2int (g->get_property ("layer"), 1) != i)
353 Offset o (g->relative_coordinate (this, X_AXIS),
354 g->relative_coordinate (this, Y_AXIS));
356 Offset extra = robust_scm2offset (g->get_property ("extra-offset"),
358 * Staff_symbol_referencer::staff_space (g);
360 /* Must copy the stencil, for we cannot change the stencil
364 st.translate (o + extra);
366 *tail = scm_cons (st.expr (), SCM_EOL);
367 tail = SCM_CDRLOC (*tail);
370 if (Stencil *me = get_stencil ())
371 exprs = scm_cons (me->expr (), exprs);
373 Interval x (extent (this, X_AXIS));
374 Interval y (extent (this, Y_AXIS));
375 Stencil sys_stencil (Box (x, y),
376 scm_cons (ly_symbol2scm ("combine-stencil"),
379 Interval staff_refpoints;
380 staff_refpoints.set_empty ();
381 extract_grob_set (this, "spaceable-staves", staves);
382 for (int i = staves.size (); i--;)
385 staff_refpoints.add_point (g->relative_coordinate (this, Y_AXIS));
388 Paper_system *pl = new Paper_system (sys_stencil, false);
389 pl->staff_refpoints_ = staff_refpoints;
390 Item *break_point = this->get_bound (LEFT);
391 pl->break_before_penalty_
392 = robust_scm2double (break_point->get_property ("page-penalty"), 0.0);
394 return pl->unprotect ();
398 System::broken_col_range (Item const *left, Item const *right) const
400 Link_array<Item> ret;
402 left = left->get_column ();
403 right = right->get_column ();
405 extract_grob_set (this, "columns", cols);
407 while (i < cols.size ()
411 if (i < cols.size ())
414 while (i < cols.size ()
417 Paper_column *c = dynamic_cast<Paper_column *> (cols[i]);
418 if (Item::is_breakable (c) && !c->system_)
426 /** Return all columns, but filter out any unused columns , since they might
427 disrupt the spacing problem. */
429 System::columns () const
431 extract_grob_set (this, "columns", ro_columns);
433 int last_breakable = ro_columns.size ();
435 while (last_breakable--)
437 if (Item::is_breakable (ro_columns [last_breakable]))
441 Link_array<Grob> columns;
442 for (int i = 0; i <= last_breakable; i++)
444 if (Paper_column::is_used (ro_columns[i]))
445 columns.push (ro_columns[i]);
452 System::get_rank () const
457 ADD_INTERFACE (System, "system-interface",
458 "This is the toplevel object: each object in a score "
459 "ultimately has a System object as its X and Y parent. ",
460 "all-elements spaceable-staves columns")