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