]> git.donarmstrong.com Git - lilypond.git/blob - lily/system.cc
* lily/include/lily-guile.hh: many new ly_ functions. Thanks to
[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--2004 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8
9 #include "axis-group-interface.hh"
10 #include "warn.hh"
11 #include "system.hh"
12 #include "main.hh"
13 #include "paper-column.hh"
14 #include "paper-def.hh"
15 #include "paper-outputter.hh"
16 #include "paper-score.hh"
17 #include "string.hh"
18 #include "warn.hh"
19 #include "stencil.hh"
20 #include "all-font-metrics.hh"
21 #include "spacing-interface.hh"
22 #include "staff-symbol-referencer.hh"
23 #include "paper-book.hh"
24 #include "paper-line.hh"
25
26 System::System (SCM s)
27   : Spanner (s)
28 {
29   rank_ = 0;
30 }
31
32 int
33 System::element_count () const
34 {
35   return scm_ilength (get_property ("all-elements"));
36 }
37
38 int
39 System::spanner_count () const
40 {
41   int k =0;
42   for (SCM s = get_property ("all-elements");
43        ly_pair_p (s); s = ly_cdr (s))
44     {
45       if (dynamic_cast<Spanner*> (unsmob_grob (ly_car (s))))
46         k++;
47     }
48
49   return k;
50 }
51   
52
53 int
54 scm_default_compare (const void * a, const void *b)
55 {
56   SCM pa = *(SCM *)a;
57   SCM pb = *(SCM *)b;
58
59   if (pa < pb)
60     return -1;
61   else if (pa > pb)
62     return 1;
63   else
64     return 0;
65 }
66
67 /*
68   modify L in place: sort it 
69 */
70 SCM
71 uniquify_list (SCM l)
72 {
73   int len = scm_ilength (l);
74   SCM  * arr = new SCM[len];
75   int k = 0;
76   for (SCM s = l; SCM_NNULLP (s); s = SCM_CDR (s))
77     arr[k++] = SCM_CAR (s);
78
79   assert (k == len);
80   qsort (arr, len, sizeof (SCM), &scm_default_compare);
81
82   k = 0;
83   SCM *tail = &l;
84   
85   for (int i = 0; i < len ; i++)
86     {
87       if (i && arr[i] == arr[i-1])
88         continue;
89
90       SCM_SETCAR (*tail, arr[i]);
91       tail = SCM_CDRLOC(*tail);
92     }
93
94   *tail = SCM_EOL;
95   delete[] arr;
96   
97   return l; 
98 }
99
100 void
101 System::typeset_grob (Grob * elem)
102 {
103   if (elem->pscore_)
104     programming_error ("Adding element twice.");
105   
106   elem->pscore_ = pscore_;
107   Pointer_group_interface::add_grob (this, ly_symbol2scm ("all-elements"), elem);
108   scm_gc_unprotect_object (elem->self_scm ());
109 }
110
111 // todo: use map.
112 static void
113 fixup_refpoints (SCM s)
114 {
115   for (; ly_pair_p (s); s = ly_cdr (s))
116     {
117       Grob::fixup_refpoint (ly_car (s));
118     }
119 }
120
121 SCM
122 System::get_lines ()
123 {
124   for (SCM s = get_property ("all-elements"); ly_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::is_breakable (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_property ("all-elements");
152       for (SCM s = all; ly_pair_p (s); s = ly_cdr (s))
153         fixup_refpoint (ly_car (s));
154       count += scm_ilength (all);
155     }
156   
157   /*
158     needed for doing items.
159    */
160   fixup_refpoints (get_property ("all-elements"));
161
162   for (SCM s = get_property ("all-elements"); ly_pair_p (s); s = ly_cdr (s))
163     unsmob_grob (ly_car (s))->handle_broken_dependencies ();
164   handle_broken_dependencies ();
165
166 #if 0  /* don't do this: strange side effects.  */
167   
168   /* Because the this->get_property (all-elements) contains items in 3
169      versions, handle_broken_dependencies () will leave duplicated
170      items in all-elements.  Strictly speaking this is harmless, but
171      it leads to duplicated symbols in the output.  uniquify_list ()
172      makes sure that no duplicates are in the list.  */
173   for (int i = 0; i < line_count; i++)
174     {
175       SCM all = broken_intos_[i]->get_property ("all-elements");
176       all = uniquify_list (all); 
177     }
178 #endif
179   
180   if (verbose_global_b)
181     progress_indication (_f ("Element count %d.",  count + element_count ()));
182
183   int line_count = broken_intos_.size ();
184   SCM lines = scm_c_make_vector (line_count, SCM_UNDEFINED);
185   
186    for (int i = 0; i < line_count; i++)
187     {
188       if (verbose_global_b)
189         progress_indication ("[");
190
191       System *system = dynamic_cast<System*> (broken_intos_[i]);
192       system->post_processing ();
193       scm_vector_set_x (lines, scm_int2num (i), system->get_line ());
194
195       if (verbose_global_b)
196         progress_indication (to_string (i) + "]");
197     }
198    return lines;
199 }
200
201
202
203
204 /*
205   Find the loose columns in POSNS, and drape them around the columns
206   specified in BETWEEN-COLS.  */
207 void
208 set_loose_columns (System* which, Column_x_positions const *posns)
209 {
210   for (int i = 0; i < posns->loose_cols_.size (); i++)
211     {
212       int divide_over = 1;
213       Item *loose = dynamic_cast<Item*> (posns->loose_cols_[i]);
214       Paper_column* col = dynamic_cast<Paper_column*> (loose);
215       
216       if (col->system_)
217         continue;
218       
219       Item * left = 0;
220       Item * right = 0;
221       do
222         {
223           SCM between = loose->get_property ("between-cols");
224           if (!ly_pair_p (between))
225             break;
226
227
228           Item * l=dynamic_cast<Item*> (unsmob_grob (ly_car (between)));
229           Item * r=dynamic_cast<Item*> (unsmob_grob (ly_cdr (between)));
230
231           if (!(l && r))
232             break ;
233           
234           if (!left && l)
235             {
236               left = l->get_column ();
237               if (!left->get_system ())
238                 left = left->find_prebroken_piece (RIGHT);
239             }
240
241           divide_over ++;
242           loose = right = r->get_column ();
243         }
244       while (1);
245
246       if (!right->get_system ())
247         right = right->find_prebroken_piece (LEFT);
248       
249       /*
250         We divide the remaining space of the column over the left and
251         right side. At the moment, we  
252         
253       */
254       Grob * common = right->common_refpoint (left, X_AXIS);
255       
256       Real rx = right->extent (common, X_AXIS)[LEFT];
257       Real lx = left->extent (common, X_AXIS)[RIGHT];
258       Real total_dx = rx - lx;
259       Interval cval =col->extent (col, X_AXIS);
260
261       /*
262         
263         We put it in the middle. This is not an ideal solution -- the
264         break alignment code inserts a fixed space before the clef
265         (about 1 SS), while the space following the clef is
266         flexible. In tight situations, the clef will almost be on top
267         of the following note. 
268         
269       */
270       Real dx = rx-lx - cval.length ();
271       if (total_dx < 2* cval.length ())
272         {
273           /*
274             todo: this is discontinuous. I'm too tired to
275             invent a sliding mechanism. Duh.
276
277             TODO.
278            */
279           dx *= 0.25;
280         }
281       else
282         dx *= 0.5;
283
284       col->system_ = which;
285       col->translate_axis (- col->relative_coordinate (common, X_AXIS), X_AXIS);
286       col->translate_axis (lx + dx - cval[LEFT], X_AXIS); 
287     }
288 }
289
290 // const?
291 void
292 System::break_into_pieces (Array<Column_x_positions> const &breaking)
293 {
294   for (int i=0; i < breaking.size (); i++)
295     {
296       System *system = dynamic_cast <System*> (clone ());
297       system->rank_ = i;
298
299       Link_array<Grob> c (breaking[i].cols_);
300       pscore_->typeset_line (system);
301       
302       system->set_bound (LEFT,c[0]);
303       system->set_bound (RIGHT,c.top ());
304       for (int j=0; j < c.size (); j++)
305         {
306           c[j]->translate_axis (breaking[i].config_[j],X_AXIS);
307           dynamic_cast<Paper_column*> (c[j])->system_ = system;
308         }
309       set_loose_columns (system, &breaking[i]);
310       broken_intos_.push (system);
311     }
312 }
313
314 void
315 System::add_column (Paper_column*p)
316 {
317   Grob *me = this;
318   SCM cs = me->get_property ("columns");
319   Grob * prev =  ly_pair_p (cs) ? unsmob_grob (ly_car (cs)) : 0;
320
321   p->rank_ = prev ? Paper_column::get_rank (prev) + 1 : 0; 
322
323   me->set_property ("columns",  scm_cons (p->self_scm (), cs));
324
325   Axis_group_interface::add_element (me, p);
326 }
327
328 void
329 System::pre_processing ()
330 {
331   for (SCM s = get_property ("all-elements"); ly_pair_p (s); s = ly_cdr (s))
332     unsmob_grob (ly_car (s))->discretionary_processing ();
333
334   if (verbose_global_b)
335     progress_indication (_f ("Grob count %d",  element_count ()));
336
337   
338   for (SCM s = get_property ("all-elements"); ly_pair_p (s); s = ly_cdr (s))
339     unsmob_grob (ly_car (s))->handle_prebroken_dependencies ();
340   
341   fixup_refpoints (get_property ("all-elements"));
342   
343   for (SCM s = get_property ("all-elements"); ly_pair_p (s); s = ly_cdr (s))
344     {
345       Grob* sc = unsmob_grob (ly_car (s));
346       sc->calculate_dependencies (PRECALCED, PRECALCING, ly_symbol2scm ("before-line-breaking-callback"));
347     }
348   
349   progress_indication ("\n" + _ ("Calculating line breaks...") + " ");
350   for (SCM s = get_property ("all-elements"); ly_pair_p (s); s = ly_cdr (s))
351     {
352       Grob * e = unsmob_grob (ly_car (s));
353       SCM proc = e->get_property ("spacing-procedure");
354       if (ly_procedure_p (proc))
355         scm_call_1 (proc, e->self_scm ());
356     }
357 }
358
359 void
360 System::post_processing ()
361 {
362   for (SCM s = get_property ("all-elements"); ly_pair_p (s); s = ly_cdr (s))
363     {
364       Grob *g = unsmob_grob (ly_car (s));
365       g->calculate_dependencies (POSTCALCED, POSTCALCING,
366           ly_symbol2scm ("after-line-breaking-callback"));
367     }
368
369   Interval iv (extent (this, Y_AXIS));
370   if (iv.is_empty ())
371     programming_error ("System with zero extent.");
372   else
373     translate_axis (-iv[MAX], Y_AXIS);
374
375   /* Generate all stencils to trigger font loads.
376      This might seem inefficient, but Stencils are cached per grob
377      anyway. */
378   SCM all = get_property ("all-elements");
379   all = uniquify_list (all);
380
381   this->get_stencil ();
382   for (SCM s = all; ly_pair_p (s); s = ly_cdr (s))
383     {
384       Grob *g = unsmob_grob (ly_car (s));
385       g->get_stencil ();
386     }
387 }
388
389 SCM
390 System::get_line ()
391 {  
392   static int const LAYER_COUNT = 3;
393
394   SCM stencils = SCM_EOL;
395   if (Stencil *me = get_stencil ())
396     stencils = scm_cons (me->smobbed_copy (), stencils);
397
398   /* Output stencils in three layers: 0, 1, 2.  The default layer is
399      1.
400
401      Start with layer 3, since  scm_cons prepends to list.
402      
403   */
404   SCM all = get_property ("all-elements");
405   
406   for (int i = LAYER_COUNT; i--;)
407     for (SCM s = all; ly_pair_p (s); s = ly_cdr (s))
408       {
409         Grob *g = unsmob_grob (ly_car (s));
410         Stencil *stil = g->get_stencil ();
411
412         /* Skip empty stencils and grobs that are not in this layer.  */
413         if (!stil
414             || robust_scm2int (g->get_property ("layer"), 1) != i)
415           continue;
416
417         Offset o (g->relative_coordinate (this, X_AXIS),
418                   g->relative_coordinate (this, Y_AXIS));
419
420         Offset extra = robust_scm2offset (g->get_property ("extra-offset"),
421                                           Offset (0, 0))
422           * Staff_symbol_referencer::staff_space (g);
423
424         /*
425           must copy the stencil, for we cannot change the stencil
426           cached in G.
427         */
428         SCM my_stencil = stil->smobbed_copy ();
429         unsmob_stencil (my_stencil)->translate (o + extra);
430         stencils = scm_cons (my_stencil, stencils);
431       }
432
433   if (output_format_global != PAGE_LAYOUT)
434     {
435       SCM lastcol = ly_car (get_property ("columns"));
436       Grob *g = unsmob_grob (lastcol);
437       
438       SCM between = ly_symbol2scm ("between-system-string");
439       SCM inter = g->internal_get_property (between);
440       if (ly_string_p (inter))
441         stencils = scm_cons (scm_cons (between, inter), stencils);
442     }
443
444   Interval x (extent (this, X_AXIS));
445   Interval y (extent (this, Y_AXIS));
446   Paper_line *pl = new Paper_line (Offset (x.length (), y.length ()),
447                                    stencils);
448
449   scm_gc_unprotect_object (pl->self_scm ());
450   return pl->self_scm ();
451 }
452
453 Link_array<Item> 
454 System::broken_col_range (Item const*l, Item const*r) const
455 {
456   Link_array<Item> ret;
457
458   l = l->get_column ();
459   r = r->get_column ();
460   SCM s = get_property ("columns");
461
462   while (ly_pair_p (s) && ly_car (s) != r->self_scm ())
463     s = ly_cdr (s);
464
465   if (ly_pair_p (s))
466     s = ly_cdr (s);
467
468   while (ly_pair_p (s) && ly_car (s) != l->self_scm ())
469     {
470       Paper_column*c = dynamic_cast<Paper_column*> (unsmob_grob (ly_car (s)));
471       if (Item::is_breakable (c) && !c->system_)
472         ret.push (c);
473
474       s = ly_cdr (s);
475     }
476
477   ret.reverse ();
478   return ret;
479 }
480
481 /**
482    Return all columns, but filter out any unused columns , since they might
483    disrupt the spacing problem.
484  */
485 Link_array<Grob>
486 System::columns ()const
487 {
488   Link_array<Grob> acs
489     = Pointer_group_interface__extract_grobs (this, (Grob*) 0, "columns");
490   bool bfound = false;
491   for (int i= acs.size (); i -- ;)
492     {
493       bool brb = Item::is_breakable (acs[i]);
494       bfound = bfound || brb;
495
496       /*
497         the last column should be breakable. Weed out any columns that
498         seem empty. We need to retain breakable columns, in case
499         someone forced a breakpoint.
500       */
501       if (!bfound || !Paper_column::is_used (acs[i]))
502         acs.del (i);
503     }
504   return acs;
505 }
506
507 ADD_INTERFACE (System,"system-interface",
508                "This is the toplevel object: each object in a score "
509                "ultimately has a System object as its X and Y parent. ",
510                "between-system-string all-elements columns")