]> git.donarmstrong.com Git - lilypond.git/blob - lily/system.cc
* lily/separating-line-group-engraver.cc (acknowledge_grob):
[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 (g->internal_has_interface ("only-prebreak-interface"))
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         We divide the remaining space of the column over the left and
206         right side. At the moment, we  
207         
208       */
209       Grob * common = right->common_refpoint (left, X_AXIS);
210       
211       Real rx = right->extent(common, X_AXIS)[LEFT];
212       Real lx =  left->extent(common, X_AXIS)[RIGHT];
213       Real total_dx = rx - lx;
214       Interval cval =col->extent (col, X_AXIS);
215
216       /*
217         
218         We put it in the middle. This is not an ideal solution -- the
219         break alignment code inserts a fixed space before the clef
220         (about 1 SS), while the space following the clef is
221         flexible. In tight situations, the clef will almost be on top
222         of the following note. 
223         
224       */
225       Real dx = rx-lx - cval.length ();
226       if (total_dx < 2* cval.length ())
227         {
228           /*
229             todo: this is discontinuous. I'm too tired to
230             invent a sliding mechanism. Duh.
231
232             TODO.
233            */
234           dx *= 0.25;
235         }
236       else
237         dx *= 0.5;
238
239       col->system_ = which;
240       col->translate_axis (lx + dx - cval[LEFT], X_AXIS); 
241     }
242 }
243
244 // const?
245 void
246 System::break_into_pieces (Array<Column_x_positions> const &breaking)
247 {
248   for (int i=0; i < breaking.size (); i++)
249     {
250       System *system = dynamic_cast <System*> (clone ());
251       system->rank_ = i;
252
253       Link_array<Grob> c (breaking[i].cols_);
254       pscore_->typeset_line (system);
255       
256       system->set_bound (LEFT,c[0]);
257       system->set_bound (RIGHT,c.top ());
258       for (int j=0; j < c.size (); j++)
259         {
260           c[j]->translate_axis (breaking[i].config_[j],X_AXIS);
261           dynamic_cast<Paper_column*> (c[j])->system_ = system;
262         }
263       set_loose_columns (system, &breaking[i]);
264       broken_intos_.push (system);
265     }
266 }
267
268
269 void
270 System::output_molecule (SCM expr, Offset o)
271 {
272
273   while (1)
274     {
275       if (!gh_pair_p (expr))
276         return;
277   
278       SCM head =ly_car (expr);
279       if (unsmob_input (head))
280         {
281           Input * ip = unsmob_input (head);
282       
283           pscore_->outputter_->output_scheme (scm_list_n (ly_symbol2scm ("define-origin"),
284                                                            scm_makfrom0str (ip->file_string ().to_str0 ()),
285                                                            gh_int2scm (ip->line_number ()),
286                                                            gh_int2scm (ip->column_number ()),
287                                                            SCM_UNDEFINED));
288           expr = ly_cadr (expr);
289         }
290       else  if (head ==  ly_symbol2scm ("no-origin"))
291         {
292           pscore_->outputter_->output_scheme (scm_list_n (head, SCM_UNDEFINED));
293           expr = ly_cadr (expr);
294         }
295       else if (head == ly_symbol2scm ("translate-molecule"))
296         {
297           o += ly_scm2offset (ly_cadr (expr));
298           expr = ly_caddr (expr);
299         }
300       else if (head == ly_symbol2scm ("combine-molecule"))
301         {
302           output_molecule (ly_cadr (expr), o);
303           expr = ly_caddr (expr);
304         }
305       else
306         {
307           pscore_->outputter_->
308             output_scheme (scm_list_n (ly_symbol2scm ("placebox"),
309                                     gh_double2scm (o[X_AXIS]),
310                                     gh_double2scm (o[Y_AXIS]),
311                                     expr,
312                                     SCM_UNDEFINED));
313
314           return;
315         }
316     }
317 }
318
319 void
320 System::output_scheme (SCM s)
321 {
322   pscore_->outputter_->output_scheme (s);
323 }
324
325 void
326 System::add_column (Paper_column*p)
327 {
328   Grob *me = this;
329   SCM cs = me->get_grob_property ("columns");
330   Grob * prev =  gh_pair_p (cs) ? unsmob_grob (ly_car (cs)) : 0;
331
332   p->rank_ = prev ? Paper_column::get_rank (prev) + 1 : 0; 
333
334   me->set_grob_property ("columns",  gh_cons (p->self_scm (), cs));
335
336   Axis_group_interface::add_element (me, p);
337 }
338
339 void
340 System::pre_processing ()
341 {
342   for (SCM s = get_grob_property ("all-elements"); gh_pair_p (s); s = ly_cdr (s))
343     unsmob_grob (ly_car (s))->discretionary_processing ();
344
345   if (verbose_global_b)
346     progress_indication (_f ("Grob count %d ",  element_count ()));
347
348   
349   for (SCM s = get_grob_property ("all-elements"); gh_pair_p (s); s = ly_cdr (s))
350     unsmob_grob (ly_car (s))->handle_prebroken_dependencies ();
351   
352   fixup_refpoints (get_grob_property ("all-elements"));
353   
354   for (SCM s = get_grob_property ("all-elements"); gh_pair_p (s); s = ly_cdr (s))
355     {
356       Grob* sc = unsmob_grob (ly_car (s));
357       sc->calculate_dependencies (PRECALCED, PRECALCING, ly_symbol2scm ("before-line-breaking-callback"));
358     }
359   
360   progress_indication ("\n" + _ ("Calculating line breaks...") + " ");
361   for (SCM s = get_grob_property ("all-elements"); gh_pair_p (s); s = ly_cdr (s))
362     {
363       Grob * e = unsmob_grob (ly_car (s));
364       SCM proc = e->get_grob_property ("spacing-procedure");
365       if (gh_procedure_p (proc))
366         gh_call1 (proc, e->self_scm ());
367     }
368 }
369
370 void
371 System::post_processing (bool last_line)
372 {
373   for (SCM s = get_grob_property ("all-elements");
374        gh_pair_p (s); s = ly_cdr (s))
375     {
376       Grob* sc = unsmob_grob (ly_car (s));
377       sc->calculate_dependencies (POSTCALCED, POSTCALCING,
378                                   ly_symbol2scm ("after-line-breaking-callback"));
379     }
380
381   Interval i (extent (this, Y_AXIS));
382   if (i.is_empty ())
383     programming_error ("Huh?  Empty System?");
384   else
385     translate_axis (- i[MAX], Y_AXIS);
386
387   Real height = i.length ();
388   if (height > 50 CM)
389     {
390       programming_error ("Improbable system height");
391       height = 50 CM;
392     }
393
394   /*
395     generate all molecules  to trigger all font loads.
396
397     (ugh. This is not very memory efficient.)  */
398   this->get_molecule();
399   for (SCM s = get_grob_property ("all-elements"); gh_pair_p (s); s = ly_cdr (s))
400     {
401       unsmob_grob (ly_car (s))->get_molecule ();
402     }
403   /*
404     font defs;
405    */
406   SCM font_names = ly_quote_scm (get_paper ()->font_descriptions ());  
407   output_scheme (scm_list_n (ly_symbol2scm ("define-fonts"),
408                              font_names,
409                              SCM_UNDEFINED));
410
411   /*
412     line preamble.
413    */
414   Interval j (extent (this, X_AXIS));
415   Real length = j[RIGHT];
416     
417   output_scheme (scm_list_n (ly_symbol2scm ("start-system"),
418                           gh_double2scm (length),
419                           gh_double2scm (height),
420                           SCM_UNDEFINED));
421   
422   /* Output elements in three layers, 0, 1, 2.
423      The default layer is 1. */
424   {
425     Molecule *m = this->get_molecule();
426     if (m)
427       output_molecule (m->get_expr (), Offset(0,0));
428   }
429   
430   for (int i = 0; i < 3; i++)
431     for (SCM s = get_grob_property ("all-elements"); gh_pair_p (s);
432          s = ly_cdr (s))
433       {
434         Grob *sc = unsmob_grob (ly_car (s));
435         Molecule *m = sc->get_molecule ();
436         if (!m)
437           continue;
438         
439         SCM s = sc->get_grob_property ("layer");
440         int layer = gh_number_p (s) ? gh_scm2int (s) : 1;
441         if (layer != i)
442           continue;
443         
444         Offset o (sc->relative_coordinate (this, X_AXIS),
445                   sc->relative_coordinate (this, Y_AXIS));
446         
447         SCM e = sc->get_grob_property ("extra-offset");
448         if (gh_pair_p (e))
449           {
450             o[X_AXIS] += gh_scm2double (ly_car (e));
451             o[Y_AXIS] += gh_scm2double (ly_cdr (e));      
452           }
453         
454         output_molecule (m->get_expr (), o);
455       }
456
457   
458   
459   if (last_line)
460     {
461       output_scheme (scm_list_n (ly_symbol2scm ("stop-last-system"), SCM_UNDEFINED));
462     }
463   else
464     {
465       output_scheme (scm_list_n (ly_symbol2scm ("stop-system"), SCM_UNDEFINED));
466     }
467 }
468
469
470 Link_array<Item> 
471 System::broken_col_range (Item const*l, Item const*r) const
472 {
473   Link_array<Item> ret;
474
475   l = l->get_column ();
476   r = r->get_column ();
477   SCM s = get_grob_property ("columns");
478
479   while (gh_pair_p (s) && ly_car (s) != r->self_scm ())
480     s = ly_cdr (s);
481     
482   if (gh_pair_p (s))
483     s = ly_cdr (s);
484   
485   while (gh_pair_p (s) && ly_car (s) != l->self_scm ())
486     {
487       Paper_column*c = dynamic_cast<Paper_column*> (unsmob_grob (ly_car (s)));
488       if (Item::breakable_b (c) && !c->system_)
489         ret.push (c);
490
491       s = ly_cdr (s);
492     }
493
494   ret.reverse ();
495   return ret;
496 }
497
498 /**
499    Return all columns, but filter out any unused columns , since they might
500    disrupt the spacing problem.
501  */
502 Link_array<Grob>
503 System::columns ()const
504 {
505   Link_array<Grob> acs
506     = Pointer_group_interface__extract_grobs (this, (Grob*) 0, "columns");
507   bool bfound = false;
508   for (int i= acs.size (); i -- ;)
509     {
510       bool brb = Item::breakable_b (acs[i]);
511       bfound = bfound || brb;
512
513       /*
514         the last column should be breakable. Weed out any columns that
515         seem empty. We need to retain breakable columns, in case
516         someone forced a breakpoint.
517       */
518       if (!bfound || !Paper_column::used_b (acs[i]))
519         acs.del (i);
520     }
521   return acs;
522 }
523   
524
525
526
527 ADD_INTERFACE (System,"system-interface",
528   "Super grob, parent of all: "
529 "\n\n"
530 "The columns of a score that form one line.  The toplevel grob.  Any "
531 "grob has a Line_of_score as both X and Y reference point. The "
532 "Paper_score contains one grob of this type. Control enters the "
533 "Grob dependency calculation from this single Line_of_score "
534 "object.",
535   "between-system-string all-elements columns");