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"
29 System::System (System const &src, int count)
30 : Spanner (src, count)
35 System::System (SCM s, Object_key const*key)
43 System::clone (int count) const
45 return new System (*this, count);
50 System::element_count () const
52 return scm_ilength (get_property ("all-elements"));
56 System::spanner_count () const
59 for (SCM s = get_property ("all-elements"); scm_is_pair (s); s = scm_cdr (s))
60 if (dynamic_cast<Spanner*> (unsmob_grob (scm_car (s))))
66 System::typeset_grob (Grob * elem)
69 programming_error ("Adding element twice.");
72 elem->pscore_ = pscore_;
73 Pointer_group_interface::add_grob (this, ly_symbol2scm ("all-elements"), elem);
74 scm_gc_unprotect_object (elem->self_scm ());
80 fixup_refpoints (SCM s)
82 for (; scm_is_pair (s); s = scm_cdr (s))
84 Grob::fixup_refpoint (scm_car (s));
91 for (SCM s = get_property ("all-elements"); scm_is_pair (s); s = scm_cdr (s))
93 Grob *g = unsmob_grob (scm_car (s));
94 if (g->internal_has_interface (ly_symbol2scm ("only-prebreak-interface")))
97 Kill no longer needed grobs.
99 Item * it = dynamic_cast<Item*> (g);
100 if (it && Item::is_breakable (it))
102 it->find_prebroken_piece (LEFT)->suicide ();
103 it->find_prebroken_piece (RIGHT)->suicide ();
107 else if (g->is_live ())
108 g->do_break_processing ();
112 fixups must be done in broken line_of_scores, because new elements
113 are put over there. */
115 for (int i = 0; i < broken_intos_.size (); i++)
117 Grob *se = broken_intos_[i];
118 SCM all = se->get_property ("all-elements");
119 for (SCM s = all; scm_is_pair (s); s = scm_cdr (s))
120 fixup_refpoint (scm_car (s));
121 count += scm_ilength (all);
125 needed for doing items.
127 fixup_refpoints (get_property ("all-elements"));
129 for (SCM s = get_property ("all-elements"); scm_is_pair (s); s = scm_cdr (s))
130 unsmob_grob (scm_car (s))->handle_broken_dependencies ();
131 handle_broken_dependencies ();
133 #if 0 /* don't do this: strange side effects. */
135 /* Because the this->get_property (all-elements) contains items in 3
136 versions, handle_broken_dependencies () will leave duplicated
137 items in all-elements. Strictly speaking this is harmless, but
138 it leads to duplicated symbols in the output. ly_list_qsort_uniq_x ()
139 makes sure that no duplicates are in the list. */
140 for (int i = 0; i < line_count; i++)
142 SCM all = broken_intos_[i]->get_property ("all-elements");
143 all = ly_list_qsort_uniq_x(all);
147 if (be_verbose_global)
148 progress_indication (_f ("Element count %d.", count + element_count ()));
150 int line_count = broken_intos_.size ();
151 SCM lines = scm_c_make_vector (line_count, SCM_EOL);
153 for (int i = 0; i < line_count; i++)
155 if (be_verbose_global)
156 progress_indication ("[");
158 System *system = dynamic_cast<System*> (broken_intos_[i]);
159 system->post_processing ();
160 scm_vector_set_x (lines, scm_int2num (i), system->get_line ());
162 if (be_verbose_global)
163 progress_indication (to_string (i) + "]");
169 System::break_into_pieces (Array<Column_x_positions> const &breaking)
171 for (int i = 0; i < breaking.size (); i++)
173 System *system = dynamic_cast <System*> (clone (i));
176 Link_array<Grob> c (breaking[i].cols_);
177 pscore_->typeset_line (system);
179 system->set_bound (LEFT, c[0]);
180 system->set_bound (RIGHT, c.top ());
181 for (int j = 0; j < c.size (); j++)
183 c[j]->translate_axis (breaking[i].config_[j], X_AXIS);
184 dynamic_cast<Paper_column*> (c[j])->system_ = system;
186 set_loose_columns (system, &breaking[i]);
187 broken_intos_.push (system);
192 System::add_column (Paper_column*p)
195 SCM cs = me->get_property ("columns");
196 Grob *prev = scm_is_pair (cs) ? unsmob_grob (scm_car (cs)) : 0;
198 p->rank_ = prev ? Paper_column::get_rank (prev) + 1 : 0;
200 me->set_property ("columns", scm_cons (p->self_scm (), cs));
202 Axis_group_interface::add_element (me, p);
206 apply_tweaks (Grob *g, bool broken)
208 if (bool (g->original_) == broken)
210 SCM tweaks = global_registry_->get_tweaks (g);
211 for (SCM s = tweaks; scm_is_pair (s); s = scm_cdr (s))
213 SCM proc = scm_caar (s);
214 SCM rest = scm_cdar (s);
215 scm_apply_1 (proc, g->self_scm(), rest);
221 System::pre_processing ()
223 for (SCM s = get_property ("all-elements"); scm_is_pair (s); s = scm_cdr (s))
224 unsmob_grob (scm_car (s))->discretionary_processing ();
226 if (be_verbose_global)
227 progress_indication (_f ("Grob count %d", element_count ()));
229 for (SCM s = get_property ("all-elements"); scm_is_pair (s); s = scm_cdr (s))
230 unsmob_grob (scm_car (s))->handle_prebroken_dependencies ();
232 fixup_refpoints (get_property ("all-elements"));
235 for (SCM s = get_property ("all-elements"); scm_is_pair (s); s = scm_cdr (s))
236 apply_tweaks (unsmob_grob (scm_car (s)), false);
238 for (SCM s = get_property ("all-elements"); scm_is_pair (s); s = scm_cdr (s))
240 Grob *sc = unsmob_grob (scm_car (s));
241 sc->calculate_dependencies (PRECALCED, PRECALCING, ly_symbol2scm ("before-line-breaking-callback"));
244 progress_indication ("\n");
245 progress_indication (_ ("Calculating line breaks..."));
246 progress_indication (" ");
247 for (SCM s = get_property ("all-elements"); scm_is_pair (s); s = scm_cdr (s))
249 Grob *e = unsmob_grob (scm_car (s));
250 SCM proc = e->get_property ("spacing-procedure");
251 if (ly_c_procedure_p (proc))
252 scm_call_1 (proc, e->self_scm ());
257 System::post_processing ()
259 for (SCM s = get_property ("all-elements"); scm_is_pair (s); s = scm_cdr (s))
261 Grob *g = unsmob_grob (scm_car (s));
263 apply_tweaks (g, true);
265 g->calculate_dependencies (POSTCALCED, POSTCALCING,
266 ly_symbol2scm ("after-line-breaking-callback"));
269 Interval iv (extent (this, Y_AXIS));
271 programming_error ("System with zero extent.");
273 translate_axis (-iv[MAX], Y_AXIS);
275 /* Generate all stencils to trigger font loads.
276 This might seem inefficient, but Stencils are cached per grob
278 SCM all = get_property ("all-elements");
279 all = ly_list_qsort_uniq_x (all);
281 this->get_stencil ();
282 for (SCM s = all; scm_is_pair (s); s = scm_cdr (s))
284 Grob *g = unsmob_grob (scm_car (s));
292 static int const LAYER_COUNT = 3;
297 /* Output stencils in three layers: 0, 1, 2. Default layer: 1. */
298 SCM all = get_property ("all-elements");
300 for (int i = 0; i < LAYER_COUNT; i++)
301 for (SCM s = all; scm_is_pair (s); s = scm_cdr (s))
303 Grob *g = unsmob_grob (scm_car (s));
304 Stencil *stil = g->get_stencil ();
306 /* Skip empty stencils and grobs that are not in this layer. */
308 || robust_scm2int (g->get_property ("layer"), 1) != i)
311 Offset o (g->relative_coordinate (this, X_AXIS),
312 g->relative_coordinate (this, Y_AXIS));
314 Offset extra = robust_scm2offset (g->get_property ("extra-offset"),
316 * Staff_symbol_referencer::staff_space (g);
318 /* Must copy the stencil, for we cannot change the stencil
322 st.translate (o + extra);
324 *tail = scm_cons (st.expr (), SCM_EOL);
325 tail = SCM_CDRLOC(*tail);
328 if (Stencil *me = get_stencil ())
329 exprs = scm_cons (me->expr (), exprs);
331 Interval x (extent (this, X_AXIS));
332 Interval y (extent (this, Y_AXIS));
333 Stencil sys_stencil (Box (x, y),
334 scm_cons (ly_symbol2scm ("combine-stencil"),
337 Interval staff_refpoints;
338 staff_refpoints.set_empty();
339 for (SCM s = get_property ("spaceable-staves");
340 scm_is_pair (s); s = scm_cdr (s))
342 Grob *g = unsmob_grob (scm_car (s));
343 staff_refpoints.add_point (g->relative_coordinate (this, Y_AXIS));
347 Paper_system *pl = new Paper_system (sys_stencil, false);
348 pl->staff_refpoints_ = staff_refpoints;
349 Item * break_point = this->get_bound(LEFT);
350 pl->break_before_penalty_ =
351 robust_scm2double (break_point->get_property ("page-penalty"), 0.0);
353 return scm_gc_unprotect_object (pl->self_scm ());
357 System::broken_col_range (Item const *left, Item const *right) const
359 Link_array<Item> ret;
361 left = left->get_column ();
362 right = right->get_column ();
363 SCM s = get_property ("columns");
365 while (scm_is_pair (s) && scm_car (s) != right->self_scm ())
371 while (scm_is_pair (s) && scm_car (s) != left->self_scm ())
373 Paper_column*c = dynamic_cast<Paper_column*> (unsmob_grob (scm_car (s)));
374 if (Item::is_breakable (c) && !c->system_)
384 /** Return all columns, but filter out any unused columns , since they might
385 disrupt the spacing problem. */
387 System::columns () const
390 = extract_grob_array (this, ly_symbol2scm ("columns"));
392 for (int i = acs.size (); i--;)
394 bool brb = Item::is_breakable (acs[i]);
395 found = found || brb;
398 the last column should be breakable. Weed out any columns that
399 seem empty. We need to retain breakable columns, in case
400 someone forced a breakpoint.
402 if (!found || !Paper_column::is_used (acs[i]))
408 ADD_INTERFACE (System, "system-interface",
409 "This is the toplevel object: each object in a score "
410 "ultimately has a System object as its X and Y parent. ",
411 "all-elements spaceable-staves columns")