]> git.donarmstrong.com Git - lilypond.git/blob - lily/system.cc
* lily/include/lily-guile.hh: rename ly_c_X_p -> ly_is_X
[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--2005 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 System::System (System const &src, int count)
29   : Spanner (src, count)
30 {
31   rank_ = 0;
32 }
33
34 System::System (SCM s, Object_key const *key)
35   : Spanner (s, key)
36 {
37   rank_ = 0;
38 }
39
40 Grob *
41 System::clone (int count) const
42 {
43   return new System (*this, count);
44 }
45
46 int
47 System::element_count () const
48 {
49   return scm_ilength (get_property ("all-elements"));
50 }
51
52 int
53 System::spanner_count () const
54 {
55   int k = 0;
56   for (SCM s = get_property ("all-elements"); scm_is_pair (s); s = scm_cdr (s))
57     if (dynamic_cast<Spanner *> (unsmob_grob (scm_car (s))))
58       k++;
59   return k;
60 }
61
62 void
63 System::typeset_grob (Grob *elem)
64 {
65   if (elem->pscore_)
66     programming_error ("adding element twice");
67   else
68     {
69       elem->pscore_ = pscore_;
70       Pointer_group_interface::add_grob (this, ly_symbol2scm ("all-elements"), elem);
71       scm_gc_unprotect_object (elem->self_scm ());
72     }
73 }
74
75 // todo: use map.
76 static void
77 fixup_refpoints (SCM s)
78 {
79   for (; scm_is_pair (s); s = scm_cdr (s))
80     {
81       Grob::fixup_refpoint (scm_car (s));
82     }
83 }
84
85 SCM
86 System::get_paper_systems ()
87 {
88   for (SCM s = get_property ("all-elements"); scm_is_pair (s); s = scm_cdr (s))
89     {
90       Grob *g = unsmob_grob (scm_car (s));
91       if (g->internal_has_interface (ly_symbol2scm ("only-prebreak-interface")))
92         {
93           /*
94             Kill no longer needed grobs.
95           */
96           Item *it = dynamic_cast<Item *> (g);
97           if (it && Item::is_breakable (it))
98             {
99               it->find_prebroken_piece (LEFT)->suicide ();
100               it->find_prebroken_piece (RIGHT)->suicide ();
101             }
102           g->suicide ();
103         }
104       else if (g->is_live ())
105         g->do_break_processing ();
106     }
107
108   /*
109     fixups must be done in broken line_of_scores, because new elements
110     are put over there.  */
111   int count = 0;
112   for (int i = 0; i < broken_intos_.size (); i++)
113     {
114       Grob *se = broken_intos_[i];
115       SCM all = se->get_property ("all-elements");
116       for (SCM s = all; scm_is_pair (s); s = scm_cdr (s))
117         fixup_refpoint (scm_car (s));
118       count += scm_ilength (all);
119     }
120
121   /*
122     needed for doing items.
123   */
124   fixup_refpoints (get_property ("all-elements"));
125
126   for (SCM s = get_property ("all-elements"); scm_is_pair (s); s = scm_cdr (s))
127     unsmob_grob (scm_car (s))->handle_broken_dependencies ();
128   handle_broken_dependencies ();
129
130 #if 0  /* don't do this: strange side effects.  */
131
132   /* Because the this->get_property (all-elements) contains items in 3
133      versions, handle_broken_dependencies () will leave duplicated
134      items in all-elements.  Strictly speaking this is harmless, but
135      it leads to duplicated symbols in the output.  ly_list_qsort_uniq_x ()
136      makes sure that no duplicates are in the list.  */
137   for (int i = 0; i < line_count; i++)
138     {
139       SCM all = broken_intos_[i]->get_property ("all-elements");
140       all = ly_list_qsort_uniq_x (all);
141     }
142 #endif
143
144   if (be_verbose_global)
145     message (_f ("Element count %d.", count + element_count ()));
146
147   int line_count = broken_intos_.size ();
148   SCM lines = scm_c_make_vector (line_count, SCM_EOL);
149
150   for (int i = 0; i < line_count; i++)
151     {
152       if (be_verbose_global)
153         progress_indication ("[");
154
155       System *system = dynamic_cast<System *> (broken_intos_[i]);
156       system->post_processing ();
157       scm_vector_set_x (lines, scm_int2num (i), system->get_paper_system ());
158
159       if (be_verbose_global)
160         progress_indication (to_string (i) + "]");
161     }
162   return lines;
163 }
164
165 void
166 System::break_into_pieces (Array<Column_x_positions> const &breaking)
167 {
168   for (int i = 0; i < breaking.size (); i++)
169     {
170       System *system = dynamic_cast<System *> (clone (i));
171       system->rank_ = i;
172
173       Link_array<Grob> c (breaking[i].cols_);
174       pscore_->typeset_system (system);
175
176       system->set_bound (LEFT, c[0]);
177       system->set_bound (RIGHT, c.top ());
178       for (int j = 0; j < c.size (); j++)
179         {
180           c[j]->translate_axis (breaking[i].config_[j], X_AXIS);
181           dynamic_cast<Paper_column *> (c[j])->system_ = system;
182         }
183       set_loose_columns (system, &breaking[i]);
184       broken_intos_.push (system);
185     }
186 }
187
188 void
189 System::add_column (Paper_column *p)
190 {
191   Grob *me = this;
192   SCM cs = me->get_property ("columns");
193   Grob *prev = scm_is_pair (cs) ? unsmob_grob (scm_car (cs)) : 0;
194
195   p->rank_ = prev ? Paper_column::get_rank (prev) + 1 : 0;
196
197   me->set_property ("columns", scm_cons (p->self_scm (), cs));
198
199   Axis_group_interface::add_element (me, p);
200 }
201
202 void
203 apply_tweaks (Grob *g, bool broken)
204 {
205   if (bool (g->original_) == broken)
206     {
207       SCM tweaks = global_registry_->get_tweaks (g);
208       for (SCM s = tweaks; scm_is_pair (s); s = scm_cdr (s))
209         {
210           SCM proc = scm_caar (s);
211           SCM rest = scm_cdar (s);
212           scm_apply_1 (proc, g->self_scm (), rest);
213         }
214     }
215 }
216
217 void
218 System::pre_processing ()
219 {
220   for (SCM s = get_property ("all-elements"); scm_is_pair (s); s = scm_cdr (s))
221     unsmob_grob (scm_car (s))->discretionary_processing ();
222
223   if (be_verbose_global)
224     message (_f ("Grob count %d", element_count ()));
225
226   for (SCM s = get_property ("all-elements"); scm_is_pair (s); s = scm_cdr (s))
227     unsmob_grob (scm_car (s))->handle_prebroken_dependencies ();
228
229   fixup_refpoints (get_property ("all-elements"));
230
231   for (SCM s = get_property ("all-elements"); scm_is_pair (s); s = scm_cdr (s))
232     apply_tweaks (unsmob_grob (scm_car (s)), false);
233
234   for (SCM s = get_property ("all-elements"); scm_is_pair (s); s = scm_cdr (s))
235     {
236       Grob *sc = unsmob_grob (scm_car (s));
237       sc->calculate_dependencies (PRECALCED, PRECALCING, ly_symbol2scm ("before-line-breaking-callback"));
238     }
239
240   message (_ ("Calculating line breaks..."));
241   progress_indication (" ");
242   for (SCM s = get_property ("all-elements"); scm_is_pair (s); s = scm_cdr (s))
243     {
244       Grob *e = unsmob_grob (scm_car (s));
245       SCM proc = e->get_property ("spacing-procedure");
246       if (ly_is_procedure (proc))
247         scm_call_1 (proc, e->self_scm ());
248     }
249 }
250
251 void
252 System::post_processing ()
253 {
254   for (SCM s = get_property ("all-elements"); scm_is_pair (s); s = scm_cdr (s))
255     {
256       Grob *g = unsmob_grob (scm_car (s));
257
258       apply_tweaks (g, true);
259
260       g->calculate_dependencies (POSTCALCED, POSTCALCING,
261                                  ly_symbol2scm ("after-line-breaking-callback"));
262     }
263
264   Interval iv (extent (this, Y_AXIS));
265   if (iv.is_empty ())
266     programming_error ("system with zero extent");
267   else
268     translate_axis (-iv[MAX], Y_AXIS);
269
270   /* Generate all stencils to trigger font loads.
271      This might seem inefficient, but Stencils are cached per grob
272      anyway. */
273   SCM all = get_property ("all-elements");
274   all = ly_list_qsort_uniq_x (all);
275
276   this->get_stencil ();
277   for (SCM s = all; scm_is_pair (s); s = scm_cdr (s))
278     {
279       Grob *g = unsmob_grob (scm_car (s));
280       g->get_stencil ();
281     }
282 }
283
284 SCM
285 System::get_paper_system ()
286 {
287   static int const LAYER_COUNT = 3;
288
289   SCM exprs = SCM_EOL;
290   SCM *tail = &exprs;
291
292   /* Output stencils in three layers: 0, 1, 2.  Default layer: 1. */
293   SCM all = get_property ("all-elements");
294
295   for (int i = 0; i < LAYER_COUNT; i++)
296     for (SCM s = all; scm_is_pair (s); s = scm_cdr (s))
297       {
298         Grob *g = unsmob_grob (scm_car (s));
299         Stencil *stil = g->get_stencil ();
300
301         /* Skip empty stencils and grobs that are not in this layer.  */
302         if (!stil
303             || robust_scm2int (g->get_property ("layer"), 1) != i)
304           continue;
305
306         Offset o (g->relative_coordinate (this, X_AXIS),
307                   g->relative_coordinate (this, Y_AXIS));
308
309         Offset extra = robust_scm2offset (g->get_property ("extra-offset"),
310                                           Offset (0, 0))
311           * Staff_symbol_referencer::staff_space (g);
312
313         /* Must copy the stencil, for we cannot change the stencil
314            cached in G.  */
315
316         Stencil st = *stil;
317         st.translate (o + extra);
318
319         *tail = scm_cons (st.expr (), SCM_EOL);
320         tail = SCM_CDRLOC (*tail);
321       }
322
323   if (Stencil *me = get_stencil ())
324     exprs = scm_cons (me->expr (), exprs);
325
326   Interval x (extent (this, X_AXIS));
327   Interval y (extent (this, Y_AXIS));
328   Stencil sys_stencil (Box (x, y),
329                        scm_cons (ly_symbol2scm ("combine-stencil"),
330                                  exprs));
331
332   Interval staff_refpoints;
333   staff_refpoints.set_empty ();
334   for (SCM s = get_property ("spaceable-staves");
335        scm_is_pair (s); s = scm_cdr (s))
336     {
337       Grob *g = unsmob_grob (scm_car (s));
338       staff_refpoints.add_point (g->relative_coordinate (this, Y_AXIS));
339     }
340
341   Paper_system *pl = new Paper_system (sys_stencil, false);
342   pl->staff_refpoints_ = staff_refpoints;
343   Item *break_point = this->get_bound (LEFT);
344   pl->break_before_penalty_
345     = robust_scm2double (break_point->get_property ("page-penalty"), 0.0);
346
347   return scm_gc_unprotect_object (pl->self_scm ());
348 }
349
350 Link_array<Item>
351 System::broken_col_range (Item const *left, Item const *right) const
352 {
353   Link_array<Item> ret;
354
355   left = left->get_column ();
356   right = right->get_column ();
357   SCM s = get_property ("columns");
358
359   while (scm_is_pair (s) && scm_car (s) != right->self_scm ())
360     s = scm_cdr (s);
361
362   if (scm_is_pair (s))
363     s = scm_cdr (s);
364
365   while (scm_is_pair (s) && scm_car (s) != left->self_scm ())
366     {
367       Paper_column *c = dynamic_cast<Paper_column *> (unsmob_grob (scm_car (s)));
368       if (Item::is_breakable (c) && !c->system_)
369         ret.push (c);
370
371       s = scm_cdr (s);
372     }
373
374   ret.reverse ();
375   return ret;
376 }
377
378 /** Return all columns, but filter out any unused columns , since they might
379     disrupt the spacing problem. */
380 Link_array<Grob>
381 System::columns () const
382 {
383   Link_array<Grob> acs
384     = extract_grob_array (this, ly_symbol2scm ("columns"));
385   bool found = false;
386   for (int i = acs.size (); i--;)
387     {
388       bool brb = Item::is_breakable (acs[i]);
389       found = found || brb;
390
391       /*
392         the last column should be breakable. Weed out any columns that
393         seem empty. We need to retain breakable columns, in case
394         someone forced a breakpoint.
395       */
396       if (!found || !Paper_column::is_used (acs[i]))
397         acs.del (i);
398     }
399   return acs;
400 }
401
402
403 ADD_INTERFACE (System, "system-interface",
404                "This is the toplevel object: each object in a score "
405                "ultimately has a System object as its X and Y parent. ",
406                "all-elements spaceable-staves columns")