]> git.donarmstrong.com Git - lilypond.git/blob - lily/line-of-score.cc
release: 1.3.68
[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--2000 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8
9 #include "axis-group-interface.hh"
10 #include "debug.hh"
11 #include "line-of-score.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 "dimensions.hh"
20 #include "molecule.hh"
21 #include "all-font-metrics.hh"
22
23 Line_of_score::Line_of_score()
24   : Spanner (SCM_EOL)
25 {
26   set_elt_pointer ("columns", SCM_EOL);
27   set_elt_pointer ("all-elements", SCM_EOL);
28
29   Axis_group_interface (this).set_interface ();
30   Axis_group_interface (this).set_axes (Y_AXIS,X_AXIS);
31 }
32
33 int
34 Line_of_score::element_count () const
35 {
36   return scm_ilength ( get_elt_pointer ("all-elements"));
37 }
38
39 void
40 Line_of_score::typeset_element (Score_element * elem_p)
41 {
42   elem_p->pscore_l_ = pscore_l_;
43   Pointer_group_interface (this, "all-elements").add_element (elem_p);
44   scm_unprotect_object (elem_p->self_scm_);
45 }
46
47 void
48 Line_of_score::output_lines ()
49 {
50   for (SCM s = get_elt_pointer ("all-elements");
51        gh_pair_p (s); s = gh_cdr (s))
52     {
53       unsmob_element (gh_car (s))->do_break_processing ();
54     }
55   /*
56     fixups must be done in broken line_of_scores, because new elements
57     are put over there.  */
58   int count = 0;
59   for (int i=0; i < broken_into_l_arr_.size (); i++)
60     {
61       Score_element *se = broken_into_l_arr_[i];
62       SCM all = se->get_elt_pointer ("all-elements");
63       for (SCM s = all; gh_pair_p (s); s = gh_cdr (s))
64         {
65           unsmob_element (gh_car (s))->fixup_refpoint ();
66         }
67       count += scm_ilength (all);
68     }
69
70   
71   /*
72     needed for doing items.
73    */
74   for (SCM s = get_elt_pointer ("all-elements");
75        gh_pair_p (s); s = gh_cdr (s))
76     {
77       unsmob_element (gh_car (s))->fixup_refpoint ();
78     }
79   
80   for (SCM s = get_elt_pointer ("all-elements");
81        gh_pair_p (s); s = gh_cdr (s))
82     {
83       unsmob_element (gh_car (s))->handle_broken_dependencies ();
84     }
85   handle_broken_dependencies ();
86
87   if (verbose_global_b)
88     progress_indication ( _f("Element count %d.",  count + element_count()));
89
90   
91   for (int i=0; i < broken_into_l_arr_.size (); i++)
92     {
93       Line_of_score *line_l = dynamic_cast<Line_of_score*> (broken_into_l_arr_[i]);
94
95       if (verbose_global_b)
96         progress_indication ("[");
97       line_l->post_processing ();
98
99       if (verbose_global_b)
100         {
101           progress_indication (to_str (i));
102           progress_indication ("]");
103         }
104
105       if (i < broken_into_l_arr_.size () - 1)
106         {
107           SCM lastcol =  gh_car (line_l->get_elt_pointer ("columns"));
108           Score_element*  e = unsmob_element (lastcol);
109           SCM inter = e->get_elt_property ("between-system-string");
110           if (gh_string_p (inter))
111             {
112               pscore_l_->outputter_l_->output_string (inter);         
113             }
114         }
115     }
116 }
117
118 // const?
119 void
120 Line_of_score::break_into_pieces (Array<Column_x_positions> const &breaking)
121 {
122   for (int i=0; i < breaking.size (); i++)
123     {
124       Line_of_score *line_l = dynamic_cast <Line_of_score*> (clone());
125       line_l->rank_i_ = i;
126       Link_array<Paper_column> c (breaking[i].cols_);
127       pscore_l_->typeset_line (line_l);
128       
129       line_l->set_bound(LEFT,c[0]);
130       line_l->set_bound(RIGHT,c.top ());
131       for (int j=0; j < c.size(); j++)
132         {
133           c[j]->translate_axis (breaking[i].config_[j],X_AXIS);
134           c[j]->line_l_ = line_l;
135         }
136       
137       broken_into_l_arr_.push (line_l);
138     }
139 }
140
141
142 void
143 Line_of_score::output_molecule (SCM expr, Offset o)
144 {
145   SCM offset_sym = ly_symbol2scm ("translate-molecule");
146   SCM combine_sym = ly_symbol2scm ("combine-molecule");
147 enter:
148
149   if (!gh_pair_p (expr))
150     return;
151   
152   SCM head =gh_car (expr);
153   if (head == offset_sym)
154     {
155       o += ly_scm2offset (gh_cadr (expr));
156       expr = gh_caddr (expr);
157       goto enter;
158     }
159   else if (head == combine_sym)
160     {
161       output_molecule (gh_cadr (expr), o);
162       expr = gh_caddr (expr);
163       goto enter;               // tail recursion
164     }
165   else
166     {
167       pscore_l_->outputter_l_->
168         output_scheme (gh_list (ly_symbol2scm ("placebox"),
169                                 gh_double2scm (o[X_AXIS]),
170                                 gh_double2scm (o[Y_AXIS]),
171                                 expr,
172                                 SCM_UNDEFINED));
173     }
174 }
175
176 void
177 Line_of_score::output_scheme (SCM s)
178 {
179   pscore_l_->outputter_l_->output_scheme (s);
180 }
181
182 void
183 Line_of_score::add_column (Paper_column*p)
184 {
185   SCM cs = get_elt_pointer ("columns");
186   Score_element * prev =  gh_pair_p (cs) ? unsmob_element (gh_car (cs)) : 0;
187   int rank = prev ? dynamic_cast<Paper_column*> (prev)->rank_i () + 1 : 0; 
188
189   p->set_rank (rank);
190   set_elt_pointer ("columns",  gh_cons (p->self_scm_, cs));
191
192   Axis_group_interface (this).add_element (p);
193 }
194
195
196 void
197 fixup_refpoints (SCM s)
198 {
199   for (; gh_pair_p (s); s = gh_cdr (s))
200     {
201       Score_element * se = unsmob_element (gh_car (s));
202       if (se)
203         {
204           se->fixup_refpoint ();
205           if (!dynamic_cast<Line_of_score*> (se) && !se->parent_l (Y_AXIS))
206             {
207               programming_error ("No parent!");
208             }
209         }
210     }
211 }
212
213
214 void
215 Line_of_score::pre_processing ()
216 {
217   for (SCM s = get_elt_pointer ("all-elements"); gh_pair_p (s); s = gh_cdr (s))
218     unsmob_element (gh_car (s))->discretionary_processing ();
219
220   if(verbose_global_b)
221     progress_indication ( _f("Element count %d ",  element_count ()));
222
223   
224   for (SCM s = get_elt_pointer ("all-elements"); gh_pair_p (s); s = gh_cdr (s))
225     unsmob_element (gh_car (s))->handle_prebroken_dependencies ();
226   
227   fixup_refpoints (get_elt_pointer ("all-elements"));
228   
229   for (SCM s = get_elt_pointer ("all-elements"); gh_pair_p (s); s = gh_cdr (s))
230     {
231       Score_element* sc = unsmob_element (gh_car (s));
232       sc->calculate_dependencies (PRECALCED, PRECALCING, ly_symbol2scm ("before-line-breaking-callback"));
233     }
234   
235   progress_indication ("\n" + _ ("Calculating column positions...") + " " );
236   for (SCM s = get_elt_pointer ("all-elements"); gh_pair_p (s); s = gh_cdr (s))
237     unsmob_element (gh_car (s))->do_space_processing ();
238 }
239
240 void
241 Line_of_score::post_processing ()
242 {
243   for (SCM s = get_elt_pointer ("all-elements");
244        gh_pair_p (s); s = gh_cdr (s))
245     {
246       Score_element* sc = unsmob_element (gh_car (s));
247       sc->calculate_dependencies (POSTCALCED, POSTCALCING,
248                                   ly_symbol2scm ("after-line-breaking-callback"));
249     }
250
251   Interval i(extent(Y_AXIS));
252   if (i.empty_b())
253     programming_error ("Huh?  Empty Line_of_score?");
254   else
255     translate_axis (- i[MAX], Y_AXIS);
256
257   Real height = i.length ();
258   if (height > 50 CM)
259     {
260       programming_error ("Improbable system height");
261       height = 50 CM;
262     }
263
264   /*
265     generate all molecules  to trigger all font loads.
266
267     (ugh. This is not very memory efficient.)  */
268   for (SCM s = get_elt_pointer ("all-elements"); gh_pair_p (s); s = gh_cdr (s))
269     unsmob_element (gh_car (s))->get_molecule ();
270   
271   /*
272     font defs;
273    */
274   SCM font_names = ly_quote_scm (all_fonts_global_p->font_descriptions ());  
275   output_scheme (gh_list (ly_symbol2scm ("define-fonts"),
276                                         font_names,
277                                         SCM_UNDEFINED));
278
279   /*
280     line preamble.
281    */
282   output_scheme (gh_list (ly_symbol2scm ("start-line"),
283                           gh_double2scm (height),
284                           SCM_UNDEFINED));
285   
286   Real il = paper_l ()->get_var ("interline");
287
288   /*
289     all elements.
290    */ 
291   for (SCM s = get_elt_pointer ("all-elements"); gh_pair_p (s); s = gh_cdr (s))
292     {
293       Score_element * sc =  unsmob_element (gh_car (s));
294       Molecule m = sc->get_molecule ();
295       
296       Offset o (sc->relative_coordinate (this, X_AXIS),
297                 sc->relative_coordinate (this, Y_AXIS));
298
299       SCM e = sc->get_elt_property ("extra-offset");
300       if (gh_pair_p (e))
301         {
302           o[X_AXIS] += il * gh_scm2double (gh_car (e));
303           o[Y_AXIS] += il * gh_scm2double (gh_cdr (e));      
304         }
305
306       output_molecule (m.get_expr (), o);
307     }
308   output_scheme (gh_list (ly_symbol2scm ("stop-line"), SCM_UNDEFINED));
309 }
310
311
312 Link_array<Item> 
313 Line_of_score::broken_col_range (Item const*l, Item const*r) const
314 {
315   Link_array<Item> ret;
316
317   l = l->column_l ();
318   r = r->column_l ();
319   SCM s = get_elt_pointer ("columns");
320
321   while (gh_pair_p (s) && gh_car (s) != r->self_scm_)
322     s = gh_cdr  (s);
323     
324   if (gh_pair_p (s))
325     s = gh_cdr (s);
326   
327   while (gh_pair_p (s) && gh_car (s) != l->self_scm_)
328     {
329       Paper_column *c
330         = dynamic_cast<Paper_column*> (unsmob_element (gh_car (s)));
331       if (c->breakable_b () && !c->line_l_)
332         ret.push (c);
333
334       s = gh_cdr  (s);
335     }
336
337   ret.reverse ();
338   return ret;
339 }
340
341 /**
342    Return all columns, but filter out any unused columns , since they might
343    disrupt the spacing problem.
344  */
345 Link_array<Paper_column>
346 Line_of_score::column_l_arr ()const
347 {
348   Link_array<Paper_column> acs
349     = Pointer_group_interface__extract_elements (this, (Paper_column*) 0, "columns");
350   bool bfound = false;
351   for (int i= acs.size (); i -- ; )
352     {
353       bool brb = acs[i]->breakable_b();
354       bfound = bfound || brb;
355
356       /*
357         the last column should be breakable. Weed out any columns that
358         seem empty. We need to retain breakable columns, in case
359         someone forced a breakpoint.
360       */
361       if (!bfound || !acs[i]->used_b ())
362         acs.del (i);
363     }
364   return acs;
365 }