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