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