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))))
64 scm_default_compare (const void * a, const void *b)
69 if (pa < pb) return -1 ;
70 else if (pa > pb) return 1;
75 modify L in place: sort it
81 int len = scm_ilength (l);
82 SCM * arr = new SCM[len];
84 for (SCM s =l ; SCM_NNULLP (s); s = SCM_CDR(s))
85 arr[k++] = SCM_CAR(s);
88 qsort (arr, len, sizeof (SCM), &scm_default_compare);
92 for (int i = 0; i < len ; i++)
94 if (i && arr[i] == arr[i-1])
97 SCM_SETCAR(s, arr[i]);
101 SCM_SETCDR(s, SCM_EOL);
107 System::typeset_grob (Grob * elem)
110 programming_error ("Adding element twice.");
112 elem->pscore_ = pscore_;
113 Pointer_group_interface::add_grob (this, ly_symbol2scm ("all-elements"),elem);
114 scm_gc_unprotect_object (elem->self_scm ());
118 System::output_lines ()
120 for (SCM s = get_grob_property ("all-elements");
121 gh_pair_p (s); s = ly_cdr (s))
123 Grob * g = unsmob_grob (ly_car (s));
124 if (g->internal_has_interface (ly_symbol2scm ("only-prebreak-interface")))
127 Kill no longer needed grobs.
129 Item * it = dynamic_cast<Item*> (g);
130 if (it && Item::breakable_b(it))
132 it->find_prebroken_piece (LEFT)->suicide();
133 it->find_prebroken_piece (RIGHT)->suicide();
138 g->do_break_processing ();
142 fixups must be done in broken line_of_scores, because new elements
143 are put over there. */
145 for (int i=0; i < broken_intos_.size (); i++)
147 Grob *se = broken_intos_[i];
148 SCM all = se->get_grob_property ("all-elements");
149 for (SCM s = all; gh_pair_p (s); s = ly_cdr (s))
151 fixup_refpoint (ly_car (s));
153 count += scm_ilength (all);
157 needed for doing items.
159 fixup_refpoints (get_grob_property ("all-elements"));
162 for (SCM s = get_grob_property ("all-elements");
163 gh_pair_p (s); s = ly_cdr (s))
165 unsmob_grob (ly_car (s))->handle_broken_dependencies ();
167 handle_broken_dependencies ();
170 Because the this->get_grob_property (all-elements) contains items
171 in 3 versions, handle_broken_dependencies () will leave duplicated
172 items in all-elements. Strictly speaking this is harmless, but it
173 leads to duplicated symbols in the output. uniquify_list() makes
174 sure that no duplicates are in the list.
176 for (int i=0; i < broken_intos_.size (); i++)
178 SCM al = broken_intos_[i]->get_grob_property ("all-elements");
179 al = uniquify_list (al);
184 if (verbose_global_b)
185 progress_indication (_f ("Element count %d.", count + element_count ()));
188 for (int i=0; i < broken_intos_.size (); i++)
190 System *system = dynamic_cast<System*> (broken_intos_[i]);
192 if (verbose_global_b)
193 progress_indication ("[");
194 system->post_processing (i+1 == broken_intos_.size ());
196 if (verbose_global_b)
198 progress_indication (to_string (i));
199 progress_indication ("]");
202 if (i < broken_intos_.size () - 1)
204 SCM lastcol = ly_car (system->get_grob_property ("columns"));
205 Grob* e = unsmob_grob (lastcol);
207 SCM between = ly_symbol2scm ("between-system-string");
208 SCM inter = e->internal_get_grob_property (between);
209 if (gh_string_p (inter))
212 ->output_scheme (scm_list_n (between,
213 inter, SCM_UNDEFINED));
223 Find the loose columns in POSNS, and drape them around the columns
224 specified in BETWEEN-COLS. */
226 set_loose_columns (System* which, Column_x_positions const *posns)
228 for (int i = 0; i < posns->loose_cols_.size (); i++)
231 Item *loose = dynamic_cast<Item*> (posns->loose_cols_[i]);
232 Paper_column* col = dynamic_cast<Paper_column*> (loose);
242 SCM between = loose->get_grob_property ("between-cols");
243 if (!gh_pair_p (between))
247 Item * l=dynamic_cast<Item*> (unsmob_grob (ly_car (between)));
248 Item * r=dynamic_cast<Item*> (unsmob_grob (ly_cdr (between)));
255 left = l->get_column ();
260 loose = right = r->get_column ();
265 We divide the remaining space of the column over the left and
266 right side. At the moment, we
269 Grob * common = right->common_refpoint (left, X_AXIS);
271 Real rx = right->extent(common, X_AXIS)[LEFT];
272 Real lx = left->extent(common, X_AXIS)[RIGHT];
273 Real total_dx = rx - lx;
274 Interval cval =col->extent (col, X_AXIS);
278 We put it in the middle. This is not an ideal solution -- the
279 break alignment code inserts a fixed space before the clef
280 (about 1 SS), while the space following the clef is
281 flexible. In tight situations, the clef will almost be on top
282 of the following note.
285 Real dx = rx-lx - cval.length ();
286 if (total_dx < 2* cval.length ())
289 todo: this is discontinuous. I'm too tired to
290 invent a sliding mechanism. Duh.
299 col->system_ = which;
300 col->translate_axis (lx + dx - cval[LEFT], X_AXIS);
306 System::break_into_pieces (Array<Column_x_positions> const &breaking)
308 for (int i=0; i < breaking.size (); i++)
310 System *system = dynamic_cast <System*> (clone ());
313 Link_array<Grob> c (breaking[i].cols_);
314 pscore_->typeset_line (system);
316 system->set_bound (LEFT,c[0]);
317 system->set_bound (RIGHT,c.top ());
318 for (int j=0; j < c.size (); j++)
320 c[j]->translate_axis (breaking[i].config_[j],X_AXIS);
321 dynamic_cast<Paper_column*> (c[j])->system_ = system;
323 set_loose_columns (system, &breaking[i]);
324 broken_intos_.push (system);
329 System::output_molecule (SCM expr, Offset o)
333 if (!gh_pair_p (expr))
336 SCM head =ly_car (expr);
337 if (unsmob_input (head))
339 Input * ip = unsmob_input (head);
341 pscore_->outputter_->output_scheme (scm_list_n (ly_symbol2scm ("define-origin"),
342 scm_makfrom0str (ip->file_string ().to_str0 ()),
343 gh_int2scm (ip->line_number ()),
344 gh_int2scm (ip->column_number ()),
346 expr = ly_cadr (expr);
348 else if (head == ly_symbol2scm ("no-origin"))
350 pscore_->outputter_->output_scheme (scm_list_n (head, SCM_UNDEFINED));
351 expr = ly_cadr (expr);
353 else if (head == ly_symbol2scm ("translate-molecule"))
355 o += ly_scm2offset (ly_cadr (expr));
356 expr = ly_caddr (expr);
358 else if (head == ly_symbol2scm ("combine-molecule"))
360 output_molecule (ly_cadr (expr), o);
361 expr = ly_caddr (expr);
365 pscore_->outputter_->
366 output_scheme (scm_list_n (ly_symbol2scm ("placebox"),
367 gh_double2scm (o[X_AXIS]),
368 gh_double2scm (o[Y_AXIS]),
378 System::output_scheme (SCM s)
380 pscore_->outputter_->output_scheme (s);
384 System::add_column (Paper_column*p)
387 SCM cs = me->get_grob_property ("columns");
388 Grob * prev = gh_pair_p (cs) ? unsmob_grob (ly_car (cs)) : 0;
390 p->rank_ = prev ? Paper_column::get_rank (prev) + 1 : 0;
392 me->set_grob_property ("columns", gh_cons (p->self_scm (), cs));
394 Axis_group_interface::add_element (me, p);
398 System::pre_processing ()
400 for (SCM s = get_grob_property ("all-elements"); gh_pair_p (s); s = ly_cdr (s))
401 unsmob_grob (ly_car (s))->discretionary_processing ();
403 if (verbose_global_b)
404 progress_indication (_f ("Grob count %d ", element_count ()));
407 for (SCM s = get_grob_property ("all-elements"); gh_pair_p (s); s = ly_cdr (s))
408 unsmob_grob (ly_car (s))->handle_prebroken_dependencies ();
410 fixup_refpoints (get_grob_property ("all-elements"));
412 for (SCM s = get_grob_property ("all-elements"); gh_pair_p (s); s = ly_cdr (s))
414 Grob* sc = unsmob_grob (ly_car (s));
415 sc->calculate_dependencies (PRECALCED, PRECALCING, ly_symbol2scm ("before-line-breaking-callback"));
418 progress_indication ("\n" + _ ("Calculating line breaks...") + " ");
419 for (SCM s = get_grob_property ("all-elements"); gh_pair_p (s); s = ly_cdr (s))
421 Grob * e = unsmob_grob (ly_car (s));
422 SCM proc = e->get_grob_property ("spacing-procedure");
423 if (gh_procedure_p (proc))
424 gh_call1 (proc, e->self_scm ());
429 System::post_processing (bool last_line)
431 for (SCM s = get_grob_property ("all-elements");
432 gh_pair_p (s); s = ly_cdr (s))
434 Grob* sc = unsmob_grob (ly_car (s));
435 sc->calculate_dependencies (POSTCALCED, POSTCALCING,
436 ly_symbol2scm ("after-line-breaking-callback"));
439 Interval i (extent (this, Y_AXIS));
441 programming_error ("Huh? Empty System?");
443 translate_axis (- i[MAX], Y_AXIS);
445 Real height = i.length ();
448 programming_error ("Improbable system height");
453 generate all molecules to trigger all font loads.
455 (ugh. This is not very memory efficient.) */
456 this->get_molecule();
457 for (SCM s = get_grob_property ("all-elements"); gh_pair_p (s); s = ly_cdr (s))
459 unsmob_grob (ly_car (s))->get_molecule ();
464 SCM font_names = ly_quote_scm (get_paper ()->font_descriptions ());
465 output_scheme (scm_list_n (ly_symbol2scm ("define-fonts"),
472 Interval j (extent (this, X_AXIS));
473 Real length = j[RIGHT];
475 output_scheme (scm_list_n (ly_symbol2scm ("start-system"),
476 gh_double2scm (length),
477 gh_double2scm (height),
480 /* Output elements in three layers, 0, 1, 2.
481 The default layer is 1. */
483 Molecule *m = this->get_molecule();
485 output_molecule (m->get_expr (), Offset(0,0));
488 for (int i = 0; i < 3; i++)
489 for (SCM s = get_grob_property ("all-elements"); gh_pair_p (s);
492 Grob *sc = unsmob_grob (ly_car (s));
493 Molecule *m = sc->get_molecule ();
497 SCM s = sc->get_grob_property ("layer");
498 int layer = gh_number_p (s) ? gh_scm2int (s) : 1;
502 Offset o (sc->relative_coordinate (this, X_AXIS),
503 sc->relative_coordinate (this, Y_AXIS));
505 SCM e = sc->get_grob_property ("extra-offset");
508 o[X_AXIS] += gh_scm2double (ly_car (e));
509 o[Y_AXIS] += gh_scm2double (ly_cdr (e));
512 output_molecule (m->get_expr (), o);
519 output_scheme (scm_list_n (ly_symbol2scm ("stop-last-system"), SCM_UNDEFINED));
523 output_scheme (scm_list_n (ly_symbol2scm ("stop-system"), SCM_UNDEFINED));
529 System::broken_col_range (Item const*l, Item const*r) const
531 Link_array<Item> ret;
533 l = l->get_column ();
534 r = r->get_column ();
535 SCM s = get_grob_property ("columns");
537 while (gh_pair_p (s) && ly_car (s) != r->self_scm ())
543 while (gh_pair_p (s) && ly_car (s) != l->self_scm ())
545 Paper_column*c = dynamic_cast<Paper_column*> (unsmob_grob (ly_car (s)));
546 if (Item::breakable_b (c) && !c->system_)
557 Return all columns, but filter out any unused columns , since they might
558 disrupt the spacing problem.
561 System::columns ()const
564 = Pointer_group_interface__extract_grobs (this, (Grob*) 0, "columns");
566 for (int i= acs.size (); i -- ;)
568 bool brb = Item::breakable_b (acs[i]);
569 bfound = bfound || brb;
572 the last column should be breakable. Weed out any columns that
573 seem empty. We need to retain breakable columns, in case
574 someone forced a breakpoint.
576 if (!bfound || !Paper_column::used_b (acs[i]))
585 ADD_INTERFACE (System,"system-interface",
586 "Super grob, parent of all: "
588 "The columns of a score that form one line. The toplevel grob. Any "
589 "grob has a Line_of_score as both X and Y reference point. The "
590 "Paper_score contains one grob of this type. Control enters the "
591 "Grob dependency calculation from this single Line_of_score "
593 "between-system-string all-elements columns");