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