]> git.donarmstrong.com Git - lilypond.git/blob - lily/line-of-score.cc
62bd6a153d0b2ac5acc8711f8c0bb1f6a5b39441
[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 }
106
107 // const?
108 void
109 Line_of_score::break_into_pieces (Array<Column_x_positions> const &breaking)
110 {
111   for (int i=0; i < breaking.size (); i++)
112     {
113       Line_of_score *line_l = dynamic_cast <Line_of_score*> (clone());
114       line_l->rank_i_ = i;
115       Link_array<Paper_column> c (breaking[i].cols_);
116       pscore_l_->typeset_line (line_l);
117       
118       line_l->set_bound(LEFT,c[0]);
119       line_l->set_bound(RIGHT,c.top ());
120       for (int j=0; j < c.size(); j++)
121         {
122           c[j]->translate_axis (breaking[i].config_[j],X_AXIS);
123           c[j]->line_l_ = line_l;
124         }
125       
126       broken_into_l_arr_.push (line_l);
127     }
128 }
129
130
131 void
132 Line_of_score::output_molecule (SCM expr, Offset o)
133 {
134   SCM offset_sym = ly_symbol2scm ("translate-molecule");
135   SCM combine_sym = ly_symbol2scm ("combine-molecule");
136 enter:
137
138   if (!gh_pair_p (expr))
139     return;
140   
141   SCM head =gh_car (expr);
142   if (head == offset_sym)
143     {
144       o += ly_scm2offset (gh_cadr (expr));
145       expr = gh_caddr (expr);
146       goto enter;
147     }
148   else if (head == combine_sym)
149     {
150       output_molecule (gh_cadr (expr), o);
151       expr = gh_caddr (expr);
152       goto enter;               // tail recursion
153     }
154   else
155     {
156       pscore_l_->outputter_l_->
157         output_scheme (gh_list (ly_symbol2scm ("placebox"),
158                                 gh_double2scm (o[X_AXIS]),
159                                 gh_double2scm (o[Y_AXIS]),
160                                 expr,
161                                 SCM_UNDEFINED));
162     }
163 }
164
165 void
166 Line_of_score::output_scheme (SCM s)
167 {
168   pscore_l_->outputter_l_->output_scheme (s);
169 }
170
171 void
172 Line_of_score::add_column (Paper_column*p)
173 {
174   SCM cs = get_elt_pointer ("columns");
175   Score_element * prev =  gh_pair_p (cs) ? unsmob_element (gh_car (cs)) : 0;
176   int rank = prev ? dynamic_cast<Paper_column*> (prev)->rank_i () + 1 : 0; 
177
178   p->set_rank (rank);
179   set_elt_pointer ("columns",  gh_cons (p->self_scm_, cs));
180
181   Axis_group_interface (this).add_element (p);
182   typeset_element (p);
183 }
184
185
186 void
187 fixup_refpoints (SCM s)
188 {
189   for (; gh_pair_p (s); s = gh_cdr (s))
190     {
191       Score_element * se = unsmob_element (gh_car (s));
192       if (se)
193         {
194           se->fixup_refpoint ();
195           if (!dynamic_cast<Line_of_score*> (se) && !se->parent_l (Y_AXIS))
196             {
197               programming_error ("No parent!");
198             }
199         }
200     }
201 }
202
203
204 void
205 Line_of_score::pre_processing ()
206 {
207   for (SCM s = get_elt_pointer ("all-elements"); gh_pair_p (s); s = gh_cdr (s))
208     unsmob_element (gh_car (s))->discretionary_processing ();
209
210   if(verbose_global_b)
211     progress_indication ( _f("Element count %d ",  element_count ()));
212
213   
214   for (SCM s = get_elt_pointer ("all-elements"); gh_pair_p (s); s = gh_cdr (s))
215     unsmob_element (gh_car (s))->handle_prebroken_dependencies ();
216   
217   fixup_refpoints (get_elt_pointer ("all-elements"));
218   
219   for (SCM s = get_elt_pointer ("all-elements"); gh_pair_p (s); s = gh_cdr (s))
220     {
221       Score_element* sc = unsmob_element (gh_car (s));
222       sc->calculate_dependencies (PRECALCED, PRECALCING, ly_symbol2scm ("before-line-breaking-callback"));
223     }
224   
225   progress_indication ("\n" + _ ("Calculating column positions...") + " " );
226   for (SCM s = get_elt_pointer ("all-elements"); gh_pair_p (s); s = gh_cdr (s))
227     unsmob_element (gh_car (s))->do_space_processing ();
228 }
229
230 void
231 Line_of_score::post_processing ()
232 {
233   for (SCM s = get_elt_pointer ("all-elements");
234        gh_pair_p (s); s = gh_cdr (s))
235     {
236       Score_element* sc = unsmob_element (gh_car (s));
237       sc->calculate_dependencies (POSTCALCED, POSTCALCING,
238                                   ly_symbol2scm ("after-line-breaking-callback"));
239     }
240
241   Interval i(extent(Y_AXIS));
242   if (i.empty_b())
243     programming_error ("Huh?  Empty Line_of_score?");
244   else
245     translate_axis (- i[MAX], Y_AXIS);
246
247   Real height = i.length ();
248   if (height > 50 CM)
249     {
250       programming_error ("Improbable system height");
251       height = 50 CM;
252     }
253
254   /*
255     generate all molecules  to trigger all font loads.
256
257     (ugh. This is not very memory efficient.)  */
258   for (SCM s = get_elt_pointer ("all-elements"); gh_pair_p (s); s = gh_cdr (s))
259     unsmob_element (gh_car (s))->get_molecule ();
260   
261   /*
262     font defs;
263    */
264   SCM font_names = ly_quote_scm (all_fonts_global_p->font_descriptions ());  
265   output_scheme (gh_list (ly_symbol2scm ("define-fonts"),
266                                         font_names,
267                                         SCM_UNDEFINED));
268
269   /*
270     line preamble.
271    */
272   output_scheme (gh_list (ly_symbol2scm ("start-line"),
273                           gh_double2scm (height),
274                           SCM_UNDEFINED));
275   
276   Real il = paper_l ()->get_var ("interline");
277
278   /*
279     all elements.
280    */ 
281   for (SCM s = get_elt_pointer ("all-elements"); gh_pair_p (s); s = gh_cdr (s))
282     {
283       Score_element * sc =  unsmob_element (gh_car (s));
284       Molecule m = sc->get_molecule ();
285       
286       Offset o (sc->relative_coordinate (this, X_AXIS),
287                 sc->relative_coordinate (this, Y_AXIS));
288
289       SCM e = sc->get_elt_property ("extra-offset");
290       if (gh_pair_p (e))
291         {
292           o[X_AXIS] += il * gh_scm2double (gh_car (e));
293           o[Y_AXIS] += il * gh_scm2double (gh_cdr (e));      
294         }
295
296       output_molecule (m.get_expr (), o);
297     }
298   output_scheme (gh_list (ly_symbol2scm ("stop-line"), SCM_UNDEFINED));
299 }
300
301
302 Link_array<Item> 
303 Line_of_score::broken_col_range (Item const*l, Item const*r) const
304 {
305   Link_array<Item> ret;
306
307   l = l->column_l ();
308   r = r->column_l ();
309   SCM s = get_elt_pointer ("columns");
310
311   while (gh_pair_p (s) && gh_car (s) != r->self_scm_)
312     s = gh_cdr  (s);
313     
314   if (gh_pair_p (s))
315     s = gh_cdr (s);
316   
317   while (gh_pair_p (s) && gh_car (s) != l->self_scm_)
318     {
319       Paper_column *c
320         = dynamic_cast<Paper_column*> (unsmob_element (gh_car (s)));
321       if (c->breakable_b () && !c->line_l_)
322         ret.push (c);
323
324       s = gh_cdr  (s);
325     }
326
327   ret.reverse ();
328   return ret;
329 }
330
331 /**
332    Return all columns, but filter out any unused columns , since they might
333    disrupt the spacing problem.
334  */
335 Link_array<Paper_column>
336 Line_of_score::column_l_arr ()const
337 {
338   Link_array<Paper_column> acs
339     = Pointer_group_interface__extract_elements (this, (Paper_column*) 0, "columns");
340   bool bfound = false;
341   for (int i= acs.size (); i -- ; )
342     {
343       bool brb = acs[i]->breakable_b();
344       bfound = bfound || brb;
345
346       /*
347         the last column should be breakable. Weed out any columns that
348         seem empty. We need to retain breakable columns, in case
349         someone forced a breakpoint.
350       */
351       if (!bfound || !acs[i]->used_b ())
352         acs.del (i);
353     }
354   return acs;
355 }