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"
24 #include "staff-symbol-referencer.hh"
28 fixup_refpoints (SCM s)
30 for (; gh_pair_p (s); s = ly_cdr (s))
32 Grob::fixup_refpoint (ly_car (s));
37 System::System (SCM s)
44 System::element_count () const
46 return scm_ilength (get_grob_property ("all-elements"));
50 System::spanner_count () const
53 for (SCM s = get_grob_property ("all-elements");
54 gh_pair_p (s); s = ly_cdr (s))
56 if (dynamic_cast<Spanner*> (unsmob_grob (gh_car(s))))
65 scm_default_compare (const void * a, const void *b)
70 if (pa < pb) return -1 ;
71 else if (pa > pb) return 1;
76 modify L in place: sort it
82 int len = scm_ilength (l);
83 SCM * arr = new SCM[len];
85 for (SCM s =l ; SCM_NNULLP (s); s = SCM_CDR(s))
86 arr[k++] = SCM_CAR(s);
89 qsort (arr, len, sizeof (SCM), &scm_default_compare);
93 for (int i = 0; i < len ; i++)
95 if (i && arr[i] == arr[i-1])
98 SCM_SETCAR(s, arr[i]);
104 SCM_SETCDR(s, SCM_EOL);
111 System::typeset_grob (Grob * elem)
114 programming_error ("Adding element twice.");
116 elem->pscore_ = pscore_;
117 Pointer_group_interface::add_grob (this, ly_symbol2scm ("all-elements"),elem);
118 scm_gc_unprotect_object (elem->self_scm ());
122 System::output_lines ()
124 for (SCM s = get_grob_property ("all-elements");
125 gh_pair_p (s); s = ly_cdr (s))
127 Grob * g = unsmob_grob (ly_car (s));
128 if (g->internal_has_interface (ly_symbol2scm ("only-prebreak-interface")))
131 Kill no longer needed grobs.
133 Item * it = dynamic_cast<Item*> (g);
134 if (it && Item::breakable_b(it))
136 it->find_prebroken_piece (LEFT)->suicide();
137 it->find_prebroken_piece (RIGHT)->suicide();
142 g->do_break_processing ();
146 fixups must be done in broken line_of_scores, because new elements
147 are put over there. */
149 for (int i=0; i < broken_intos_.size (); i++)
151 Grob *se = broken_intos_[i];
152 SCM all = se->get_grob_property ("all-elements");
153 for (SCM s = all; gh_pair_p (s); s = ly_cdr (s))
155 fixup_refpoint (ly_car (s));
157 count += scm_ilength (all);
161 needed for doing items.
163 fixup_refpoints (get_grob_property ("all-elements"));
166 for (SCM s = get_grob_property ("all-elements");
167 gh_pair_p (s); s = ly_cdr (s))
169 unsmob_grob (ly_car (s))->handle_broken_dependencies ();
171 handle_broken_dependencies ();
174 Because the this->get_grob_property (all-elements) contains items
175 in 3 versions, handle_broken_dependencies () will leave duplicated
176 items in all-elements. Strictly speaking this is harmless, but it
177 leads to duplicated symbols in the output. uniquify_list() makes
178 sure that no duplicates are in the list.
180 for (int i=0; i < broken_intos_.size (); i++)
183 don't do this: strange side effects.
185 // SCM al = broken_intos_[i]->get_grob_property ("all-elements");
186 // al = uniquify_list (al);
189 if (verbose_global_b)
190 progress_indication (_f ("Element count %d.", count + element_count ()));
193 for (int i=0; i < broken_intos_.size (); i++)
195 System *system = dynamic_cast<System*> (broken_intos_[i]);
197 if (verbose_global_b)
198 progress_indication ("[");
199 bool last = i+1 == broken_intos_.size ();
200 system->post_processing (last);
202 if (verbose_global_b)
204 progress_indication (to_string (i));
205 progress_indication ("]");
208 if (i < broken_intos_.size () - 1)
210 SCM lastcol = ly_car (system->get_grob_property ("columns"));
211 Grob* e = unsmob_grob (lastcol);
213 SCM between = ly_symbol2scm ("between-system-string");
214 SCM inter = e->internal_get_grob_property (between);
215 if (gh_string_p (inter))
218 ->output_scheme (scm_list_n (between,
219 inter, SCM_UNDEFINED));
229 Find the loose columns in POSNS, and drape them around the columns
230 specified in BETWEEN-COLS. */
232 set_loose_columns (System* which, Column_x_positions const *posns)
234 for (int i = 0; i < posns->loose_cols_.size (); i++)
237 Item *loose = dynamic_cast<Item*> (posns->loose_cols_[i]);
238 Paper_column* col = dynamic_cast<Paper_column*> (loose);
247 SCM between = loose->get_grob_property ("between-cols");
248 if (!gh_pair_p (between))
252 Item * l=dynamic_cast<Item*> (unsmob_grob (ly_car (between)));
253 Item * r=dynamic_cast<Item*> (unsmob_grob (ly_cdr (between)));
260 left = l->get_column ();
261 if (!left->get_system ())
262 left = left->find_prebroken_piece (RIGHT);
266 loose = right = r->get_column ();
270 if (!right->get_system ())
271 right = right->find_prebroken_piece (LEFT);
274 We divide the remaining space of the column over the left and
275 right side. At the moment, we
278 Grob * common = right->common_refpoint (left, X_AXIS);
280 Real rx = right->extent(common, X_AXIS)[LEFT];
281 Real lx = left->extent(common, X_AXIS)[RIGHT];
282 Real total_dx = rx - lx;
283 Interval cval =col->extent (col, X_AXIS);
287 We put it in the middle. This is not an ideal solution -- the
288 break alignment code inserts a fixed space before the clef
289 (about 1 SS), while the space following the clef is
290 flexible. In tight situations, the clef will almost be on top
291 of the following note.
294 Real dx = rx-lx - cval.length ();
295 if (total_dx < 2* cval.length ())
298 todo: this is discontinuous. I'm too tired to
299 invent a sliding mechanism. Duh.
308 col->system_ = which;
309 col->translate_axis (- col->relative_coordinate (common, X_AXIS), X_AXIS);
310 col->translate_axis (lx + dx - cval[LEFT], X_AXIS);
316 System::break_into_pieces (Array<Column_x_positions> const &breaking)
318 for (int i=0; i < breaking.size (); i++)
320 System *system = dynamic_cast <System*> (clone ());
323 Link_array<Grob> c (breaking[i].cols_);
324 pscore_->typeset_line (system);
326 system->set_bound (LEFT,c[0]);
327 system->set_bound (RIGHT,c.top ());
328 for (int j=0; j < c.size (); j++)
330 c[j]->translate_axis (breaking[i].config_[j],X_AXIS);
331 dynamic_cast<Paper_column*> (c[j])->system_ = system;
333 set_loose_columns (system, &breaking[i]);
334 broken_intos_.push (system);
339 System::output_molecule (SCM expr, Offset o)
343 if (!gh_pair_p (expr))
346 SCM head =ly_car (expr);
347 if (unsmob_input (head))
349 Input * ip = unsmob_input (head);
351 pscore_->outputter_->output_scheme (scm_list_n (ly_symbol2scm ("define-origin"),
352 scm_makfrom0str (ip->file_string ().to_str0 ()),
353 gh_int2scm (ip->line_number ()),
354 gh_int2scm (ip->column_number ()),
356 expr = ly_cadr (expr);
358 else if (head == ly_symbol2scm ("no-origin"))
360 pscore_->outputter_->output_scheme (scm_list_n (head, SCM_UNDEFINED));
361 expr = ly_cadr (expr);
363 else if (head == ly_symbol2scm ("translate-molecule"))
365 o += ly_scm2offset (ly_cadr (expr));
366 expr = ly_caddr (expr);
368 else if (head == ly_symbol2scm ("combine-molecule"))
370 output_molecule (ly_cadr (expr), o);
371 expr = ly_caddr (expr);
375 pscore_->outputter_->
376 output_scheme (scm_list_n (ly_symbol2scm ("placebox"),
377 gh_double2scm (o[X_AXIS]),
378 gh_double2scm (o[Y_AXIS]),
388 System::output_scheme (SCM s)
390 pscore_->outputter_->output_scheme (s);
394 System::add_column (Paper_column*p)
397 SCM cs = me->get_grob_property ("columns");
398 Grob * prev = gh_pair_p (cs) ? unsmob_grob (ly_car (cs)) : 0;
400 p->rank_ = prev ? Paper_column::get_rank (prev) + 1 : 0;
402 me->set_grob_property ("columns", gh_cons (p->self_scm (), cs));
404 Axis_group_interface::add_element (me, p);
408 System::pre_processing ()
410 for (SCM s = get_grob_property ("all-elements"); gh_pair_p (s); s = ly_cdr (s))
411 unsmob_grob (ly_car (s))->discretionary_processing ();
413 if (verbose_global_b)
414 progress_indication (_f ("Grob count %d ", element_count ()));
417 for (SCM s = get_grob_property ("all-elements"); gh_pair_p (s); s = ly_cdr (s))
418 unsmob_grob (ly_car (s))->handle_prebroken_dependencies ();
420 fixup_refpoints (get_grob_property ("all-elements"));
422 for (SCM s = get_grob_property ("all-elements"); gh_pair_p (s); s = ly_cdr (s))
424 Grob* sc = unsmob_grob (ly_car (s));
425 sc->calculate_dependencies (PRECALCED, PRECALCING, ly_symbol2scm ("before-line-breaking-callback"));
428 progress_indication ("\n" + _ ("Calculating line breaks...") + " ");
429 for (SCM s = get_grob_property ("all-elements"); gh_pair_p (s); s = ly_cdr (s))
431 Grob * e = unsmob_grob (ly_car (s));
432 SCM proc = e->get_grob_property ("spacing-procedure");
433 if (gh_procedure_p (proc))
434 gh_call1 (proc, e->self_scm ());
439 const int LAYER_COUNT= 3;
444 System::post_processing (bool last_line)
446 for (SCM s = get_grob_property ("all-elements");
447 gh_pair_p (s); s = ly_cdr (s))
449 Grob* sc = unsmob_grob (ly_car (s));
450 sc->calculate_dependencies (POSTCALCED, POSTCALCING,
451 ly_symbol2scm ("after-line-breaking-callback"));
454 Interval i (extent (this, Y_AXIS));
456 programming_error ("Huh? Empty System?");
458 translate_axis (- i[MAX], Y_AXIS);
460 Real height = i.length ();
463 programming_error ("Improbable system height");
468 generate all molecules to trigger all font loads.
470 (ugh. This is not very memory efficient.) */
472 SCM all = get_grob_property ("all-elements") ;
473 all = uniquify_list (all);
476 triger font loads first.
478 This might seem inefficient, but Molecules are cached per grob
481 this->get_molecule();
482 for (SCM s = all; gh_pair_p (s); s = ly_cdr (s))
484 Grob * g = unsmob_grob (ly_car (s));
491 SCM font_names = ly_quote_scm (get_paper ()->font_descriptions ());
492 output_scheme (scm_list_n (ly_symbol2scm ("define-fonts"),
499 Interval j (extent (this, X_AXIS));
500 Real length = j[RIGHT];
502 output_scheme (scm_list_n (ly_symbol2scm ("start-system"),
503 gh_double2scm (length),
504 gh_double2scm (height),
507 /* Output elements in three layers, 0, 1, 2.
508 The default layer is 1. */
510 Molecule *m = this->get_molecule();
512 output_molecule (m->get_expr (), Offset(0,0));
515 for (int i = 0; i < 3; i++)
516 for (SCM s = get_grob_property ("all-elements"); gh_pair_p (s);
519 Grob *sc = unsmob_grob (ly_car (s));
520 Molecule *m = sc->get_molecule ();
524 SCM s = sc->get_grob_property ("layer");
525 int layer = gh_number_p (s) ? gh_scm2int (s) : 1;
529 Offset o (sc->relative_coordinate (this, X_AXIS),
530 sc->relative_coordinate (this, Y_AXIS));
532 SCM e = sc->get_grob_property ("extra-offset");
535 Offset z = ly_scm2offset (e);
536 z *= Staff_symbol_referencer::staff_space (sc);
541 output_molecule (m->get_expr (), o);
548 output_scheme (scm_list_n (ly_symbol2scm ("stop-last-system"), SCM_UNDEFINED));
552 output_scheme (scm_list_n (ly_symbol2scm ("stop-system"), SCM_UNDEFINED));
558 System::broken_col_range (Item const*l, Item const*r) const
560 Link_array<Item> ret;
562 l = l->get_column ();
563 r = r->get_column ();
564 SCM s = get_grob_property ("columns");
566 while (gh_pair_p (s) && ly_car (s) != r->self_scm ())
572 while (gh_pair_p (s) && ly_car (s) != l->self_scm ())
574 Paper_column*c = dynamic_cast<Paper_column*> (unsmob_grob (ly_car (s)));
575 if (Item::breakable_b (c) && !c->system_)
586 Return all columns, but filter out any unused columns , since they might
587 disrupt the spacing problem.
590 System::columns ()const
593 = Pointer_group_interface__extract_grobs (this, (Grob*) 0, "columns");
595 for (int i= acs.size (); i -- ;)
597 bool brb = Item::breakable_b (acs[i]);
598 bfound = bfound || brb;
601 the last column should be breakable. Weed out any columns that
602 seem empty. We need to retain breakable columns, in case
603 someone forced a breakpoint.
605 if (!bfound || !Paper_column::used_b (acs[i]))
614 ADD_INTERFACE (System,"system-interface",
615 "Super grob, parent of all: "
617 "The columns of a score that form one line. The toplevel grob. Any "
618 "grob has a Line_of_score as both X and Y reference point. The "
619 "Paper_score contains one grob of this type. Control enters the "
620 "Grob dependency calculation from this single Line_of_score "
622 "between-system-string all-elements columns");