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