]> git.donarmstrong.com Git - lilypond.git/blob - lily/paper-book.cc
Remove cruft.
[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 <stdio.h>
10 #include <math.h>
11
12 #include "ly-module.hh"
13 #include "main.hh"
14 #include "paper-book.hh"
15 #include "paper-def.hh"
16 #include "paper-outputter.hh"
17 #include "paper-line.hh"
18 #include "paper-score.hh"
19 #include "stencil.hh"
20
21 static Real const MIN_COVERAGE = 0.66;
22
23 static SCM
24 stencil2line (Stencil* stil, bool is_title = false)
25 {
26   static SCM z;
27   if (!z)
28     z = scm_permanent_object (ly_offset2scm (Offset (0, 0)));
29   Offset dim = Offset (stil->extent (X_AXIS).length (),
30                        stil->extent (Y_AXIS).length ());
31   Paper_line *pl = new Paper_line (dim, scm_cons (stil->smobbed_copy (),
32                                                   SCM_EOL), is_title);
33   return pl->self_scm ();
34 }
35
36 /* Simplistic page interface */
37 class Page
38 {
39 public:
40   Paper_def *paper_;
41   static int page_count_;
42   int number_;
43   int line_count_;
44
45   Protected_scm lines_;
46   Protected_scm header_;
47   Protected_scm footer_;
48   Protected_scm copyright_;
49   Protected_scm tagline_;
50   
51   Stencil *get_header () { return unsmob_stencil (header_); }
52   Stencil *get_copyright () { return unsmob_stencil (copyright_); }
53   Stencil *get_tagline () { return unsmob_stencil (tagline_); }
54   Stencil *get_footer () { return unsmob_stencil (footer_); }
55
56   /* actual height filled with text.  */
57   Real height_;
58   
59   // HMMM all this size stuff to paper/paper-outputter?
60   Real hsize_;
61   Real vsize_;
62   Real left_margin_;
63   Real top_margin_;
64   Real bottom_margin_;
65   Real foot_sep_;
66   Real head_sep_;
67   Real text_width_;
68
69   /* available area for text.  */
70   Real text_height ();
71
72   Page (Paper_def*, int);
73   void output (Paper_outputter*, bool);
74 };
75
76 int Page::page_count_ = 0;
77
78 Page::Page (Paper_def *paper, int number)
79 {
80   paper_ = paper;
81   number_ = number;
82   page_count_++;
83   
84   height_ = 0;
85   lines_ = SCM_EOL;
86   line_count_ = 0;
87
88   hsize_ = paper->get_realvar (ly_symbol2scm ("hsize"));
89   vsize_ = paper->get_realvar (ly_symbol2scm ("vsize"));
90   top_margin_ = paper->get_realvar (ly_symbol2scm ("top-margin"));
91   bottom_margin_ = paper->get_realvar (ly_symbol2scm ("bottom-margin"));
92   head_sep_ = paper->get_realvar (ly_symbol2scm ("head-sep"));
93   foot_sep_ = paper->get_realvar (ly_symbol2scm ("foot-sep"));
94   text_width_ = paper->get_realvar (ly_symbol2scm ("linewidth"));
95   left_margin_ = (hsize_ - text_width_) / 2;
96   
97   copyright_ = SCM_EOL;
98   tagline_ = SCM_EOL;
99   
100   SCM make_header = scm_primitive_eval (ly_symbol2scm ("make-header"));
101   SCM make_footer = scm_primitive_eval (ly_symbol2scm ("make-footer"));
102
103   header_ = scm_call_2 (make_header, paper_->self_scm (),
104                         scm_int2num (number_));
105   // FIXME: why does this (generates Stencil) not trigger font load?
106   if (get_header ())
107     get_header ()->align_to (Y_AXIS, UP);
108     
109   footer_ = scm_call_2 (make_footer, paper_->self_scm (),
110                         scm_int2num (number_));
111   if (get_footer ())
112     get_footer ()->align_to (Y_AXIS, UP);
113 }
114
115 void
116 Page::output (Paper_outputter *out, bool is_last)
117 {
118   progress_indication ("[" + to_string (number_));
119   out->output_scheme (scm_list_1 (ly_symbol2scm ("start-page")));
120   Offset o (left_margin_, top_margin_);
121   Real vfill = line_count_ > 1 ? (text_height () - height_) / (line_count_ - 1)
122     : 0;
123
124   Real coverage = height_ / text_height ();
125   if (coverage < MIN_COVERAGE)
126     /* Do not space out a badly filled page.  This is too simplistic
127        (ie broken), because this should not vary too much between
128        (subsequent?) pages in a book.  */
129     vfill = 0;
130
131   if (get_header ())
132     {
133       out->output_line (stencil2line (get_header ()), &o, false);
134       o[Y_AXIS] += head_sep_;
135     }
136   for (SCM s = lines_; s != SCM_EOL; s = ly_cdr (s))
137     {
138       SCM line = ly_car (s);
139       out->output_line (line, &o,
140                         is_last && ly_cdr (s) != SCM_EOL && !get_copyright ()
141                         && !get_tagline () && !get_footer ());
142       
143       /* Do not put vfill between title and its music, */
144       if (scm_pair_p (ly_cdr (s)) && !unsmob_paper_line (line)->is_title ())
145         o[Y_AXIS] += vfill;
146       /* rather put extra just before the title.  */
147       if (ly_cdr (s) != SCM_EOL
148           && unsmob_paper_line (ly_cadr (s))->is_title ())
149         o[Y_AXIS] += vfill;
150     }
151
152   o[Y_AXIS] = vsize_ - bottom_margin_;
153   if (get_copyright ())
154     o[Y_AXIS] -= get_copyright ()->extent (Y_AXIS).length ();
155   if (get_tagline ())
156     o[Y_AXIS] -= get_tagline ()->extent (Y_AXIS).length ();
157   if (get_footer ())
158     o[Y_AXIS] -= get_footer ()->extent (Y_AXIS).length ();
159
160   if (get_copyright ())
161     out->output_line (stencil2line (get_copyright ()), &o,
162                       is_last && !get_tagline () && !get_footer ());
163   if (get_tagline ())
164     out->output_line (stencil2line (get_tagline ()), &o,
165                       is_last && !get_footer ());
166   if (get_footer ())
167     out->output_line (stencil2line (get_footer ()), &o, is_last);
168   out->output_scheme (scm_list_2 (ly_symbol2scm ("stop-page"),
169                                   gh_bool2scm (is_last && !get_footer ())));
170   progress_indication ("]");
171 }
172
173 Real
174 Page::text_height ()
175 {
176   Real h = vsize_ - top_margin_ - bottom_margin_;
177   if (get_header ())
178     h -= get_header ()->extent (Y_AXIS).length () + head_sep_;
179   if (get_copyright () || get_tagline () || get_footer ())
180     h -= foot_sep_;
181   if (get_copyright ())
182     h -= get_copyright ()->extent (Y_AXIS).length ();
183   if (get_tagline ())
184     h -= get_tagline ()->extent (Y_AXIS).length ();
185   if (get_footer ())
186     h -= get_footer ()->extent (Y_AXIS).length ();
187   return h;
188 }
189
190 /****************************************************************/
191
192 /* Current global paper book.  Gives default_rendering access from
193    input-file-results.  */
194 Paper_book *paper_book;
195
196 Paper_book::Paper_book ()
197 {
198   copyright_ = SCM_EOL;
199   tagline_ = SCM_EOL;
200   
201   smobify_self ();
202 }
203
204 Paper_book::~Paper_book ()
205 {
206 }
207
208 #include "ly-smobs.icc"
209
210 IMPLEMENT_DEFAULT_EQUAL_P (Paper_book);
211 IMPLEMENT_SMOBS (Paper_book)
212 IMPLEMENT_TYPE_P (Paper_book, "ly:paper-book?")
213
214 SCM
215 Paper_book::mark_smob (SCM smob)
216 {
217   Paper_book *pb = (Paper_book*) SCM_CELL_WORD_1 (smob);
218   for (int i = 0; i < pb->headers_.size (); i++)
219     scm_gc_mark (pb->headers_[i]);
220   for (int i = 0; i < pb->global_headers_.size (); i++)
221     scm_gc_mark (pb->global_headers_[i]);
222   for (int i = 0; i < pb->papers_.size (); i++)
223     scm_gc_mark (pb->papers_[i]->self_scm ());
224   for (int i = 0; i < pb->scores_.size (); i++)
225     scm_gc_mark (pb->scores_[i]);
226
227   scm_gc_mark (pb->copyright_);
228   
229   return pb->tagline_;
230 }
231
232 int
233 Paper_book::print_smob (SCM smob, SCM port, scm_print_state*)
234 {
235   Paper_book *b = (Paper_book*) ly_cdr (smob);
236      
237   scm_puts ("#<", port);
238   scm_puts (classname (b), port);
239   scm_puts (" ", port);
240   //scm_puts (b->, port);
241   scm_puts (">", port);
242   return 1;
243 }
244
245 void
246 Paper_book::output (String outname)
247 {
248   if (!papers_.size ())
249     return;
250     
251   /* Generate all stencils to trigger font loads.  */
252   Link_array<Page> *pages = this->pages ();
253
254   Paper_def *paper = papers_[0];
255   Paper_outputter *out = paper->get_paper_outputter (outname);
256   out->output_header (paper, scopes (0), pages->size ());
257
258   int page_count = pages->size ();
259   for (int i = 0; i < page_count; i++)
260     (*pages)[i]->output (out, i + 1 == page_count);
261
262   out->output_scheme (scm_list_1 (ly_symbol2scm ("end-output")));
263   progress_indication ("\n");
264 }
265
266 SCM
267 Paper_book::scopes (int i)
268 {
269   SCM scopes = SCM_EOL;
270   if (headers_[i])
271     scopes = scm_cons (headers_[i], scopes);
272   if (global_headers_[i] && global_headers_[i] != headers_[i])
273     scopes = scm_cons (global_headers_[i], scopes);
274   return scopes;
275 }
276
277 Stencil*
278 Paper_book::title (int i)
279 {
280   SCM user_title = scm_primitive_eval (ly_symbol2scm ("user-title"));
281   SCM book_title = scm_primitive_eval (ly_symbol2scm ("book-title"));
282   SCM score_title = scm_primitive_eval (ly_symbol2scm ("score-title"));
283   SCM field = (i == 0 ? ly_symbol2scm ("bookTitle")
284                : ly_symbol2scm ("scoreTitle"));
285
286   Stencil *title = 0;
287   SCM scopes = this->scopes (i);
288   SCM s = ly_modules_lookup (scopes, field);
289   if (s != SCM_UNDEFINED && scm_variable_bound_p (s) == SCM_BOOL_T)
290     title = unsmob_stencil (scm_call_2 (user_title,
291                                         papers_[0]->self_scm (),
292                                         scm_variable_ref (s)));
293   else
294     title = unsmob_stencil (scm_call_2 (i == 0 ? book_title : score_title,
295                                         papers_[0]->self_scm (),
296                                         scopes));
297   if (title)
298     title->align_to (Y_AXIS, UP);
299   
300   return title;
301 }
302
303 void
304 Paper_book::classic_output (String outname)
305 {
306   int count = scores_.size ();
307   Paper_outputter *out = papers_.top ()->get_paper_outputter (outname);
308   out->output_header (papers_.top (), scopes (count - 1), 0);
309
310   int line_count = SCM_VECTOR_LENGTH ((SCM) scores_.top ());
311   for (int i = 0; i < line_count; i++)
312     out->output_line (scm_vector_ref ((SCM) scores_.top (), scm_int2num (i)),
313                       0, i == line_count - 1);
314   
315   out->output_scheme (scm_list_1 (ly_symbol2scm ("end-output")));
316   progress_indication ("\n");
317 }
318
319
320 /* calculate book height, #lines, stencils.  */
321 void
322 Paper_book::init ()
323 {
324   int score_count = scores_.size ();
325
326   /* Calculate the full book height.  Hmm, can't we cache system
327      heights while making stencils?  */
328   height_ = 0;
329   for (int i = 0; i < score_count; i++)
330     {
331       Stencil *title = this->title (i);
332       if (title)
333         height_ += title->extent (Y_AXIS).length ();
334
335       int line_count = SCM_VECTOR_LENGTH ((SCM) scores_[i]);
336       for (int j = 0; j < line_count; j++)
337         {
338           SCM s = scm_vector_ref ((SCM) scores_[i], scm_int2num (j));
339           height_ += unsmob_paper_line (s)->dim ()[Y_AXIS];
340         }
341     }
342
343   Paper_def *paper = papers_[0];
344   SCM scopes = this->scopes (0);
345
346   SCM make_tagline = scm_primitive_eval (ly_symbol2scm ("make-tagline"));
347   tagline_ = scm_call_2 (make_tagline, paper->self_scm (), scopes);
348   Real tag_height = 0;
349   if (Stencil *s = unsmob_stencil (tagline_))
350     tag_height = s->extent (Y_AXIS).length ();
351   height_ += tag_height;
352
353   SCM make_copyright = scm_primitive_eval (ly_symbol2scm ("make-copyright"));
354   copyright_ = scm_call_2 (make_copyright, paper->self_scm (), scopes);
355   Real copy_height = 0;
356   if (Stencil *s = unsmob_stencil (copyright_))
357     copy_height = s->extent (Y_AXIS).length ();
358   height_ += copy_height;
359 }
360
361 SCM
362 Paper_book::lines ()
363 {
364   int score_count = scores_.size ();
365   SCM lines = SCM_EOL;
366   for (int i = 0; i < score_count; i++)
367     {
368       if (Stencil *title = this->title (i))
369         lines = ly_snoc (stencil2line (title, true), lines);
370       lines = scm_append (scm_list_2 (lines, scm_vector_to_list (scores_[i])));
371     }
372   //debug helper; ughr
373   int i = 0;
374   for (SCM s = lines; s != SCM_EOL; s = ly_cdr (s))
375     unsmob_paper_line (ly_car (s))->number_ = ++i;
376   return lines;
377 }
378
379 Link_array<Page>*
380 Paper_book::pages ()
381 {
382   init ();
383   Page::page_count_ = 0;
384   Paper_def *paper = papers_[0];
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->get_scmvar ("page-breaking");
399   SCM breaks = scm_apply_0 (proc, scm_list_n (all,
400                                               gh_double2scm (height_),
401                                               gh_double2scm (text_height),
402                                               gh_double2scm (copy_height),
403                                               gh_double2scm (tag_height),
404                                               SCM_UNDEFINED));
405
406   /* Copyright on first page.  */
407   if (unsmob_stencil (copyright_))
408     page->copyright_ = copyright_;
409
410   Link_array<Page> *pages = new Link_array<Page>;
411   int page_count = SCM_VECTOR_LENGTH ((SCM) breaks);
412   int line = 1;
413   for (int i = 0; i < page_count; i++)
414     {
415       if (i)
416         page = new Page (paper, i+1);
417       int next = i + 1 < page_count
418         ? gh_scm2int (scm_vector_ref (breaks, gh_int2scm (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       pages->push (page);
429     }
430
431   /* Tagline on last page.  */
432   if (unsmob_stencil (tagline_))
433     page->tagline_ = tagline_;
434   return pages;
435 }
436
437 static SCM
438 c_ragged_page_breaks (SCM lines, Real book_height, Real text_height,
439                       Real first, Real last)
440 {
441   int page_number = 0;
442   int page_count = int (book_height / text_height + 0.5);
443   SCM breaks = SCM_EOL;
444   Real page_height = text_height + first;
445   Real h = 0;
446   int number = 0;
447   for (SCM s = lines; s != SCM_EOL; s = ly_cdr (s))
448     {
449       Paper_line *pl = unsmob_paper_line (ly_car (s));
450       if (!pl->is_title () && h < page_height)
451         number++;
452       h += pl->dim ()[Y_AXIS];
453       if (!pl->is_title () && h > page_height)
454         {
455           breaks = ly_snoc (gh_int2scm (number), breaks);
456           page_number++;
457           page_height = text_height + (page_number == page_count) * last;
458           h = 0;
459         }
460       if (ly_cdr (s) == SCM_EOL)
461         breaks = ly_snoc (gh_int2scm (pl->number_), breaks);
462     }
463
464   return scm_vector (breaks);
465 }
466
467 LY_DEFINE (ly_ragged_page_breaks, "ly:ragged-page-breaks",
468            5, 0, 0, (SCM lines, SCM book, SCM text, SCM first, SCM last),
469            "Return a vector with line numbers of page breaks.")
470 {
471   SCM_ASSERT_TYPE (scm_pair_p (lines), lines, SCM_ARG1, __FUNCTION__, "list");
472   SCM_ASSERT_TYPE (gh_number_p (book), book, SCM_ARG2, __FUNCTION__, "real");
473   SCM_ASSERT_TYPE (gh_number_p (text), text, SCM_ARG2, __FUNCTION__, "real");
474   SCM_ASSERT_TYPE (gh_number_p (first), first, SCM_ARG2, __FUNCTION__, "real");
475   SCM_ASSERT_TYPE (gh_number_p (last), last, SCM_ARG2, __FUNCTION__, "real");
476
477   return c_ragged_page_breaks (lines,
478                                gh_scm2double (book), gh_scm2double (text),
479                                gh_scm2double (first), gh_scm2double (last));
480 }