]> git.donarmstrong.com Git - lilypond.git/blob - lily/line-of-score.cc
release: 1.5.31
[lilypond.git] / lily / line-of-score.cc
1 /*
2   scoreline.cc -- implement Line_of_score
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1996--2002 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8
9 #include "input-smob.hh"
10 #include "axis-group-interface.hh"
11 #include "debug.hh"
12 #include "line-of-score.hh"
13 #include "main.hh"
14 #include "paper-column.hh"
15 #include "paper-def.hh"
16 #include "paper-outputter.hh"
17 #include "paper-score.hh"
18 #include "string.hh"
19 #include "warn.hh"
20 #include "dimensions.hh"
21 #include "molecule.hh"
22 #include "all-font-metrics.hh"
23
24 // todo: use map.
25 void
26 fixup_refpoints (SCM s)
27 {
28   for (; gh_pair_p (s); s = ly_cdr (s))
29     {
30       Grob::fixup_refpoint (ly_car (s));
31     }
32 }
33
34
35 Line_of_score::Line_of_score (SCM s)
36   : Spanner (s)
37 {
38   rank_i_ = 0;
39
40   Axis_group_interface::set_interface (this);
41   Axis_group_interface::set_axes (this, Y_AXIS,X_AXIS);
42 }
43
44 int
45 Line_of_score::element_count () const
46 {
47   return scm_ilength (get_grob_property ("all-elements"));
48 }
49
50 void
51 Line_of_score::typeset_grob (Grob * elem_p)
52 {
53   elem_p->pscore_l_ = pscore_l_;
54   Pointer_group_interface::add_grob (this, ly_symbol2scm ("all-elements"),elem_p);
55   scm_gc_unprotect_object (elem_p->self_scm ());
56 }
57
58 void
59 Line_of_score::output_lines ()
60 {
61   for (SCM s = get_grob_property ("all-elements");
62        gh_pair_p (s); s = ly_cdr (s))
63     {
64       unsmob_grob (ly_car (s))->do_break_processing ();
65     }
66
67   /*
68     fixups must be done in broken line_of_scores, because new elements
69     are put over there.  */
70   int count = 0;
71   for (int i=0; i < broken_into_l_arr_.size (); i++)
72     {
73       Grob *se = broken_into_l_arr_[i];
74       SCM all = se->get_grob_property ("all-elements");
75       for (SCM s = all; gh_pair_p (s); s = ly_cdr (s))
76         {
77           fixup_refpoint (ly_car (s));
78         }
79       count += scm_ilength (all);
80     }
81   
82   /*
83     needed for doing items.
84    */
85   fixup_refpoints (get_grob_property ("all-elements"));
86
87   
88   for (SCM s = get_grob_property ("all-elements");
89        gh_pair_p (s); s = ly_cdr (s))
90     {
91       unsmob_grob (ly_car (s))->handle_broken_dependencies ();
92     }
93   handle_broken_dependencies ();
94
95   if (verbose_global_b)
96     progress_indication (_f ("Element count %d.",  count + element_count ()));
97
98   
99   for (int i=0; i < broken_into_l_arr_.size (); i++)
100     {
101       Line_of_score *line_l = dynamic_cast<Line_of_score*> (broken_into_l_arr_[i]);
102
103       if (verbose_global_b)
104         progress_indication ("[");
105       line_l->post_processing (i+1 == broken_into_l_arr_.size ());
106
107       if (verbose_global_b)
108         {
109           progress_indication (to_str (i));
110           progress_indication ("]");
111         }
112
113       if (i < broken_into_l_arr_.size () - 1)
114         {
115           SCM lastcol =  ly_car (line_l->get_grob_property ("columns"));
116           Grob*  e = unsmob_grob (lastcol);
117
118           SCM between = ly_symbol2scm ("between-system-string");
119           SCM inter = e->internal_get_grob_property (between);
120           if (gh_string_p (inter))
121             {
122               pscore_l_->outputter_l_
123                 ->output_scheme (scm_list_n (between, 
124                                              inter, SCM_UNDEFINED));          
125             }
126         }
127     }
128 }
129
130
131
132
133 /*
134   Find the loose columns in POSNS, and drape them around the columns
135   specified in BETWEEN-COLS.  */
136 void
137 set_loose_columns (Line_of_score* which, Column_x_positions const *posns)
138 {
139   for (int i = 0; i < posns->loose_cols_.size (); i++)
140     {
141       int divide_over = 1;
142       Item *loose = dynamic_cast<Item*> (posns->loose_cols_[i]);
143       Paper_column* col = dynamic_cast<Paper_column*> (loose);
144       
145       if (col->line_l_)
146         continue;
147
148       
149       Item * left = 0;
150       Item * right = 0;
151       do
152         {
153           SCM between = loose->get_grob_property ("between-cols");
154           if (!gh_pair_p (between))
155             break;
156
157
158           Item * l=dynamic_cast<Item*> (unsmob_grob (ly_car (between)));
159           Item * r=dynamic_cast<Item*> (unsmob_grob (ly_cdr (between)));
160
161           if (!(l && r))
162             break ;
163           
164           if (!left && l)
165             {
166               left = l->column_l ();
167             }
168
169           divide_over ++;
170
171           loose = right = r->column_l ();
172         }
173       while (1);
174       
175       Real rx = right->relative_coordinate (right->get_parent (X_AXIS), X_AXIS);
176       Real lx = left->relative_coordinate (left->get_parent (X_AXIS), X_AXIS);
177
178       int j = 1;
179       loose = col;
180       while (1)
181         {
182           SCM between = loose->get_grob_property ("between-cols");
183           if (!gh_pair_p (between))
184             break;
185
186           Paper_column *thiscol = dynamic_cast<Paper_column*> (loose);
187
188           thiscol->line_l_ = which;
189           thiscol->translate_axis (lx + j*(rx - lx)/divide_over, X_AXIS);
190
191           j ++; 
192           loose = dynamic_cast<Item*> (unsmob_grob (ly_cdr (between)));
193         }
194     }
195 }
196
197 // const?
198 void
199 Line_of_score::break_into_pieces (Array<Column_x_positions> const &breaking)
200 {
201   for (int i=0; i < breaking.size (); i++)
202     {
203       Line_of_score *line_l = dynamic_cast <Line_of_score*> (clone ());
204       line_l->rank_i_ = i;
205       //      line_l->set_immutable_grob_property ("rank", gh_int2scm (i));
206       Link_array<Grob> c (breaking[i].cols_);
207       pscore_l_->typeset_line (line_l);
208       
209       line_l->set_bound (LEFT,c[0]);
210       line_l->set_bound (RIGHT,c.top ());
211       for (int j=0; j < c.size (); j++)
212         {
213           c[j]->translate_axis (breaking[i].config_[j],X_AXIS);
214           dynamic_cast<Paper_column*> (c[j])->line_l_ = line_l;
215         }
216       set_loose_columns (line_l, &breaking[i]);
217       broken_into_l_arr_.push (line_l);
218     }
219 }
220
221
222 #define GLOBAL_SYMBOL(cname, name)  \
223 SCM cname ;                                     \
224 void \
225 cname ## _init_func ()                          \
226 {                                               \
227   cname = ly_symbol2scm (name);                 \
228   scm_permanent_object (cname);                 \
229 }                                               \
230 ADD_SCM_INIT_FUNC (cname,cname ## _init_func);\
231
232
233 GLOBAL_SYMBOL (offset_sym , "translate-molecule");
234 GLOBAL_SYMBOL (placebox_sym , "placebox");
235 GLOBAL_SYMBOL (combine_sym , "combine-molecule");
236 GLOBAL_SYMBOL (no_origin_sym , "no-origin");
237 GLOBAL_SYMBOL (define_origin_sym , "define-origin");
238
239
240
241 void
242 Line_of_score::output_molecule (SCM expr, Offset o)
243 {
244
245   while (1)
246     {
247       if (!gh_pair_p (expr))
248         return;
249   
250       SCM head =ly_car (expr);
251       if (unsmob_input (head))
252         {
253           Input * ip = unsmob_input (head);
254       
255
256           pscore_l_->outputter_l_->output_scheme (scm_list_n (define_origin_sym,
257                                                            ly_str02scm (ip->file_str ().ch_C ()),
258                                                            gh_int2scm (ip->line_number ()),
259                                                            gh_int2scm (ip->column_number ()),
260                                                            SCM_UNDEFINED));
261           expr = ly_cadr (expr);
262         }
263       else  if (head ==  no_origin_sym)
264         {
265           pscore_l_->outputter_l_->output_scheme (scm_list_n (no_origin_sym, SCM_UNDEFINED));
266           expr = ly_cadr (expr);
267         }
268       else if (head == offset_sym)
269         {
270           o += ly_scm2offset (ly_cadr (expr));
271           expr = ly_caddr (expr);
272         }
273       else if (head == combine_sym)
274         {
275           output_molecule (ly_cadr (expr), o);
276           expr = ly_caddr (expr);
277         }
278       else
279         {
280           pscore_l_->outputter_l_->
281             output_scheme (scm_list_n (placebox_sym,
282                                     gh_double2scm (o[X_AXIS]),
283                                     gh_double2scm (o[Y_AXIS]),
284                                     expr,
285                                     SCM_UNDEFINED));
286
287           return;
288         }
289     }
290 }
291
292 void
293 Line_of_score::output_scheme (SCM s)
294 {
295   pscore_l_->outputter_l_->output_scheme (s);
296 }
297
298 void
299 Line_of_score::add_column (Paper_column*p)
300 {
301   Grob *me = this;
302   SCM cs = me->get_grob_property ("columns");
303   Grob * prev =  gh_pair_p (cs) ? unsmob_grob (ly_car (cs)) : 0;
304
305   p->rank_i_ = prev ? Paper_column::rank_i (prev) + 1 : 0; 
306
307   me->set_grob_property ("columns",  gh_cons (p->self_scm (), cs));
308
309   Axis_group_interface::add_element (me, p);
310 }
311
312
313
314 /*
315   TODO: use scm_map iso. for loops.
316  */
317 void
318 Line_of_score::pre_processing ()
319 {
320   for (SCM s = get_grob_property ("all-elements"); gh_pair_p (s); s = ly_cdr (s))
321     unsmob_grob (ly_car (s))->discretionary_processing ();
322
323   if (verbose_global_b)
324     progress_indication (_f ("Element count %d ",  element_count ()));
325
326   
327   for (SCM s = get_grob_property ("all-elements"); gh_pair_p (s); s = ly_cdr (s))
328     unsmob_grob (ly_car (s))->handle_prebroken_dependencies ();
329   
330   fixup_refpoints (get_grob_property ("all-elements"));
331   
332   for (SCM s = get_grob_property ("all-elements"); gh_pair_p (s); s = ly_cdr (s))
333     {
334       Grob* sc = unsmob_grob (ly_car (s));
335       sc->calculate_dependencies (PRECALCED, PRECALCING, ly_symbol2scm ("before-line-breaking-callback"));
336     }
337   
338   progress_indication ("\n" + _ ("Calculating column positions...") + " ");
339   for (SCM s = get_grob_property ("all-elements"); gh_pair_p (s); s = ly_cdr (s))
340     {
341       Grob * e = unsmob_grob (ly_car (s));
342       SCM proc = e->get_grob_property ("spacing-procedure");
343       if (gh_procedure_p (proc))
344         gh_call1 (proc, e->self_scm ());
345     }
346 }
347
348 void
349 Line_of_score::post_processing (bool last_line)
350 {
351   for (SCM s = get_grob_property ("all-elements");
352        gh_pair_p (s); s = ly_cdr (s))
353     {
354       Grob* sc = unsmob_grob (ly_car (s));
355       sc->calculate_dependencies (POSTCALCED, POSTCALCING,
356                                   ly_symbol2scm ("after-line-breaking-callback"));
357     }
358
359   Interval i (extent (this, Y_AXIS));
360   if (i.empty_b ())
361     programming_error ("Huh?  Empty Line_of_score?");
362   else
363     translate_axis (- i[MAX], Y_AXIS);
364
365   Real height = i.length ();
366   if (height > 50 CM)
367     {
368       programming_error ("Improbable system height");
369       height = 50 CM;
370     }
371
372   /*
373     generate all molecules  to trigger all font loads.
374
375     (ugh. This is not very memory efficient.)  */
376   for (SCM s = get_grob_property ("all-elements"); gh_pair_p (s); s = ly_cdr (s))
377     {
378       unsmob_grob (ly_car (s))->get_molecule ();
379     }
380   /*
381     font defs;
382    */
383   SCM font_names = ly_quote_scm (paper_l ()->font_descriptions ());  
384   output_scheme (scm_list_n (ly_symbol2scm ("define-fonts"),
385                              font_names,
386                              SCM_UNDEFINED));
387
388   /*
389     line preamble.
390    */
391   output_scheme (scm_list_n (ly_symbol2scm ("start-line"),
392                           gh_double2scm (height),
393                           SCM_UNDEFINED));
394   
395   /* Output elements in three layers, 0, 1, 2.
396      The default layer is 1. */
397   for (int i = 0; i < 3; i++)
398     for (SCM s = get_grob_property ("all-elements"); gh_pair_p (s);
399          s = ly_cdr (s))
400       {
401         Grob *sc = unsmob_grob (ly_car (s));
402         Molecule *m = sc->get_molecule ();
403         if (!m)
404           continue;
405         
406         SCM s = sc->get_grob_property ("layer");
407         int layer = gh_number_p (s) ? gh_scm2int (s) : 1;
408         if (layer != i)
409           continue;
410         
411         Offset o (sc->relative_coordinate (this, X_AXIS),
412                   sc->relative_coordinate (this, Y_AXIS));
413         
414         SCM e = sc->get_grob_property ("extra-offset");
415         if (gh_pair_p (e))
416           {
417             o[X_AXIS] += gh_scm2double (ly_car (e));
418             o[Y_AXIS] += gh_scm2double (ly_cdr (e));      
419           }
420         
421         output_molecule (m->get_expr (), o);
422       }
423   
424   if (last_line)
425     {
426       output_scheme (scm_list_n (ly_symbol2scm ("stop-last-line"), SCM_UNDEFINED));
427     }
428   else
429     {
430       output_scheme (scm_list_n (ly_symbol2scm ("stop-line"), SCM_UNDEFINED));
431     }
432 }
433
434
435 Link_array<Item> 
436 Line_of_score::broken_col_range (Item const*l, Item const*r) const
437 {
438   Link_array<Item> ret;
439
440   l = l->column_l ();
441   r = r->column_l ();
442   SCM s = get_grob_property ("columns");
443
444   while (gh_pair_p (s) && ly_car (s) != r->self_scm ())
445     s = ly_cdr (s);
446     
447   if (gh_pair_p (s))
448     s = ly_cdr (s);
449   
450   while (gh_pair_p (s) && ly_car (s) != l->self_scm ())
451     {
452       Paper_column*c = dynamic_cast<Paper_column*> (unsmob_grob (ly_car (s)));
453       if (Item::breakable_b (c) && !c->line_l_)
454         ret.push (c);
455
456       s = ly_cdr (s);
457     }
458
459   ret.reverse ();
460   return ret;
461 }
462
463 /**
464    Return all columns, but filter out any unused columns , since they might
465    disrupt the spacing problem.
466  */
467 Link_array<Grob>
468 Line_of_score::column_l_arr ()const
469 {
470   Link_array<Grob> acs
471     = Pointer_group_interface__extract_grobs (this, (Grob*) 0, "columns");
472   bool bfound = false;
473   for (int i= acs.size (); i -- ;)
474     {
475       bool brb = Item::breakable_b (acs[i]);
476       bfound = bfound || brb;
477
478       /*
479         the last column should be breakable. Weed out any columns that
480         seem empty. We need to retain breakable columns, in case
481         someone forced a breakpoint.
482       */
483       if (!bfound || !Paper_column::used_b (acs[i]))
484         acs.del (i);
485     }
486   return acs;
487 }
488