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