]> git.donarmstrong.com Git - lilypond.git/blob - lily/paper-book.cc
a94be54856a37675f53d1af68fdfdfb25438d49c
[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 Jan Nieuwenhuizen <janneke@gnu.org>
7 */
8
9 #include "ly-module.hh"
10 #include "main.hh"
11 #include "output-def.hh"
12 #include "paper-book.hh"
13 #include "paper-outputter.hh"
14 #include "paper-score.hh"
15 #include "paper-system.hh"
16 #include "stencil.hh"
17 #include "warn.hh"
18
19 #include "ly-smobs.icc"
20
21 Paper_book::Paper_book ()
22 {
23   pages_ = SCM_BOOL_F;
24   lines_ = SCM_BOOL_F;
25   header_ = SCM_EOL;
26   
27   bookpaper_ = 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   for (int i = 0; i < b->score_lines_.size (); i++)
44     b->score_lines_[i].gc_mark ();
45
46   if (b->bookpaper_)
47     scm_gc_mark (b->bookpaper_->self_scm ());
48   scm_gc_mark (b->header_);
49   scm_gc_mark (b->pages_);
50   return b->lines_;
51 }
52
53 int
54 Paper_book::print_smob (SCM smob, SCM port, scm_print_state*)
55 {
56   Paper_book *b = (Paper_book*) ly_cdr (smob);
57      
58   scm_puts ("#<", port);
59   scm_puts (classname (b), port);
60   scm_puts (" ", port);
61   //scm_puts (b->, port);
62   scm_puts (">", port);
63   return 1;
64 }
65
66 Array<String>
67 split_string (String s, char c)
68 {
69   Array<String> rv; 
70   while (s.length ())
71     {
72       int i = s.index (c);
73       
74       if (i == 0)
75         {
76           s = s.nomid_string (0, 1);
77           continue;
78         }
79       
80       if (i < 0)
81         i = s.length () ;
82
83       rv.push (s.cut_string (0, i));
84       s = s.nomid_string (0, i);
85     }
86
87   return rv;
88 }
89
90 SCM
91 dump_fields ()
92 {
93   SCM fields = SCM_EOL;
94   for (int i = dump_header_fieldnames_global.size (); i--; )
95     fields
96       = scm_cons (ly_symbol2scm (dump_header_fieldnames_global[i].to_str0 ()),
97                   fields);
98   return fields;
99 }
100
101 LY_DEFINE (ly_output_formats, "ly:output-formats",
102            0, 0, 0, (),
103            "Formats passed to --format as a list of strings, "
104            "used for the output.")
105 {
106   Array<String> output_formats = split_string (output_format_global, ',');
107
108   SCM lst = SCM_EOL;
109   int output_formats_count = output_formats.size ();
110   for (int i = 0; i < output_formats_count; i ++)
111     lst = scm_cons (scm_makfrom0str (output_formats[i].to_str0 ()), lst);
112   
113   return lst; 
114 }
115
116 void
117 Paper_book::post_processing (SCM module,
118                              SCM file_name)
119 {
120   struct
121   {
122     bool do_it_;
123     char const *func_name_;
124   } settings[] = {
125     {make_dvi, "convert-to-dvi"},
126     {make_ps, "convert-to-ps"},
127     {make_pdf, "convert-to-pdf"},
128     {make_png, "convert-to-png"},
129     {0,0}
130   };
131
132   for (int i= 0; settings[i].func_name_; i++)
133     {
134       if (settings[i].do_it_)
135         {
136           SCM func = scm_c_module_lookup (module, settings[i].func_name_);
137           if (scm_variable_p (func) == SCM_BOOL_T)
138             {
139               func = scm_variable_ref (func);
140               if (ly_c_procedure_p (func))
141                 scm_call_2 (func, self_scm(), file_name);
142             }
143         }
144     }
145 }
146
147 void
148 Paper_book::output (String outname)
149 {
150   if (!score_lines_.size ())
151     return;
152
153   /* Generate all stencils to trigger font loads.  */
154   pages ();
155   
156   SCM formats = ly_output_formats ();
157   for (SCM s = formats; ly_c_pair_p (s); s = ly_cdr (s)) 
158     {
159       String format = ly_scm2string (ly_car (s));
160       String file_name = outname + "." + format;
161       Paper_outputter *out = get_paper_outputter (file_name, format);
162   
163       SCM scopes = SCM_EOL;
164       if (ly_c_module_p (header_))
165         scopes = scm_cons (header_, scopes);
166   
167       String mod_nm = "scm framework-" + format;
168       
169       SCM mod = scm_c_resolve_module (mod_nm.to_str0 ());
170       SCM func = scm_c_module_lookup (mod, "output-framework");
171
172       func = scm_variable_ref (func);
173       scm_apply_0 (func, scm_list_n (out->self_scm (),
174                                      self_scm (),
175                                      scopes,
176                                      dump_fields (),
177                                      scm_makfrom0str (outname.to_str0 ()),
178                                      SCM_UNDEFINED));
179       out->close ();
180       scm_gc_unprotect_object (out->self_scm ());
181
182       post_processing (mod, scm_makfrom0str (file_name.to_str0 ()));
183       
184       if (make_preview)
185         {
186           String file_name = outname + ".preview." + format;
187           Paper_outputter *out = get_paper_outputter (file_name, format);
188   
189           SCM func = scm_c_module_lookup (mod, "output-preview-framework");
190           func = scm_variable_ref (func);
191           scm_apply_0 (func, scm_list_n (out->self_scm (),
192                                          self_scm (),
193                                          scopes,
194                                          dump_fields (),
195                                          scm_makfrom0str (outname.to_str0 ()),
196                                          SCM_UNDEFINED));
197
198           out->close ();
199           scm_gc_unprotect_object (out->self_scm ());
200
201           post_processing (mod, scm_makfrom0str (file_name.to_str0 ()));
202         }
203       
204       progress_indication ("\n");
205     }
206 }
207
208 void
209 Paper_book::classic_output (String outname)
210 {
211   /* Generate all stencils to trigger font loads.  */
212   lines ();
213
214   // ugh code dup
215   SCM scopes = SCM_EOL;
216   if (ly_c_module_p (header_))
217     scopes = scm_cons (header_, scopes);
218
219   if (ly_c_module_p (score_lines_[0].header_))
220     scopes = scm_cons (score_lines_[0].header_, scopes);
221   //end ugh
222
223   Array<String> output_formats = split_string (output_format_global, ',');
224
225   for (int i = 0; i < output_formats.size (); i++)
226     {
227       String format = output_formats[i];
228       String mod_nm = "scm framework-" + format;
229       
230       SCM mod = scm_c_resolve_module (mod_nm.to_str0 ());
231       SCM func = scm_c_module_lookup (mod, "output-classic-framework");
232
233       func = scm_variable_ref (func);
234       
235       Paper_outputter *out = get_paper_outputter (outname + "." + format,
236                                                   format);
237
238       scm_apply_0 (func, scm_list_n (out->self_scm (), self_scm (), scopes,
239                                      dump_fields (),
240                                      scm_makfrom0str (outname.to_str0 ()),
241                                      SCM_UNDEFINED));
242
243       scm_gc_unprotect_object (out->self_scm ());
244       progress_indication ("\n");
245     }
246 }
247
248 LY_DEFINE (ly_paper_book_pages, "ly:paper-book-pages",
249           1, 0, 0, (SCM pb),
250           "Return pages in book PB.")
251 {
252   return unsmob_paper_book(pb)->pages ();
253 }
254
255 LY_DEFINE (ly_paper_book_scopes, "ly:paper-book-scopes",
256           1, 0, 0, (SCM book),
257           "Return pages in paper book @var{book}.")
258 {
259   Paper_book *pb = unsmob_paper_book(book);
260   SCM_ASSERT_TYPE(pb, book, SCM_ARG1, __FUNCTION__, "Paper_book");
261   
262   SCM scopes = SCM_EOL;
263   if (ly_c_module_p (pb->header_))
264     scopes = scm_cons (pb->header_, scopes);
265   
266   return scopes;
267 }
268
269 LY_DEFINE (ly_paper_book_lines, "ly:paper-book-lines",
270            1, 0, 0, (SCM pb),
271            "Return lines in book PB.")
272 {
273   return unsmob_paper_book (pb)->lines ();
274 }
275
276 LY_DEFINE (ly_paper_book_book_paper, "ly:paper-book-book-paper",
277           1, 0, 0, (SCM pb),
278           "Return pages in book PB.")
279 {
280   return unsmob_paper_book (pb)->bookpaper_->self_scm ();
281 }
282
283 /* TODO: resurrect more complex user-tweaks for titling?  */
284 Stencil
285 Paper_book::book_title ()
286 {
287   SCM title_func = bookpaper_->lookup_variable (ly_symbol2scm ("book-title"));
288   Stencil title;
289
290   SCM scopes = SCM_EOL;
291   if (ly_c_module_p (header_))
292     scopes = scm_cons (header_, scopes);
293
294  
295   SCM tit = SCM_EOL;
296   if (ly_c_procedure_p (title_func))
297     tit = scm_call_2 (title_func,
298                      bookpaper_->self_scm (),
299                      scopes);
300
301   if (unsmob_stencil (tit))
302     title = *unsmob_stencil (tit);
303
304   if (!title.is_empty ())
305     title.align_to (Y_AXIS, UP);
306   
307   return title;
308 }
309
310 Stencil
311 Paper_book::score_title (int i)
312 {
313   SCM title_func = bookpaper_->lookup_variable (ly_symbol2scm ("score-title"));
314
315   Stencil title;
316
317   // ugh code dup
318   SCM scopes = SCM_EOL;
319   if (ly_c_module_p (header_))
320     scopes = scm_cons (header_, scopes);
321
322   if (ly_c_module_p (score_lines_[i].header_))
323     scopes = scm_cons (score_lines_[i].header_, scopes);
324   //end ugh
325
326   SCM tit = SCM_EOL;
327   if (ly_c_procedure_p (title_func))
328     tit = scm_call_2 (title_func,
329                      bookpaper_->self_scm (),
330                      scopes);
331
332   if (unsmob_stencil (tit))
333     title = *unsmob_stencil (tit);
334
335   if (!title.is_empty ())
336     title.align_to (Y_AXIS, UP);
337   
338   return title;
339 }
340   
341 SCM
342 Paper_book::lines ()
343 {
344   if (lines_ != SCM_BOOL_F)
345     return lines_;
346
347   lines_ = SCM_EOL;
348   Stencil title = book_title ();
349
350   if (!title.is_empty ())
351     {
352       Paper_system *pl = new Paper_system (title, true);
353       lines_ = scm_cons (pl->self_scm (), lines_);
354       scm_gc_unprotect_object (pl->self_scm ());
355     }
356   
357   int score_count = score_lines_.size ();
358   for (int i = 0; i < score_count; i++)
359     {
360       Stencil title = score_title (i);      
361       if (!title.is_empty ())
362         {
363           Paper_system *pl = new Paper_system (title, true);
364           lines_ = scm_cons (pl->self_scm (), lines_);
365           scm_gc_unprotect_object (pl->self_scm ());
366         }
367       
368       if (scm_vector_p (score_lines_[i].lines_) == SCM_BOOL_T)
369         {
370           // guh.         
371           SCM line_list = scm_vector_to_list (score_lines_[i].lines_);
372
373           line_list = scm_reverse (line_list);
374           lines_ = scm_append (scm_list_2 (line_list, lines_));
375         }
376     }
377   
378   lines_ = scm_reverse (lines_);
379   
380   int i = 0;
381   Paper_system *last = 0;
382   for (SCM s = lines_; s != SCM_EOL; s = ly_cdr (s))
383     {
384       Paper_system * p = unsmob_paper_line (ly_car (s));
385       p->number_ = ++i;
386
387       if (last && last->is_title ())
388         // ugh, hardcoded.      
389         p->penalty_ = 10000;
390       last = p;
391     }
392   
393   return lines_;
394 }
395
396 SCM
397 Paper_book::pages ()
398 {
399   if (SCM_BOOL_F != pages_)
400     return pages_;
401
402   pages_ = SCM_EOL;
403   Output_def *paper = bookpaper_;
404   SCM proc = paper->c_variable ("page-breaking");
405   pages_ = scm_apply_0 (proc, scm_list_2 (lines (), self_scm ()));
406   return pages_;
407 }
408
409
410 /****************************************************************/
411
412 Score_lines::Score_lines ()
413 {
414   lines_ = SCM_EOL;
415   header_ = SCM_EOL;
416 }
417
418 void
419 Score_lines::gc_mark ()
420 {
421   scm_gc_mark (lines_);
422   scm_gc_mark (header_);
423 }
424