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