]> git.donarmstrong.com Git - lilypond.git/blob - lily/paper-book.cc
* lily/ly-module.cc (LY_DEFINE): bugfix.
[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 "page.hh"
12 #include "paper-book.hh"
13 #include "output-def.hh"
14 #include "paper-outputter.hh"
15 #include "paper-line.hh"
16 #include "paper-score.hh"
17 #include "stencil.hh"
18 #include "warn.hh"
19
20
21 // JUNKME
22 SCM
23 stencil2line (Stencil stil, bool is_title = false)
24 {
25   static SCM z;
26   if (!z)
27     z = scm_permanent_object (ly_offset2scm (Offset (0, 0)));
28   Offset dim = Offset (stil.extent (X_AXIS).length (),
29                        stil.extent (Y_AXIS).length ());
30   Paper_line *pl = new Paper_line (dim, scm_cons (stil.smobbed_copy (),
31                                                   SCM_EOL),
32                                    -10001 * is_title, is_title);
33
34   return scm_gc_unprotect_object (pl->self_scm ());
35 }
36
37
38 Paper_book::Paper_book ()
39 {
40   pages_ = SCM_EOL;
41   lines_ = SCM_EOL;
42   copyright_ = SCM_EOL;
43   tagline_ = SCM_EOL;
44   header_ = SCM_EOL;
45   
46   bookpaper_ = 0;
47   smobify_self ();
48 }
49
50 Paper_book::~Paper_book ()
51 {
52 }
53
54 #include "ly-smobs.icc"
55
56 IMPLEMENT_DEFAULT_EQUAL_P (Paper_book);
57 IMPLEMENT_SMOBS (Paper_book)
58 IMPLEMENT_TYPE_P (Paper_book, "ly:paper-book?")
59
60 SCM
61 Paper_book::mark_smob (SCM smob)
62 {
63   Paper_book *b = (Paper_book*) SCM_CELL_WORD_1 (smob);
64   for (int i = 0; i < b->score_lines_.size (); i++)
65     b->score_lines_[i].gc_mark ();
66
67   scm_gc_mark (b->copyright_);
68   if (b->bookpaper_)
69     scm_gc_mark (b->bookpaper_->self_scm ());
70   scm_gc_mark (b->header_);
71   scm_gc_mark (b->pages_);
72   scm_gc_mark (b->lines_);
73   return b->tagline_;
74 }
75
76 int
77 Paper_book::print_smob (SCM smob, SCM port, scm_print_state*)
78 {
79   Paper_book *b = (Paper_book*) ly_cdr (smob);
80      
81   scm_puts ("#<", port);
82   scm_puts (classname (b), port);
83   scm_puts (" ", port);
84   //scm_puts (b->, port);
85   scm_puts (">", port);
86   return 1;
87 }
88
89 Array<String>
90 split_string (String s, char c)
91 {
92   Array<String> rv; 
93   while (s.length ())
94     {
95       int i = s.index (c);
96       
97       if (i == 0)
98         {
99           s = s.nomid_string (0, 1);
100           continue;
101         }
102       
103       if (i < 0)
104         i = s.length () ;
105
106       rv.push (s.cut_string (0, i));
107       s = s.nomid_string (0, i);
108     }
109
110   return rv;
111 }
112
113 SCM
114 dump_fields ()
115 {
116   SCM fields = SCM_EOL;
117   for (int i = dump_header_fieldnames_global.size (); i--; )
118     fields
119       = scm_cons (ly_symbol2scm (dump_header_fieldnames_global[i].to_str0 ()),
120                   fields);
121   return fields;
122 }
123
124 /*
125   TODO: there is too much code dup, and the interface is not
126   clear. FIXME.
127  */
128 void
129 Paper_book::output (String outname)
130 {
131   if (!score_lines_.size ())
132     // FIXME: no end-output?
133     return;
134     
135   /* Generate all stencils to trigger font loads.  */
136   pages ();
137
138   Array<String> output_formats = split_string (output_format_global, ',');
139
140   for (int i = 0; i < output_formats.size (); i++)
141     {
142       String format = output_formats[i];
143       Paper_outputter *out = get_paper_outputter (outname + "." + output_formats[i], format);
144       
145       
146
147   
148       SCM scopes = SCM_EOL;
149       if (ly_c_module_p (header_))
150         scopes = scm_cons (header_, scopes);
151   
152       String func_nm = output_format_global;
153       func_nm = "output-framework-" + func_nm;
154         
155       SCM func = ly_scheme_function (func_nm.to_str0 ());
156       scm_apply_0 (func, scm_list_n (out->self_scm (),
157                                      self_scm (),
158                                      scopes,
159                                      dump_fields (),
160                                      scm_makfrom0str (outname.to_str0 ()),
161                                      SCM_UNDEFINED
162                                      )) ;
163
164       scm_gc_unprotect_object (out->self_scm ());
165     }
166 }
167
168
169 void
170 Paper_book::classic_output (String outname)
171 {
172   String format = "tex";
173   Paper_outputter *out = get_paper_outputter (outname + "." + format, format);
174
175   /* Generate all stencils to trigger font loads.  */
176   lines ();
177
178
179   // ugh code dup
180   SCM scopes = SCM_EOL;
181   if (ly_c_module_p (header_))
182     scopes = scm_cons (header_, scopes);
183
184   if (ly_c_module_p (score_lines_[0].header_))
185     scopes = scm_cons (score_lines_[0].header_, scopes);
186   //end ugh
187   
188   String func_nm = output_format_global;
189   func_nm = "output-classic-framework-" + func_nm;
190       
191   SCM func = ly_scheme_function (func_nm.to_str0 ());
192   scm_apply_0 (func, scm_list_n (out->self_scm (),
193                                  self_scm (),
194                                  scopes,
195                                  dump_fields (),
196                                  scm_makfrom0str (outname.to_str0 ()),
197                                  SCM_UNDEFINED
198                                  )) ;
199
200   progress_indication ("\n");
201 }
202
203
204
205
206 LY_DEFINE(ly_paper_book_pages, "ly:paper-book-pages",
207           1,0,0,
208           (SCM pb),
209           "Return pages in book PB.")
210 {
211   return unsmob_paper_book(pb)->pages ();
212 }
213
214
215 LY_DEFINE(ly_paper_book_lines, "ly:paper-book-lines",
216           1,0,0,
217           (SCM pb),
218           "Return lines in book PB.")
219 {
220   return unsmob_paper_book (pb)->lines ();
221 }
222
223
224 LY_DEFINE(ly_paper_book_book_paper, "ly:paper-book-book-paper",
225           1,0,0,
226           (SCM pb),
227           "Return pages in book PB.")
228 {
229   return unsmob_paper_book(pb)->bookpaper_->self_scm ();
230 }
231
232 /*
233
234 TODO: resurrect more complex user-tweaks for titling .
235
236 */
237 Stencil
238 Paper_book::book_title ()
239 {
240   SCM title_func = bookpaper_->lookup_variable (ly_symbol2scm ("book-title"));
241   Stencil title;
242
243   SCM scopes = SCM_EOL;
244   if (ly_c_module_p (header_))
245     scopes = scm_cons (header_, scopes);
246
247  
248   SCM tit = SCM_EOL;
249   if (ly_c_procedure_p (title_func))
250     tit = scm_call_2 (title_func,
251                      bookpaper_->self_scm (),
252                      scopes);
253
254   if (unsmob_stencil (tit))
255     title = *unsmob_stencil (tit);
256
257   if (!title.is_empty ())
258     title.align_to (Y_AXIS, UP);
259   
260   return title;
261 }
262
263   
264
265 Stencil
266 Paper_book::score_title (int i)
267 {
268   SCM title_func = bookpaper_->lookup_variable (ly_symbol2scm ("score-title"));
269
270   Stencil title;
271
272   // ugh code dup
273   SCM scopes = SCM_EOL;
274   if (ly_c_module_p (header_))
275     scopes = scm_cons (header_, scopes);
276
277   if (ly_c_module_p (score_lines_[i].header_))
278     scopes = scm_cons (score_lines_[i].header_, scopes);
279   //end ugh
280
281   SCM tit = SCM_EOL;
282   if (ly_c_procedure_p (title_func))
283     tit =scm_call_2 (title_func,
284                      bookpaper_->self_scm (),
285                      scopes);
286
287   if (unsmob_stencil (tit))
288     title = *unsmob_stencil (tit);
289
290
291   if (!title.is_empty ())
292     title.align_to (Y_AXIS, UP);
293   
294   return title;
295 }
296
297   
298
299
300 /* calculate book height, #lines, stencils.  */
301 void
302 Paper_book::init ()
303 {
304   int score_count = score_lines_.size ();
305
306   /* Calculate the full book height.  Hmm, can't we cache system
307      heights while making stencils?  */
308   height_ = 0;
309   Stencil btitle = book_title ();
310   if (!btitle.is_empty ())
311     height_ += btitle.extent (Y_AXIS).length ();
312   
313   for (int i = 0; i < score_count; i++)
314     {
315       Stencil title = score_title (i);
316       if (!title.is_empty ())
317         height_ += title.extent (Y_AXIS).length ();
318
319       int line_count = SCM_VECTOR_LENGTH (score_lines_[i].lines_);
320       for (int j = 0; j < line_count; j++)
321         {
322           SCM s = scm_vector_ref ((SCM) score_lines_[i].lines_, scm_int2num (j));
323           height_ += unsmob_paper_line (s)->dim ()[Y_AXIS];
324         }
325     }
326
327   Output_def *paper = bookpaper_;
328   
329   SCM scopes = SCM_EOL;
330   if (ly_c_module_p (header_))
331     scopes = scm_cons (header_, scopes);
332
333   SCM make_tagline = paper->c_variable ("make-tagline");
334   tagline_ = scm_call_2 (make_tagline, paper->self_scm (), scopes);
335   Real tag_height = 0;
336   if (Stencil *s = unsmob_stencil (tagline_))
337     tag_height = s->extent (Y_AXIS).length ();
338   height_ += tag_height;
339
340   SCM make_copyright = paper->c_variable ("make-copyright");
341   copyright_ = scm_call_2 (make_copyright, paper->self_scm (), scopes);
342   Real copy_height = 0;
343   if (Stencil *s = unsmob_stencil (copyright_))
344     copy_height = s->extent (Y_AXIS).length ();
345   height_ += copy_height;
346 }
347
348 SCM
349 Paper_book::lines ()
350 {
351   if (ly_c_pair_p (lines_))
352     return lines_;
353
354   Stencil title = book_title ();      
355   if (!title.is_empty ())
356     lines_ = scm_cons (stencil2line (title, true), lines_);
357   
358   int score_count = score_lines_.size ();
359   for (int i = 0; i < score_count; i++)
360     {
361       Stencil title = score_title (i);      
362       if (!title.is_empty ())
363         lines_ = scm_cons (stencil2line (title, true), lines_);
364
365       SCM line_list = scm_vector_to_list (score_lines_[i].lines_); // guh.
366       lines_ = scm_append (scm_list_2 (scm_reverse (line_list), lines_));
367     }
368   
369   lines_ = scm_reverse (lines_);
370
371   int i = 0;
372   for (SCM s = lines_; s != SCM_EOL; s = ly_cdr (s))
373     unsmob_paper_line (ly_car (s))->number_ = ++i;
374   return lines_;
375 }
376
377 SCM
378 Paper_book::pages ()
379 {
380   if (ly_c_pair_p (pages_))
381     return pages_;
382
383   init ();
384   Output_def *paper = bookpaper_;
385   Page *page = new Page (paper, 1);
386
387   Real text_height = page->text_height ();
388
389   Real copy_height = 0;
390   if (Stencil *s = unsmob_stencil (copyright_))
391     copy_height = s->extent (Y_AXIS).length ();
392
393   Real tag_height = 0;
394   if (Stencil *s = unsmob_stencil (tagline_))
395     tag_height = s->extent (Y_AXIS).length ();
396
397   SCM all = lines ();
398   SCM proc = paper->c_variable ("page-breaking");
399   SCM breaks = scm_apply_0 (proc, scm_list_n (all, scm_make_real (height_),
400                                               scm_make_real (text_height),
401                                               scm_make_real (-copy_height),
402                                               scm_make_real (-tag_height),
403                                               SCM_UNDEFINED));
404
405   /* Copyright on first page.  */
406   if (unsmob_stencil (copyright_))
407     page->copyright_ = copyright_;
408
409   int page_count = SCM_VECTOR_LENGTH ((SCM) breaks);
410   int line = 1;
411
412   for (int i = 0; i < page_count; i++)
413     {
414       if (i)
415         page = new Page (paper, i + 1);
416
417       int next = i + 1 < page_count
418         ? ly_scm2int (scm_vector_ref (breaks, scm_int2num (i))) : 0;
419       while ((!next && all != SCM_EOL) || line <= next)
420         {
421           SCM s = ly_car (all);
422           page->lines_ = ly_snoc (s, page->lines_);
423           page->height_ += unsmob_paper_line (s)->dim ()[Y_AXIS];
424           page->line_count_++;
425           all = ly_cdr (all);
426           line++;
427         }
428       if (i == page_count-1)
429         page->is_last_ = true;
430       
431       pages_ = scm_cons (page->self_scm (), pages_);
432     }
433
434   /* Tagline on last page.  */
435   if (unsmob_stencil (tagline_))
436     page->tagline_ = tagline_;
437
438   pages_ =  scm_reverse (pages_);
439   return pages_;
440 }
441
442 static SCM
443 c_ragged_page_breaks (SCM lines, Real book_height, Real text_height,
444                       Real first, Real last)
445 {
446   int page_number = 0;
447   int page_count = int (book_height / text_height + 0.5);
448   SCM breaks = SCM_EOL;
449   Real page_height = text_height + first;
450   Real h = 0;
451   int number = 0;
452   for (SCM s = lines; s != SCM_EOL; s = ly_cdr (s))
453     {
454       Paper_line *pl = unsmob_paper_line (ly_car (s));
455       if (!pl->is_title () && h < page_height)
456         number++;
457       h += pl->dim ()[Y_AXIS];
458       if (!pl->is_title () && h > page_height)
459         {
460           breaks = ly_snoc (scm_int2num (number), breaks);
461           page_number++;
462           page_height = text_height + (page_number == page_count) * last;
463           h = 0;
464         }
465       if (ly_cdr (s) == SCM_EOL)
466         breaks = ly_snoc (scm_int2num (pl->number_), breaks);
467     }
468
469   return scm_vector (breaks);
470 }
471
472 LY_DEFINE (ly_ragged_page_breaks, "ly:ragged-page-breaks",
473            5, 0, 0, (SCM lines, SCM book, SCM text, SCM first, SCM last),
474            "Return a vector with line numbers of page breaks.")
475 {
476   SCM_ASSERT_TYPE (scm_pair_p (lines), lines, SCM_ARG1, __FUNCTION__, "list");
477   SCM_ASSERT_TYPE (ly_c_number_p (book), book, SCM_ARG2, __FUNCTION__, "real");
478   SCM_ASSERT_TYPE (ly_c_number_p (text), text, SCM_ARG2, __FUNCTION__, "real");
479   SCM_ASSERT_TYPE (ly_c_number_p (first), first, SCM_ARG2, __FUNCTION__, "real");
480   SCM_ASSERT_TYPE (ly_c_number_p (last), last, SCM_ARG2, __FUNCTION__, "real");
481
482   return c_ragged_page_breaks (lines,
483                                ly_scm2double (book), ly_scm2double (text),
484                                ly_scm2double (first), ly_scm2double (last));
485 }
486
487 /****************************************************************/
488
489 Score_lines::Score_lines ()
490 {
491   lines_ = SCM_EOL;
492   header_ = SCM_EOL;
493 }
494
495 void
496 Score_lines::gc_mark ()
497 {
498   scm_gc_mark (lines_);
499   scm_gc_mark (header_);
500 }
501