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>
9 #include "axis-group-interface.hh"
13 #include "paper-column.hh"
14 #include "paper-def.hh"
15 #include "paper-outputter.hh"
16 #include "paper-score.hh"
20 #include "all-font-metrics.hh"
21 #include "spacing-interface.hh"
22 #include "staff-symbol-referencer.hh"
23 #include "paper-book.hh"
25 System::System (SCM s)
32 System::element_count () const
34 return scm_ilength (get_property ("all-elements"));
38 System::spanner_count () const
41 for (SCM s = get_property ("all-elements");
42 gh_pair_p (s); s = ly_cdr (s))
44 if (dynamic_cast<Spanner*> (unsmob_grob (gh_car (s))))
53 scm_default_compare (const void * a, const void *b)
58 if (pa < pb) return -1 ;
59 else if (pa > pb) return 1;
64 modify L in place: sort it
70 int len = scm_ilength (l);
71 SCM * arr = new SCM[len];
73 for (SCM s =l ; SCM_NNULLP (s); s = SCM_CDR (s))
74 arr[k++] = SCM_CAR (s);
77 qsort (arr, len, sizeof (SCM), &scm_default_compare);
81 for (int i = 0; i < len ; i++)
83 if (i && arr[i] == arr[i-1])
86 SCM_SETCAR (s, arr[i]);
92 SCM_SETCDR (s, SCM_EOL);
99 System::typeset_grob (Grob * elem)
102 programming_error ("Adding element twice.");
104 elem->pscore_ = pscore_;
105 Pointer_group_interface::add_grob (this, ly_symbol2scm ("all-elements"), elem);
106 scm_gc_unprotect_object (elem->self_scm ());
111 fixup_refpoints (SCM s)
113 for (; gh_pair_p (s); s = ly_cdr (s))
115 Grob::fixup_refpoint (ly_car (s));
122 for (SCM s = get_property ("all-elements"); gh_pair_p (s); s = ly_cdr (s))
124 Grob *g = unsmob_grob (ly_car (s));
125 if (g->internal_has_interface (ly_symbol2scm ("only-prebreak-interface")))
128 Kill no longer needed grobs.
130 Item * it = dynamic_cast<Item*> (g);
131 if (it && Item::is_breakable (it))
133 it->find_prebroken_piece (LEFT)->suicide ();
134 it->find_prebroken_piece (RIGHT)->suicide ();
139 g->do_break_processing ();
143 fixups must be done in broken line_of_scores, because new elements
144 are put over there. */
146 for (int i=0; i < broken_intos_.size (); i++)
148 Grob *se = broken_intos_[i];
149 SCM all = se->get_property ("all-elements");
150 for (SCM s = all; gh_pair_p (s); s = ly_cdr (s))
151 fixup_refpoint (ly_car (s));
152 count += scm_ilength (all);
156 needed for doing items.
158 fixup_refpoints (get_property ("all-elements"));
160 for (SCM s = get_property ("all-elements"); gh_pair_p (s); s = ly_cdr (s))
161 unsmob_grob (ly_car (s))->handle_broken_dependencies ();
162 handle_broken_dependencies ();
164 #if 0 /* don't do this: strange side effects. */
166 /* Because the this->get_property (all-elements) contains items in 3
167 versions, handle_broken_dependencies () will leave duplicated
168 items in all-elements. Strictly speaking this is harmless, but
169 it leads to duplicated symbols in the output. uniquify_list ()
170 makes sure that no duplicates are in the list. */
171 for (int i = 0; i < line_count; i++)
173 SCM all = broken_intos_[i]->get_property ("all-elements");
174 all = uniquify_list (all);
178 if (verbose_global_b)
179 progress_indication (_f ("Element count %d.", count + element_count ()));
181 int line_count = broken_intos_.size ();
182 SCM lines = scm_c_make_vector (line_count, SCM_UNDEFINED);
184 for (int i = 0; i < line_count; i++)
186 System *system = dynamic_cast<System*> (broken_intos_[i]);
188 if (verbose_global_b)
189 progress_indication ("[");
191 // bool last = (i + 1 == line_count);
193 system->post_processing ();
194 scm_vector_set_x (lines, scm_int2num (i), system->get_line ());
196 if (verbose_global_b)
198 progress_indication (to_string (i));
199 progress_indication ("]");
203 if (i < line_count - 1)
205 SCM lastcol = ly_car (system->get_property ("columns"));
206 Grob* e = unsmob_grob (lastcol);
208 SCM between = ly_symbol2scm ("between-system-string");
209 SCM inter = e->internal_get_property (between);
210 if (gh_string_p (inter))
213 ->output_scheme (scm_list_n (between,
214 inter, SCM_UNDEFINED));
226 Find the loose columns in POSNS, and drape them around the columns
227 specified in BETWEEN-COLS. */
229 set_loose_columns (System* which, Column_x_positions const *posns)
231 for (int i = 0; i < posns->loose_cols_.size (); i++)
234 Item *loose = dynamic_cast<Item*> (posns->loose_cols_[i]);
235 Paper_column* col = dynamic_cast<Paper_column*> (loose);
244 SCM between = loose->get_property ("between-cols");
245 if (!gh_pair_p (between))
249 Item * l=dynamic_cast<Item*> (unsmob_grob (ly_car (between)));
250 Item * r=dynamic_cast<Item*> (unsmob_grob (ly_cdr (between)));
257 left = l->get_column ();
258 if (!left->get_system ())
259 left = left->find_prebroken_piece (RIGHT);
263 loose = right = r->get_column ();
267 if (!right->get_system ())
268 right = right->find_prebroken_piece (LEFT);
271 We divide the remaining space of the column over the left and
272 right side. At the moment, we
275 Grob * common = right->common_refpoint (left, X_AXIS);
277 Real rx = right->extent (common, X_AXIS)[LEFT];
278 Real lx = left->extent (common, X_AXIS)[RIGHT];
279 Real total_dx = rx - lx;
280 Interval cval =col->extent (col, X_AXIS);
284 We put it in the middle. This is not an ideal solution -- the
285 break alignment code inserts a fixed space before the clef
286 (about 1 SS), while the space following the clef is
287 flexible. In tight situations, the clef will almost be on top
288 of the following note.
291 Real dx = rx-lx - cval.length ();
292 if (total_dx < 2* cval.length ())
295 todo: this is discontinuous. I'm too tired to
296 invent a sliding mechanism. Duh.
305 col->system_ = which;
306 col->translate_axis (- col->relative_coordinate (common, X_AXIS), X_AXIS);
307 col->translate_axis (lx + dx - cval[LEFT], X_AXIS);
313 System::break_into_pieces (Array<Column_x_positions> const &breaking)
315 for (int i=0; i < breaking.size (); i++)
317 System *system = dynamic_cast <System*> (clone ());
320 Link_array<Grob> c (breaking[i].cols_);
321 pscore_->typeset_line (system);
323 system->set_bound (LEFT,c[0]);
324 system->set_bound (RIGHT,c.top ());
325 for (int j=0; j < c.size (); j++)
327 c[j]->translate_axis (breaking[i].config_[j],X_AXIS);
328 dynamic_cast<Paper_column*> (c[j])->system_ = system;
330 set_loose_columns (system, &breaking[i]);
331 broken_intos_.push (system);
336 System::add_column (Paper_column*p)
339 SCM cs = me->get_property ("columns");
340 Grob * prev = gh_pair_p (cs) ? unsmob_grob (ly_car (cs)) : 0;
342 p->rank_ = prev ? Paper_column::get_rank (prev) + 1 : 0;
344 me->set_property ("columns", gh_cons (p->self_scm (), cs));
346 Axis_group_interface::add_element (me, p);
350 System::pre_processing ()
352 for (SCM s = get_property ("all-elements"); gh_pair_p (s); s = ly_cdr (s))
353 unsmob_grob (ly_car (s))->discretionary_processing ();
355 if (verbose_global_b)
356 progress_indication (_f ("Grob count %d ", element_count ()));
359 for (SCM s = get_property ("all-elements"); gh_pair_p (s); s = ly_cdr (s))
360 unsmob_grob (ly_car (s))->handle_prebroken_dependencies ();
362 fixup_refpoints (get_property ("all-elements"));
364 for (SCM s = get_property ("all-elements"); gh_pair_p (s); s = ly_cdr (s))
366 Grob* sc = unsmob_grob (ly_car (s));
367 sc->calculate_dependencies (PRECALCED, PRECALCING, ly_symbol2scm ("before-line-breaking-callback"));
370 progress_indication ("\n" + _ ("Calculating line breaks...") + " ");
371 for (SCM s = get_property ("all-elements"); gh_pair_p (s); s = ly_cdr (s))
373 Grob * e = unsmob_grob (ly_car (s));
374 SCM proc = e->get_property ("spacing-procedure");
375 if (gh_procedure_p (proc))
376 gh_call1 (proc, e->self_scm ());
381 System::post_processing ()
383 for (SCM s = get_property ("all-elements"); gh_pair_p (s); s = ly_cdr (s))
385 Grob *g = unsmob_grob (ly_car (s));
386 g->calculate_dependencies (POSTCALCED, POSTCALCING,
387 ly_symbol2scm ("after-line-breaking-callback"));
390 Interval iv (extent (this, Y_AXIS));
392 programming_error ("System with zero extent.");
394 translate_axis (-iv[MAX], Y_AXIS);
396 /* Generate all stencils to trigger font loads.
397 This might seem inefficient, but Stencils are cached per grob
399 SCM all = get_property ("all-elements") ;
400 all = uniquify_list (all);
402 this->get_stencil ();
403 for (SCM s = all; gh_pair_p (s); s = ly_cdr (s))
405 Grob *g = unsmob_grob (ly_car (s));
411 ((HEIGHT . WIDTH) LINE)
412 LINE: list of ((OFFSET-X . OFFSET-Y) . STENCIL)
413 Maybe make clas/smob? */
417 static int const LAYER_COUNT = 3;
419 if (Stencil *me = get_stencil ())
420 line = scm_cons (scm_cons (ly_offset2scm (Offset (0, 0)),
421 me->smobbed_copy ()), line);
423 /* Output stencils in three layers: 0, 1, 2. The default layer is
425 for (int i = 0; i < LAYER_COUNT; i++)
426 for (SCM s = get_property ("all-elements"); gh_pair_p (s); s = ly_cdr (s))
428 Grob *g = unsmob_grob (ly_car (s));
429 Stencil *stil = g->get_stencil ();
431 /* Skip empty stencils and grobs that are not in this layer. */
433 || robust_scm2int (g->get_property ("layer"), 1) != i)
436 Offset o (g->relative_coordinate (this, X_AXIS),
437 g->relative_coordinate (this, Y_AXIS));
439 Offset extra = robust_scm2offset (g->get_property ("extra-offset"),
441 * Staff_symbol_referencer::staff_space (g);
442 line = scm_cons (scm_cons (ly_offset2scm (o + extra),
443 stil->smobbed_copy ()), line);
446 Interval x (extent (this, X_AXIS));
447 Interval y (extent (this, Y_AXIS));
448 return scm_cons (ly_offset2scm (Offset (x.length (), y.length ())), line);
452 System::broken_col_range (Item const*l, Item const*r) const
454 Link_array<Item> ret;
456 l = l->get_column ();
457 r = r->get_column ();
458 SCM s = get_property ("columns");
460 while (gh_pair_p (s) && ly_car (s) != r->self_scm ())
466 while (gh_pair_p (s) && ly_car (s) != l->self_scm ())
468 Paper_column*c = dynamic_cast<Paper_column*> (unsmob_grob (ly_car (s)));
469 if (Item::is_breakable (c) && !c->system_)
480 Return all columns, but filter out any unused columns , since they might
481 disrupt the spacing problem.
484 System::columns ()const
487 = Pointer_group_interface__extract_grobs (this, (Grob*) 0, "columns");
489 for (int i= acs.size (); i -- ;)
491 bool brb = Item::is_breakable (acs[i]);
492 bfound = bfound || brb;
495 the last column should be breakable. Weed out any columns that
496 seem empty. We need to retain breakable columns, in case
497 someone forced a breakpoint.
499 if (!bfound || !Paper_column::is_used (acs[i]))
505 ADD_INTERFACE (System,"system-interface",
506 "This is the toplevel object: each object in a score "
507 "ultimately has a System object as its X and Y parent. ",
508 "between-system-string all-elements columns")