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