2 system.cc -- implement System
4 source file of the GNU LilyPond music typesetter
6 (c) 1996--2003 Han-Wen Nienhuys <hanwen@cs.uu.nl>
9 #include "input-smob.hh"
10 #include "axis-group-interface.hh"
14 #include "paper-column.hh"
15 #include "paper-def.hh"
16 #include "paper-outputter.hh"
17 #include "paper-score.hh"
20 #include "dimensions.hh"
21 #include "molecule.hh"
22 #include "all-font-metrics.hh"
23 #include "spacing-interface.hh"
27 fixup_refpoints (SCM s)
29 for (; gh_pair_p (s); s = ly_cdr (s))
31 Grob::fixup_refpoint (ly_car (s));
36 System::System (SCM s)
43 System::element_count () const
45 return scm_ilength (get_grob_property ("all-elements"));
49 System::spanner_count () const
52 for (SCM s = get_grob_property ("all-elements");
53 gh_pair_p (s); s = ly_cdr (s))
55 if (dynamic_cast<Spanner*> (unsmob_grob (gh_car(s))))
65 System::typeset_grob (Grob * elem)
68 programming_error ("Adding element twice.");
70 elem->pscore_ = pscore_;
71 Pointer_group_interface::add_grob (this, ly_symbol2scm ("all-elements"),elem);
72 scm_gc_unprotect_object (elem->self_scm ());
76 System::output_lines ()
78 for (SCM s = get_grob_property ("all-elements");
79 gh_pair_p (s); s = ly_cdr (s))
81 Grob * g = unsmob_grob (ly_car (s));
82 if (g->internal_has_interface (ly_symbol2scm ("only-prebreak-interface")))
85 Kill no longer needed grobs.
87 Item * it = dynamic_cast<Item*> (g);
88 if (it && Item::breakable_b(it))
90 it->find_prebroken_piece (LEFT)->suicide();
91 it->find_prebroken_piece (RIGHT)->suicide();
96 g->do_break_processing ();
100 fixups must be done in broken line_of_scores, because new elements
101 are put over there. */
103 for (int i=0; i < broken_intos_.size (); i++)
105 Grob *se = broken_intos_[i];
106 SCM all = se->get_grob_property ("all-elements");
107 for (SCM s = all; gh_pair_p (s); s = ly_cdr (s))
109 fixup_refpoint (ly_car (s));
111 count += scm_ilength (all);
115 needed for doing items.
117 fixup_refpoints (get_grob_property ("all-elements"));
120 for (SCM s = get_grob_property ("all-elements");
121 gh_pair_p (s); s = ly_cdr (s))
123 unsmob_grob (ly_car (s))->handle_broken_dependencies ();
125 handle_broken_dependencies ();
127 if (verbose_global_b)
128 progress_indication (_f ("Element count %d.", count + element_count ()));
131 for (int i=0; i < broken_intos_.size (); i++)
133 System *system = dynamic_cast<System*> (broken_intos_[i]);
135 if (verbose_global_b)
136 progress_indication ("[");
137 system->post_processing (i+1 == broken_intos_.size ());
139 if (verbose_global_b)
141 progress_indication (to_string (i));
142 progress_indication ("]");
145 if (i < broken_intos_.size () - 1)
147 SCM lastcol = ly_car (system->get_grob_property ("columns"));
148 Grob* e = unsmob_grob (lastcol);
150 SCM between = ly_symbol2scm ("between-system-string");
151 SCM inter = e->internal_get_grob_property (between);
152 if (gh_string_p (inter))
155 ->output_scheme (scm_list_n (between,
156 inter, SCM_UNDEFINED));
166 Find the loose columns in POSNS, and drape them around the columns
167 specified in BETWEEN-COLS. */
169 set_loose_columns (System* which, Column_x_positions const *posns)
171 for (int i = 0; i < posns->loose_cols_.size (); i++)
174 Item *loose = dynamic_cast<Item*> (posns->loose_cols_[i]);
175 Paper_column* col = dynamic_cast<Paper_column*> (loose);
185 SCM between = loose->get_grob_property ("between-cols");
186 if (!gh_pair_p (between))
190 Item * l=dynamic_cast<Item*> (unsmob_grob (ly_car (between)));
191 Item * r=dynamic_cast<Item*> (unsmob_grob (ly_cdr (between)));
198 left = l->get_column ();
203 loose = right = r->get_column ();
208 We divide the remaining space of the column over the left and
209 right side. At the moment, we
212 Grob * common = right->common_refpoint (left, X_AXIS);
214 Real rx = right->extent(common, X_AXIS)[LEFT];
215 Real lx = left->extent(common, X_AXIS)[RIGHT];
216 Real total_dx = rx - lx;
217 Interval cval =col->extent (col, X_AXIS);
221 We put it in the middle. This is not an ideal solution -- the
222 break alignment code inserts a fixed space before the clef
223 (about 1 SS), while the space following the clef is
224 flexible. In tight situations, the clef will almost be on top
225 of the following note.
228 Real dx = rx-lx - cval.length ();
229 if (total_dx < 2* cval.length ())
232 todo: this is discontinuous. I'm too tired to
233 invent a sliding mechanism. Duh.
242 col->system_ = which;
243 col->translate_axis (lx + dx - cval[LEFT], X_AXIS);
249 System::break_into_pieces (Array<Column_x_positions> const &breaking)
251 for (int i=0; i < breaking.size (); i++)
253 System *system = dynamic_cast <System*> (clone ());
256 Link_array<Grob> c (breaking[i].cols_);
257 pscore_->typeset_line (system);
259 system->set_bound (LEFT,c[0]);
260 system->set_bound (RIGHT,c.top ());
261 for (int j=0; j < c.size (); j++)
263 c[j]->translate_axis (breaking[i].config_[j],X_AXIS);
264 dynamic_cast<Paper_column*> (c[j])->system_ = system;
266 set_loose_columns (system, &breaking[i]);
267 broken_intos_.push (system);
272 System::output_molecule (SCM expr, Offset o)
276 if (!gh_pair_p (expr))
279 SCM head =ly_car (expr);
280 if (unsmob_input (head))
282 Input * ip = unsmob_input (head);
284 pscore_->outputter_->output_scheme (scm_list_n (ly_symbol2scm ("define-origin"),
285 scm_makfrom0str (ip->file_string ().to_str0 ()),
286 gh_int2scm (ip->line_number ()),
287 gh_int2scm (ip->column_number ()),
289 expr = ly_cadr (expr);
291 else if (head == ly_symbol2scm ("no-origin"))
293 pscore_->outputter_->output_scheme (scm_list_n (head, SCM_UNDEFINED));
294 expr = ly_cadr (expr);
296 else if (head == ly_symbol2scm ("translate-molecule"))
298 o += ly_scm2offset (ly_cadr (expr));
299 expr = ly_caddr (expr);
301 else if (head == ly_symbol2scm ("combine-molecule"))
303 output_molecule (ly_cadr (expr), o);
304 expr = ly_caddr (expr);
308 pscore_->outputter_->
309 output_scheme (scm_list_n (ly_symbol2scm ("placebox"),
310 gh_double2scm (o[X_AXIS]),
311 gh_double2scm (o[Y_AXIS]),
321 System::output_scheme (SCM s)
323 pscore_->outputter_->output_scheme (s);
327 System::add_column (Paper_column*p)
330 SCM cs = me->get_grob_property ("columns");
331 Grob * prev = gh_pair_p (cs) ? unsmob_grob (ly_car (cs)) : 0;
333 p->rank_ = prev ? Paper_column::get_rank (prev) + 1 : 0;
335 me->set_grob_property ("columns", gh_cons (p->self_scm (), cs));
337 Axis_group_interface::add_element (me, p);
341 System::pre_processing ()
343 for (SCM s = get_grob_property ("all-elements"); gh_pair_p (s); s = ly_cdr (s))
344 unsmob_grob (ly_car (s))->discretionary_processing ();
346 if (verbose_global_b)
347 progress_indication (_f ("Grob count %d ", element_count ()));
350 for (SCM s = get_grob_property ("all-elements"); gh_pair_p (s); s = ly_cdr (s))
351 unsmob_grob (ly_car (s))->handle_prebroken_dependencies ();
353 fixup_refpoints (get_grob_property ("all-elements"));
355 for (SCM s = get_grob_property ("all-elements"); gh_pair_p (s); s = ly_cdr (s))
357 Grob* sc = unsmob_grob (ly_car (s));
358 sc->calculate_dependencies (PRECALCED, PRECALCING, ly_symbol2scm ("before-line-breaking-callback"));
361 progress_indication ("\n" + _ ("Calculating line breaks...") + " ");
362 for (SCM s = get_grob_property ("all-elements"); gh_pair_p (s); s = ly_cdr (s))
364 Grob * e = unsmob_grob (ly_car (s));
365 SCM proc = e->get_grob_property ("spacing-procedure");
366 if (gh_procedure_p (proc))
367 gh_call1 (proc, e->self_scm ());
372 System::post_processing (bool last_line)
374 for (SCM s = get_grob_property ("all-elements");
375 gh_pair_p (s); s = ly_cdr (s))
377 Grob* sc = unsmob_grob (ly_car (s));
378 sc->calculate_dependencies (POSTCALCED, POSTCALCING,
379 ly_symbol2scm ("after-line-breaking-callback"));
382 Interval i (extent (this, Y_AXIS));
384 programming_error ("Huh? Empty System?");
386 translate_axis (- i[MAX], Y_AXIS);
388 Real height = i.length ();
391 programming_error ("Improbable system height");
396 generate all molecules to trigger all font loads.
398 (ugh. This is not very memory efficient.) */
399 this->get_molecule();
400 for (SCM s = get_grob_property ("all-elements"); gh_pair_p (s); s = ly_cdr (s))
402 unsmob_grob (ly_car (s))->get_molecule ();
407 SCM font_names = ly_quote_scm (get_paper ()->font_descriptions ());
408 output_scheme (scm_list_n (ly_symbol2scm ("define-fonts"),
415 Interval j (extent (this, X_AXIS));
416 Real length = j[RIGHT];
418 output_scheme (scm_list_n (ly_symbol2scm ("start-system"),
419 gh_double2scm (length),
420 gh_double2scm (height),
423 /* Output elements in three layers, 0, 1, 2.
424 The default layer is 1. */
426 Molecule *m = this->get_molecule();
428 output_molecule (m->get_expr (), Offset(0,0));
431 for (int i = 0; i < 3; i++)
432 for (SCM s = get_grob_property ("all-elements"); gh_pair_p (s);
435 Grob *sc = unsmob_grob (ly_car (s));
436 Molecule *m = sc->get_molecule ();
440 SCM s = sc->get_grob_property ("layer");
441 int layer = gh_number_p (s) ? gh_scm2int (s) : 1;
445 Offset o (sc->relative_coordinate (this, X_AXIS),
446 sc->relative_coordinate (this, Y_AXIS));
448 SCM e = sc->get_grob_property ("extra-offset");
451 o[X_AXIS] += gh_scm2double (ly_car (e));
452 o[Y_AXIS] += gh_scm2double (ly_cdr (e));
455 output_molecule (m->get_expr (), o);
462 output_scheme (scm_list_n (ly_symbol2scm ("stop-last-system"), SCM_UNDEFINED));
466 output_scheme (scm_list_n (ly_symbol2scm ("stop-system"), SCM_UNDEFINED));
472 System::broken_col_range (Item const*l, Item const*r) const
474 Link_array<Item> ret;
476 l = l->get_column ();
477 r = r->get_column ();
478 SCM s = get_grob_property ("columns");
480 while (gh_pair_p (s) && ly_car (s) != r->self_scm ())
486 while (gh_pair_p (s) && ly_car (s) != l->self_scm ())
488 Paper_column*c = dynamic_cast<Paper_column*> (unsmob_grob (ly_car (s)));
489 if (Item::breakable_b (c) && !c->system_)
500 Return all columns, but filter out any unused columns , since they might
501 disrupt the spacing problem.
504 System::columns ()const
507 = Pointer_group_interface__extract_grobs (this, (Grob*) 0, "columns");
509 for (int i= acs.size (); i -- ;)
511 bool brb = Item::breakable_b (acs[i]);
512 bfound = bfound || brb;
515 the last column should be breakable. Weed out any columns that
516 seem empty. We need to retain breakable columns, in case
517 someone forced a breakpoint.
519 if (!bfound || !Paper_column::used_b (acs[i]))
528 ADD_INTERFACE (System,"system-interface",
529 "Super grob, parent of all: "
531 "The columns of a score that form one line. The toplevel grob. Any "
532 "grob has a Line_of_score as both X and Y reference point. The "
533 "Paper_score contains one grob of this type. Control enters the "
534 "Grob dependency calculation from this single Line_of_score "
536 "between-system-string all-elements columns");