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