]> git.donarmstrong.com Git - lilypond.git/blob - lily/paper-book.cc
Merge branch 'master' of ssh+git://hanwen@git.sv.gnu.org/srv/git/lilypond
[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--2007 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-column.hh"
15 #include "paper-score.hh"
16 #include "paper-system.hh"
17 #include "text-interface.hh"
18 #include "warn.hh"
19 #include "program-option.hh"
20 #include "page-marker.hh"
21
22 #include "ly-smobs.icc"
23
24 Paper_book::Paper_book ()
25 {
26   header_ = SCM_EOL;
27   header_0_ = SCM_EOL;
28   pages_ = SCM_BOOL_F;
29   scores_ = SCM_EOL;
30   performances_ = SCM_EOL;
31   systems_ = SCM_BOOL_F;
32
33   paper_ = 0;
34   smobify_self ();
35 }
36
37 Paper_book::~Paper_book ()
38 {
39 }
40
41 IMPLEMENT_DEFAULT_EQUAL_P (Paper_book);
42 IMPLEMENT_SMOBS (Paper_book);
43 IMPLEMENT_TYPE_P (Paper_book, "ly:paper-book?");
44
45 SCM
46 Paper_book::mark_smob (SCM smob)
47 {
48   Paper_book *b = (Paper_book *) SCM_CELL_WORD_1 (smob);
49   if (b->paper_)
50     scm_gc_mark (b->paper_->self_scm ());
51   scm_gc_mark (b->header_);
52   scm_gc_mark (b->header_0_);
53   scm_gc_mark (b->pages_);
54   scm_gc_mark (b->performances_);
55   scm_gc_mark (b->scores_);
56   return b->systems_;
57 }
58
59 int
60 Paper_book::print_smob (SCM smob, SCM port, scm_print_state*)
61 {
62   Paper_book *b = (Paper_book *) SCM_CELL_WORD_1 (smob);
63   (void)b;
64   scm_puts ("#<Paper_book>", port);
65   return 1;
66 }
67
68 SCM
69 dump_fields ()
70 {
71   SCM fields = SCM_EOL;
72   for (vsize i = dump_header_fieldnames_global.size (); i--;)
73     fields
74       = scm_cons (ly_symbol2scm (dump_header_fieldnames_global[i].c_str ()),
75                   fields);
76   return fields;
77 }
78
79 void
80 Paper_book::add_score (SCM s)
81 {
82   scores_ = scm_cons (s, scores_);
83 }
84
85 void
86 Paper_book::add_performance (SCM s)
87 {
88   performances_ = scm_cons (s, performances_);
89 }
90
91 void
92 Paper_book::output (SCM output_channel)
93 {
94   if (scm_is_pair (performances_))
95     {
96       SCM proc = ly_lily_module_constant ("write-performances-midis");
97  
98       scm_call_2 (proc, performances (), output_channel);
99     }
100
101   if (scores_ == SCM_EOL)
102     return;
103
104   /* Generate all stencils to trigger font loads.  */
105   pages ();
106
107   SCM scopes = SCM_EOL;
108   if (ly_is_module (header_))
109     scopes = scm_cons (header_, scopes);
110
111   string mod_nm = "scm framework-" + get_output_backend_name ();
112
113   SCM mod = scm_c_resolve_module (mod_nm.c_str ());
114
115   if (get_program_option ("print-pages"))
116     {
117       SCM func = scm_c_module_lookup (mod, "output-framework");
118
119       func = scm_variable_ref (func);
120       scm_apply_0 (func, scm_list_n (output_channel,
121                                      self_scm (),
122                                      scopes,
123                                      dump_fields (),
124                                      SCM_UNDEFINED));
125     }
126
127   if (get_program_option ("preview"))
128     {
129       SCM func = scm_c_module_lookup (mod, "output-preview-framework");
130       func = scm_variable_ref (func);
131       scm_apply_0 (func, scm_list_n (output_channel,
132                                      self_scm (),
133                                      scopes,
134                                      dump_fields (),
135                                      SCM_UNDEFINED));
136     }
137 }
138
139 void
140 Paper_book::classic_output (SCM output)
141 {
142   if (scm_is_pair (performances_))
143     {
144       SCM proc = ly_lily_module_constant ("write-performances-midis");
145  
146       scm_call_2 (proc, performances (), output);
147     }
148   
149   /* Generate all stencils to trigger font loads.  */
150   systems ();
151
152   SCM scopes = SCM_EOL;
153   if (ly_is_module (header_))
154     scopes = scm_cons (header_, scopes);
155
156   if (ly_is_module (header_0_))
157     scopes = scm_cons (header_0_, scopes);
158
159   string format = get_output_backend_name ();
160   string mod_nm = "scm framework-" + format;
161
162   SCM mod = scm_c_resolve_module (mod_nm.c_str ());
163   SCM func = scm_c_module_lookup (mod, "output-classic-framework");
164
165   func = scm_variable_ref (func);
166   scm_apply_0 (func, scm_list_n (output,
167                                  self_scm (),
168                                  scopes,
169                                  dump_fields (),
170                                  SCM_UNDEFINED));
171
172   progress_indication ("\n");
173 }
174
175 /* TODO: resurrect more complex user-tweaks for titling?  */
176 Stencil
177 Paper_book::book_title ()
178 {
179   SCM title_func = paper_->lookup_variable (ly_symbol2scm ("book-title"));
180   Stencil title;
181
182   SCM scopes = SCM_EOL;
183   if (ly_is_module (header_))
184     scopes = scm_cons (header_, scopes);
185
186   SCM tit = SCM_EOL;
187   if (ly_is_procedure (title_func))
188     tit = scm_call_2 (title_func,
189                       paper_->self_scm (),
190                       scopes);
191
192   if (unsmob_stencil (tit))
193     title = *unsmob_stencil (tit);
194
195   if (!title.is_empty ())
196     title.align_to (Y_AXIS, UP);
197
198   return title;
199 }
200
201 Stencil
202 Paper_book::score_title (SCM header)
203 {
204   SCM title_func = paper_->lookup_variable (ly_symbol2scm ("score-title"));
205
206   Stencil title;
207
208   SCM scopes = SCM_EOL;
209   if (ly_is_module (header_))
210     scopes = scm_cons (header_, scopes);
211
212   if (ly_is_module (header))
213     scopes = scm_cons (header, scopes);
214
215   SCM tit = SCM_EOL;
216   if (ly_is_procedure (title_func))
217     tit = scm_call_2 (title_func,
218                       paper_->self_scm (),
219                       scopes);
220
221   if (unsmob_stencil (tit))
222     title = *unsmob_stencil (tit);
223
224   if (!title.is_empty ())
225     title.align_to (Y_AXIS, UP);
226
227   return title;
228 }
229
230 void
231 set_page_permission (SCM sys, SCM symbol, SCM permission)
232 {
233   if (Paper_score *ps = dynamic_cast<Paper_score*> (unsmob_music_output (sys)))
234     {
235       vector<Grob*> cols = ps->get_columns ();
236       if (cols.size ())
237         {
238           Paper_column *col = dynamic_cast<Paper_column*> (cols.back ());
239           col->set_property (symbol, permission);
240           col->find_prebroken_piece (LEFT)->set_property (symbol, permission);
241         }
242     }
243   else if (Prob *pb = unsmob_prob (sys))
244     pb->set_property (symbol, permission);
245 }
246
247 /* read the breakbefore property of a score block and set up the preceding
248    system-spec to honour it. That is, SYS should be the system spec that
249    immediately precedes the score (from which HEADER is taken)
250    in the get_system_specs () list */
251 void
252 set_system_penalty (SCM sys, SCM header)
253 {
254   if (ly_is_module (header))
255     {
256       SCM force = ly_module_lookup (header, ly_symbol2scm ("breakbefore"));
257       if (SCM_VARIABLEP (force)
258           && scm_is_bool (SCM_VARIABLE_REF (force)))
259         {
260           if (to_boolean (SCM_VARIABLE_REF (force)))
261             {
262               set_page_permission (sys, ly_symbol2scm ("page-break-permission"),
263                                    ly_symbol2scm ("force"));
264               set_page_permission (sys, ly_symbol2scm ("line-break-permission"),
265                                    ly_symbol2scm ("force"));
266             }
267           else
268             set_page_permission (sys, ly_symbol2scm ("page-break-permission"), SCM_EOL);
269         }
270     }
271 }
272
273 void
274 set_label (SCM sys, SCM label)
275 {
276   if (Paper_score *ps = dynamic_cast<Paper_score*> (unsmob_music_output (sys)))
277     {
278       vector<Grob*> cols = ps->get_columns ();
279       if (cols.size ())
280         {
281           Paper_column *col = dynamic_cast<Paper_column*> (cols[0]);
282           col->set_property ("labels", scm_cons (label, col->get_property ("labels")));
283           Paper_column *col_right = dynamic_cast<Paper_column*> (col->find_prebroken_piece (RIGHT));
284           col_right->set_property ("labels", scm_cons (label, col_right->get_property ("labels")));
285         }
286     }
287   else if (Prob *pb = unsmob_prob (sys))
288     pb->set_property ("labels", scm_cons (label, pb->get_property ("labels")));
289 }
290
291 SCM
292 Paper_book::get_score_title (SCM header)
293 {
294   Stencil title = score_title (header);
295   if (title.is_empty ())
296     title = score_title (header_);
297   if (!title.is_empty ())
298     {
299       /*
300         TODO: this should come from the \layout {} block, which should
301         override settings from \paper {}
302       */
303       SCM props = paper_->lookup_variable (ly_symbol2scm ("score-title-properties"));
304       Prob *ps = make_paper_system (props);
305       paper_system_set_stencil (ps, title);
306
307       return ps->self_scm ();
308     }
309
310   return SCM_BOOL_F;
311 }
312
313
314 SCM
315 Paper_book::get_system_specs ()
316 {
317   SCM system_specs = SCM_EOL;
318   
319   Stencil title = book_title ();
320   if (!title.is_empty ())
321     {
322       SCM props = paper_->lookup_variable (ly_symbol2scm ("book-title-properties"));
323       Prob *ps = make_paper_system (props);
324       paper_system_set_stencil (ps, title);
325
326       system_specs = scm_cons (ps->self_scm (), system_specs);
327       ps->unprotect ();
328     }
329
330   SCM page_properties
331     = scm_call_1 (ly_lily_module_constant ("layout-extract-page-properties"),
332                   paper_->self_scm ());
333
334   SCM header = SCM_EOL;
335   for (SCM s = scm_reverse (scores_); scm_is_pair (s); s = scm_cdr (s))
336     {
337       if (ly_is_module (scm_car (s)))
338         {
339           header = scm_car (s);
340           if (header_0_ == SCM_EOL)
341             header_0_ = header;
342         }
343       else if (Page_marker *page_marker = unsmob_page_marker (scm_car (s)))
344         {
345           /* page markers are used to set page breaking/turning permission,
346              or to place bookmarking labels */ 
347           if (scm_is_symbol (page_marker->permission_symbol ()))
348             {
349               /* set previous element page break or turn permission */
350               if (scm_is_pair (system_specs))
351                 set_page_permission (scm_car (system_specs),
352                                      page_marker->permission_symbol (),
353                                      page_marker->permission_value ());
354             }
355           if (scm_is_symbol (page_marker->label ()))
356             {
357               /* set previous element label */
358               if (scm_is_pair (system_specs))
359                 set_label (scm_car (system_specs), page_marker->label ());
360             }
361         }
362       else if (Music_output *mop = unsmob_music_output (scm_car (s)))
363         {
364           if (Paper_score *pscore = dynamic_cast<Paper_score *> (mop))
365             {
366               SCM title = get_score_title (header);
367
368               if (scm_is_pair (system_specs))
369                 set_system_penalty (scm_car (system_specs), header);
370
371               if (unsmob_prob (title))
372                 {
373                   system_specs = scm_cons (title, system_specs);
374                   unsmob_prob (title)->unprotect ();
375                 }
376
377               header = SCM_EOL;
378               system_specs = scm_cons (pscore->self_scm (), system_specs);
379             }
380           else
381             {
382               /*
383                 Ignore MIDI
384               */
385             }
386         }
387       else if (Text_interface::is_markup (scm_car (s)))
388         {
389           SCM t = Text_interface::interpret_markup (paper_->self_scm (),
390                                                     page_properties,
391                                                     scm_car (s));
392           
393           // TODO: init props
394           Prob *ps = make_paper_system (SCM_EOL);
395           ps->set_property ("page-break-permission", ly_symbol2scm ("allow"));
396           ps->set_property ("page-turn-permission", ly_symbol2scm ("allow"));
397           
398           paper_system_set_stencil (ps, *unsmob_stencil (t));
399           ps->set_property ("is-title", SCM_BOOL_T); 
400           system_specs = scm_cons (ps->self_scm (), system_specs);
401           ps->unprotect ();
402           
403           // FIXME: figure out penalty.
404           //set_system_penalty (ps, scores_[i].header_);
405         }
406       else
407         assert (0);
408     }
409
410   system_specs = scm_reverse_x (system_specs, SCM_EOL);
411   return system_specs;
412 }
413
414 SCM
415 Paper_book::systems ()
416 {
417   if (systems_ != SCM_BOOL_F)
418     return systems_;
419
420   systems_ = SCM_EOL;
421   SCM specs = get_system_specs ();
422   for (SCM s = specs; scm_is_pair (s); s = scm_cdr (s))
423     {
424       if (Paper_score *pscore = dynamic_cast<Paper_score*> (unsmob_music_output (scm_car (s))))
425         {
426           SCM system_list = scm_vector_to_list (pscore->get_paper_systems ());
427           system_list = scm_reverse (system_list);
428           systems_ = scm_append (scm_list_2 (system_list, systems_));
429         }
430       else
431         {
432           systems_ = scm_cons (scm_car (s), systems_);
433         }
434     }
435   
436   systems_ = scm_reverse (systems_);
437
438   /* backwards compatibility for the old page breaker */
439   int i = 0;
440   Prob *last = 0;
441   for (SCM s = systems_; scm_is_pair (s); s = scm_cdr (s))
442     {
443       Prob *ps = unsmob_prob (scm_car (s));
444       ps->set_property ("number", scm_from_int (++i));
445
446       if (last
447           && to_boolean (last->get_property ("is-title"))
448           && !scm_is_number (ps->get_property ("penalty")))
449         ps->set_property ("penalty", scm_from_int (10000));
450       last = ps;
451
452       if (scm_is_pair (scm_cdr (s)))
453         {
454           SCM perm = ps->get_property ("page-break-permission");
455           Prob *next = unsmob_prob (scm_cadr (s));
456           if (perm == SCM_EOL)
457             next->set_property ("penalty", scm_from_int (10001));
458           else if (perm == ly_symbol2scm ("force"))
459             next->set_property ("penalty", scm_from_int (-10001));
460         }
461     }
462
463   return systems_;
464 }
465
466 SCM
467 Paper_book::pages ()
468 {
469   if (SCM_BOOL_F != pages_)
470     return pages_;
471
472   pages_ = SCM_EOL;
473   SCM proc = paper_->c_variable ("page-breaking-wrapper");
474   pages_ = scm_apply_0 (proc, scm_list_1 (self_scm ()));
475
476   /* set systems_ from the pages */
477   if (systems_ == SCM_BOOL_F)
478     {
479       systems_ = SCM_EOL;
480       for (SCM p = pages_; scm_is_pair (p); p = scm_cdr (p))
481         {
482           Prob *page = unsmob_prob (scm_car (p));
483           SCM systems = page->get_property ("lines");
484
485           systems_ = scm_append (scm_list_2 (systems_, systems));
486         }
487     }
488
489   return pages_;
490 }
491
492 SCM
493 Paper_book::performances () const
494 {
495   return scm_reverse (performances_);
496 }