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