]> git.donarmstrong.com Git - lilypond.git/blob - lily/paper-book.cc
* lily/book.cc:
[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--2005 Jan Nieuwenhuizen <janneke@gnu.org>
7 */
8
9 #include "paper-book.hh"
10
11 #include "main.hh"
12 #include "output-def.hh"
13 #include "paper-score.hh"
14 #include "paper-system.hh"
15 #include "text-item.hh"
16 #include "warn.hh"
17
18 #include "ly-smobs.icc"
19
20 Paper_book::Paper_book ()
21 {
22   header_ = SCM_EOL;
23   pages_ = SCM_BOOL_F;
24   scores_ = SCM_EOL;
25   systems_ = SCM_BOOL_F;
26   
27   paper_ = 0;
28   smobify_self ();
29 }
30
31 Paper_book::~Paper_book ()
32 {
33 }
34
35 IMPLEMENT_DEFAULT_EQUAL_P (Paper_book);
36 IMPLEMENT_SMOBS (Paper_book)
37 IMPLEMENT_TYPE_P (Paper_book, "ly:paper-book?")
38
39 SCM
40 Paper_book::mark_smob (SCM smob)
41 {
42   Paper_book *b = (Paper_book*) SCM_CELL_WORD_1 (smob);
43   if (b->paper_)
44     scm_gc_mark (b->paper_->self_scm ());
45   scm_gc_mark (b->header_);
46   scm_gc_mark (b->pages_);
47   scm_gc_mark (b->scores_);
48   return b->systems_;
49 }
50
51 int
52 Paper_book::print_smob (SCM smob, SCM port, scm_print_state*)
53 {
54   Paper_book *b = (Paper_book*) SCM_CELL_WORD_1 (smob);
55      
56   scm_puts ("#<", port);
57   scm_puts (classname (b), port);
58   scm_puts (" ", port);
59   scm_puts (">", port);
60   return 1;
61 }
62
63 SCM
64 dump_fields ()
65 {
66   SCM fields = SCM_EOL;
67   for (int i = dump_header_fieldnames_global.size (); i--; )
68     fields
69       = scm_cons (ly_symbol2scm (dump_header_fieldnames_global[i].to_str0 ()),
70                   fields);
71   return fields;
72 }
73
74 void
75 Paper_book::add_score (SCM s)
76 {
77   scores_ = scm_cons (s, scores_);
78 }
79
80 void
81 Paper_book::output (String outname)
82 {
83   if (scores_ == SCM_EOL)
84     return;
85
86   /* Generate all stencils to trigger font loads.  */
87   pages ();
88   
89   SCM scopes = SCM_EOL;
90   if (ly_c_module_p (header_))
91     scopes = scm_cons (header_, scopes);
92   
93   String mod_nm = "scm framework-" + output_backend_global;
94
95   SCM mod = scm_c_resolve_module (mod_nm.to_str0 ());
96   if (make_pages)
97     {
98       SCM func = scm_c_module_lookup (mod, "output-framework");
99
100       func = scm_variable_ref (func);
101       scm_apply_0 (func, scm_list_n (scm_makfrom0str (outname.to_str0 ()),
102                                      self_scm (),
103                                      scopes,
104                                      dump_fields (),
105                                      SCM_UNDEFINED));
106     }
107       
108   if (make_preview)
109     {
110       SCM func = scm_c_module_lookup (mod, "output-preview-framework");
111       func = scm_variable_ref (func);
112       scm_apply_0 (func, scm_list_n (scm_makfrom0str (outname.to_str0 ()),
113                                      self_scm (),
114                                      scopes,
115                                      dump_fields (),
116                                      SCM_UNDEFINED));
117     }
118   progress_indication ("\n");
119 }
120
121 void
122 Paper_book::classic_output (String outname)
123 {
124   /* Generate all stencils to trigger font loads.  */
125   systems ();
126
127   // ugh code dup
128   SCM scopes = SCM_EOL;
129   if (ly_c_module_p (header_))
130     scopes = scm_cons (header_, scopes);
131
132 #if 0
133   if (ly_c_module_p (scores_[0].header_))
134     scopes = scm_cons (scores_[0].header_, scopes);
135 #endif  
136   //end ugh
137
138   String format = output_backend_global;
139   String mod_nm = "scm framework-" + format;
140       
141   SCM mod = scm_c_resolve_module (mod_nm.to_str0 ());
142   SCM func = scm_c_module_lookup (mod, "output-classic-framework");
143  
144
145   func = scm_variable_ref (func);
146   scm_apply_0 (func, scm_list_n (scm_makfrom0str (outname.to_str0 ()),
147                                  self_scm (),
148                                  scopes,
149                                  dump_fields (),
150                                  SCM_UNDEFINED));
151
152   progress_indication ("\n");
153 }
154
155
156 /* TODO: resurrect more complex user-tweaks for titling?  */
157 Stencil
158 Paper_book::book_title ()
159 {
160   SCM title_func = paper_->lookup_variable (ly_symbol2scm ("book-title"));
161   Stencil title;
162
163   SCM scopes = SCM_EOL;
164   if (ly_c_module_p (header_))
165     scopes = scm_cons (header_, scopes);
166
167  
168   SCM tit = SCM_EOL;
169   if (ly_c_procedure_p (title_func))
170     tit = scm_call_2 (title_func,
171                      paper_->self_scm (),
172                      scopes);
173
174   if (unsmob_stencil (tit))
175     title = *unsmob_stencil (tit);
176
177   if (!title.is_empty ())
178     title.align_to (Y_AXIS, UP);
179   
180   return title;
181 }
182
183 Stencil
184 Paper_book::score_title (SCM header)
185 {
186   SCM title_func = paper_->lookup_variable (ly_symbol2scm ("score-title"));
187
188   Stencil title;
189
190   SCM scopes = SCM_EOL;
191   if (ly_c_module_p (header_))
192     scopes = scm_cons (header_, scopes);
193
194   if (ly_c_module_p (header))
195     scopes = scm_cons (header, scopes);
196
197   SCM tit = SCM_EOL;
198   if (ly_c_procedure_p (title_func))
199     tit = scm_call_2 (title_func,
200                      paper_->self_scm (),
201                      scopes);
202
203   if (unsmob_stencil (tit))
204     title = *unsmob_stencil (tit);
205
206   if (!title.is_empty ())
207     title.align_to (Y_AXIS, UP);
208   
209   return title;
210 }
211
212 void
213 set_system_penalty (Paper_system * ps, SCM header)
214 {
215   if (ly_c_module_p (header))
216     {
217       SCM force = ly_module_lookup (header, ly_symbol2scm ("breakbefore"));
218       if (SCM_VARIABLEP(force)
219           && scm_is_bool (SCM_VARIABLE_REF(force)))
220         {
221           ps->break_before_penalty_ = to_boolean (SCM_VARIABLE_REF(force))
222             ? -10000
223             : 10000;
224         }
225     }
226 }
227             
228 SCM
229 Paper_book::systems ()
230 {
231   if (systems_ != SCM_BOOL_F)
232     return systems_;
233
234   systems_ = SCM_EOL;
235   Stencil title = book_title ();
236
237   if (!title.is_empty ())
238     {
239       Paper_system *ps = new Paper_system (title, true);
240       set_system_penalty (ps, header_);
241       
242       systems_ = scm_cons (ps->self_scm (), systems_);
243       scm_gc_unprotect_object (ps->self_scm ());
244     }
245
246   SCM page_properties
247     = scm_call_1 (ly_lily_module_constant ("layout-extract-page-properties"),
248                   paper_->self_scm ());
249
250   SCM header = SCM_EOL;
251   for (SCM s = scm_reverse (scores_); s != SCM_EOL; s = scm_cdr (s))
252     {
253       if (ly_c_module_p (scm_car (s)))
254         header = scm_car (s);
255       else if (scm_is_vector (scm_car (s)))
256         {
257           Stencil title = score_title (header);
258           if (!title.is_empty ())
259             {
260               Paper_system *ps = new Paper_system (title, true);
261               systems_ = scm_cons (ps->self_scm (), systems_);
262               scm_gc_unprotect_object (ps->self_scm ());
263               set_system_penalty (ps, header);
264             }
265           header = SCM_EOL;
266       
267           SCM system_list = scm_vector_to_list (scm_car (s));
268           system_list = scm_reverse (system_list);
269           systems_ = scm_append (scm_list_2 (system_list, systems_));
270         }
271       else if (Text_interface::markup_p (scm_car (s)))
272         {
273           SCM t = Text_interface::interpret_markup (paper_->self_scm (),
274                                                     page_properties,
275                                                     scm_car (s));
276           // FIXME: title=true?
277           Paper_system *ps = new Paper_system (*unsmob_stencil (t), true);
278           systems_ = scm_cons (ps->self_scm (), systems_);
279           scm_gc_unprotect_object (ps->self_scm ());
280           // FIXME: figure out penalty.
281           //set_system_penalty (ps, scores_[i].header_);
282         }
283       else
284         assert (0);
285     }
286   
287   systems_ = scm_reverse (systems_);
288   
289   int i = 0;
290   Paper_system *last = 0;
291   for (SCM s = systems_; s != SCM_EOL; s = scm_cdr (s))
292     {
293       Paper_system *ps = unsmob_paper_system (scm_car (s));
294       ps->number_ = ++i;
295
296       if (last
297           && last->is_title ()
298           && !ps->break_before_penalty_)
299         ps->break_before_penalty_ = 10000;
300       last = ps;
301     }
302   
303   return systems_;
304 }
305
306 SCM
307 Paper_book::pages ()
308 {
309   if (SCM_BOOL_F != pages_)
310     return pages_;
311
312   pages_ = SCM_EOL;
313   SCM proc = paper_->c_variable ("page-breaking");
314   pages_ = scm_apply_0 (proc, scm_list_2 (systems (), self_scm ()));
315   return pages_;
316 }