]> git.donarmstrong.com Git - lilypond.git/blob - lily/line-of-score.cc
release: 1.5.13
[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--2001 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_element (this, "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     fixups must be done in broken line_of_scores, because new elements
68     are put over there.  */
69   int count = 0;
70   for (int i=0; i < broken_into_l_arr_.size (); i++)
71     {
72       Grob *se = broken_into_l_arr_[i];
73       SCM all = se->get_grob_property ("all-elements");
74       for (SCM s = all; gh_pair_p (s); s = ly_cdr (s))
75         {
76           fixup_refpoint (ly_car (s));
77         }
78       count += scm_ilength (all);
79     }
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           SCM inter = e->get_grob_property ("between-system-string");
118           if (gh_string_p (inter))
119             {
120               pscore_l_->outputter_l_->output_string (inter);         
121             }
122         }
123     }
124 }
125
126 /*
127   Find the loose columns in POSNS, and drape them around the columns
128   specified in BETWEEN-COLS.  */
129 void
130 set_loose_columns (Line_of_score* which, Column_x_positions const *posns)
131 {
132   for (int i = 0; i<posns->loose_cols_.size (); i++)
133     {
134       int divide_over = 1;
135       Item *loose = dynamic_cast<Item*> (posns->loose_cols_[i]);
136       Paper_column* col = dynamic_cast<Paper_column*> (loose);
137       
138       if (col->line_l_)
139         continue;
140
141       
142       Item * left = 0;
143       Item * right = 0;
144       while (1)
145         {
146           
147           SCM between = loose->get_grob_property ("between-cols");
148           if (!gh_pair_p (between))
149             break;
150
151           if (!left)
152             {
153               left = dynamic_cast<Item*> (unsmob_grob (ly_car (between)));
154               left = left->column_l ();
155             }
156           divide_over ++;       
157           loose = dynamic_cast<Item*> (unsmob_grob (ly_cdr (between)));
158           loose = loose->column_l ();
159         }
160
161       right = loose;
162
163       Real rx = right->relative_coordinate (right->parent_l (X_AXIS), X_AXIS);
164       Real lx = left->relative_coordinate (left->parent_l (X_AXIS), X_AXIS);
165
166       int j = 1;
167       loose = col;
168       while (1)
169         {
170           SCM between = loose->get_grob_property ("between-cols");
171           if (!gh_pair_p (between))
172             break;
173
174           Paper_column *thiscol = dynamic_cast<Paper_column*> (loose);
175
176           thiscol->line_l_ = which;
177           thiscol->translate_axis (lx + j*(rx - lx)/divide_over, X_AXIS);
178
179           j ++; 
180           loose = dynamic_cast<Item*> (unsmob_grob (ly_cdr (between)));
181         }
182       
183     }
184 }
185
186 // const?
187 void
188 Line_of_score::break_into_pieces (Array<Column_x_positions> const &breaking)
189 {
190   for (int i=0; i < breaking.size (); i++)
191     {
192       Line_of_score *line_l = dynamic_cast <Line_of_score*> (clone ());
193       line_l->rank_i_ = i;
194       //      line_l->set_immutable_grob_property ("rank", gh_int2scm (i));
195       Link_array<Grob> c (breaking[i].cols_);
196       pscore_l_->typeset_line (line_l);
197       
198       line_l->set_bound (LEFT,c[0]);
199       line_l->set_bound (RIGHT,c.top ());
200       for (int j=0; j < c.size (); j++)
201         {
202           c[j]->translate_axis (breaking[i].config_[j],X_AXIS);
203           dynamic_cast<Paper_column*> (c[j])->line_l_ = line_l;
204         }
205       set_loose_columns (line_l, &breaking[i]);
206       broken_into_l_arr_.push (line_l);
207     }
208 }
209
210
211 #define GLOBAL_SYMBOL(cname, name)  \
212 SCM cname ;                                     \
213 void \
214 cname ## _init_func ()                          \
215 {                                               \
216   cname = ly_symbol2scm (name);                 \
217   scm_permanent_object (cname);                 \
218 }                                               \
219 ADD_SCM_INIT_FUNC (cname,cname ## _init_func);\
220
221
222 GLOBAL_SYMBOL (offset_sym , "translate-molecule");
223 GLOBAL_SYMBOL (placebox_sym , "placebox");
224 GLOBAL_SYMBOL (combine_sym , "combine-molecule");
225 GLOBAL_SYMBOL (no_origin_sym , "no-origin");
226 GLOBAL_SYMBOL (define_origin_sym , "define-origin");
227
228
229
230 void
231 Line_of_score::output_molecule (SCM expr, Offset o)
232 {
233
234   while (1)
235     {
236       if (!gh_pair_p (expr))
237         return;
238   
239       SCM head =ly_car (expr);
240       if (unsmob_input (head))
241         {
242           Input * ip = unsmob_input (head);
243       
244
245           pscore_l_->outputter_l_->output_scheme (scm_list_n (define_origin_sym,
246                                                            ly_str02scm (ip->file_str ().ch_C ()),
247                                                            gh_int2scm (ip->line_number ()),
248                                                            gh_int2scm (ip->column_number ()),
249                                                            SCM_UNDEFINED));
250           expr = ly_cadr (expr);
251         }
252       else  if (head ==  no_origin_sym)
253         {
254           pscore_l_->outputter_l_->output_scheme (scm_list_n (no_origin_sym, SCM_UNDEFINED));
255           expr = ly_cadr (expr);
256         }
257       else if (head == offset_sym)
258         {
259           o += ly_scm2offset (ly_cadr (expr));
260           expr = ly_caddr (expr);
261         }
262       else if (head == combine_sym)
263         {
264           output_molecule (ly_cadr (expr), o);
265           expr = ly_caddr (expr);
266         }
267       else
268         {
269           pscore_l_->outputter_l_->
270             output_scheme (scm_list_n (placebox_sym,
271                                     gh_double2scm (o[X_AXIS]),
272                                     gh_double2scm (o[Y_AXIS]),
273                                     expr,
274                                     SCM_UNDEFINED));
275
276           return;
277         }
278     }
279 }
280
281 void
282 Line_of_score::output_scheme (SCM s)
283 {
284   pscore_l_->outputter_l_->output_scheme (s);
285 }
286
287 void
288 Line_of_score::add_column (Paper_column*p)
289 {
290   Grob *me = this;
291   SCM cs = me->get_grob_property ("columns");
292   Grob * prev =  gh_pair_p (cs) ? unsmob_grob (ly_car (cs)) : 0;
293
294   p->rank_i_ = prev ? Paper_column::rank_i (prev) + 1 : 0; 
295
296   me->set_grob_property ("columns",  gh_cons (p->self_scm (), cs));
297
298   Axis_group_interface::add_element (me, p);
299 }
300
301
302
303 /*
304   TODO: use scm_map iso. for loops.
305  */
306 void
307 Line_of_score::pre_processing ()
308 {
309   for (SCM s = get_grob_property ("all-elements"); gh_pair_p (s); s = ly_cdr (s))
310     unsmob_grob (ly_car (s))->discretionary_processing ();
311
312   if (verbose_global_b)
313     progress_indication (_f ("Element count %d ",  element_count ()));
314
315   
316   for (SCM s = get_grob_property ("all-elements"); gh_pair_p (s); s = ly_cdr (s))
317     unsmob_grob (ly_car (s))->handle_prebroken_dependencies ();
318   
319   fixup_refpoints (get_grob_property ("all-elements"));
320   
321   for (SCM s = get_grob_property ("all-elements"); gh_pair_p (s); s = ly_cdr (s))
322     {
323       Grob* sc = unsmob_grob (ly_car (s));
324       sc->calculate_dependencies (PRECALCED, PRECALCING, ly_symbol2scm ("before-line-breaking-callback"));
325     }
326   
327   progress_indication ("\n" + _ ("Calculating column positions...") + " ");
328   for (SCM s = get_grob_property ("all-elements"); gh_pair_p (s); s = ly_cdr (s))
329     {
330       Grob * e = unsmob_grob (ly_car (s));
331       SCM proc = e->get_grob_property ("spacing-procedure");
332       if (gh_procedure_p (proc))
333         gh_call1 (proc, e->self_scm ());
334     }
335 }
336
337 void
338 Line_of_score::post_processing (bool last_line)
339 {
340   for (SCM s = get_grob_property ("all-elements");
341        gh_pair_p (s); s = ly_cdr (s))
342     {
343       Grob* sc = unsmob_grob (ly_car (s));
344       sc->calculate_dependencies (POSTCALCED, POSTCALCING,
345                                   ly_symbol2scm ("after-line-breaking-callback"));
346     }
347
348   Interval i (extent (this, Y_AXIS));
349   if (i.empty_b ())
350     programming_error ("Huh?  Empty Line_of_score?");
351   else
352     translate_axis (- i[MAX], Y_AXIS);
353
354   Real height = i.length ();
355   if (height > 50 CM)
356     {
357       programming_error ("Improbable system height");
358       height = 50 CM;
359     }
360
361   /*
362     generate all molecules  to trigger all font loads.
363
364  (ugh. This is not very memory efficient.)  */
365   for (SCM s = get_grob_property ("all-elements"); gh_pair_p (s); s = ly_cdr (s))
366     {
367       unsmob_grob (ly_car (s))->get_molecule ();
368     }
369   /*
370     font defs;
371    */
372   SCM font_names = ly_quote_scm (paper_l ()->font_descriptions ());  
373   output_scheme (scm_list_n (ly_symbol2scm ("define-fonts"),
374                                         font_names,
375                                         SCM_UNDEFINED));
376
377   /*
378     line preamble.
379    */
380   output_scheme (scm_list_n (ly_symbol2scm ("start-line"),
381                           gh_double2scm (height),
382                           SCM_UNDEFINED));
383   
384   /* Output elements in three layers, 0, 1, 2.
385      The default layer is 1. */
386   for (int i = 0; i < 3; i++)
387     for (SCM s = get_grob_property ("all-elements"); gh_pair_p (s);
388          s = ly_cdr (s))
389       {
390         Grob *sc = unsmob_grob (ly_car (s));
391         Molecule *m = sc->get_molecule ();
392         if (!m)
393           continue;
394         
395         SCM s = sc->get_grob_property ("layer");
396         int layer = gh_number_p (s) ? gh_scm2int (s) : 1;
397         if (layer != i)
398           continue;
399         
400         Offset o (sc->relative_coordinate (this, X_AXIS),
401                   sc->relative_coordinate (this, Y_AXIS));
402         
403         SCM e = sc->get_grob_property ("extra-offset");
404         if (gh_pair_p (e))
405           {
406             o[X_AXIS] += gh_scm2double (ly_car (e));
407             o[Y_AXIS] += gh_scm2double (ly_cdr (e));      
408           }
409         
410         output_molecule (m->get_expr (), o);
411       }
412   
413   if (last_line)
414     {
415       output_scheme (scm_list_n (ly_symbol2scm ("stop-last-line"), SCM_UNDEFINED));
416     }
417   else
418     {
419       output_scheme (scm_list_n (ly_symbol2scm ("stop-line"), SCM_UNDEFINED));
420     }
421 }
422
423
424 Link_array<Item> 
425 Line_of_score::broken_col_range (Item const*l, Item const*r) const
426 {
427   Link_array<Item> ret;
428
429   l = l->column_l ();
430   r = r->column_l ();
431   SCM s = get_grob_property ("columns");
432
433   while (gh_pair_p (s) && ly_car (s) != r->self_scm ())
434     s = ly_cdr (s);
435     
436   if (gh_pair_p (s))
437     s = ly_cdr (s);
438   
439   while (gh_pair_p (s) && ly_car (s) != l->self_scm ())
440     {
441       Paper_column*c = dynamic_cast<Paper_column*> (unsmob_grob (ly_car (s)));
442       if (Item::breakable_b (c) && !c->line_l_)
443         ret.push (c);
444
445       s = ly_cdr (s);
446     }
447
448   ret.reverse ();
449   return ret;
450 }
451
452 /**
453    Return all columns, but filter out any unused columns , since they might
454    disrupt the spacing problem.
455  */
456 Link_array<Grob>
457 Line_of_score::column_l_arr ()const
458 {
459   Link_array<Grob> acs
460     = Pointer_group_interface__extract_elements (this, (Grob*) 0, "columns");
461   bool bfound = false;
462   for (int i= acs.size (); i -- ;)
463     {
464       bool brb = Item::breakable_b (acs[i]);
465       bfound = bfound || brb;
466
467       /*
468         the last column should be breakable. Weed out any columns that
469         seem empty. We need to retain breakable columns, in case
470         someone forced a breakpoint.
471       */
472       if (!bfound || !Paper_column::used_b (acs[i]))
473         acs.del (i);
474     }
475   return acs;
476 }
477