''
[lilypond.git] / lily / system.cc
blobf9fe1f4edd8e316e1433b528e0f8d3f268c6652f
1 /*
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>
7 */
9 #include "input-smob.hh"
10 #include "axis-group-interface.hh"
11 #include "debug.hh"
12 #include "system.hh"
13 #include "main.hh"
14 #include "paper-column.hh"
15 #include "paper-def.hh"
16 #include "paper-outputter.hh"
17 #include "paper-score.hh"
18 #include "string.hh"
19 #include "warn.hh"
20 #include "dimensions.hh"
21 #include "molecule.hh"
22 #include "all-font-metrics.hh"
23 #include "spacing-interface.hh"
25 // todo: use map.
26 void
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)
37 : Spanner (s)
39 rank_i_ = 0;
42 int
43 System::element_count () const
45 return scm_ilength (get_grob_property ("all-elements"));
48 void
49 System::typeset_grob (Grob * elem_p)
51 elem_p->pscore_l_ = pscore_l_;
52 Pointer_group_interface::add_grob (this, ly_symbol2scm ("all-elements"),elem_p);
53 scm_gc_unprotect_object (elem_p->self_scm ());
56 void
57 System::output_lines ()
59 for (SCM s = get_grob_property ("all-elements");
60 gh_pair_p (s); s = ly_cdr (s))
62 Grob * g = unsmob_grob (ly_car (s));
63 if (Spacing_interface::has_interface (g))
66 Kill no longer needed grobs.
68 Item * it = dynamic_cast<Item*> (g);
69 if (it && Item::breakable_b(it))
71 it->find_prebroken_piece (LEFT)->suicide();
72 it->find_prebroken_piece (RIGHT)->suicide();
74 g->suicide ();
76 else
77 g->do_break_processing ();
81 fixups must be done in broken line_of_scores, because new elements
82 are put over there. */
83 int count = 0;
84 for (int i=0; i < broken_into_l_arr_.size (); i++)
86 Grob *se = broken_into_l_arr_[i];
87 SCM all = se->get_grob_property ("all-elements");
88 for (SCM s = all; gh_pair_p (s); s = ly_cdr (s))
90 fixup_refpoint (ly_car (s));
92 count += scm_ilength (all);
96 needed for doing items.
98 fixup_refpoints (get_grob_property ("all-elements"));
101 for (SCM s = get_grob_property ("all-elements");
102 gh_pair_p (s); s = ly_cdr (s))
104 unsmob_grob (ly_car (s))->handle_broken_dependencies ();
106 handle_broken_dependencies ();
108 if (verbose_global_b)
109 progress_indication (_f ("Element count %d.", count + element_count ()));
112 for (int i=0; i < broken_into_l_arr_.size (); i++)
114 System *line_l = dynamic_cast<System*> (broken_into_l_arr_[i]);
116 if (verbose_global_b)
117 progress_indication ("[");
118 line_l->post_processing (i+1 == broken_into_l_arr_.size ());
120 if (verbose_global_b)
122 progress_indication (to_str (i));
123 progress_indication ("]");
126 if (i < broken_into_l_arr_.size () - 1)
128 SCM lastcol = ly_car (line_l->get_grob_property ("columns"));
129 Grob* e = unsmob_grob (lastcol);
131 SCM between = ly_symbol2scm ("between-system-string");
132 SCM inter = e->internal_get_grob_property (between);
133 if (gh_string_p (inter))
135 pscore_l_->outputter_l_
136 ->output_scheme (scm_list_n (between,
137 inter, SCM_UNDEFINED));
147 Find the loose columns in POSNS, and drape them around the columns
148 specified in BETWEEN-COLS. */
149 void
150 set_loose_columns (System* which, Column_x_positions const *posns)
152 for (int i = 0; i < posns->loose_cols_.size (); i++)
154 int divide_over = 1;
155 Item *loose = dynamic_cast<Item*> (posns->loose_cols_[i]);
156 Paper_column* col = dynamic_cast<Paper_column*> (loose);
158 if (col->line_l_)
159 continue;
162 Item * left = 0;
163 Item * right = 0;
166 SCM between = loose->get_grob_property ("between-cols");
167 if (!gh_pair_p (between))
168 break;
171 Item * l=dynamic_cast<Item*> (unsmob_grob (ly_car (between)));
172 Item * r=dynamic_cast<Item*> (unsmob_grob (ly_cdr (between)));
174 if (!(l && r))
175 break ;
177 if (!left && l)
179 left = l->column_l ();
182 divide_over ++;
184 loose = right = r->column_l ();
186 while (1);
189 #if 0
190 Real rx = right->relative_coordinate (right->get_parent (X_AXIS), X_AXIS);
191 Real lx = left->relative_coordinate (left->get_parent (X_AXIS), X_AXIS);
194 divide space equally over loose columns.
196 int j = 1;
197 loose = col;
198 while (1)
200 SCM between = loose->get_grob_property ("between-cols");
201 if (!gh_pair_p (between))
202 break;
204 Paper_column *thiscol = dynamic_cast<Paper_column*> (loose);
206 thiscol->line_l_ = which;
207 thiscol->translate_axis (lx + j*(rx - lx)/divide_over, X_AXIS);
209 j ++;
210 loose = dynamic_cast<Item*> (unsmob_grob (ly_cdr (between)));
212 #else
214 We divide the remaining space of the column over the left and
215 right side. At the moment, we
218 Grob * common = right->common_refpoint (left, X_AXIS);
220 Real rx = right->extent(common, X_AXIS)[LEFT];
221 Real lx = left->extent(common, X_AXIS)[RIGHT];
222 Real total_dx = rx - lx;
223 Interval cval =col->extent (col, X_AXIS);
227 We put it in the middle. This is not an ideal solution -- the
228 break alignment code inserts a fixed space before the clef
229 (about 1 SS), while the space following the clef is
230 flexible. In tight situations, the clef will almost be on top
231 of the following note.
234 Real dx = rx-lx - cval.length ();
235 if (total_dx < 2* cval.length ())
238 todo: this is discontinuous. I'm too tired to
239 invent a sliding mechanism. Duh.
241 TODO.
243 dx *= 0.25;
245 else
246 dx *= 0.5;
248 col->line_l_ = which;
249 col->translate_axis (lx + dx - cval[LEFT], X_AXIS);
250 #endif
254 // const?
255 void
256 System::break_into_pieces (Array<Column_x_positions> const &breaking)
258 for (int i=0; i < breaking.size (); i++)
260 System *line_l = dynamic_cast <System*> (clone ());
261 line_l->rank_i_ = i;
262 // line_l->set_immutable_grob_property ("rank", gh_int2scm (i));
263 Link_array<Grob> c (breaking[i].cols_);
264 pscore_l_->typeset_line (line_l);
266 line_l->set_bound (LEFT,c[0]);
267 line_l->set_bound (RIGHT,c.top ());
268 for (int j=0; j < c.size (); j++)
270 c[j]->translate_axis (breaking[i].config_[j],X_AXIS);
271 dynamic_cast<Paper_column*> (c[j])->line_l_ = line_l;
273 set_loose_columns (line_l, &breaking[i]);
274 broken_into_l_arr_.push (line_l);
279 void
280 System::output_molecule (SCM expr, Offset o)
283 while (1)
285 if (!gh_pair_p (expr))
286 return;
288 SCM head =ly_car (expr);
289 if (unsmob_input (head))
291 Input * ip = unsmob_input (head);
294 pscore_l_->outputter_l_->output_scheme (scm_list_n (ly_symbol2scm ("define-origin"),
295 ly_str02scm (ip->file_str ().ch_C ()),
296 gh_int2scm (ip->line_number ()),
297 gh_int2scm (ip->column_number ()),
298 SCM_UNDEFINED));
299 expr = ly_cadr (expr);
301 else if (head == ly_symbol2scm ("no-origin"))
303 pscore_l_->outputter_l_->output_scheme (scm_list_n (head, SCM_UNDEFINED));
304 expr = ly_cadr (expr);
306 else if (head == ly_symbol2scm ("translate-molecule"))
308 o += ly_scm2offset (ly_cadr (expr));
309 expr = ly_caddr (expr);
311 else if (head == ly_symbol2scm ("combine-molecule"))
313 output_molecule (ly_cadr (expr), o);
314 expr = ly_caddr (expr);
316 else
318 pscore_l_->outputter_l_->
319 output_scheme (scm_list_n (ly_symbol2scm ("placebox"),
320 gh_double2scm (o[X_AXIS]),
321 gh_double2scm (o[Y_AXIS]),
322 expr,
323 SCM_UNDEFINED));
325 return;
330 void
331 System::output_scheme (SCM s)
333 pscore_l_->outputter_l_->output_scheme (s);
336 void
337 System::add_column (Paper_column*p)
339 Grob *me = this;
340 SCM cs = me->get_grob_property ("columns");
341 Grob * prev = gh_pair_p (cs) ? unsmob_grob (ly_car (cs)) : 0;
343 p->rank_i_ = prev ? Paper_column::rank_i (prev) + 1 : 0;
345 me->set_grob_property ("columns", gh_cons (p->self_scm (), cs));
347 Axis_group_interface::add_element (me, p);
353 TODO: use scm_map iso. for loops.
355 void
356 System::pre_processing ()
358 for (SCM s = get_grob_property ("all-elements"); gh_pair_p (s); s = ly_cdr (s))
359 unsmob_grob (ly_car (s))->discretionary_processing ();
361 if (verbose_global_b)
362 progress_indication (_f ("Grob count %d ", element_count ()));
365 for (SCM s = get_grob_property ("all-elements"); gh_pair_p (s); s = ly_cdr (s))
366 unsmob_grob (ly_car (s))->handle_prebroken_dependencies ();
368 fixup_refpoints (get_grob_property ("all-elements"));
370 for (SCM s = get_grob_property ("all-elements"); gh_pair_p (s); s = ly_cdr (s))
372 Grob* sc = unsmob_grob (ly_car (s));
373 sc->calculate_dependencies (PRECALCED, PRECALCING, ly_symbol2scm ("before-line-breaking-callback"));
376 progress_indication ("\n" + _ ("Calculating column positions...") + " ");
377 for (SCM s = get_grob_property ("all-elements"); gh_pair_p (s); s = ly_cdr (s))
379 Grob * e = unsmob_grob (ly_car (s));
380 SCM proc = e->get_grob_property ("spacing-procedure");
381 if (gh_procedure_p (proc))
382 gh_call1 (proc, e->self_scm ());
386 void
387 System::post_processing (bool last_line)
389 for (SCM s = get_grob_property ("all-elements");
390 gh_pair_p (s); s = ly_cdr (s))
392 Grob* sc = unsmob_grob (ly_car (s));
393 sc->calculate_dependencies (POSTCALCED, POSTCALCING,
394 ly_symbol2scm ("after-line-breaking-callback"));
397 Interval i (extent (this, Y_AXIS));
398 if (i.empty_b ())
399 programming_error ("Huh? Empty System?");
400 else
401 translate_axis (- i[MAX], Y_AXIS);
403 Real height = i.length ();
404 if (height > 50 CM)
406 programming_error ("Improbable system height");
407 height = 50 CM;
411 generate all molecules to trigger all font loads.
413 (ugh. This is not very memory efficient.) */
414 this->get_molecule();
415 for (SCM s = get_grob_property ("all-elements"); gh_pair_p (s); s = ly_cdr (s))
417 unsmob_grob (ly_car (s))->get_molecule ();
420 font defs;
422 SCM font_names = ly_quote_scm (paper_l ()->font_descriptions ());
423 output_scheme (scm_list_n (ly_symbol2scm ("define-fonts"),
424 font_names,
425 SCM_UNDEFINED));
428 line preamble.
430 output_scheme (scm_list_n (ly_symbol2scm ("start-system"),
431 gh_double2scm (height),
432 SCM_UNDEFINED));
434 /* Output elements in three layers, 0, 1, 2.
435 The default layer is 1. */
438 Molecule *m = this->get_molecule();
439 if (m)
440 output_molecule (m->get_expr (), Offset(0,0));
443 for (int i = 0; i < 3; i++)
444 for (SCM s = get_grob_property ("all-elements"); gh_pair_p (s);
445 s = ly_cdr (s))
447 Grob *sc = unsmob_grob (ly_car (s));
448 Molecule *m = sc->get_molecule ();
449 if (!m)
450 continue;
452 SCM s = sc->get_grob_property ("layer");
453 int layer = gh_number_p (s) ? gh_scm2int (s) : 1;
454 if (layer != i)
455 continue;
457 Offset o (sc->relative_coordinate (this, X_AXIS),
458 sc->relative_coordinate (this, Y_AXIS));
460 SCM e = sc->get_grob_property ("extra-offset");
461 if (gh_pair_p (e))
463 o[X_AXIS] += gh_scm2double (ly_car (e));
464 o[Y_AXIS] += gh_scm2double (ly_cdr (e));
467 output_molecule (m->get_expr (), o);
472 if (last_line)
474 output_scheme (scm_list_n (ly_symbol2scm ("stop-last-system"), SCM_UNDEFINED));
476 else
478 output_scheme (scm_list_n (ly_symbol2scm ("stop-system"), SCM_UNDEFINED));
483 Link_array<Item>
484 System::broken_col_range (Item const*l, Item const*r) const
486 Link_array<Item> ret;
488 l = l->column_l ();
489 r = r->column_l ();
490 SCM s = get_grob_property ("columns");
492 while (gh_pair_p (s) && ly_car (s) != r->self_scm ())
493 s = ly_cdr (s);
495 if (gh_pair_p (s))
496 s = ly_cdr (s);
498 while (gh_pair_p (s) && ly_car (s) != l->self_scm ())
500 Paper_column*c = dynamic_cast<Paper_column*> (unsmob_grob (ly_car (s)));
501 if (Item::breakable_b (c) && !c->line_l_)
502 ret.push (c);
504 s = ly_cdr (s);
507 ret.reverse ();
508 return ret;
512 Return all columns, but filter out any unused columns , since they might
513 disrupt the spacing problem.
515 Link_array<Grob>
516 System::column_l_arr ()const
518 Link_array<Grob> acs
519 = Pointer_group_interface__extract_grobs (this, (Grob*) 0, "columns");
520 bool bfound = false;
521 for (int i= acs.size (); i -- ;)
523 bool brb = Item::breakable_b (acs[i]);
524 bfound = bfound || brb;
527 the last column should be breakable. Weed out any columns that
528 seem empty. We need to retain breakable columns, in case
529 someone forced a breakpoint.
531 if (!bfound || !Paper_column::used_b (acs[i]))
532 acs.del (i);
534 return acs;
540 ADD_INTERFACE (System,"system-interface",
541 "Super grob, parent of all:
543 The columns of a score that form one line. The toplevel grob. Any
544 grob has a Line_of_score as both X and Y reference point. The
545 Paper_score contains one grob of this type. Control enters the
546 Grob dependency calculation from this single Line_of_score
547 object.",
548 "between-system-string all-elements columns");