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>
13 #include "align-interface.hh"
14 #include "axis-group-interface.hh"
17 #include "paper-column.hh"
18 #include "output-def.hh"
19 #include "paper-score.hh"
21 #include "all-font-metrics.hh"
22 #include "spacing-interface.hh"
23 #include "staff-symbol-referencer.hh"
24 #include "paper-book.hh"
25 #include "paper-system.hh"
26 #include "tweak-registration.hh"
27 #include "grob-array.hh"
28 #include "pointer-group-interface.hh"
30 System::System (System const &src, int count)
31 : 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 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--; )
115 grobs[i]->fixup_refpoint ();
120 System::get_paper_systems ()
122 for (int i = 0; i < all_elements_->size(); i++)
124 Grob *g = all_elements_->grob (i);
125 if (g->internal_has_interface (ly_symbol2scm ("only-prebreak-interface")))
128 Kill no longer needed grobs.
130 Item *it = dynamic_cast<Item *> (g);
131 if (it && Item::is_breakable (it))
133 it->find_prebroken_piece (LEFT)->suicide ();
134 it->find_prebroken_piece (RIGHT)->suicide ();
138 else if (g->is_live ())
139 g->do_break_processing ();
143 fixups must be done in broken line_of_scores, because new elements
144 are put over there. */
146 for (int i = 0; i < broken_intos_.size (); i++)
148 Grob *se = broken_intos_[i];
150 extract_grob_set (se, "all-elements", all_elts);
151 for (int j = 0; j < all_elts.size(); j++)
153 Grob *g = all_elts[j];
154 g->fixup_refpoint ();
157 count += all_elts.size ();
161 needed for doing items.
163 fixup_refpoints (all_elements_->array ());
165 for (int i = 0 ; i < all_elements_->size(); i++)
166 all_elements_->grob (i)->handle_broken_dependencies ();
168 handle_broken_dependencies ();
170 #if 0 /* FIXME: strange side effects. */
172 /* Because the this->get_property (all-elements) contains items in 3
173 versions, handle_broken_dependencies () will leave duplicated
174 items in all-elements. Strictly speaking this is harmless, but
175 it leads to duplicated symbols in the output. ly_list_qsort_uniq_x ()
176 makes sure that no duplicates are in the list. */
177 for (int i = 0; i < line_count; i++)
179 SCM all = broken_intos_[i]->get_object ("all-elements");
180 all = ly_list_qsort_uniq_x (all);
184 if (be_verbose_global)
185 message (_f ("Element count %d.", count + element_count ()));
187 int line_count = broken_intos_.size ();
188 SCM lines = scm_c_make_vector (line_count, SCM_EOL);
190 for (int i = 0; i < line_count; i++)
192 if (be_verbose_global)
193 progress_indication ("[");
195 System *system = dynamic_cast<System *> (broken_intos_[i]);
196 system->post_processing ();
197 scm_vector_set_x (lines, scm_from_int (i), system->get_paper_system ());
199 if (be_verbose_global)
200 progress_indication (to_string (i) + "]");
206 System::break_into_pieces (Array<Column_x_positions> const &breaking)
208 for (int i = 0; i < breaking.size (); i++)
210 System *system = dynamic_cast<System *> (clone (i));
213 Link_array<Grob> c (breaking[i].cols_);
214 pscore_->typeset_system (system);
216 system->set_bound (LEFT, c[0]);
217 system->set_bound (RIGHT, c.top ());
218 for (int j = 0; j < c.size (); j++)
220 c[j]->translate_axis (breaking[i].config_[j], X_AXIS);
221 dynamic_cast<Paper_column *> (c[j])->system_ = system;
223 set_loose_columns (system, &breaking[i]);
224 broken_intos_.push (system);
229 System::add_column (Paper_column *p)
232 Grob_array *ga = unsmob_grob_array (me->get_object ("columns"));
235 SCM scm_ga = Grob_array::make_array ();
236 me->set_object ("columns", scm_ga);
237 ga = unsmob_grob_array (scm_ga);
242 ? Paper_column::get_rank (ga->array ().top ()) + 1
246 Axis_group_interface::add_element (this, p);
250 apply_tweaks (Grob *g, bool broken)
252 if (bool (g->original_) == broken)
254 SCM tweaks = global_registry_->get_tweaks (g);
255 for (SCM s = tweaks; scm_is_pair (s); s = scm_cdr (s))
257 SCM proc = scm_caar (s);
258 SCM rest = scm_cdar (s);
259 scm_apply_1 (proc, g->self_scm (), rest);
265 System::pre_processing ()
267 for (int i = 0 ; i < all_elements_->size(); i ++)
268 all_elements_->grob (i)->discretionary_processing ();
271 if (be_verbose_global)
272 message (_f ("Grob count %d", element_count ()));
275 order is significant: broken grobs are added to the end of the
276 array, and should be processed before the original is potentially
279 for (int i = all_elements_->size(); i --; )
280 all_elements_->grob (i)->handle_prebroken_dependencies ();
282 fixup_refpoints (all_elements_->array ());
284 for (int i = 0 ; i < all_elements_->size(); i ++)
285 apply_tweaks (all_elements_->grob (i), false);
287 for (int i = 0 ; i < all_elements_->size(); i ++)
288 all_elements_->grob (i)->calculate_dependencies (PRECALCED, PRECALCING,
289 ly_symbol2scm ("before-line-breaking-callback"));
291 message (_ ("Calculating line breaks..."));
292 progress_indication (" ");
294 for (int i = 0 ; i < all_elements_->size(); i ++)
296 Grob *e = all_elements_->grob (i);
297 SCM proc = e->get_property ("spacing-procedure");
298 if (ly_is_procedure (proc))
299 scm_call_1 (proc, e->self_scm ());
304 System::post_processing ()
306 for (int i = 0 ; i < all_elements_->size(); i ++)
308 Grob *g = all_elements_->grob (i);
310 apply_tweaks (g, true);
311 g->calculate_dependencies (POSTCALCED, POSTCALCING,
312 ly_symbol2scm ("after-line-breaking-callback"));
315 Interval iv (extent (this, Y_AXIS));
318 programming_error ("system with empty extent");
321 translate_axis (-iv[MAX], Y_AXIS);
323 /* Generate all stencils to trigger font loads.
324 This might seem inefficient, but Stencils are cached per grob
328 Link_array<Grob> all_elts_sorted (all_elements_->array ());
329 all_elts_sorted.default_sort ();
330 all_elts_sorted.uniq ();
331 this->get_stencil ();
332 for (int i = all_elts_sorted.size (); i--;)
334 Grob *g = all_elts_sorted[i];
340 System::get_paper_system ()
342 static int const LAYER_COUNT = 3;
347 /* Output stencils in three layers: 0, 1, 2. Default layer: 1. */
348 for (int i = 0; i < LAYER_COUNT; i++)
349 for (int j = all_elements_->size (); j --;)
351 Grob *g = all_elements_->grob (j);
352 Stencil *stil = g->get_stencil ();
354 /* Skip empty stencils and grobs that are not in this layer. */
356 || robust_scm2int (g->get_property ("layer"), 1) != i)
359 Offset o (g->relative_coordinate (this, X_AXIS),
360 g->relative_coordinate (this, Y_AXIS));
362 Offset extra = robust_scm2offset (g->get_property ("extra-offset"),
364 * Staff_symbol_referencer::staff_space (g);
366 /* Must copy the stencil, for we cannot change the stencil
370 st.translate (o + extra);
372 *tail = scm_cons (st.expr (), SCM_EOL);
373 tail = SCM_CDRLOC (*tail);
376 if (Stencil *me = get_stencil ())
377 exprs = scm_cons (me->expr (), exprs);
379 Interval x (extent (this, X_AXIS));
380 Interval y (extent (this, Y_AXIS));
381 Stencil sys_stencil (Box (x, y),
382 scm_cons (ly_symbol2scm ("combine-stencil"),
385 Interval staff_refpoints;
386 staff_refpoints.set_empty ();
387 extract_grob_set (this, "spaceable-staves", staves);
388 for (int i = staves.size (); i--; )
391 staff_refpoints.add_point (g->relative_coordinate (this, Y_AXIS));
394 Paper_system *pl = new Paper_system (sys_stencil, false);
395 pl->staff_refpoints_ = staff_refpoints;
396 Item *break_point = this->get_bound (LEFT);
397 pl->break_before_penalty_
398 = robust_scm2double (break_point->get_property ("page-penalty"), 0.0);
400 return pl->unprotect ();
404 System::broken_col_range (Item const *left, Item const *right) const
406 Link_array<Item> ret;
408 left = left->get_column ();
409 right = right->get_column ();
411 extract_grob_set (this, "columns", cols);
413 while (i < cols.size()
420 while (i < cols.size()
423 Paper_column *c = dynamic_cast<Paper_column *> (cols[i]);
424 if (Item::is_breakable (c) && !c->system_)
432 /** Return all columns, but filter out any unused columns , since they might
433 disrupt the spacing problem. */
435 System::columns () const
437 extract_grob_set (this, "columns", ro_columns);
439 int last_breakable = ro_columns.size ();
441 while (last_breakable --)
443 if (Item::is_breakable (ro_columns [last_breakable]))
447 Link_array<Grob> columns;
448 for (int i = 0; i <= last_breakable; i++)
450 if (Paper_column::is_used (ro_columns[i]))
451 columns.push (ro_columns[i]);
458 System::get_rank () const
463 ADD_INTERFACE (System, "system-interface",
464 "This is the toplevel object: each object in a score "
465 "ultimately has a System object as its X and Y parent. ",
466 "all-elements spaceable-staves columns")