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"
24 #include "paper-line.hh"
26 System::System (SCM s)
33 System::element_count () const
35 return scm_ilength (get_property ("all-elements"));
39 System::spanner_count () const
42 for (SCM s = get_property ("all-elements");
43 gh_pair_p (s); s = ly_cdr (s))
45 if (dynamic_cast<Spanner*> (unsmob_grob (gh_car (s))))
54 scm_default_compare (const void * a, const void *b)
59 if (pa < pb) return -1 ;
60 else if (pa > pb) return 1;
65 modify L in place: sort it
71 int len = scm_ilength (l);
72 SCM * arr = new SCM[len];
74 for (SCM s =l ; SCM_NNULLP (s); s = SCM_CDR (s))
75 arr[k++] = SCM_CAR (s);
78 qsort (arr, len, sizeof (SCM), &scm_default_compare);
82 for (int i = 0; i < len ; i++)
84 if (i && arr[i] == arr[i-1])
87 SCM_SETCAR (s, arr[i]);
93 SCM_SETCDR (s, SCM_EOL);
100 System::typeset_grob (Grob * elem)
103 programming_error ("Adding element twice.");
105 elem->pscore_ = pscore_;
106 Pointer_group_interface::add_grob (this, ly_symbol2scm ("all-elements"), elem);
107 scm_gc_unprotect_object (elem->self_scm ());
112 fixup_refpoints (SCM s)
114 for (; gh_pair_p (s); s = ly_cdr (s))
116 Grob::fixup_refpoint (ly_car (s));
123 for (SCM s = get_property ("all-elements"); gh_pair_p (s); s = ly_cdr (s))
125 Grob *g = unsmob_grob (ly_car (s));
126 if (g->internal_has_interface (ly_symbol2scm ("only-prebreak-interface")))
129 Kill no longer needed grobs.
131 Item * it = dynamic_cast<Item*> (g);
132 if (it && Item::is_breakable (it))
134 it->find_prebroken_piece (LEFT)->suicide ();
135 it->find_prebroken_piece (RIGHT)->suicide ();
140 g->do_break_processing ();
144 fixups must be done in broken line_of_scores, because new elements
145 are put over there. */
147 for (int i=0; i < broken_intos_.size (); i++)
149 Grob *se = broken_intos_[i];
150 SCM all = se->get_property ("all-elements");
151 for (SCM s = all; gh_pair_p (s); s = ly_cdr (s))
152 fixup_refpoint (ly_car (s));
153 count += scm_ilength (all);
157 needed for doing items.
159 fixup_refpoints (get_property ("all-elements"));
161 for (SCM s = get_property ("all-elements"); gh_pair_p (s); s = ly_cdr (s))
162 unsmob_grob (ly_car (s))->handle_broken_dependencies ();
163 handle_broken_dependencies ();
165 #if 0 /* don't do this: strange side effects. */
167 /* Because the this->get_property (all-elements) contains items in 3
168 versions, handle_broken_dependencies () will leave duplicated
169 items in all-elements. Strictly speaking this is harmless, but
170 it leads to duplicated symbols in the output. uniquify_list ()
171 makes sure that no duplicates are in the list. */
172 for (int i = 0; i < line_count; i++)
174 SCM all = broken_intos_[i]->get_property ("all-elements");
175 all = uniquify_list (all);
179 if (verbose_global_b)
180 progress_indication (_f ("Element count %d.", count + element_count ()));
182 int line_count = broken_intos_.size ();
183 SCM lines = scm_c_make_vector (line_count, SCM_UNDEFINED);
185 for (int i = 0; i < line_count; i++)
187 if (verbose_global_b)
188 progress_indication ("[");
190 System *system = dynamic_cast<System*> (broken_intos_[i]);
191 system->post_processing ();
192 scm_vector_set_x (lines, scm_int2num (i), system->get_line ());
194 if (verbose_global_b)
195 progress_indication (to_string (i) + "]");
204 Find the loose columns in POSNS, and drape them around the columns
205 specified in BETWEEN-COLS. */
207 set_loose_columns (System* which, Column_x_positions const *posns)
209 for (int i = 0; i < posns->loose_cols_.size (); i++)
212 Item *loose = dynamic_cast<Item*> (posns->loose_cols_[i]);
213 Paper_column* col = dynamic_cast<Paper_column*> (loose);
222 SCM between = loose->get_property ("between-cols");
223 if (!gh_pair_p (between))
227 Item * l=dynamic_cast<Item*> (unsmob_grob (ly_car (between)));
228 Item * r=dynamic_cast<Item*> (unsmob_grob (ly_cdr (between)));
235 left = l->get_column ();
236 if (!left->get_system ())
237 left = left->find_prebroken_piece (RIGHT);
241 loose = right = r->get_column ();
245 if (!right->get_system ())
246 right = right->find_prebroken_piece (LEFT);
249 We divide the remaining space of the column over the left and
250 right side. At the moment, we
253 Grob * common = right->common_refpoint (left, X_AXIS);
255 Real rx = right->extent (common, X_AXIS)[LEFT];
256 Real lx = left->extent (common, X_AXIS)[RIGHT];
257 Real total_dx = rx - lx;
258 Interval cval =col->extent (col, X_AXIS);
262 We put it in the middle. This is not an ideal solution -- the
263 break alignment code inserts a fixed space before the clef
264 (about 1 SS), while the space following the clef is
265 flexible. In tight situations, the clef will almost be on top
266 of the following note.
269 Real dx = rx-lx - cval.length ();
270 if (total_dx < 2* cval.length ())
273 todo: this is discontinuous. I'm too tired to
274 invent a sliding mechanism. Duh.
283 col->system_ = which;
284 col->translate_axis (- col->relative_coordinate (common, X_AXIS), X_AXIS);
285 col->translate_axis (lx + dx - cval[LEFT], X_AXIS);
291 System::break_into_pieces (Array<Column_x_positions> const &breaking)
293 for (int i=0; i < breaking.size (); i++)
295 System *system = dynamic_cast <System*> (clone ());
298 Link_array<Grob> c (breaking[i].cols_);
299 pscore_->typeset_line (system);
301 system->set_bound (LEFT,c[0]);
302 system->set_bound (RIGHT,c.top ());
303 for (int j=0; j < c.size (); j++)
305 c[j]->translate_axis (breaking[i].config_[j],X_AXIS);
306 dynamic_cast<Paper_column*> (c[j])->system_ = system;
308 set_loose_columns (system, &breaking[i]);
309 broken_intos_.push (system);
314 System::add_column (Paper_column*p)
317 SCM cs = me->get_property ("columns");
318 Grob * prev = gh_pair_p (cs) ? unsmob_grob (ly_car (cs)) : 0;
320 p->rank_ = prev ? Paper_column::get_rank (prev) + 1 : 0;
322 me->set_property ("columns", gh_cons (p->self_scm (), cs));
324 Axis_group_interface::add_element (me, p);
328 System::pre_processing ()
330 for (SCM s = get_property ("all-elements"); gh_pair_p (s); s = ly_cdr (s))
331 unsmob_grob (ly_car (s))->discretionary_processing ();
333 if (verbose_global_b)
334 progress_indication (_f ("Grob count %d ", element_count ()));
337 for (SCM s = get_property ("all-elements"); gh_pair_p (s); s = ly_cdr (s))
338 unsmob_grob (ly_car (s))->handle_prebroken_dependencies ();
340 fixup_refpoints (get_property ("all-elements"));
342 for (SCM s = get_property ("all-elements"); gh_pair_p (s); s = ly_cdr (s))
344 Grob* sc = unsmob_grob (ly_car (s));
345 sc->calculate_dependencies (PRECALCED, PRECALCING, ly_symbol2scm ("before-line-breaking-callback"));
348 progress_indication ("\n" + _ ("Calculating line breaks...") + " ");
349 for (SCM s = get_property ("all-elements"); gh_pair_p (s); s = ly_cdr (s))
351 Grob * e = unsmob_grob (ly_car (s));
352 SCM proc = e->get_property ("spacing-procedure");
353 if (gh_procedure_p (proc))
354 gh_call1 (proc, e->self_scm ());
359 System::post_processing ()
361 for (SCM s = get_property ("all-elements"); gh_pair_p (s); s = ly_cdr (s))
363 Grob *g = unsmob_grob (ly_car (s));
364 g->calculate_dependencies (POSTCALCED, POSTCALCING,
365 ly_symbol2scm ("after-line-breaking-callback"));
368 Interval iv (extent (this, Y_AXIS));
370 programming_error ("System with zero extent.");
372 translate_axis (-iv[MAX], Y_AXIS);
374 /* Generate all stencils to trigger font loads.
375 This might seem inefficient, but Stencils are cached per grob
377 SCM all = get_property ("all-elements");
378 all = uniquify_list (all);
380 this->get_stencil ();
381 for (SCM s = all; gh_pair_p (s); s = ly_cdr (s))
383 Grob *g = unsmob_grob (ly_car (s));
391 static int const LAYER_COUNT = 3;
392 SCM stencils = SCM_EOL;
393 if (Stencil *me = get_stencil ())
394 stencils = scm_cons (me->smobbed_copy (), stencils);
396 /* Output stencils in three layers: 0, 1, 2. The default layer is
398 for (int i = 0; i < LAYER_COUNT; i++)
399 for (SCM s = get_property ("all-elements"); gh_pair_p (s); s = ly_cdr (s))
401 Grob *g = unsmob_grob (ly_car (s));
402 Stencil *stil = g->get_stencil ();
404 /* Skip empty stencils and grobs that are not in this layer. */
406 || robust_scm2int (g->get_property ("layer"), 1) != i)
409 Offset o (g->relative_coordinate (this, X_AXIS),
410 g->relative_coordinate (this, Y_AXIS));
412 Offset extra = robust_scm2offset (g->get_property ("extra-offset"),
414 * Staff_symbol_referencer::staff_space (g);
416 stil->translate ((o + extra) * 0.5);
417 stencils = scm_cons (stil->smobbed_copy (), stencils);
420 if (output_format_global != PAGE_LAYOUT)
422 SCM lastcol = ly_car (get_property ("columns"));
423 Grob *g = unsmob_grob (lastcol);
425 SCM between = ly_symbol2scm ("between-system-string");
426 SCM inter = g->internal_get_property (between);
427 if (gh_string_p (inter))
428 stencils = scm_cons (scm_cons (between, inter), stencils);
431 Interval x (extent (this, X_AXIS));
432 Interval y (extent (this, Y_AXIS));
433 Paper_line pl (Offset (x.length (), y.length ()), stencils);
434 return pl.smobbed_copy ();
438 System::broken_col_range (Item const*l, Item const*r) const
440 Link_array<Item> ret;
442 l = l->get_column ();
443 r = r->get_column ();
444 SCM s = get_property ("columns");
446 while (gh_pair_p (s) && ly_car (s) != r->self_scm ())
452 while (gh_pair_p (s) && ly_car (s) != l->self_scm ())
454 Paper_column*c = dynamic_cast<Paper_column*> (unsmob_grob (ly_car (s)));
455 if (Item::is_breakable (c) && !c->system_)
466 Return all columns, but filter out any unused columns , since they might
467 disrupt the spacing problem.
470 System::columns ()const
473 = Pointer_group_interface__extract_grobs (this, (Grob*) 0, "columns");
475 for (int i= acs.size (); i -- ;)
477 bool brb = Item::is_breakable (acs[i]);
478 bfound = bfound || brb;
481 the last column should be breakable. Weed out any columns that
482 seem empty. We need to retain breakable columns, in case
483 someone forced a breakpoint.
485 if (!bfound || !Paper_column::is_used (acs[i]))
491 ADD_INTERFACE (System,"system-interface",
492 "This is the toplevel object: each object in a score "
493 "ultimately has a System object as its X and Y parent. ",
494 "between-system-string all-elements columns")