]> git.donarmstrong.com Git - lilypond.git/blob - lily/system.cc
* lily/paper-score.cc (process): Do not show progress newline.
[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 "output-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-system.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   else
56     {
57       elem->pscore_ = pscore_;
58       Pointer_group_interface::add_grob (this, ly_symbol2scm ("all-elements"), elem);
59       scm_gc_unprotect_object (elem->self_scm ());
60     }
61 }
62
63 // todo: use map.
64 static void
65 fixup_refpoints (SCM s)
66 {
67   for (; ly_c_pair_p (s); s = ly_cdr (s))
68     {
69       Grob::fixup_refpoint (ly_car (s));
70     }
71 }
72
73 SCM
74 System::get_lines ()
75 {
76   for (SCM s = get_property ("all-elements"); ly_c_pair_p (s); s = ly_cdr (s))
77     {
78       Grob *g = unsmob_grob (ly_car (s));
79       if (g->internal_has_interface (ly_symbol2scm ("only-prebreak-interface")))
80         {
81           /*
82             Kill no longer needed grobs. 
83            */
84           Item * it = dynamic_cast<Item*> (g);
85           if (it && Item::is_breakable (it))
86             {
87               it->find_prebroken_piece (LEFT)->suicide ();
88               it->find_prebroken_piece (RIGHT)->suicide ();
89             }
90           g->suicide ();
91         }
92       else if (g->is_live ())
93         g->do_break_processing ();
94     }
95
96   /*
97     fixups must be done in broken line_of_scores, because new elements
98     are put over there.  */
99   int count = 0;
100   for (int i=0; i < broken_intos_.size (); i++)
101     {
102       Grob *se = broken_intos_[i];
103       SCM all = se->get_property ("all-elements");
104       for (SCM s = all; ly_c_pair_p (s); s = ly_cdr (s))
105         fixup_refpoint (ly_car (s));
106       count += scm_ilength (all);
107     }
108   
109   /*
110     needed for doing items.
111    */
112   fixup_refpoints (get_property ("all-elements"));
113
114   for (SCM s = get_property ("all-elements"); ly_c_pair_p (s); s = ly_cdr (s))
115     unsmob_grob (ly_car (s))->handle_broken_dependencies ();
116   handle_broken_dependencies ();
117
118 #if 0  /* don't do this: strange side effects.  */
119   
120   /* Because the this->get_property (all-elements) contains items in 3
121      versions, handle_broken_dependencies () will leave duplicated
122      items in all-elements.  Strictly speaking this is harmless, but
123      it leads to duplicated symbols in the output.  ly_list_qsort_uniq_x ()
124      makes sure that no duplicates are in the list.  */
125   for (int i = 0; i < line_count; i++)
126     {
127       SCM all = broken_intos_[i]->get_property ("all-elements");
128       all = ly_list_qsort_uniq_x(all); 
129     }
130 #endif
131   
132   if (verbose_global_b)
133     progress_indication (_f ("Element count %d.",  count + element_count ()));
134
135   int line_count = broken_intos_.size ();
136   SCM lines = scm_c_make_vector (line_count, SCM_EOL);
137   
138   for (int i = 0; i < line_count; i++)
139     {
140       if (verbose_global_b)
141         progress_indication ("[");
142
143       System *system = dynamic_cast<System*> (broken_intos_[i]);
144       system->post_processing ();
145       scm_vector_set_x (lines, scm_int2num (i), system->get_line ());
146
147       if (verbose_global_b)
148         progress_indication (to_string (i) + "]");
149     }
150   return lines;
151 }
152
153 /* Find the loose columns in POSNS, and drape them around the columns
154    specified in BETWEEN-COLS.  */
155 static void
156 set_loose_columns (System* which, Column_x_positions const *posns)
157 {
158   int loose_col_count = posns->loose_cols_.size ();
159   for (int i = 0; i < loose_col_count; i++)
160     {
161       int divide_over = 1;
162       Item *loose = dynamic_cast<Item*> (posns->loose_cols_[i]);
163       Paper_column* col = dynamic_cast<Paper_column*> (loose);
164       
165       if (col->system_)
166         continue;
167       
168       Item *left = 0;
169       Item *right = 0;
170       while (1)
171         {
172           SCM between = loose->get_property ("between-cols");
173           if (!ly_c_pair_p (between))
174             break;
175
176           Item *le = dynamic_cast<Item*> (unsmob_grob (ly_car (between)));
177           Item *re = dynamic_cast<Item*> (unsmob_grob (ly_cdr (between)));
178
179           if (!(le && re))
180             break;
181           
182           if (!left && le)
183             {
184               left = le->get_column ();
185               if (!left->get_system ())
186                 left = left->find_prebroken_piece (RIGHT);
187             }
188
189           divide_over++;
190           loose = right = re->get_column ();
191         }
192
193       if (!right->get_system ())
194         right = right->find_prebroken_piece (LEFT);
195       
196       /* Divide the remaining space of the column over the left and
197         right side.  At the moment,  FIXME  */
198       Grob *common = right->common_refpoint (left, X_AXIS);
199       
200       Real rx = right->extent (common, X_AXIS)[LEFT];
201       Real lx = left->extent (common, X_AXIS)[RIGHT];
202       Real total_dx = rx - lx;
203       Interval cval =col->extent (col, X_AXIS);
204
205       /* Put it in the middle.  This is not an ideal solution -- the
206          break alignment code inserts a fixed space before the clef
207          (about 1 SS), while the space following the clef is flexible.
208          In tight situations, the clef will almost be on top of the
209          following note.  */
210       Real dx = rx - lx - cval.length ();
211       if (total_dx < 2* cval.length ())
212         {
213           /* TODO: this is discontinuous. I'm too tired to
214             invent a sliding mechanism.  Duh. */
215           dx *= 0.25;
216         }
217       else
218         dx *= 0.5;
219
220       col->system_ = which;
221       col->translate_axis (-col->relative_coordinate (common, X_AXIS), X_AXIS);
222       col->translate_axis (lx + dx - cval[LEFT], X_AXIS); 
223     }
224 }
225
226 // const?
227 void
228 System::break_into_pieces (Array<Column_x_positions> const &breaking)
229 {
230   for (int i = 0; i < breaking.size (); i++)
231     {
232       System *system = dynamic_cast <System*> (clone ());
233       system->rank_ = i;
234
235       Link_array<Grob> c (breaking[i].cols_);
236       pscore_->typeset_line (system);
237       
238       system->set_bound (LEFT,c[0]);
239       system->set_bound (RIGHT,c.top ());
240       for (int j = 0; j < c.size (); j++)
241         {
242           c[j]->translate_axis (breaking[i].config_[j], X_AXIS);
243           dynamic_cast<Paper_column*> (c[j])->system_ = system;
244         }
245       set_loose_columns (system, &breaking[i]);
246       broken_intos_.push (system);
247     }
248 }
249
250 void
251 System::add_column (Paper_column*p)
252 {
253   Grob *me = this;
254   SCM cs = me->get_property ("columns");
255   Grob *prev =  ly_c_pair_p (cs) ? unsmob_grob (ly_car (cs)) : 0;
256
257   p->rank_ = prev ? Paper_column::get_rank (prev) + 1 : 0; 
258
259   me->set_property ("columns", scm_cons (p->self_scm (), cs));
260
261   Axis_group_interface::add_element (me, p);
262 }
263
264 void
265 System::pre_processing ()
266 {
267   for (SCM s = get_property ("all-elements"); ly_c_pair_p (s); s = ly_cdr (s))
268     unsmob_grob (ly_car (s))->discretionary_processing ();
269
270   if (verbose_global_b)
271     progress_indication (_f ("Grob count %d", element_count ()));
272   
273   for (SCM s = get_property ("all-elements"); ly_c_pair_p (s); s = ly_cdr (s))
274     unsmob_grob (ly_car (s))->handle_prebroken_dependencies ();
275   
276   fixup_refpoints (get_property ("all-elements"));
277   
278   for (SCM s = get_property ("all-elements"); ly_c_pair_p (s); s = ly_cdr (s))
279     {
280       Grob *sc = unsmob_grob (ly_car (s));
281       sc->calculate_dependencies (PRECALCED, PRECALCING, ly_symbol2scm ("before-line-breaking-callback"));
282     }
283   
284   progress_indication ("\n");
285   progress_indication (_ ("Calculating line breaks..."));
286   progress_indication (" ");
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   SCM exprs = SCM_EOL;
341   SCM *tail = &exprs;
342
343   for (int i = LAYER_COUNT; i--;)
344     for (SCM s = all; ly_c_pair_p (s); s = ly_cdr (s))
345       {
346         Grob *g = unsmob_grob (ly_car (s));
347         Stencil *stil = g->get_stencil ();
348
349         /* Skip empty stencils and grobs that are not in this layer.  */
350         if (!stil
351             || robust_scm2int (g->get_property ("layer"), 1) != i)
352           continue;
353
354         Offset o (g->relative_coordinate (this, X_AXIS),
355                   g->relative_coordinate (this, Y_AXIS));
356
357         Offset extra = robust_scm2offset (g->get_property ("extra-offset"),
358                                           Offset (0, 0))
359           * Staff_symbol_referencer::staff_space (g);
360
361         /* Must copy the stencil, for we cannot change the stencil
362            cached in G.  */
363
364         Stencil st = *stil;
365         st.translate (o + extra);
366         *tail = scm_cons (st.expr (), SCM_EOL);
367         tail = SCM_CDRLOC(*tail);
368         
369       }
370
371   
372
373   Interval x (extent (this, X_AXIS));
374   Interval y (extent (this, Y_AXIS));
375   Stencil sys_stencil (Box (x,y),
376                        scm_cons (ly_symbol2scm ("combine-stencil"),
377                                  exprs));
378   
379   Paper_system *pl = new Paper_system (sys_stencil, false);
380
381   Item * break_point =this->get_bound(LEFT);
382   pl->penalty_ =
383     robust_scm2double (break_point->get_property ("page-penalty"), 0.0);
384   
385   return scm_gc_unprotect_object (pl->self_scm ());
386 }
387
388 Link_array<Item> 
389 System::broken_col_range (Item const *left, Item const *right) const
390 {
391   Link_array<Item> ret;
392
393   left = left->get_column ();
394   right = right->get_column ();
395   SCM s = get_property ("columns");
396
397   while (ly_c_pair_p (s) && ly_car (s) != right->self_scm ())
398     s = ly_cdr (s);
399
400   if (ly_c_pair_p (s))
401     s = ly_cdr (s);
402
403   while (ly_c_pair_p (s) && ly_car (s) != left->self_scm ())
404     {
405       Paper_column*c = dynamic_cast<Paper_column*> (unsmob_grob (ly_car (s)));
406       if (Item::is_breakable (c) && !c->system_)
407         ret.push (c);
408
409       s = ly_cdr (s);
410     }
411
412   ret.reverse ();
413   return ret;
414 }
415
416 /** Return all columns, but filter out any unused columns , since they might
417     disrupt the spacing problem. */
418 Link_array<Grob>
419 System::columns () const
420 {
421   Link_array<Grob> acs
422     = Pointer_group_interface__extract_grobs (this, (Grob*) 0, "columns");
423   bool found = false;
424   for (int i = acs.size (); i--;)
425     {
426       bool brb = Item::is_breakable (acs[i]);
427       found = found || brb;
428
429       /*
430         the last column should be breakable. Weed out any columns that
431         seem empty. We need to retain breakable columns, in case
432         someone forced a breakpoint.
433       */
434       if (!found || !Paper_column::is_used (acs[i]))
435         acs.del (i);
436     }
437   return acs;
438 }
439
440 ADD_INTERFACE (System,"system-interface",
441                "This is the toplevel object: each object in a score "
442                "ultimately has a System object as its X and Y parent. ",
443                "all-elements columns")