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