]> git.donarmstrong.com Git - lilypond.git/blob - lily/system.cc
(get_line): don't take piano-pedal & dynamic line
[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
29 System::System (SCM s)
30   : Spanner (s)
31 {
32   rank_ = 0;
33 }
34
35 int
36 System::element_count () const
37 {
38   return scm_ilength (get_property ("all-elements"));
39 }
40
41 int
42 System::spanner_count () const
43 {
44   int k = 0;
45   for (SCM s = get_property ("all-elements"); ly_c_pair_p (s); s = ly_cdr (s))
46     if (dynamic_cast<Spanner*> (unsmob_grob (ly_car (s))))
47       k++;
48   return k;
49 }
50
51 void
52 System::typeset_grob (Grob * elem)
53 {
54   if (elem->pscore_)
55     programming_error ("Adding element twice.");
56   else
57     {
58       elem->pscore_ = pscore_;
59       Pointer_group_interface::add_grob (this, ly_symbol2scm ("all-elements"), elem);
60       scm_gc_unprotect_object (elem->self_scm ());
61     }
62 }
63
64 // todo: use map.
65 static void
66 fixup_refpoints (SCM s)
67 {
68   for (; ly_c_pair_p (s); s = ly_cdr (s))
69     {
70       Grob::fixup_refpoint (ly_car (s));
71     }
72 }
73
74 SCM
75 System::get_lines ()
76 {
77   for (SCM s = get_property ("all-elements"); ly_c_pair_p (s); s = ly_cdr (s))
78     {
79       Grob *g = unsmob_grob (ly_car (s));
80       if (g->internal_has_interface (ly_symbol2scm ("only-prebreak-interface")))
81         {
82           /*
83             Kill no longer needed grobs. 
84            */
85           Item * it = dynamic_cast<Item*> (g);
86           if (it && Item::is_breakable (it))
87             {
88               it->find_prebroken_piece (LEFT)->suicide ();
89               it->find_prebroken_piece (RIGHT)->suicide ();
90             }
91           g->suicide ();
92         }
93       else if (g->is_live ())
94         g->do_break_processing ();
95     }
96
97   /*
98     fixups must be done in broken line_of_scores, because new elements
99     are put over there.  */
100   int count = 0;
101   for (int i=0; i < broken_intos_.size (); i++)
102     {
103       Grob *se = broken_intos_[i];
104       SCM all = se->get_property ("all-elements");
105       for (SCM s = all; ly_c_pair_p (s); s = ly_cdr (s))
106         fixup_refpoint (ly_car (s));
107       count += scm_ilength (all);
108     }
109   
110   /*
111     needed for doing items.
112    */
113   fixup_refpoints (get_property ("all-elements"));
114
115   for (SCM s = get_property ("all-elements"); ly_c_pair_p (s); s = ly_cdr (s))
116     unsmob_grob (ly_car (s))->handle_broken_dependencies ();
117   handle_broken_dependencies ();
118
119 #if 0  /* don't do this: strange side effects.  */
120   
121   /* Because the this->get_property (all-elements) contains items in 3
122      versions, handle_broken_dependencies () will leave duplicated
123      items in all-elements.  Strictly speaking this is harmless, but
124      it leads to duplicated symbols in the output.  ly_list_qsort_uniq_x ()
125      makes sure that no duplicates are in the list.  */
126   for (int i = 0; i < line_count; i++)
127     {
128       SCM all = broken_intos_[i]->get_property ("all-elements");
129       all = ly_list_qsort_uniq_x(all); 
130     }
131 #endif
132   
133   if (verbose_global_b)
134     progress_indication (_f ("Element count %d.",  count + element_count ()));
135
136   int line_count = broken_intos_.size ();
137   SCM lines = scm_c_make_vector (line_count, SCM_EOL);
138   
139   for (int i = 0; i < line_count; i++)
140     {
141       if (verbose_global_b)
142         progress_indication ("[");
143
144       System *system = dynamic_cast<System*> (broken_intos_[i]);
145       system->post_processing ();
146       scm_vector_set_x (lines, scm_int2num (i), system->get_line ());
147
148       if (verbose_global_b)
149         progress_indication (to_string (i) + "]");
150     }
151   return lines;
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   for (SCM s = get_property ("all-elements"); ly_c_pair_p (s); s = ly_cdr (s))
275     unsmob_grob (ly_car (s))->handle_prebroken_dependencies ();
276   
277   fixup_refpoints (get_property ("all-elements"));
278   
279   for (SCM s = get_property ("all-elements"); ly_c_pair_p (s); s = ly_cdr (s))
280     {
281       Grob *sc = unsmob_grob (ly_car (s));
282       sc->calculate_dependencies (PRECALCED, PRECALCING, ly_symbol2scm ("before-line-breaking-callback"));
283     }
284   
285   progress_indication ("\n");
286   progress_indication (_ ("Calculating line breaks..."));
287   progress_indication (" ");
288   for (SCM s = get_property ("all-elements"); ly_c_pair_p (s); s = ly_cdr (s))
289     {
290       Grob *e = unsmob_grob (ly_car (s));
291       SCM proc = e->get_property ("spacing-procedure");
292       if (ly_c_procedure_p (proc))
293         scm_call_1 (proc, e->self_scm ());
294     }
295 }
296
297 void
298 System::post_processing ()
299 {
300   for (SCM s = get_property ("all-elements"); ly_c_pair_p (s); s = ly_cdr (s))
301     {
302       Grob *g = unsmob_grob (ly_car (s));
303       g->calculate_dependencies (POSTCALCED, POSTCALCING,
304           ly_symbol2scm ("after-line-breaking-callback"));
305     }
306
307   Interval iv (extent (this, Y_AXIS));
308   if (iv.is_empty ())
309     programming_error ("System with zero extent.");
310   else
311     translate_axis (-iv[MAX], Y_AXIS);
312   
313   /* Generate all stencils to trigger font loads.
314      This might seem inefficient, but Stencils are cached per grob
315      anyway. */
316   SCM all = get_property ("all-elements");
317   all = ly_list_qsort_uniq_x (all);
318
319   this->get_stencil ();
320   for (SCM s = all; ly_c_pair_p (s); s = ly_cdr (s))
321     {
322       Grob *g = unsmob_grob (ly_car (s));
323       g->get_stencil ();
324     }
325 }
326
327 SCM
328 System::get_line ()
329 {  
330   static int const LAYER_COUNT = 3;
331
332   SCM exprs = SCM_EOL;
333   SCM *tail = &exprs;
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   Interval staff_refpoints;
340   staff_refpoints.set_empty();
341   
342   for (int i = LAYER_COUNT; i--;)
343     for (SCM s = all; ly_c_pair_p (s); s = ly_cdr (s))
344       {
345         Grob *g = unsmob_grob (ly_car (s));
346         Stencil *stil = g->get_stencil ();
347
348         if (i == 0
349             && Axis_group_interface::has_interface (g)
350             && !Align_interface::has_interface (g)
351             && dynamic_cast<Spanner*> (g)
352             /*
353               UGH.
354              */
355             && !g->internal_has_interface (ly_symbol2scm ("dynamic-interface"))
356             && !g->internal_has_interface (ly_symbol2scm ("piano-pedal-interface"))
357             )
358           {
359             staff_refpoints.add_point (g->relative_coordinate (this, Y_AXIS));
360           }
361   
362         /* Skip empty stencils and grobs that are not in this layer.  */
363         if (!stil
364             || robust_scm2int (g->get_property ("layer"), 1) != i)
365           continue;
366
367         Offset o (g->relative_coordinate (this, X_AXIS),
368                   g->relative_coordinate (this, Y_AXIS));
369
370         Offset extra = robust_scm2offset (g->get_property ("extra-offset"),
371                                           Offset (0, 0))
372           * Staff_symbol_referencer::staff_space (g);
373
374         /* Must copy the stencil, for we cannot change the stencil
375            cached in G.  */
376
377         Stencil st = *stil;
378         st.translate (o + extra);
379         *tail = scm_cons (st.expr (), SCM_EOL);
380         tail = SCM_CDRLOC(*tail);
381       }
382
383   if (Stencil *me = get_stencil ())
384     exprs = scm_cons (me->expr (), exprs);
385
386   Interval x (extent (this, X_AXIS));
387   Interval y (extent (this, Y_AXIS));
388   Stencil sys_stencil (Box (x,y),
389                        scm_cons (ly_symbol2scm ("combine-stencil"),
390                                  exprs));
391   
392   Paper_system *pl = new Paper_system (sys_stencil, false);
393   pl->staff_refpoints_ = staff_refpoints;
394   Item * break_point =this->get_bound(LEFT);
395   pl->penalty_ =
396     robust_scm2double (break_point->get_property ("page-penalty"), 0.0);
397   
398   return scm_gc_unprotect_object (pl->self_scm ());
399 }
400
401 Link_array<Item> 
402 System::broken_col_range (Item const *left, Item const *right) const
403 {
404   Link_array<Item> ret;
405
406   left = left->get_column ();
407   right = right->get_column ();
408   SCM s = get_property ("columns");
409
410   while (ly_c_pair_p (s) && ly_car (s) != right->self_scm ())
411     s = ly_cdr (s);
412
413   if (ly_c_pair_p (s))
414     s = ly_cdr (s);
415
416   while (ly_c_pair_p (s) && ly_car (s) != left->self_scm ())
417     {
418       Paper_column*c = dynamic_cast<Paper_column*> (unsmob_grob (ly_car (s)));
419       if (Item::is_breakable (c) && !c->system_)
420         ret.push (c);
421
422       s = ly_cdr (s);
423     }
424
425   ret.reverse ();
426   return ret;
427 }
428
429 /** Return all columns, but filter out any unused columns , since they might
430     disrupt the spacing problem. */
431 Link_array<Grob>
432 System::columns () const
433 {
434   Link_array<Grob> acs
435     = Pointer_group_interface__extract_grobs (this, (Grob*) 0, "columns");
436   bool found = false;
437   for (int i = acs.size (); i--;)
438     {
439       bool brb = Item::is_breakable (acs[i]);
440       found = found || brb;
441
442       /*
443         the last column should be breakable. Weed out any columns that
444         seem empty. We need to retain breakable columns, in case
445         someone forced a breakpoint.
446       */
447       if (!found || !Paper_column::is_used (acs[i]))
448         acs.del (i);
449     }
450   return acs;
451 }
452
453 ADD_INTERFACE (System,"system-interface",
454                "This is the toplevel object: each object in a score "
455                "ultimately has a System object as its X and Y parent. ",
456                "all-elements columns")