]> git.donarmstrong.com Git - lilypond.git/blob - lily/system.cc
Remove paper var caching.
[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 /* Find the loose columns in POSNS, and drape them around the columns
152    specified in BETWEEN-COLS.  */
153 static void
154 set_loose_columns (System* which, Column_x_positions const *posns)
155 {
156   int loose_col_count = posns->loose_cols_.size ();
157   for (int i = 0; i < loose_col_count; i++)
158     {
159       int divide_over = 1;
160       Item *loose = dynamic_cast<Item*> (posns->loose_cols_[i]);
161       Paper_column* col = dynamic_cast<Paper_column*> (loose);
162       
163       if (col->system_)
164         continue;
165       
166       Item *left = 0;
167       Item *right = 0;
168       while (1)
169         {
170           SCM between = loose->get_property ("between-cols");
171           if (!ly_c_pair_p (between))
172             break;
173
174           Item *le = dynamic_cast<Item*> (unsmob_grob (ly_car (between)));
175           Item *re = dynamic_cast<Item*> (unsmob_grob (ly_cdr (between)));
176
177           if (!(le && re))
178             break;
179           
180           if (!left && le)
181             {
182               left = le->get_column ();
183               if (!left->get_system ())
184                 left = left->find_prebroken_piece (RIGHT);
185             }
186
187           divide_over++;
188           loose = right = re->get_column ();
189         }
190
191       if (!right->get_system ())
192         right = right->find_prebroken_piece (LEFT);
193       
194       /* Divide the remaining space of the column over the left and
195         right side.  At the moment,  FIXME  */
196       Grob *common = right->common_refpoint (left, X_AXIS);
197       
198       Real rx = right->extent (common, X_AXIS)[LEFT];
199       Real lx = left->extent (common, X_AXIS)[RIGHT];
200       Real total_dx = rx - lx;
201       Interval cval =col->extent (col, X_AXIS);
202
203       /* Put it in the middle.  This is not an ideal solution -- the
204          break alignment code inserts a fixed space before the clef
205          (about 1 SS), while the space following the clef is flexible.
206          In tight situations, the clef will almost be on top of the
207          following note.  */
208       Real dx = rx - lx - cval.length ();
209       if (total_dx < 2* cval.length ())
210         {
211           /* TODO: this is discontinuous. I'm too tired to
212             invent a sliding mechanism.  Duh. */
213           dx *= 0.25;
214         }
215       else
216         dx *= 0.5;
217
218       col->system_ = which;
219       col->translate_axis (-col->relative_coordinate (common, X_AXIS), X_AXIS);
220       col->translate_axis (lx + dx - cval[LEFT], X_AXIS); 
221     }
222 }
223
224 // const?
225 void
226 System::break_into_pieces (Array<Column_x_positions> const &breaking)
227 {
228   for (int i = 0; i < breaking.size (); i++)
229     {
230       System *system = dynamic_cast <System*> (clone ());
231       system->rank_ = i;
232
233       Link_array<Grob> c (breaking[i].cols_);
234       pscore_->typeset_line (system);
235       
236       system->set_bound (LEFT,c[0]);
237       system->set_bound (RIGHT,c.top ());
238       for (int j = 0; j < c.size (); j++)
239         {
240           c[j]->translate_axis (breaking[i].config_[j], X_AXIS);
241           dynamic_cast<Paper_column*> (c[j])->system_ = system;
242         }
243       set_loose_columns (system, &breaking[i]);
244       broken_intos_.push (system);
245     }
246 }
247
248 void
249 System::add_column (Paper_column*p)
250 {
251   Grob *me = this;
252   SCM cs = me->get_property ("columns");
253   Grob *prev =  ly_c_pair_p (cs) ? unsmob_grob (ly_car (cs)) : 0;
254
255   p->rank_ = prev ? Paper_column::get_rank (prev) + 1 : 0; 
256
257   me->set_property ("columns", scm_cons (p->self_scm (), cs));
258
259   Axis_group_interface::add_element (me, p);
260 }
261
262 void
263 System::pre_processing ()
264 {
265   for (SCM s = get_property ("all-elements"); ly_c_pair_p (s); s = ly_cdr (s))
266     unsmob_grob (ly_car (s))->discretionary_processing ();
267
268   if (verbose_global_b)
269     progress_indication (_f ("Grob count %d", element_count ()));
270
271   
272   for (SCM s = get_property ("all-elements"); ly_c_pair_p (s); s = ly_cdr (s))
273     unsmob_grob (ly_car (s))->handle_prebroken_dependencies ();
274   
275   fixup_refpoints (get_property ("all-elements"));
276   
277   for (SCM s = get_property ("all-elements"); ly_c_pair_p (s); s = ly_cdr (s))
278     {
279       Grob* sc = unsmob_grob (ly_car (s));
280       sc->calculate_dependencies (PRECALCED, PRECALCING, ly_symbol2scm ("before-line-breaking-callback"));
281     }
282   
283   progress_indication ("\n" + _ ("Calculating line breaks...") + " ");
284   for (SCM s = get_property ("all-elements"); ly_c_pair_p (s); s = ly_cdr (s))
285     {
286       Grob *e = unsmob_grob (ly_car (s));
287       SCM proc = e->get_property ("spacing-procedure");
288       if (ly_c_procedure_p (proc))
289         scm_call_1 (proc, e->self_scm ());
290     }
291 }
292
293 void
294 System::post_processing ()
295 {
296   for (SCM s = get_property ("all-elements"); ly_c_pair_p (s); s = ly_cdr (s))
297     {
298       Grob *g = unsmob_grob (ly_car (s));
299       g->calculate_dependencies (POSTCALCED, POSTCALCING,
300           ly_symbol2scm ("after-line-breaking-callback"));
301     }
302
303   Interval iv (extent (this, Y_AXIS));
304   if (iv.is_empty ())
305     programming_error ("System with zero extent.");
306   else
307     translate_axis (-iv[MAX], Y_AXIS);
308
309   /* Generate all stencils to trigger font loads.
310      This might seem inefficient, but Stencils are cached per grob
311      anyway. */
312   SCM all = get_property ("all-elements");
313   all = ly_list_qsort_uniq_x (all);
314
315   this->get_stencil ();
316   for (SCM s = all; ly_c_pair_p (s); s = ly_cdr (s))
317     {
318       Grob *g = unsmob_grob (ly_car (s));
319       g->get_stencil ();
320     }
321 }
322
323 SCM
324 System::get_line ()
325 {  
326   static int const LAYER_COUNT = 3;
327
328   SCM stencils = SCM_EOL;
329   if (Stencil *me = get_stencil ())
330     stencils = scm_cons (me->smobbed_copy (), stencils);
331
332   /* Output stencils in three layers: 0, 1, 2.  Default layer: 1.
333
334      Start with layer 3, since scm_cons prepends to list.  */
335   SCM all = get_property ("all-elements");
336
337   Real penalty = 0;
338   for (int i = LAYER_COUNT; i--;)
339     for (SCM s = all; ly_c_pair_p (s); s = ly_cdr (s))
340       {
341         Grob *g = unsmob_grob (ly_car (s));
342         Stencil *stil = g->get_stencil ();
343
344         /* Skip empty stencils and grobs that are not in this layer.  */
345         if (!stil
346             || robust_scm2int (g->get_property ("layer"), 1) != i)
347           continue;
348
349         Offset o (g->relative_coordinate (this, X_AXIS),
350                   g->relative_coordinate (this, Y_AXIS));
351
352         Offset extra = robust_scm2offset (g->get_property ("extra-offset"),
353                                           Offset (0, 0))
354           * Staff_symbol_referencer::staff_space (g);
355
356         /* Must copy the stencil, for we cannot change the stencil
357            cached in G.  */
358         SCM my_stencil = stil->smobbed_copy ();
359         unsmob_stencil (my_stencil)->translate (o + extra);
360         stencils = scm_cons (my_stencil, stencils);
361
362         if (g->original_)
363           {
364             if (Item *it = dynamic_cast <Item*> (g))
365               {
366                 Grob *col = it->get_column ();
367                 SCM s = col->get_property ("page-penalty");
368                 // FIXME; page breaking is not discrete at +-10000
369                 if (ly_c_number_p (s)) // && fabs (ly_scm2double (s)) < 10000)
370                   penalty += ly_scm2double (s);
371               }
372           }
373       }
374
375   Interval x (extent (this, X_AXIS));
376   Interval y (extent (this, Y_AXIS));
377   Paper_line *pl = new Paper_line (Offset (x.length (), y.length ()),
378                                    stencils, penalty);
379
380   scm_gc_unprotect_object (pl->self_scm ());
381   return pl->self_scm ();
382 }
383
384 Link_array<Item> 
385 System::broken_col_range (Item const *left, Item const *right) const
386 {
387   Link_array<Item> ret;
388
389   left = left->get_column ();
390   right = right->get_column ();
391   SCM s = get_property ("columns");
392
393   while (ly_c_pair_p (s) && ly_car (s) != right->self_scm ())
394     s = ly_cdr (s);
395
396   if (ly_c_pair_p (s))
397     s = ly_cdr (s);
398
399   while (ly_c_pair_p (s) && ly_car (s) != left->self_scm ())
400     {
401       Paper_column*c = dynamic_cast<Paper_column*> (unsmob_grob (ly_car (s)));
402       if (Item::is_breakable (c) && !c->system_)
403         ret.push (c);
404
405       s = ly_cdr (s);
406     }
407
408   ret.reverse ();
409   return ret;
410 }
411
412 /** Return all columns, but filter out any unused columns , since they might
413     disrupt the spacing problem. */
414 Link_array<Grob>
415 System::columns () const
416 {
417   Link_array<Grob> acs
418     = Pointer_group_interface__extract_grobs (this, (Grob*) 0, "columns");
419   bool found = false;
420   for (int i = acs.size (); i--;)
421     {
422       bool brb = Item::is_breakable (acs[i]);
423       found = found || brb;
424
425       /*
426         the last column should be breakable. Weed out any columns that
427         seem empty. We need to retain breakable columns, in case
428         someone forced a breakpoint.
429       */
430       if (!found || !Paper_column::is_used (acs[i]))
431         acs.del (i);
432     }
433   return acs;
434 }
435
436 ADD_INTERFACE (System,"system-interface",
437                "This is the toplevel object: each object in a score "
438                "ultimately has a System object as its X and Y parent. ",
439                "all-elements columns")