]> git.donarmstrong.com Git - lilypond.git/blob - lily/paper-book.cc
* lily/paper-book.cc (output): call output-preview-framework
[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   if (make_pdf)
121     {
122       SCM func = scm_c_module_lookup (module, "convert-to-pdf");
123       if (scm_variable_p (func) == SCM_BOOL_T)
124         {
125           func = scm_variable_ref (func);
126           if (ly_c_procedure_p (func))
127             scm_call_2 (func, self_scm(), file_name);
128         }
129     }
130
131   if (make_png)
132     {
133       SCM func = scm_c_module_lookup (module, "convert-to-png");
134       if (scm_variable_p (func) ==  SCM_BOOL_T)
135         {
136           func = scm_variable_ref (func);
137           if (ly_c_procedure_p (func))
138             scm_call_2 (func, self_scm(), file_name);
139         }
140     }
141 }
142 void
143 Paper_book::output (String outname)
144 {
145   if (!score_lines_.size ())
146     return;
147
148   /* Generate all stencils to trigger font loads.  */
149   pages ();
150   
151   SCM formats = ly_output_formats ();
152   for (SCM s = formats; ly_c_pair_p (s); s = ly_cdr (s)) 
153     {
154       String format = ly_scm2string (ly_car (s));
155       String file_name = outname + "." + format;
156       Paper_outputter *out = get_paper_outputter (file_name, format);
157   
158       SCM scopes = SCM_EOL;
159       if (ly_c_module_p (header_))
160         scopes = scm_cons (header_, scopes);
161   
162       String mod_nm = "scm framework-" + format;
163       
164       SCM mod = scm_c_resolve_module (mod_nm.to_str0 ());
165       SCM func = scm_c_module_lookup (mod, "output-framework");
166
167       func = scm_variable_ref (func);
168       scm_apply_0 (func, scm_list_n (out->self_scm (),
169                                      self_scm (),
170                                      scopes,
171                                      dump_fields (),
172                                      scm_makfrom0str (outname.to_str0 ()),
173                                      SCM_UNDEFINED));
174       out->close ();
175       scm_gc_unprotect_object (out->self_scm ());
176
177       post_processing (mod, scm_makfrom0str (file_name.to_str0 ()));
178       
179       if (make_preview)
180         {
181           String file_name = outname + ".preview." + format;
182           Paper_outputter *out = get_paper_outputter (file_name, format);
183   
184           SCM func = scm_c_module_lookup (mod, "output-preview-framework");
185           func = scm_variable_ref (func);
186           scm_apply_0 (func, scm_list_n (out->self_scm (),
187                                          self_scm (),
188                                          scopes,
189                                          dump_fields (),
190                                          scm_makfrom0str (outname.to_str0 ()),
191                                          SCM_UNDEFINED));
192
193           out->close ();
194           scm_gc_unprotect_object (out->self_scm ());
195
196           post_processing (mod, scm_makfrom0str (file_name.to_str0 ()));
197         }
198       
199       progress_indication ("\n");
200     }
201 }
202
203 void
204 Paper_book::classic_output (String outname)
205 {
206   /* Generate all stencils to trigger font loads.  */
207   lines ();
208
209   // ugh code dup
210   SCM scopes = SCM_EOL;
211   if (ly_c_module_p (header_))
212     scopes = scm_cons (header_, scopes);
213
214   if (ly_c_module_p (score_lines_[0].header_))
215     scopes = scm_cons (score_lines_[0].header_, scopes);
216   //end ugh
217
218   Array<String> output_formats = split_string (output_format_global, ',');
219
220   for (int i = 0; i < output_formats.size (); i++)
221     {
222       String format = output_formats[i];
223       String mod_nm = "scm framework-" + format;
224       
225       SCM mod = scm_c_resolve_module (mod_nm.to_str0 ());
226       SCM func = scm_c_module_lookup (mod, "output-classic-framework");
227
228       func = scm_variable_ref (func);
229       
230       Paper_outputter *out = get_paper_outputter (outname + "." + format,
231                                                   format);
232
233       scm_apply_0 (func, scm_list_n (out->self_scm (), self_scm (), scopes,
234                                      dump_fields (),
235                                      scm_makfrom0str (outname.to_str0 ()),
236                                      SCM_UNDEFINED));
237
238       scm_gc_unprotect_object (out->self_scm ());
239       progress_indication ("\n");
240     }
241 }
242
243 LY_DEFINE (ly_paper_book_pages, "ly:paper-book-pages",
244           1, 0, 0, (SCM pb),
245           "Return pages in book PB.")
246 {
247   return unsmob_paper_book(pb)->pages ();
248 }
249
250 LY_DEFINE (ly_paper_book_scopes, "ly:paper-book-scopes",
251           1, 0, 0, (SCM book),
252           "Return pages in paper book @var{book}.")
253 {
254   Paper_book *pb = unsmob_paper_book(book);
255   SCM_ASSERT_TYPE(pb, book, SCM_ARG1, __FUNCTION__, "Paper_book");
256   
257   SCM scopes = SCM_EOL;
258   if (ly_c_module_p (pb->header_))
259     scopes = scm_cons (pb->header_, scopes);
260   
261   return scopes;
262 }
263
264 LY_DEFINE (ly_paper_book_lines, "ly:paper-book-lines",
265            1, 0, 0, (SCM pb),
266            "Return lines in book PB.")
267 {
268   return unsmob_paper_book (pb)->lines ();
269 }
270
271 LY_DEFINE (ly_paper_book_book_paper, "ly:paper-book-book-paper",
272           1, 0, 0, (SCM pb),
273           "Return pages in book PB.")
274 {
275   return unsmob_paper_book (pb)->bookpaper_->self_scm ();
276 }
277
278 /* TODO: resurrect more complex user-tweaks for titling?  */
279 Stencil
280 Paper_book::book_title ()
281 {
282   SCM title_func = bookpaper_->lookup_variable (ly_symbol2scm ("book-title"));
283   Stencil title;
284
285   SCM scopes = SCM_EOL;
286   if (ly_c_module_p (header_))
287     scopes = scm_cons (header_, scopes);
288
289  
290   SCM tit = SCM_EOL;
291   if (ly_c_procedure_p (title_func))
292     tit = scm_call_2 (title_func,
293                      bookpaper_->self_scm (),
294                      scopes);
295
296   if (unsmob_stencil (tit))
297     title = *unsmob_stencil (tit);
298
299   if (!title.is_empty ())
300     title.align_to (Y_AXIS, UP);
301   
302   return title;
303 }
304
305 Stencil
306 Paper_book::score_title (int i)
307 {
308   SCM title_func = bookpaper_->lookup_variable (ly_symbol2scm ("score-title"));
309
310   Stencil title;
311
312   // ugh code dup
313   SCM scopes = SCM_EOL;
314   if (ly_c_module_p (header_))
315     scopes = scm_cons (header_, scopes);
316
317   if (ly_c_module_p (score_lines_[i].header_))
318     scopes = scm_cons (score_lines_[i].header_, scopes);
319   //end ugh
320
321   SCM tit = SCM_EOL;
322   if (ly_c_procedure_p (title_func))
323     tit = scm_call_2 (title_func,
324                      bookpaper_->self_scm (),
325                      scopes);
326
327   if (unsmob_stencil (tit))
328     title = *unsmob_stencil (tit);
329
330   if (!title.is_empty ())
331     title.align_to (Y_AXIS, UP);
332   
333   return title;
334 }
335   
336 SCM
337 Paper_book::lines ()
338 {
339   if (lines_ != SCM_BOOL_F)
340     return lines_;
341
342   lines_ = SCM_EOL;
343   Stencil title = book_title ();
344
345   if (!title.is_empty ())
346     {
347       Paper_system *pl = new Paper_system (title, true);
348       lines_ = scm_cons (pl->self_scm (), lines_);
349       scm_gc_unprotect_object (pl->self_scm ());
350     }
351   
352   int score_count = score_lines_.size ();
353   for (int i = 0; i < score_count; i++)
354     {
355       Stencil title = score_title (i);      
356       if (!title.is_empty ())
357         {
358           Paper_system *pl = new Paper_system (title, true);
359           lines_ = scm_cons (pl->self_scm (), lines_);
360           scm_gc_unprotect_object (pl->self_scm ());
361         }
362       
363       if (scm_vector_p (score_lines_[i].lines_) == SCM_BOOL_T)
364         {
365           // guh.         
366           SCM line_list = scm_vector_to_list (score_lines_[i].lines_);
367
368           line_list = scm_reverse (line_list);
369           lines_ = scm_append (scm_list_2 (line_list, lines_));
370         }
371     }
372   
373   lines_ = scm_reverse (lines_);
374   
375   int i = 0;
376   Paper_system *last = 0;
377   for (SCM s = lines_; s != SCM_EOL; s = ly_cdr (s))
378     {
379       Paper_system * p = unsmob_paper_line (ly_car (s));
380       p->number_ = ++i;
381
382       if (last && last->is_title ())
383         // ugh, hardcoded.      
384         p->penalty_ = 10000;
385       last = p;
386     }
387   
388   return lines_;
389 }
390
391 SCM
392 Paper_book::pages ()
393 {
394   if (SCM_BOOL_F != pages_)
395     return pages_;
396
397   pages_ = SCM_EOL;
398   Output_def *paper = bookpaper_;
399   SCM proc = paper->c_variable ("page-breaking");
400   pages_ = scm_apply_0 (proc, scm_list_2 (lines (), self_scm ()));
401   return pages_;
402 }
403
404
405 /****************************************************************/
406
407 Score_lines::Score_lines ()
408 {
409   lines_ = SCM_EOL;
410   header_ = SCM_EOL;
411 }
412
413 void
414 Score_lines::gc_mark ()
415 {
416   scm_gc_mark (lines_);
417   scm_gc_mark (header_);
418 }
419