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