]> git.donarmstrong.com Git - lilypond.git/blob - lily/system.cc
* lily/include/debug.hh: deprecate.
[lilypond.git] / lily / system.cc
1 /*
2   system.cc -- implement System
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1996--2002 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8
9 #include "input-smob.hh"
10 #include "axis-group-interface.hh"
11 #include "warn.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"
24
25 // todo: use map.
26 void
27 fixup_refpoints (SCM s)
28 {
29   for (; gh_pair_p (s); s = ly_cdr (s))
30     {
31       Grob::fixup_refpoint (ly_car (s));
32     }
33 }
34
35
36 System::System (SCM s)
37   : Spanner (s)
38 {
39   rank_i_ = 0;
40 }
41
42 int
43 System::element_count () const
44 {
45   return scm_ilength (get_grob_property ("all-elements"));
46 }
47
48 void
49 System::typeset_grob (Grob * elem_p)
50 {
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 ());
54 }
55
56 void
57 System::output_lines ()
58 {
59   for (SCM s = get_grob_property ("all-elements");
60        gh_pair_p (s); s = ly_cdr (s))
61     {
62       Grob * g = unsmob_grob (ly_car (s));
63       if (Spacing_interface::has_interface (g))
64         {
65           /*
66             Kill no longer needed grobs. 
67            */
68           Item * it = dynamic_cast<Item*> (g);
69           if (it && Item::breakable_b(it))
70             {
71               it->find_prebroken_piece (LEFT)->suicide();
72               it->find_prebroken_piece (RIGHT)->suicide();
73             }
74           g->suicide ();
75         }
76       else if (g->live ())
77         g->do_break_processing ();
78     }
79
80   /*
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++)
85     {
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))
89         {
90           fixup_refpoint (ly_car (s));
91         }
92       count += scm_ilength (all);
93     }
94   
95   /*
96     needed for doing items.
97    */
98   fixup_refpoints (get_grob_property ("all-elements"));
99
100   
101   for (SCM s = get_grob_property ("all-elements");
102        gh_pair_p (s); s = ly_cdr (s))
103     {
104       unsmob_grob (ly_car (s))->handle_broken_dependencies ();
105     }
106   handle_broken_dependencies ();
107
108   if (verbose_global_b)
109     progress_indication (_f ("Element count %d.",  count + element_count ()));
110
111   
112   for (int i=0; i < broken_into_l_arr_.size (); i++)
113     {
114       System *line_l = dynamic_cast<System*> (broken_into_l_arr_[i]);
115
116       if (verbose_global_b)
117         progress_indication ("[");
118       line_l->post_processing (i+1 == broken_into_l_arr_.size ());
119
120       if (verbose_global_b)
121         {
122           progress_indication (to_str (i));
123           progress_indication ("]");
124         }
125
126       if (i < broken_into_l_arr_.size () - 1)
127         {
128           SCM lastcol =  ly_car (line_l->get_grob_property ("columns"));
129           Grob*  e = unsmob_grob (lastcol);
130
131           SCM between = ly_symbol2scm ("between-system-string");
132           SCM inter = e->internal_get_grob_property (between);
133           if (gh_string_p (inter))
134             {
135               pscore_l_->outputter_l_
136                 ->output_scheme (scm_list_n (between, 
137                                              inter, SCM_UNDEFINED));          
138             }
139         }
140     }
141 }
142
143
144
145
146 /*
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)
151 {
152   for (int i = 0; i < posns->loose_cols_.size (); i++)
153     {
154       int divide_over = 1;
155       Item *loose = dynamic_cast<Item*> (posns->loose_cols_[i]);
156       Paper_column* col = dynamic_cast<Paper_column*> (loose);
157       
158       if (col->line_l_)
159         continue;
160
161       
162       Item * left = 0;
163       Item * right = 0;
164       do
165         {
166           SCM between = loose->get_grob_property ("between-cols");
167           if (!gh_pair_p (between))
168             break;
169
170
171           Item * l=dynamic_cast<Item*> (unsmob_grob (ly_car (between)));
172           Item * r=dynamic_cast<Item*> (unsmob_grob (ly_cdr (between)));
173
174           if (!(l && r))
175             break ;
176           
177           if (!left && l)
178             {
179               left = l->column_l ();
180             }
181
182           divide_over ++;
183
184           loose = right = r->column_l ();
185         }
186       while (1);
187       
188     
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);
192
193       /*
194         divide space equally over loose columns.
195        */
196       int j = 1;
197       loose = col;
198       while (1)
199         {
200           SCM between = loose->get_grob_property ("between-cols");
201           if (!gh_pair_p (between))
202             break;
203
204           Paper_column *thiscol = dynamic_cast<Paper_column*> (loose);
205
206           thiscol->line_l_ = which;
207           thiscol->translate_axis (lx + j*(rx - lx)/divide_over, X_AXIS);
208
209           j ++; 
210           loose = dynamic_cast<Item*> (unsmob_grob (ly_cdr (between)));
211         }
212 #else
213       /*
214         We divide the remaining space of the column over the left and
215         right side. At the moment, we  
216         
217       */
218       Grob * common = right->common_refpoint (left, X_AXIS);
219       
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);
224
225       /*
226         
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. 
232         
233       */
234       Real dx = rx-lx - cval.length ();
235       if (total_dx < 2* cval.length ())
236         {
237           /*
238             todo: this is discontinuous. I'm too tired to
239             invent a sliding mechanism. Duh.
240
241             TODO.
242            */
243           dx *= 0.25;
244         }
245       else
246         dx *= 0.5;
247
248       col->line_l_ = which;
249       col->translate_axis (lx + dx - cval[LEFT], X_AXIS); 
250 #endif
251     }
252 }
253
254 // const?
255 void
256 System::break_into_pieces (Array<Column_x_positions> const &breaking)
257 {
258   for (int i=0; i < breaking.size (); i++)
259     {
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);
265       
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++)
269         {
270           c[j]->translate_axis (breaking[i].config_[j],X_AXIS);
271           dynamic_cast<Paper_column*> (c[j])->line_l_ = line_l;
272         }
273       set_loose_columns (line_l, &breaking[i]);
274       broken_into_l_arr_.push (line_l);
275     }
276 }
277
278
279 void
280 System::output_molecule (SCM expr, Offset o)
281 {
282
283   while (1)
284     {
285       if (!gh_pair_p (expr))
286         return;
287   
288       SCM head =ly_car (expr);
289       if (unsmob_input (head))
290         {
291           Input * ip = unsmob_input (head);
292       
293
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);
300         }
301       else  if (head ==  ly_symbol2scm ("no-origin"))
302         {
303           pscore_l_->outputter_l_->output_scheme (scm_list_n (head, SCM_UNDEFINED));
304           expr = ly_cadr (expr);
305         }
306       else if (head == ly_symbol2scm ("translate-molecule"))
307         {
308           o += ly_scm2offset (ly_cadr (expr));
309           expr = ly_caddr (expr);
310         }
311       else if (head == ly_symbol2scm ("combine-molecule"))
312         {
313           output_molecule (ly_cadr (expr), o);
314           expr = ly_caddr (expr);
315         }
316       else
317         {
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));
324
325           return;
326         }
327     }
328 }
329
330 void
331 System::output_scheme (SCM s)
332 {
333   pscore_l_->outputter_l_->output_scheme (s);
334 }
335
336 void
337 System::add_column (Paper_column*p)
338 {
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;
342
343   p->rank_i_ = prev ? Paper_column::rank_i (prev) + 1 : 0; 
344
345   me->set_grob_property ("columns",  gh_cons (p->self_scm (), cs));
346
347   Axis_group_interface::add_element (me, p);
348 }
349
350
351
352 /*
353   TODO: use scm_map iso. for loops.
354  */
355 void
356 System::pre_processing ()
357 {
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 ();
360
361   if (verbose_global_b)
362     progress_indication (_f ("Grob count %d ",  element_count ()));
363
364   
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 ();
367   
368   fixup_refpoints (get_grob_property ("all-elements"));
369   
370   for (SCM s = get_grob_property ("all-elements"); gh_pair_p (s); s = ly_cdr (s))
371     {
372       Grob* sc = unsmob_grob (ly_car (s));
373       sc->calculate_dependencies (PRECALCED, PRECALCING, ly_symbol2scm ("before-line-breaking-callback"));
374     }
375   
376   progress_indication ("\n" + _ ("Calculating column positions...") + " ");
377   for (SCM s = get_grob_property ("all-elements"); gh_pair_p (s); s = ly_cdr (s))
378     {
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 ());
383     }
384 }
385
386 void
387 System::post_processing (bool last_line)
388 {
389   for (SCM s = get_grob_property ("all-elements");
390        gh_pair_p (s); s = ly_cdr (s))
391     {
392       Grob* sc = unsmob_grob (ly_car (s));
393       sc->calculate_dependencies (POSTCALCED, POSTCALCING,
394                                   ly_symbol2scm ("after-line-breaking-callback"));
395     }
396
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);
402
403   Real height = i.length ();
404   if (height > 50 CM)
405     {
406       programming_error ("Improbable system height");
407       height = 50 CM;
408     }
409
410   /*
411     generate all molecules  to trigger all font loads.
412
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))
416     {
417       unsmob_grob (ly_car (s))->get_molecule ();
418     }
419   /*
420     font defs;
421    */
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));
426
427   /*
428     line preamble.
429    */
430   output_scheme (scm_list_n (ly_symbol2scm ("start-system"),
431                           gh_double2scm (height),
432                           SCM_UNDEFINED));
433   
434   /* Output elements in three layers, 0, 1, 2.
435      The default layer is 1. */
436
437   {
438     Molecule *m = this->get_molecule();
439     if (m)
440       output_molecule (m->get_expr (), Offset(0,0));
441   }
442   
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))
446       {
447         Grob *sc = unsmob_grob (ly_car (s));
448         Molecule *m = sc->get_molecule ();
449         if (!m)
450           continue;
451         
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;
456         
457         Offset o (sc->relative_coordinate (this, X_AXIS),
458                   sc->relative_coordinate (this, Y_AXIS));
459         
460         SCM e = sc->get_grob_property ("extra-offset");
461         if (gh_pair_p (e))
462           {
463             o[X_AXIS] += gh_scm2double (ly_car (e));
464             o[Y_AXIS] += gh_scm2double (ly_cdr (e));      
465           }
466         
467         output_molecule (m->get_expr (), o);
468       }
469
470   
471   
472   if (last_line)
473     {
474       output_scheme (scm_list_n (ly_symbol2scm ("stop-last-system"), SCM_UNDEFINED));
475     }
476   else
477     {
478       output_scheme (scm_list_n (ly_symbol2scm ("stop-system"), SCM_UNDEFINED));
479     }
480 }
481
482
483 Link_array<Item> 
484 System::broken_col_range (Item const*l, Item const*r) const
485 {
486   Link_array<Item> ret;
487
488   l = l->column_l ();
489   r = r->column_l ();
490   SCM s = get_grob_property ("columns");
491
492   while (gh_pair_p (s) && ly_car (s) != r->self_scm ())
493     s = ly_cdr (s);
494     
495   if (gh_pair_p (s))
496     s = ly_cdr (s);
497   
498   while (gh_pair_p (s) && ly_car (s) != l->self_scm ())
499     {
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);
503
504       s = ly_cdr (s);
505     }
506
507   ret.reverse ();
508   return ret;
509 }
510
511 /**
512    Return all columns, but filter out any unused columns , since they might
513    disrupt the spacing problem.
514  */
515 Link_array<Grob>
516 System::column_l_arr ()const
517 {
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 -- ;)
522     {
523       bool brb = Item::breakable_b (acs[i]);
524       bfound = bfound || brb;
525
526       /*
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.
530       */
531       if (!bfound || !Paper_column::used_b (acs[i]))
532         acs.del (i);
533     }
534   return acs;
535 }
536   
537
538
539
540 ADD_INTERFACE (System,"system-interface",
541   "Super grob, parent of all:
542
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");