]> git.donarmstrong.com Git - lilypond.git/blob - lily/paper-book.cc
926f9f5a962afed18ba94dfdc394ace7df50d5bf
[lilypond.git] / lily / paper-book.cc
1 /*
2   paper-book.cc -- implement Paper_book
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 2004--2006 Jan Nieuwenhuizen <janneke@gnu.org>
7 */
8
9 #include "paper-book.hh"
10
11 #include "grob.hh"
12 #include "main.hh"
13 #include "output-def.hh"
14 #include "paper-score.hh"
15 #include "paper-system.hh"
16 #include "text-interface.hh"
17 #include "warn.hh"
18
19 #include "ly-smobs.icc"
20
21 Paper_book::Paper_book ()
22 {
23   header_ = SCM_EOL;
24   header_0_ = SCM_EOL;
25   pages_ = SCM_BOOL_F;
26   scores_ = SCM_EOL;
27   performances_ = SCM_EOL;
28   systems_ = SCM_BOOL_F;
29
30   paper_ = 0;
31   smobify_self ();
32 }
33
34 Paper_book::~Paper_book ()
35 {
36 }
37
38 IMPLEMENT_DEFAULT_EQUAL_P (Paper_book);
39 IMPLEMENT_SMOBS (Paper_book);
40 IMPLEMENT_TYPE_P (Paper_book, "ly:paper-book?");
41
42 SCM
43 Paper_book::mark_smob (SCM smob)
44 {
45   Paper_book *b = (Paper_book *) SCM_CELL_WORD_1 (smob);
46   if (b->paper_)
47     scm_gc_mark (b->paper_->self_scm ());
48   scm_gc_mark (b->header_);
49   scm_gc_mark (b->header_0_);
50   scm_gc_mark (b->pages_);
51   scm_gc_mark (b->performances_);
52   scm_gc_mark (b->scores_);
53   return b->systems_;
54 }
55
56 int
57 Paper_book::print_smob (SCM smob, SCM port, scm_print_state*)
58 {
59   Paper_book *b = (Paper_book *) SCM_CELL_WORD_1 (smob);
60   (void)b;
61   scm_puts ("#<Paper_book>", port);
62   return 1;
63 }
64
65 SCM
66 dump_fields ()
67 {
68   SCM fields = SCM_EOL;
69   for (vsize i = dump_header_fieldnames_global.size (); i--;)
70     fields
71       = scm_cons (ly_symbol2scm (dump_header_fieldnames_global[i].c_str ()),
72                   fields);
73   return fields;
74 }
75
76 void
77 Paper_book::add_score (SCM s)
78 {
79   scores_ = scm_cons (s, scores_);
80 }
81
82 void
83 Paper_book::add_performance (SCM s)
84 {
85   performances_ = scm_cons (s, performances_);
86 }
87
88 void
89 Paper_book::output (SCM output_channel)
90 {
91   if (scm_is_pair (performances_))
92     {
93       SCM proc = ly_lily_module_constant ("paper-book-write-midis");
94
95       scm_call_2 (proc, self_scm (), output_channel);
96     }
97
98   if (scores_ == SCM_EOL)
99     return;
100
101   /* Generate all stencils to trigger font loads.  */
102   pages ();
103
104   SCM scopes = SCM_EOL;
105   if (ly_is_module (header_))
106     scopes = scm_cons (header_, scopes);
107
108   string mod_nm = "scm framework-" + output_backend_global;
109
110   SCM mod = scm_c_resolve_module (mod_nm.c_str ());
111   if (make_print)
112     {
113       SCM func = scm_c_module_lookup (mod, "output-framework");
114
115       func = scm_variable_ref (func);
116       scm_apply_0 (func, scm_list_n (output_channel,
117                                      self_scm (),
118                                      scopes,
119                                      dump_fields (),
120                                      SCM_UNDEFINED));
121     }
122
123   if (make_preview)
124     {
125       SCM func = scm_c_module_lookup (mod, "output-preview-framework");
126       func = scm_variable_ref (func);
127       scm_apply_0 (func, scm_list_n (output_channel,
128                                      self_scm (),
129                                      scopes,
130                                      dump_fields (),
131                                      SCM_UNDEFINED));
132     }
133 }
134
135 void
136 Paper_book::classic_output (SCM output)
137 {
138   /* Generate all stencils to trigger font loads.  */
139   systems ();
140
141   SCM scopes = SCM_EOL;
142   if (ly_is_module (header_))
143     scopes = scm_cons (header_, scopes);
144
145   if (ly_is_module (header_0_))
146     scopes = scm_cons (header_0_, scopes);
147
148   string format = output_backend_global;
149   string mod_nm = "scm framework-" + format;
150
151   SCM mod = scm_c_resolve_module (mod_nm.c_str ());
152   SCM func = scm_c_module_lookup (mod, "output-classic-framework");
153
154   func = scm_variable_ref (func);
155   scm_apply_0 (func, scm_list_n (output,
156                                  self_scm (),
157                                  scopes,
158                                  dump_fields (),
159                                  SCM_UNDEFINED));
160
161   progress_indication ("\n");
162 }
163
164 /* TODO: resurrect more complex user-tweaks for titling?  */
165 Stencil
166 Paper_book::book_title ()
167 {
168   SCM title_func = paper_->lookup_variable (ly_symbol2scm ("book-title"));
169   Stencil title;
170
171   SCM scopes = SCM_EOL;
172   if (ly_is_module (header_))
173     scopes = scm_cons (header_, scopes);
174
175   SCM tit = SCM_EOL;
176   if (ly_is_procedure (title_func))
177     tit = scm_call_2 (title_func,
178                       paper_->self_scm (),
179                       scopes);
180
181   if (unsmob_stencil (tit))
182     title = *unsmob_stencil (tit);
183
184   if (!title.is_empty ())
185     title.align_to (Y_AXIS, UP);
186
187   return title;
188 }
189
190 Stencil
191 Paper_book::score_title (SCM header)
192 {
193   SCM title_func = paper_->lookup_variable (ly_symbol2scm ("score-title"));
194
195   Stencil title;
196
197   SCM scopes = SCM_EOL;
198   if (ly_is_module (header_))
199     scopes = scm_cons (header_, scopes);
200
201   if (ly_is_module (header))
202     scopes = scm_cons (header, scopes);
203
204   SCM tit = SCM_EOL;
205   if (ly_is_procedure (title_func))
206     tit = scm_call_2 (title_func,
207                       paper_->self_scm (),
208                       scopes);
209
210   if (unsmob_stencil (tit))
211     title = *unsmob_stencil (tit);
212
213   if (!title.is_empty ())
214     title.align_to (Y_AXIS, UP);
215
216   return title;
217 }
218
219 /* read the breakbefore property of a score block and set up the preceding
220    system-spec to honour it. That is, SYM should be the system spec that
221    immediately precedes the score (from which HEADER is taken)
222    in the get_system_specs() list */
223 void
224 set_system_penalty (SCM sys, SCM header)
225 {
226   if (ly_is_module (header))
227     {
228       SCM force = ly_module_lookup (header, ly_symbol2scm ("breakbefore"));
229       if (SCM_VARIABLEP (force)
230           && scm_is_bool (SCM_VARIABLE_REF (force)))
231         {
232           bool b = to_boolean (SCM_VARIABLE_REF (force));
233           SCM sym = b ? ly_symbol2scm ("force") : SCM_EOL;
234
235           if (Paper_score *ps = dynamic_cast<Paper_score*> (unsmob_music_output (sys)))
236             {
237               vector<Grob*> cols = ps->get_columns ();
238               if (cols.size ())
239                 cols.back ()->set_property ("page-break-permission", sym);
240             }
241           else if (Prob *pb = unsmob_prob (sys))
242             pb->set_property ("page-break-permission", sym);
243         }
244     }
245 }
246
247
248 SCM
249 Paper_book::get_score_title (SCM header)
250 {
251   Stencil title = score_title (header);
252   if (title.is_empty ())
253     title = score_title (header_);
254   if (!title.is_empty ())
255     {
256       /*
257         TODO: this should come from the \layout {} block, which should
258         override settings from \paper {}
259       */
260       SCM props = paper_->lookup_variable (ly_symbol2scm ("score-title-properties"));
261       Prob *ps = make_paper_system (props);
262       paper_system_set_stencil (ps, title);
263
264       return ps->self_scm();
265     }
266
267   return SCM_BOOL_F;
268 }
269
270
271 SCM
272 Paper_book::get_system_specs ()
273 {
274   SCM system_specs = SCM_EOL;
275   
276   Stencil title = book_title ();
277   if (!title.is_empty ())
278     {
279       SCM props = paper_->lookup_variable (ly_symbol2scm ("book-title-properties"));
280       Prob *ps = make_paper_system (props);
281       paper_system_set_stencil (ps, title);
282
283       system_specs = scm_cons (ps->self_scm (), system_specs);
284       ps->unprotect ();
285     }
286
287   SCM page_properties
288     = scm_call_1 (ly_lily_module_constant ("layout-extract-page-properties"),
289                   paper_->self_scm ());
290
291   SCM header = SCM_EOL;
292   for (SCM s = scm_reverse (scores_); scm_is_pair (s); s = scm_cdr (s))
293     {
294       if (ly_is_module (scm_car (s)))
295         {
296           header = scm_car (s);
297           if (header_0_ == SCM_EOL)
298             header_0_ = header;
299         }
300       else if (Music_output *mop = unsmob_music_output (scm_car (s)))
301         {
302           if (Paper_score *pscore = dynamic_cast<Paper_score *> (mop))
303             {
304               SCM title = get_score_title (header);
305
306               if (scm_is_pair (system_specs))
307                 set_system_penalty (scm_car (system_specs), header);
308
309               if (unsmob_prob (title))
310                 {
311                   system_specs = scm_cons (title, system_specs);
312                   unsmob_prob (title)->unprotect ();
313                 }
314
315               header = SCM_EOL;
316               system_specs = scm_cons (pscore->self_scm (), system_specs);
317             }
318           else
319             {
320               /*
321                 Ignore MIDI
322               */
323             }
324         }
325       else if (Text_interface::is_markup (scm_car (s)))
326         {
327           SCM t = Text_interface::interpret_markup (paper_->self_scm (),
328                                                     page_properties,
329                                                     scm_car (s));
330           
331           // TODO: init props
332           Prob *ps = make_paper_system (SCM_EOL);
333           paper_system_set_stencil (ps, *unsmob_stencil (t));
334           ps->set_property ("is-title", SCM_BOOL_T); 
335           system_specs = scm_cons (ps->self_scm (), system_specs);
336           ps->unprotect ();
337           
338           // FIXME: figure out penalty.
339           //set_system_penalty (ps, scores_[i].header_);
340         }
341       else
342         assert (0);
343     }
344
345   system_specs = scm_reverse_x (system_specs, SCM_EOL);
346   return system_specs;
347 }
348
349 SCM
350 Paper_book::systems ()
351 {
352   if (systems_ != SCM_BOOL_F)
353     return systems_;
354
355   systems_ = SCM_EOL;
356   SCM specs = get_system_specs ();
357   for (SCM s = specs; scm_is_pair (s); s = scm_cdr (s))
358     {
359       if (Paper_score *pscore = dynamic_cast<Paper_score*> (unsmob_music_output (scm_car (s))))
360         {
361           SCM system_list = scm_vector_to_list (pscore->get_paper_systems ());
362           system_list = scm_reverse (system_list);
363           systems_ = scm_append (scm_list_2 (system_list, systems_));
364         }
365       else
366         {
367           systems_ = scm_cons (scm_car (s), systems_);
368         }
369     }
370   
371   systems_ = scm_reverse (systems_);
372
373   int i = 0;
374   Prob *last = 0;
375   for (SCM s = systems_; scm_is_pair (s); s = scm_cdr (s))
376     {
377       Prob *ps = unsmob_prob (scm_car (s));
378       ps->set_property ("number", scm_from_int (++i));
379
380       if (last
381           && to_boolean (last->get_property ("is-title"))
382           && !scm_is_number (ps->get_property ("penalty")))
383         ps->set_property ("penalty", scm_from_int (10000));
384       last = ps;
385     }
386
387   return systems_;
388 }
389
390 SCM
391 Paper_book::pages ()
392 {
393   if (SCM_BOOL_F != pages_)
394     return pages_;
395
396   pages_ = SCM_EOL;
397   SCM proc = paper_->c_variable ("page-breaking-wrapper");
398   pages_ = scm_apply_0 (proc, scm_list_1(self_scm ()));
399
400   /* set systems_ from the pages */
401   if (systems_ == SCM_BOOL_F)
402     {
403       systems_ = SCM_EOL;
404       for (SCM p = pages_; scm_is_pair (p); p = scm_cdr (p))
405         {
406           Prob *page = unsmob_prob (scm_car (p));
407           SCM systems = page->get_property ("lines");
408
409           systems_ = scm_append (scm_list_2 (systems_, systems));
410         }
411     }
412
413   return pages_;
414 }
415
416 SCM
417 Paper_book::performances () const
418 {
419   return scm_reverse (performances_);
420 }