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