]> git.donarmstrong.com Git - lilypond.git/blob - lily/paper-book.cc
* scm/music-functions.scm (def-grace-function): move macros from
[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 Stencil
233 Paper_book::title (int i)
234 {
235   SCM user_title = bookpaper_->lookup_variable (ly_symbol2scm ("user-title"));
236   SCM book_title = bookpaper_->lookup_variable (ly_symbol2scm ("book-title"));
237   SCM score_title = bookpaper_->lookup_variable (ly_symbol2scm ("score-title"));
238   SCM field = (i == 0 ? ly_symbol2scm ("bookTitle")
239                : ly_symbol2scm ("scoreTitle"));
240
241   Stencil title;
242
243   // ugh code dup
244   SCM scopes = SCM_EOL;
245   if (ly_c_module_p (header_))
246     scopes = scm_cons (header_, scopes);
247
248   if (ly_c_module_p (score_lines_[i].header_))
249     scopes = scm_cons (score_lines_[i].header_, scopes);
250    //end ugh
251   
252   SCM s = ly_modules_lookup (scopes, field, SCM_BOOL_F);
253   if (s != SCM_BOOL_F)
254     title = *unsmob_stencil (scm_call_2 (user_title,
255                                          bookpaper_->self_scm (),
256                                          s));
257   else
258     title = *unsmob_stencil (scm_call_2 (i == 0 ? book_title : score_title,
259                                          bookpaper_->self_scm (),
260                                          scopes));
261   if (!title.is_empty ())
262     title.align_to (Y_AXIS, UP);
263   
264   return title;
265 }
266
267 /* calculate book height, #lines, stencils.  */
268 void
269 Paper_book::init ()
270 {
271   int score_count = score_lines_.size ();
272
273   /* Calculate the full book height.  Hmm, can't we cache system
274      heights while making stencils?  */
275   height_ = 0;
276   for (int i = 0; i < score_count; i++)
277     {
278       Stencil title = this->title (i);
279       if (!title.is_empty ())
280         height_ += title.extent (Y_AXIS).length ();
281
282       int line_count = SCM_VECTOR_LENGTH (score_lines_[i].lines_);
283       for (int j = 0; j < line_count; j++)
284         {
285           SCM s = scm_vector_ref ((SCM) score_lines_[i].lines_, scm_int2num (j));
286           height_ += unsmob_paper_line (s)->dim ()[Y_AXIS];
287         }
288     }
289
290   Output_def *paper = bookpaper_;
291   
292   SCM scopes = SCM_EOL;
293   if (ly_c_module_p (header_))
294     scopes = scm_cons (header_, scopes);
295
296   SCM make_tagline = paper->c_variable ("make-tagline");
297   tagline_ = scm_call_2 (make_tagline, paper->self_scm (), scopes);
298   Real tag_height = 0;
299   if (Stencil *s = unsmob_stencil (tagline_))
300     tag_height = s->extent (Y_AXIS).length ();
301   height_ += tag_height;
302
303   SCM make_copyright = paper->c_variable ("make-copyright");
304   copyright_ = scm_call_2 (make_copyright, paper->self_scm (), scopes);
305   Real copy_height = 0;
306   if (Stencil *s = unsmob_stencil (copyright_))
307     copy_height = s->extent (Y_AXIS).length ();
308   height_ += copy_height;
309 }
310
311 SCM
312 Paper_book::lines ()
313 {
314   if (ly_c_pair_p (lines_))
315     return lines_;
316       
317   int score_count = score_lines_.size ();
318   for (int i = 0; i < score_count; i++)
319     {
320       Stencil title = this->title (i);      
321       if (!title.is_empty ())
322         lines_ = scm_cons (stencil2line (title, true), lines_);
323
324       lines_ = scm_append (scm_list_2 (scm_vector_to_list (score_lines_[i].lines_), lines_));
325     }
326   
327   lines_ = scm_reverse (lines_);
328
329   int i = 0;
330   for (SCM s = lines_; s != SCM_EOL; s = ly_cdr (s))
331     unsmob_paper_line (ly_car (s))->number_ = ++i;
332   return lines_;
333 }
334
335 SCM
336 Paper_book::pages ()
337 {
338   if (ly_c_pair_p (pages_))
339     return pages_;
340
341   init ();
342   Output_def *paper = bookpaper_;
343   Page *page = new Page (paper, 1);
344
345   Real text_height = page->text_height ();
346
347   Real copy_height = 0;
348   if (Stencil *s = unsmob_stencil (copyright_))
349     copy_height = s->extent (Y_AXIS).length ();
350
351   Real tag_height = 0;
352   if (Stencil *s = unsmob_stencil (tagline_))
353     tag_height = s->extent (Y_AXIS).length ();
354
355   SCM all = lines ();
356   SCM proc = paper->c_variable ("page-breaking");
357   SCM breaks = scm_apply_0 (proc, scm_list_n (all, scm_make_real (height_),
358                                               scm_make_real (text_height),
359                                               scm_make_real (-copy_height),
360                                               scm_make_real (-tag_height),
361                                               SCM_UNDEFINED));
362
363   /* Copyright on first page.  */
364   if (unsmob_stencil (copyright_))
365     page->copyright_ = copyright_;
366
367   int page_count = SCM_VECTOR_LENGTH ((SCM) breaks);
368   int line = 1;
369
370   for (int i = 0; i < page_count; i++)
371     {
372       if (i)
373         page = new Page (paper, i + 1);
374
375       int next = i + 1 < page_count
376         ? ly_scm2int (scm_vector_ref (breaks, scm_int2num (i))) : 0;
377       while ((!next && all != SCM_EOL) || line <= next)
378         {
379           SCM s = ly_car (all);
380           page->lines_ = ly_snoc (s, page->lines_);
381           page->height_ += unsmob_paper_line (s)->dim ()[Y_AXIS];
382           page->line_count_++;
383           all = ly_cdr (all);
384           line++;
385         }
386       if (i == page_count-1)
387         page->is_last_ = true;
388       
389       pages_ = scm_cons (page->self_scm (), pages_);
390     }
391
392   /* Tagline on last page.  */
393   if (unsmob_stencil (tagline_))
394     page->tagline_ = tagline_;
395
396   pages_ =  scm_reverse (pages_);
397   return pages_;
398 }
399
400 static SCM
401 c_ragged_page_breaks (SCM lines, Real book_height, Real text_height,
402                       Real first, Real last)
403 {
404   int page_number = 0;
405   int page_count = int (book_height / text_height + 0.5);
406   SCM breaks = SCM_EOL;
407   Real page_height = text_height + first;
408   Real h = 0;
409   int number = 0;
410   for (SCM s = lines; s != SCM_EOL; s = ly_cdr (s))
411     {
412       Paper_line *pl = unsmob_paper_line (ly_car (s));
413       if (!pl->is_title () && h < page_height)
414         number++;
415       h += pl->dim ()[Y_AXIS];
416       if (!pl->is_title () && h > page_height)
417         {
418           breaks = ly_snoc (scm_int2num (number), breaks);
419           page_number++;
420           page_height = text_height + (page_number == page_count) * last;
421           h = 0;
422         }
423       if (ly_cdr (s) == SCM_EOL)
424         breaks = ly_snoc (scm_int2num (pl->number_), breaks);
425     }
426
427   return scm_vector (breaks);
428 }
429
430 LY_DEFINE (ly_ragged_page_breaks, "ly:ragged-page-breaks",
431            5, 0, 0, (SCM lines, SCM book, SCM text, SCM first, SCM last),
432            "Return a vector with line numbers of page breaks.")
433 {
434   SCM_ASSERT_TYPE (scm_pair_p (lines), lines, SCM_ARG1, __FUNCTION__, "list");
435   SCM_ASSERT_TYPE (ly_c_number_p (book), book, SCM_ARG2, __FUNCTION__, "real");
436   SCM_ASSERT_TYPE (ly_c_number_p (text), text, SCM_ARG2, __FUNCTION__, "real");
437   SCM_ASSERT_TYPE (ly_c_number_p (first), first, SCM_ARG2, __FUNCTION__, "real");
438   SCM_ASSERT_TYPE (ly_c_number_p (last), last, SCM_ARG2, __FUNCTION__, "real");
439
440   return c_ragged_page_breaks (lines,
441                                ly_scm2double (book), ly_scm2double (text),
442                                ly_scm2double (first), ly_scm2double (last));
443 }
444
445 /****************************************************************/
446
447 Score_lines::Score_lines ()
448 {
449   lines_ = SCM_EOL;
450   header_ = SCM_EOL;
451 }
452
453 void
454 Score_lines::gc_mark ()
455 {
456   scm_gc_mark (lines_);
457   scm_gc_mark (header_);
458 }
459