2 system.cc -- implement System
4 source file of the GNU LilyPond music typesetter
6 (c) 1996--2002 Han-Wen Nienhuys <hanwen@cs.uu.nl>
9 #include "input-smob.hh"
10 #include "axis-group-interface.hh"
12 #include "line-of-score.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"
26 fixup_refpoints (SCM s)
28 for (; gh_pair_p (s); s = ly_cdr (s))
30 Grob::fixup_refpoint (ly_car (s));
35 System::System (SCM s)
42 System::element_count () const
44 return scm_ilength (get_grob_property ("all-elements"));
48 System::typeset_grob (Grob * elem_p)
50 elem_p->pscore_l_ = pscore_l_;
51 Pointer_group_interface::add_grob (this, ly_symbol2scm ("all-elements"),elem_p);
52 scm_gc_unprotect_object (elem_p->self_scm ());
56 System::output_lines ()
58 for (SCM s = get_grob_property ("all-elements");
59 gh_pair_p (s); s = ly_cdr (s))
61 unsmob_grob (ly_car (s))->do_break_processing ();
65 fixups must be done in broken line_of_scores, because new elements
66 are put over there. */
68 for (int i=0; i < broken_into_l_arr_.size (); i++)
70 Grob *se = broken_into_l_arr_[i];
71 SCM all = se->get_grob_property ("all-elements");
72 for (SCM s = all; gh_pair_p (s); s = ly_cdr (s))
74 fixup_refpoint (ly_car (s));
76 count += scm_ilength (all);
80 needed for doing items.
82 fixup_refpoints (get_grob_property ("all-elements"));
85 for (SCM s = get_grob_property ("all-elements");
86 gh_pair_p (s); s = ly_cdr (s))
88 unsmob_grob (ly_car (s))->handle_broken_dependencies ();
90 handle_broken_dependencies ();
93 progress_indication (_f ("Element count %d.", count + element_count ()));
96 for (int i=0; i < broken_into_l_arr_.size (); i++)
98 System *line_l = dynamic_cast<System*> (broken_into_l_arr_[i]);
100 if (verbose_global_b)
101 progress_indication ("[");
102 line_l->post_processing (i+1 == broken_into_l_arr_.size ());
104 if (verbose_global_b)
106 progress_indication (to_str (i));
107 progress_indication ("]");
110 if (i < broken_into_l_arr_.size () - 1)
112 SCM lastcol = ly_car (line_l->get_grob_property ("columns"));
113 Grob* e = unsmob_grob (lastcol);
115 SCM between = ly_symbol2scm ("between-system-string");
116 SCM inter = e->internal_get_grob_property (between);
117 if (gh_string_p (inter))
119 pscore_l_->outputter_l_
120 ->output_scheme (scm_list_n (between,
121 inter, SCM_UNDEFINED));
131 Find the loose columns in POSNS, and drape them around the columns
132 specified in BETWEEN-COLS. */
134 set_loose_columns (System* which, Column_x_positions const *posns)
136 for (int i = 0; i < posns->loose_cols_.size (); i++)
139 Item *loose = dynamic_cast<Item*> (posns->loose_cols_[i]);
140 Paper_column* col = dynamic_cast<Paper_column*> (loose);
150 SCM between = loose->get_grob_property ("between-cols");
151 if (!gh_pair_p (between))
155 Item * l=dynamic_cast<Item*> (unsmob_grob (ly_car (between)));
156 Item * r=dynamic_cast<Item*> (unsmob_grob (ly_cdr (between)));
163 left = l->column_l ();
168 loose = right = r->column_l ();
174 Real rx = right->relative_coordinate (right->get_parent (X_AXIS), X_AXIS);
175 Real lx = left->relative_coordinate (left->get_parent (X_AXIS), X_AXIS);
178 divide space equally over loose columns.
184 SCM between = loose->get_grob_property ("between-cols");
185 if (!gh_pair_p (between))
188 Paper_column *thiscol = dynamic_cast<Paper_column*> (loose);
190 thiscol->line_l_ = which;
191 thiscol->translate_axis (lx + j*(rx - lx)/divide_over, X_AXIS);
194 loose = dynamic_cast<Item*> (unsmob_grob (ly_cdr (between)));
198 We divide the remaining space of the column over the left and
199 right side. At the moment, we
202 Grob * common = right->common_refpoint (left, X_AXIS);
204 Real rx = right->extent(common, X_AXIS)[LEFT];
205 Real lx = left->extent(common, X_AXIS)[RIGHT];
206 Real total_dx = rx - lx;
207 Interval cval =col->extent (col, X_AXIS);
211 We put it in the middle. This is not an ideal solution -- the
212 break alignment code inserts a fixed space before the clef
213 (about 1 SS), while the space following the clef is
214 flexible. In tight situations, the clef will almost be on top
215 of the following note.
218 Real dx = rx-lx - cval.length ();
219 if (total_dx < 2* cval.length ())
222 todo: this is discontinuous. I'm too tired to
223 invent a sliding mechanism. Duh.
232 col->line_l_ = which;
233 col->translate_axis (lx + dx - cval[LEFT], X_AXIS);
240 System::break_into_pieces (Array<Column_x_positions> const &breaking)
242 for (int i=0; i < breaking.size (); i++)
244 System *line_l = dynamic_cast <System*> (clone ());
246 // line_l->set_immutable_grob_property ("rank", gh_int2scm (i));
247 Link_array<Grob> c (breaking[i].cols_);
248 pscore_l_->typeset_line (line_l);
250 line_l->set_bound (LEFT,c[0]);
251 line_l->set_bound (RIGHT,c.top ());
252 for (int j=0; j < c.size (); j++)
254 c[j]->translate_axis (breaking[i].config_[j],X_AXIS);
255 dynamic_cast<Paper_column*> (c[j])->line_l_ = line_l;
257 set_loose_columns (line_l, &breaking[i]);
258 broken_into_l_arr_.push (line_l);
263 #define GLOBAL_SYMBOL(cname, name) \
266 cname ## _init_func () \
268 cname = ly_symbol2scm (name); \
269 scm_permanent_object (cname); \
271 ADD_SCM_INIT_FUNC (cname,cname ## _init_func);\
274 GLOBAL_SYMBOL (offset_sym , "translate-molecule");
275 GLOBAL_SYMBOL (placebox_sym , "placebox");
276 GLOBAL_SYMBOL (combine_sym , "combine-molecule");
281 System::output_molecule (SCM expr, Offset o)
286 if (!gh_pair_p (expr))
289 SCM head =ly_car (expr);
290 if (unsmob_input (head))
292 Input * ip = unsmob_input (head);
295 pscore_l_->outputter_l_->output_scheme (scm_list_n (ly_symbol2scm ("define-origin"),
296 ly_str02scm (ip->file_str ().ch_C ()),
297 gh_int2scm (ip->line_number ()),
298 gh_int2scm (ip->column_number ()),
300 expr = ly_cadr (expr);
302 else if (head == ly_symbol2scm ("no-origin"))
304 pscore_l_->outputter_l_->output_scheme (scm_list_n (head, SCM_UNDEFINED));
305 expr = ly_cadr (expr);
307 else if (head == ly_symbol2scm ("translate-molecule"))
309 o += ly_scm2offset (ly_cadr (expr));
310 expr = ly_caddr (expr);
312 else if (head == ly_symbol2scm ("combine-molecule"))
314 output_molecule (ly_cadr (expr), o);
315 expr = ly_caddr (expr);
319 pscore_l_->outputter_l_->
320 output_scheme (scm_list_n (placebox_sym,
321 gh_double2scm (o[X_AXIS]),
322 gh_double2scm (o[Y_AXIS]),
332 System::output_scheme (SCM s)
334 pscore_l_->outputter_l_->output_scheme (s);
338 System::add_column (Paper_column*p)
341 SCM cs = me->get_grob_property ("columns");
342 Grob * prev = gh_pair_p (cs) ? unsmob_grob (ly_car (cs)) : 0;
344 p->rank_i_ = prev ? Paper_column::rank_i (prev) + 1 : 0;
346 me->set_grob_property ("columns", gh_cons (p->self_scm (), cs));
348 Axis_group_interface::add_element (me, p);
354 TODO: use scm_map iso. for loops.
357 System::pre_processing ()
359 for (SCM s = get_grob_property ("all-elements"); gh_pair_p (s); s = ly_cdr (s))
360 unsmob_grob (ly_car (s))->discretionary_processing ();
362 if (verbose_global_b)
363 progress_indication (_f ("Element count %d ", element_count ()));
366 for (SCM s = get_grob_property ("all-elements"); gh_pair_p (s); s = ly_cdr (s))
367 unsmob_grob (ly_car (s))->handle_prebroken_dependencies ();
369 fixup_refpoints (get_grob_property ("all-elements"));
371 for (SCM s = get_grob_property ("all-elements"); gh_pair_p (s); s = ly_cdr (s))
373 Grob* sc = unsmob_grob (ly_car (s));
374 sc->calculate_dependencies (PRECALCED, PRECALCING, ly_symbol2scm ("before-line-breaking-callback"));
377 progress_indication ("\n" + _ ("Calculating column positions...") + " ");
378 for (SCM s = get_grob_property ("all-elements"); gh_pair_p (s); s = ly_cdr (s))
380 Grob * e = unsmob_grob (ly_car (s));
381 SCM proc = e->get_grob_property ("spacing-procedure");
382 if (gh_procedure_p (proc))
383 gh_call1 (proc, e->self_scm ());
388 System::post_processing (bool last_line)
390 for (SCM s = get_grob_property ("all-elements");
391 gh_pair_p (s); s = ly_cdr (s))
393 Grob* sc = unsmob_grob (ly_car (s));
394 sc->calculate_dependencies (POSTCALCED, POSTCALCING,
395 ly_symbol2scm ("after-line-breaking-callback"));
398 Interval i (extent (this, Y_AXIS));
400 programming_error ("Huh? Empty System?");
402 translate_axis (- i[MAX], Y_AXIS);
404 Real height = i.length ();
407 programming_error ("Improbable system height");
412 generate all molecules to trigger all font loads.
414 (ugh. This is not very memory efficient.) */
415 this->get_molecule();
416 for (SCM s = get_grob_property ("all-elements"); gh_pair_p (s); s = ly_cdr (s))
418 unsmob_grob (ly_car (s))->get_molecule ();
423 SCM font_names = ly_quote_scm (paper_l ()->font_descriptions ());
424 output_scheme (scm_list_n (ly_symbol2scm ("define-fonts"),
431 output_scheme (scm_list_n (ly_symbol2scm ("start-system"),
432 gh_double2scm (height),
435 /* Output elements in three layers, 0, 1, 2.
436 The default layer is 1. */
439 Molecule *m = this->get_molecule();
441 output_molecule (m->get_expr (), Offset(0,0));
444 for (int i = 0; i < 3; i++)
445 for (SCM s = get_grob_property ("all-elements"); gh_pair_p (s);
448 Grob *sc = unsmob_grob (ly_car (s));
449 Molecule *m = sc->get_molecule ();
453 SCM s = sc->get_grob_property ("layer");
454 int layer = gh_number_p (s) ? gh_scm2int (s) : 1;
458 Offset o (sc->relative_coordinate (this, X_AXIS),
459 sc->relative_coordinate (this, Y_AXIS));
461 SCM e = sc->get_grob_property ("extra-offset");
464 o[X_AXIS] += gh_scm2double (ly_car (e));
465 o[Y_AXIS] += gh_scm2double (ly_cdr (e));
468 output_molecule (m->get_expr (), o);
475 output_scheme (scm_list_n (ly_symbol2scm ("stop-last-system"), SCM_UNDEFINED));
479 output_scheme (scm_list_n (ly_symbol2scm ("stop-system"), SCM_UNDEFINED));
485 System::broken_col_range (Item const*l, Item const*r) const
487 Link_array<Item> ret;
491 SCM s = get_grob_property ("columns");
493 while (gh_pair_p (s) && ly_car (s) != r->self_scm ())
499 while (gh_pair_p (s) && ly_car (s) != l->self_scm ())
501 Paper_column*c = dynamic_cast<Paper_column*> (unsmob_grob (ly_car (s)));
502 if (Item::breakable_b (c) && !c->line_l_)
513 Return all columns, but filter out any unused columns , since they might
514 disrupt the spacing problem.
517 System::column_l_arr ()const
520 = Pointer_group_interface__extract_grobs (this, (Grob*) 0, "columns");
522 for (int i= acs.size (); i -- ;)
524 bool brb = Item::breakable_b (acs[i]);
525 bfound = bfound || brb;
528 the last column should be breakable. Weed out any columns that
529 seem empty. We need to retain breakable columns, in case
530 someone forced a breakpoint.
532 if (!bfound || !Paper_column::used_b (acs[i]))
541 ADD_INTERFACE (System,"system-interface",
542 "Super grob, parent of all:
544 The columns of a score that form one line. The toplevel grob. Any
545 grob has a Line_of_score as both X and Y reference point. The
546 Paper_score contains one grob of this type. Control enters the
547 Grob dependency calculation from this single Line_of_score
549 "between-system-string spacing-procedure before-line-breaking-callback after-line-breaking-callback all-elements columns");