2 system.cc -- implement System
4 source file of the GNU LilyPond music typesetter
6 (c) 1996--2004 Han-Wen Nienhuys <hanwen@cs.uu.nl>
11 #include "align-interface.hh"
12 #include "axis-group-interface.hh"
16 #include "paper-column.hh"
17 #include "output-def.hh"
18 #include "paper-outputter.hh"
19 #include "paper-score.hh"
23 #include "all-font-metrics.hh"
24 #include "spacing-interface.hh"
25 #include "staff-symbol-referencer.hh"
26 #include "paper-book.hh"
27 #include "paper-system.hh"
28 #include "tweak-registration.hh"
31 System::System (System const &src, int count)
32 : Spanner (src, count)
37 System::System (SCM s, Object_key const*key)
45 System::clone (int count) const
47 return new System (*this, count);
52 System::element_count () const
54 return scm_ilength (get_property ("all-elements"));
58 System::spanner_count () const
61 for (SCM s = get_property ("all-elements"); scm_is_pair (s); s = scm_cdr (s))
62 if (dynamic_cast<Spanner*> (unsmob_grob (scm_car (s))))
68 System::typeset_grob (Grob * elem)
71 programming_error ("Adding element twice.");
74 elem->pscore_ = pscore_;
75 Pointer_group_interface::add_grob (this, ly_symbol2scm ("all-elements"), elem);
76 scm_gc_unprotect_object (elem->self_scm ());
82 fixup_refpoints (SCM s)
84 for (; scm_is_pair (s); s = scm_cdr (s))
86 Grob::fixup_refpoint (scm_car (s));
93 for (SCM s = get_property ("all-elements"); scm_is_pair (s); s = scm_cdr (s))
95 Grob *g = unsmob_grob (scm_car (s));
96 if (g->internal_has_interface (ly_symbol2scm ("only-prebreak-interface")))
99 Kill no longer needed grobs.
101 Item * it = dynamic_cast<Item*> (g);
102 if (it && Item::is_breakable (it))
104 it->find_prebroken_piece (LEFT)->suicide ();
105 it->find_prebroken_piece (RIGHT)->suicide ();
109 else if (g->is_live ())
110 g->do_break_processing ();
114 fixups must be done in broken line_of_scores, because new elements
115 are put over there. */
117 for (int i=0; i < broken_intos_.size (); i++)
119 Grob *se = broken_intos_[i];
120 SCM all = se->get_property ("all-elements");
121 for (SCM s = all; scm_is_pair (s); s = scm_cdr (s))
122 fixup_refpoint (scm_car (s));
123 count += scm_ilength (all);
127 needed for doing items.
129 fixup_refpoints (get_property ("all-elements"));
131 for (SCM s = get_property ("all-elements"); scm_is_pair (s); s = scm_cdr (s))
132 unsmob_grob (scm_car (s))->handle_broken_dependencies ();
133 handle_broken_dependencies ();
135 #if 0 /* don't do this: strange side effects. */
137 /* Because the this->get_property (all-elements) contains items in 3
138 versions, handle_broken_dependencies () will leave duplicated
139 items in all-elements. Strictly speaking this is harmless, but
140 it leads to duplicated symbols in the output. ly_list_qsort_uniq_x ()
141 makes sure that no duplicates are in the list. */
142 for (int i = 0; i < line_count; i++)
144 SCM all = broken_intos_[i]->get_property ("all-elements");
145 all = ly_list_qsort_uniq_x(all);
149 if (verbose_global_b)
150 progress_indication (_f ("Element count %d.", count + element_count ()));
152 int line_count = broken_intos_.size ();
153 SCM lines = scm_c_make_vector (line_count, SCM_EOL);
155 for (int i = 0; i < line_count; i++)
157 if (verbose_global_b)
158 progress_indication ("[");
160 System *system = dynamic_cast<System*> (broken_intos_[i]);
161 system->post_processing ();
162 scm_vector_set_x (lines, scm_int2num (i), system->get_line ());
164 if (verbose_global_b)
165 progress_indication (to_string (i) + "]");
170 /* Find the loose columns in POSNS, and drape them around the columns
171 specified in BETWEEN-COLS. */
173 set_loose_columns (System* which, Column_x_positions const *posns)
175 int loose_col_count = posns->loose_cols_.size ();
176 for (int i = 0; i < loose_col_count; i++)
179 Item *loose = dynamic_cast<Item*> (posns->loose_cols_[i]);
180 Paper_column* col = dynamic_cast<Paper_column*> (loose);
189 SCM between = loose->get_property ("between-cols");
190 if (!scm_is_pair (between))
193 Item *le = dynamic_cast<Item*> (unsmob_grob (scm_car (between)));
194 Item *re = dynamic_cast<Item*> (unsmob_grob (scm_cdr (between)));
201 left = le->get_column ();
202 if (!left->get_system ())
203 left = left->find_prebroken_piece (RIGHT);
207 loose = right = re->get_column ();
210 if (!right->get_system ())
211 right = right->find_prebroken_piece (LEFT);
213 /* Divide the remaining space of the column over the left and
214 right side. At the moment, FIXME */
215 Grob *common = right->common_refpoint (left, X_AXIS);
217 Real rx = right->extent (common, X_AXIS)[LEFT];
218 Real lx = left->extent (common, X_AXIS)[RIGHT];
219 Real total_dx = rx - lx;
220 Interval cval =col->extent (col, X_AXIS);
222 /* Put it in the middle. This is not an ideal solution -- the
223 break alignment code inserts a fixed space before the clef
224 (about 1 SS), while the space following the clef is flexible.
225 In tight situations, the clef will almost be on top of the
227 Real dx = rx - lx - cval.length ();
228 if (total_dx < 2* cval.length ())
230 /* TODO: this is discontinuous. I'm too tired to
231 invent a sliding mechanism. Duh. */
237 col->system_ = which;
238 col->translate_axis (-col->relative_coordinate (common, X_AXIS), X_AXIS);
239 col->translate_axis (lx + dx - cval[LEFT], X_AXIS);
245 System::break_into_pieces (Array<Column_x_positions> const &breaking)
247 for (int i = 0; i < breaking.size (); i++)
249 System *system = dynamic_cast <System*> (clone (i));
252 Link_array<Grob> c (breaking[i].cols_);
253 pscore_->typeset_line (system);
255 system->set_bound (LEFT,c[0]);
256 system->set_bound (RIGHT,c.top ());
257 for (int j = 0; j < c.size (); j++)
259 c[j]->translate_axis (breaking[i].config_[j], X_AXIS);
260 dynamic_cast<Paper_column*> (c[j])->system_ = system;
262 set_loose_columns (system, &breaking[i]);
263 broken_intos_.push (system);
268 System::add_column (Paper_column*p)
271 SCM cs = me->get_property ("columns");
272 Grob *prev = scm_is_pair (cs) ? unsmob_grob (scm_car (cs)) : 0;
274 p->rank_ = prev ? Paper_column::get_rank (prev) + 1 : 0;
276 me->set_property ("columns", scm_cons (p->self_scm (), cs));
278 Axis_group_interface::add_element (me, p);
282 apply_tweaks (Grob *g, bool broken)
284 if (bool (g->original_) == broken)
286 SCM tweaks = global_registry_->get_tweaks (g);
287 for (SCM s = tweaks; scm_is_pair (s); s = scm_cdr (s))
289 SCM proc = scm_caar (s);
290 SCM rest = scm_cdar (s);
291 scm_apply_1 (proc, g->self_scm(), rest);
297 System::pre_processing ()
299 for (SCM s = get_property ("all-elements"); scm_is_pair (s); s = scm_cdr (s))
300 unsmob_grob (scm_car (s))->discretionary_processing ();
302 if (verbose_global_b)
303 progress_indication (_f ("Grob count %d", element_count ()));
305 for (SCM s = get_property ("all-elements"); scm_is_pair (s); s = scm_cdr (s))
306 unsmob_grob (scm_car (s))->handle_prebroken_dependencies ();
308 fixup_refpoints (get_property ("all-elements"));
311 for (SCM s = get_property ("all-elements"); scm_is_pair (s); s = scm_cdr (s))
312 apply_tweaks (unsmob_grob (scm_car (s)), false);
314 for (SCM s = get_property ("all-elements"); scm_is_pair (s); s = scm_cdr (s))
316 Grob *sc = unsmob_grob (scm_car (s));
317 sc->calculate_dependencies (PRECALCED, PRECALCING, ly_symbol2scm ("before-line-breaking-callback"));
320 progress_indication ("\n");
321 progress_indication (_ ("Calculating line breaks..."));
322 progress_indication (" ");
323 for (SCM s = get_property ("all-elements"); scm_is_pair (s); s = scm_cdr (s))
325 Grob *e = unsmob_grob (scm_car (s));
326 SCM proc = e->get_property ("spacing-procedure");
327 if (ly_c_procedure_p (proc))
328 scm_call_1 (proc, e->self_scm ());
333 System::post_processing ()
335 for (SCM s = get_property ("all-elements"); scm_is_pair (s); s = scm_cdr (s))
337 Grob *g = unsmob_grob (scm_car (s));
339 apply_tweaks (g, true);
341 g->calculate_dependencies (POSTCALCED, POSTCALCING,
342 ly_symbol2scm ("after-line-breaking-callback"));
345 Interval iv (extent (this, Y_AXIS));
347 programming_error ("System with zero extent.");
349 translate_axis (-iv[MAX], Y_AXIS);
351 /* Generate all stencils to trigger font loads.
352 This might seem inefficient, but Stencils are cached per grob
354 SCM all = get_property ("all-elements");
355 all = ly_list_qsort_uniq_x (all);
357 this->get_stencil ();
358 for (SCM s = all; scm_is_pair (s); s = scm_cdr (s))
360 Grob *g = unsmob_grob (scm_car (s));
368 static int const LAYER_COUNT = 3;
373 /* Output stencils in three layers: 0, 1, 2. Default layer: 1.
375 Start with layer 3, since scm_cons prepends to list. */
376 SCM all = get_property ("all-elements");
378 for (int i = LAYER_COUNT; i--;)
379 for (SCM s = all; scm_is_pair (s); s = scm_cdr (s))
381 Grob *g = unsmob_grob (scm_car (s));
382 Stencil *stil = g->get_stencil ();
384 /* Skip empty stencils and grobs that are not in this layer. */
386 || robust_scm2int (g->get_property ("layer"), 1) != i)
389 Offset o (g->relative_coordinate (this, X_AXIS),
390 g->relative_coordinate (this, Y_AXIS));
392 Offset extra = robust_scm2offset (g->get_property ("extra-offset"),
394 * Staff_symbol_referencer::staff_space (g);
396 /* Must copy the stencil, for we cannot change the stencil
400 st.translate (o + extra);
401 *tail = scm_cons (st.expr (), SCM_EOL);
402 tail = SCM_CDRLOC(*tail);
405 if (Stencil *me = get_stencil ())
406 exprs = scm_cons (me->expr (), exprs);
408 Interval x (extent (this, X_AXIS));
409 Interval y (extent (this, Y_AXIS));
410 Stencil sys_stencil (Box (x,y),
411 scm_cons (ly_symbol2scm ("combine-stencil"),
414 Interval staff_refpoints;
415 staff_refpoints.set_empty();
416 for (SCM s = get_property ("spaceable-staves");
417 scm_is_pair (s); s = scm_cdr (s))
419 Grob *g = unsmob_grob (scm_car (s));
420 staff_refpoints.add_point (g->relative_coordinate (this, Y_AXIS));
424 Paper_system *pl = new Paper_system (sys_stencil, false);
425 pl->staff_refpoints_ = staff_refpoints;
426 Item * break_point =this->get_bound(LEFT);
428 robust_scm2double (break_point->get_property ("page-penalty"), 0.0);
430 return scm_gc_unprotect_object (pl->self_scm ());
434 System::broken_col_range (Item const *left, Item const *right) const
436 Link_array<Item> ret;
438 left = left->get_column ();
439 right = right->get_column ();
440 SCM s = get_property ("columns");
442 while (scm_is_pair (s) && scm_car (s) != right->self_scm ())
448 while (scm_is_pair (s) && scm_car (s) != left->self_scm ())
450 Paper_column*c = dynamic_cast<Paper_column*> (unsmob_grob (scm_car (s)));
451 if (Item::is_breakable (c) && !c->system_)
461 /** Return all columns, but filter out any unused columns , since they might
462 disrupt the spacing problem. */
464 System::columns () const
467 = Pointer_group_interface__extract_grobs (this, (Grob*) 0, "columns");
469 for (int i = acs.size (); i--;)
471 bool brb = Item::is_breakable (acs[i]);
472 found = found || brb;
475 the last column should be breakable. Weed out any columns that
476 seem empty. We need to retain breakable columns, in case
477 someone forced a breakpoint.
479 if (!found || !Paper_column::is_used (acs[i]))
485 ADD_INTERFACE (System,"system-interface",
486 "This is the toplevel object: each object in a score "
487 "ultimately has a System object as its X and Y parent. ",
488 "all-elements spaceable-staves columns")