]> git.donarmstrong.com Git - lilypond.git/blob - lily/system.cc
1527ac9978b976a51bcf05b613783fda8910387a
[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--2003 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_ = 0;
40 }
41
42 int
43 System::element_count () const
44 {
45   return scm_ilength (get_grob_property ("all-elements"));
46 }
47
48 int
49 System::spanner_count () const
50 {
51   int k =0;
52   for (SCM s = get_grob_property ("all-elements");
53        gh_pair_p (s); s = ly_cdr (s))
54     {
55       if (dynamic_cast<Spanner*> (unsmob_grob (gh_car(s))))
56         k++;
57     }
58
59   return k;
60 }
61   
62
63
64 void
65 System::typeset_grob (Grob * elem)
66 {
67   elem->pscore_ = pscore_;
68   Pointer_group_interface::add_grob (this, ly_symbol2scm ("all-elements"),elem);
69   scm_gc_unprotect_object (elem->self_scm ());
70 }
71
72 void
73 System::output_lines ()
74 {
75   for (SCM s = get_grob_property ("all-elements");
76        gh_pair_p (s); s = ly_cdr (s))
77     {
78       Grob * g = unsmob_grob (ly_car (s));
79       if (Spacing_interface::has_interface (g))
80         {
81           /*
82             Kill no longer needed grobs. 
83            */
84           Item * it = dynamic_cast<Item*> (g);
85           if (it && Item::breakable_b(it))
86             {
87               it->find_prebroken_piece (LEFT)->suicide();
88               it->find_prebroken_piece (RIGHT)->suicide();
89             }
90           g->suicide ();
91         }
92       else if (g->live ())
93         g->do_break_processing ();
94     }
95
96   /*
97     fixups must be done in broken line_of_scores, because new elements
98     are put over there.  */
99   int count = 0;
100   for (int i=0; i < broken_intos_.size (); i++)
101     {
102       Grob *se = broken_intos_[i];
103       SCM all = se->get_grob_property ("all-elements");
104       for (SCM s = all; gh_pair_p (s); s = ly_cdr (s))
105         {
106           fixup_refpoint (ly_car (s));
107         }
108       count += scm_ilength (all);
109     }
110   
111   /*
112     needed for doing items.
113    */
114   fixup_refpoints (get_grob_property ("all-elements"));
115
116   
117   for (SCM s = get_grob_property ("all-elements");
118        gh_pair_p (s); s = ly_cdr (s))
119     {
120       unsmob_grob (ly_car (s))->handle_broken_dependencies ();
121     }
122   handle_broken_dependencies ();
123
124   if (verbose_global_b)
125     progress_indication (_f ("Element count %d.",  count + element_count ()));
126
127   
128   for (int i=0; i < broken_intos_.size (); i++)
129     {
130       System *system = dynamic_cast<System*> (broken_intos_[i]);
131
132       if (verbose_global_b)
133         progress_indication ("[");
134       system->post_processing (i+1 == broken_intos_.size ());
135
136       if (verbose_global_b)
137         {
138           progress_indication (to_string (i));
139           progress_indication ("]");
140         }
141
142       if (i < broken_intos_.size () - 1)
143         {
144           SCM lastcol =  ly_car (system->get_grob_property ("columns"));
145           Grob*  e = unsmob_grob (lastcol);
146
147           SCM between = ly_symbol2scm ("between-system-string");
148           SCM inter = e->internal_get_grob_property (between);
149           if (gh_string_p (inter))
150             {
151               pscore_->outputter_
152                 ->output_scheme (scm_list_n (between, 
153                                              inter, SCM_UNDEFINED));          
154             }
155         }
156     }
157 }
158
159
160
161
162 /*
163   Find the loose columns in POSNS, and drape them around the columns
164   specified in BETWEEN-COLS.  */
165 void
166 set_loose_columns (System* which, Column_x_positions const *posns)
167 {
168   for (int i = 0; i < posns->loose_cols_.size (); i++)
169     {
170       int divide_over = 1;
171       Item *loose = dynamic_cast<Item*> (posns->loose_cols_[i]);
172       Paper_column* col = dynamic_cast<Paper_column*> (loose);
173       
174       if (col->system_)
175         continue;
176
177       
178       Item * left = 0;
179       Item * right = 0;
180       do
181         {
182           SCM between = loose->get_grob_property ("between-cols");
183           if (!gh_pair_p (between))
184             break;
185
186
187           Item * l=dynamic_cast<Item*> (unsmob_grob (ly_car (between)));
188           Item * r=dynamic_cast<Item*> (unsmob_grob (ly_cdr (between)));
189
190           if (!(l && r))
191             break ;
192           
193           if (!left && l)
194             {
195               left = l->get_column ();
196             }
197
198           divide_over ++;
199
200           loose = right = r->get_column ();
201         }
202       while (1);
203       
204     
205 #if 0
206       Real rx = right->relative_coordinate (right->get_parent (X_AXIS), X_AXIS);
207       Real lx = left->relative_coordinate (left->get_parent (X_AXIS), X_AXIS);
208
209       /*
210         divide space equally over loose columns.
211        */
212       int j = 1;
213       loose = col;
214       while (1)
215         {
216           SCM between = loose->get_grob_property ("between-cols");
217           if (!gh_pair_p (between))
218             break;
219
220           Paper_column *thiscol = dynamic_cast<Paper_column*> (loose);
221
222           thiscol->system_ = which;
223           thiscol->translate_axis (lx + j*(rx - lx)/divide_over, X_AXIS);
224
225           j ++; 
226           loose = dynamic_cast<Item*> (unsmob_grob (ly_cdr (between)));
227         }
228 #else
229       /*
230         We divide the remaining space of the column over the left and
231         right side. At the moment, we  
232         
233       */
234       Grob * common = right->common_refpoint (left, X_AXIS);
235       
236       Real rx = right->extent(common, X_AXIS)[LEFT];
237       Real lx =  left->extent(common, X_AXIS)[RIGHT];
238       Real total_dx = rx - lx;
239       Interval cval =col->extent (col, X_AXIS);
240
241       /*
242         
243         We put it in the middle. This is not an ideal solution -- the
244         break alignment code inserts a fixed space before the clef
245         (about 1 SS), while the space following the clef is
246         flexible. In tight situations, the clef will almost be on top
247         of the following note. 
248         
249       */
250       Real dx = rx-lx - cval.length ();
251       if (total_dx < 2* cval.length ())
252         {
253           /*
254             todo: this is discontinuous. I'm too tired to
255             invent a sliding mechanism. Duh.
256
257             TODO.
258            */
259           dx *= 0.25;
260         }
261       else
262         dx *= 0.5;
263
264       col->system_ = which;
265       col->translate_axis (lx + dx - cval[LEFT], X_AXIS); 
266 #endif
267     }
268 }
269
270 // const?
271 void
272 System::break_into_pieces (Array<Column_x_positions> const &breaking)
273 {
274   for (int i=0; i < breaking.size (); i++)
275     {
276       System *system = dynamic_cast <System*> (clone ());
277       system->rank_ = i;
278
279       Link_array<Grob> c (breaking[i].cols_);
280       pscore_->typeset_line (system);
281       
282       system->set_bound (LEFT,c[0]);
283       system->set_bound (RIGHT,c.top ());
284       for (int j=0; j < c.size (); j++)
285         {
286           c[j]->translate_axis (breaking[i].config_[j],X_AXIS);
287           dynamic_cast<Paper_column*> (c[j])->system_ = system;
288         }
289       set_loose_columns (system, &breaking[i]);
290       broken_intos_.push (system);
291     }
292 }
293
294
295 void
296 System::output_molecule (SCM expr, Offset o)
297 {
298
299   while (1)
300     {
301       if (!gh_pair_p (expr))
302         return;
303   
304       SCM head =ly_car (expr);
305       if (unsmob_input (head))
306         {
307           Input * ip = unsmob_input (head);
308       
309           pscore_->outputter_->output_scheme (scm_list_n (ly_symbol2scm ("define-origin"),
310                                                            scm_makfrom0str (ip->file_string ().to_str0 ()),
311                                                            gh_int2scm (ip->line_number ()),
312                                                            gh_int2scm (ip->column_number ()),
313                                                            SCM_UNDEFINED));
314           expr = ly_cadr (expr);
315         }
316       else  if (head ==  ly_symbol2scm ("no-origin"))
317         {
318           pscore_->outputter_->output_scheme (scm_list_n (head, SCM_UNDEFINED));
319           expr = ly_cadr (expr);
320         }
321       else if (head == ly_symbol2scm ("translate-molecule"))
322         {
323           o += ly_scm2offset (ly_cadr (expr));
324           expr = ly_caddr (expr);
325         }
326       else if (head == ly_symbol2scm ("combine-molecule"))
327         {
328           output_molecule (ly_cadr (expr), o);
329           expr = ly_caddr (expr);
330         }
331       else
332         {
333           pscore_->outputter_->
334             output_scheme (scm_list_n (ly_symbol2scm ("placebox"),
335                                     gh_double2scm (o[X_AXIS]),
336                                     gh_double2scm (o[Y_AXIS]),
337                                     expr,
338                                     SCM_UNDEFINED));
339
340           return;
341         }
342     }
343 }
344
345 void
346 System::output_scheme (SCM s)
347 {
348   pscore_->outputter_->output_scheme (s);
349 }
350
351 void
352 System::add_column (Paper_column*p)
353 {
354   Grob *me = this;
355   SCM cs = me->get_grob_property ("columns");
356   Grob * prev =  gh_pair_p (cs) ? unsmob_grob (ly_car (cs)) : 0;
357
358   p->rank_ = prev ? Paper_column::get_rank (prev) + 1 : 0; 
359
360   me->set_grob_property ("columns",  gh_cons (p->self_scm (), cs));
361
362   Axis_group_interface::add_element (me, p);
363 }
364
365
366
367 /*
368   TODO: use scm_map iso. for loops.
369  */
370 void
371 System::pre_processing ()
372 {
373   for (SCM s = get_grob_property ("all-elements"); gh_pair_p (s); s = ly_cdr (s))
374     unsmob_grob (ly_car (s))->discretionary_processing ();
375
376   if (verbose_global_b)
377     progress_indication (_f ("Grob count %d ",  element_count ()));
378
379   
380   for (SCM s = get_grob_property ("all-elements"); gh_pair_p (s); s = ly_cdr (s))
381     unsmob_grob (ly_car (s))->handle_prebroken_dependencies ();
382   
383   fixup_refpoints (get_grob_property ("all-elements"));
384   
385   for (SCM s = get_grob_property ("all-elements"); gh_pair_p (s); s = ly_cdr (s))
386     {
387       Grob* sc = unsmob_grob (ly_car (s));
388       sc->calculate_dependencies (PRECALCED, PRECALCING, ly_symbol2scm ("before-line-breaking-callback"));
389     }
390   
391   progress_indication ("\n" + _ ("Calculating line breaks...") + " ");
392   for (SCM s = get_grob_property ("all-elements"); gh_pair_p (s); s = ly_cdr (s))
393     {
394       Grob * e = unsmob_grob (ly_car (s));
395       SCM proc = e->get_grob_property ("spacing-procedure");
396       if (gh_procedure_p (proc))
397         gh_call1 (proc, e->self_scm ());
398     }
399 }
400
401 void
402 System::post_processing (bool last_line)
403 {
404   for (SCM s = get_grob_property ("all-elements");
405        gh_pair_p (s); s = ly_cdr (s))
406     {
407       Grob* sc = unsmob_grob (ly_car (s));
408       sc->calculate_dependencies (POSTCALCED, POSTCALCING,
409                                   ly_symbol2scm ("after-line-breaking-callback"));
410     }
411
412   Interval i (extent (this, Y_AXIS));
413   if (i.empty_b ())
414     programming_error ("Huh?  Empty System?");
415   else
416     translate_axis (- i[MAX], Y_AXIS);
417
418   Real height = i.length ();
419   if (height > 50 CM)
420     {
421       programming_error ("Improbable system height");
422       height = 50 CM;
423     }
424
425   /*
426     generate all molecules  to trigger all font loads.
427
428     (ugh. This is not very memory efficient.)  */
429   this->get_molecule();
430   for (SCM s = get_grob_property ("all-elements"); gh_pair_p (s); s = ly_cdr (s))
431     {
432       unsmob_grob (ly_car (s))->get_molecule ();
433     }
434   /*
435     font defs;
436    */
437   SCM font_names = ly_quote_scm (get_paper ()->font_descriptions ());  
438   output_scheme (scm_list_n (ly_symbol2scm ("define-fonts"),
439                              font_names,
440                              SCM_UNDEFINED));
441
442   /*
443     line preamble.
444    */
445   Interval j (extent (this, X_AXIS));
446   Real length = j[RIGHT];
447   
448   output_scheme (scm_list_n (ly_symbol2scm ("start-system"),
449                           gh_double2scm (length),
450                           gh_double2scm (height),
451                           SCM_UNDEFINED));
452   
453   /* Output elements in three layers, 0, 1, 2.
454      The default layer is 1. */
455
456   {
457     Molecule *m = this->get_molecule();
458     if (m)
459       output_molecule (m->get_expr (), Offset(0,0));
460   }
461   
462   for (int i = 0; i < 3; i++)
463     for (SCM s = get_grob_property ("all-elements"); gh_pair_p (s);
464          s = ly_cdr (s))
465       {
466         Grob *sc = unsmob_grob (ly_car (s));
467         Molecule *m = sc->get_molecule ();
468         if (!m)
469           continue;
470         
471         SCM s = sc->get_grob_property ("layer");
472         int layer = gh_number_p (s) ? gh_scm2int (s) : 1;
473         if (layer != i)
474           continue;
475         
476         Offset o (sc->relative_coordinate (this, X_AXIS),
477                   sc->relative_coordinate (this, Y_AXIS));
478         
479         SCM e = sc->get_grob_property ("extra-offset");
480         if (gh_pair_p (e))
481           {
482             o[X_AXIS] += gh_scm2double (ly_car (e));
483             o[Y_AXIS] += gh_scm2double (ly_cdr (e));      
484           }
485         
486         output_molecule (m->get_expr (), o);
487       }
488
489   
490   
491   if (last_line)
492     {
493       output_scheme (scm_list_n (ly_symbol2scm ("stop-last-system"), SCM_UNDEFINED));
494     }
495   else
496     {
497       output_scheme (scm_list_n (ly_symbol2scm ("stop-system"), SCM_UNDEFINED));
498     }
499 }
500
501
502 Link_array<Item> 
503 System::broken_col_range (Item const*l, Item const*r) const
504 {
505   Link_array<Item> ret;
506
507   l = l->get_column ();
508   r = r->get_column ();
509   SCM s = get_grob_property ("columns");
510
511   while (gh_pair_p (s) && ly_car (s) != r->self_scm ())
512     s = ly_cdr (s);
513     
514   if (gh_pair_p (s))
515     s = ly_cdr (s);
516   
517   while (gh_pair_p (s) && ly_car (s) != l->self_scm ())
518     {
519       Paper_column*c = dynamic_cast<Paper_column*> (unsmob_grob (ly_car (s)));
520       if (Item::breakable_b (c) && !c->system_)
521         ret.push (c);
522
523       s = ly_cdr (s);
524     }
525
526   ret.reverse ();
527   return ret;
528 }
529
530 /**
531    Return all columns, but filter out any unused columns , since they might
532    disrupt the spacing problem.
533  */
534 Link_array<Grob>
535 System::columns ()const
536 {
537   Link_array<Grob> acs
538     = Pointer_group_interface__extract_grobs (this, (Grob*) 0, "columns");
539   bool bfound = false;
540   for (int i= acs.size (); i -- ;)
541     {
542       bool brb = Item::breakable_b (acs[i]);
543       bfound = bfound || brb;
544
545       /*
546         the last column should be breakable. Weed out any columns that
547         seem empty. We need to retain breakable columns, in case
548         someone forced a breakpoint.
549       */
550       if (!bfound || !Paper_column::used_b (acs[i]))
551         acs.del (i);
552     }
553   return acs;
554 }
555   
556
557
558
559 ADD_INTERFACE (System,"system-interface",
560   "Super grob, parent of all:
561
562 The columns of a score that form one line.  The toplevel grob.  Any
563 grob has a Line_of_score as both X and Y reference point. The
564 Paper_score contains one grob of this type. Control enters the
565 Grob dependency calculation from this single Line_of_score
566 object.",
567   "between-system-string all-elements columns");