]> git.donarmstrong.com Git - lilypond.git/blob - lily/paper-book.cc
* lily/book.cc (process): return Paper_book
[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   copyright_ = SCM_EOL;
41   tagline_ = SCM_EOL;
42   header_ = SCM_EOL;
43   
44   bookpaper_ = 0;
45   smobify_self ();
46 }
47
48 Paper_book::~Paper_book ()
49 {
50 }
51
52 #include "ly-smobs.icc"
53
54 IMPLEMENT_DEFAULT_EQUAL_P (Paper_book);
55 IMPLEMENT_SMOBS (Paper_book)
56 IMPLEMENT_TYPE_P (Paper_book, "ly:paper-book?")
57
58 SCM
59 Paper_book::mark_smob (SCM smob)
60 {
61   Paper_book *b = (Paper_book*) SCM_CELL_WORD_1 (smob);
62   for (int i = 0; i < b->score_lines_.size (); i++)
63     b->score_lines_[i].gc_mark ();
64
65   scm_gc_mark (b->copyright_);
66   if (b->bookpaper_)
67     scm_gc_mark (b->bookpaper_->self_scm ());
68   scm_gc_mark (b->header_);
69   return b->tagline_;
70 }
71
72 int
73 Paper_book::print_smob (SCM smob, SCM port, scm_print_state*)
74 {
75   Paper_book *b = (Paper_book*) ly_cdr (smob);
76      
77   scm_puts ("#<", port);
78   scm_puts (classname (b), port);
79   scm_puts (" ", port);
80   //scm_puts (b->, port);
81   scm_puts (">", port);
82   return 1;
83 }
84
85
86 /*
87   TODO: there is too much code dup, and the interface is not
88   clear. FIXME.
89  */
90
91 void
92 Paper_book::output (String outname)
93 {
94   if (!score_lines_.size ())
95     // FIXME: no end-output?
96     return;
97     
98   /* Generate all stencils to trigger font loads.  */
99   SCM pages = this->pages ();
100
101   Paper_outputter *out = get_paper_outputter (outname);
102   int page_count = scm_ilength (pages);
103
104   out->output_header (bookpaper_,   scm_list_1 (header_), page_count, false);
105
106   for (SCM s = pages; s != SCM_EOL; s = ly_cdr (s))
107     {
108       Page *p = unsmob_page (ly_car (s));
109       progress_indication ("[" + to_string (p->number_));
110       out->output_page (p, ly_cdr (s) == SCM_EOL);
111       progress_indication ("]");
112     }
113
114   out->output_scheme (scm_list_1 (ly_symbol2scm ("end-output")));
115   progress_indication ("\n");
116 }
117
118
119 Stencil
120 Paper_book::title (int i)
121 {
122   SCM user_title = bookpaper_->lookup_variable (ly_symbol2scm ("user-title"));
123   SCM book_title = bookpaper_->lookup_variable (ly_symbol2scm ("book-title"));
124   SCM score_title = bookpaper_->lookup_variable (ly_symbol2scm ("score-title"));
125   SCM field = (i == 0 ? ly_symbol2scm ("bookTitle")
126                : ly_symbol2scm ("scoreTitle"));
127
128   Stencil title;
129
130   // ugh code dup
131   SCM scopes = SCM_EOL;
132   if (ly_c_module_p (header_))
133     scopes = scm_cons (header_, scopes);
134
135   if (ly_c_module_p (score_lines_[i].header_))
136     scopes = scm_cons (score_lines_[i].header_, scopes);
137    //end ugh
138   
139   SCM s = ly_modules_lookup (scopes, field, SCM_BOOL_F);
140   if (s != SCM_BOOL_F)
141     title = *unsmob_stencil (scm_call_2 (user_title,
142                                          bookpaper_->self_scm (),
143                                          s));
144   else
145     title = *unsmob_stencil (scm_call_2 (i == 0 ? book_title : score_title,
146                                          bookpaper_->self_scm (),
147                                          scopes));
148   if (!title.is_empty ())
149     title.align_to (Y_AXIS, UP);
150   
151   return title;
152 }
153
154 void
155 Paper_book::classic_output (String outname)
156 {
157   Paper_outputter *out = get_paper_outputter (outname);
158
159   Output_def * p = bookpaper_;
160   while (p && p->parent_)
161     p = p->parent_;
162
163   // ugh code dup
164   SCM scopes = SCM_EOL;
165   if (ly_c_module_p (header_))
166     scopes = scm_cons (header_, scopes);
167
168   if (ly_c_module_p (score_lines_[0].header_))
169     scopes = scm_cons (score_lines_[0].header_, scopes);
170   //end ugh
171   
172   out->output_header (p, header_, 0, true);
173
174   SCM top_lines = score_lines_.top ().lines_;
175   Paper_line *first = unsmob_paper_line (scm_vector_ref (top_lines,
176                                                          scm_int2num (0)));
177   Offset o (0, -0.5 * first->dim ()[Y_AXIS]);
178   int line_count = SCM_VECTOR_LENGTH (top_lines);
179   for (int i = 0; i < line_count; i++)
180     {
181       /* In classic compatibility TeX tracks how large things are, and
182          advances the Y pos for us.  If we advance it too, we get too
183          much space.
184
185          FIXME: vague... why is TeX is different from other ouput
186                 backends, why not fix the TeX backend? -- jcn */
187       if (output_format_global == "tex")
188         o = Offset (0, 0);
189
190       out->output_line (scm_vector_ref (top_lines, scm_int2num (i)),
191                         &o, i == line_count - 1);
192     }
193   
194   out->output_scheme (scm_list_1 (ly_symbol2scm ("end-output")));
195   progress_indication ("\n");
196 }
197
198 /* calculate book height, #lines, stencils.  */
199 void
200 Paper_book::init ()
201 {
202   int score_count = score_lines_.size ();
203
204   /* Calculate the full book height.  Hmm, can't we cache system
205      heights while making stencils?  */
206   height_ = 0;
207   for (int i = 0; i < score_count; i++)
208     {
209       Stencil title = this->title (i);
210       if (!title.is_empty ())
211         height_ += title.extent (Y_AXIS).length ();
212
213       int line_count = SCM_VECTOR_LENGTH (score_lines_[i].lines_);
214       for (int j = 0; j < line_count; j++)
215         {
216           SCM s = scm_vector_ref ((SCM) score_lines_[i].lines_, scm_int2num (j));
217           height_ += unsmob_paper_line (s)->dim ()[Y_AXIS];
218         }
219     }
220
221   Output_def *paper = bookpaper_;
222   
223   SCM scopes = SCM_EOL;
224   if (ly_c_module_p (header_))
225     scopes = scm_cons (header_, scopes);
226
227   SCM make_tagline = paper->c_variable ("make-tagline");
228   tagline_ = scm_call_2 (make_tagline, paper->self_scm (), scopes);
229   Real tag_height = 0;
230   if (Stencil *s = unsmob_stencil (tagline_))
231     tag_height = s->extent (Y_AXIS).length ();
232   height_ += tag_height;
233
234   SCM make_copyright = paper->c_variable ("make-copyright");
235   copyright_ = scm_call_2 (make_copyright, paper->self_scm (), scopes);
236   Real copy_height = 0;
237   if (Stencil *s = unsmob_stencil (copyright_))
238     copy_height = s->extent (Y_AXIS).length ();
239   height_ += copy_height;
240 }
241
242 SCM
243 Paper_book::lines ()
244 {
245   int score_count = score_lines_.size ();
246   SCM lines = SCM_EOL;
247   for (int i = 0; i < score_count; i++)
248     {
249       Stencil title = this->title (i);      
250       if (!title.is_empty ())
251         lines = ly_snoc (stencil2line (title, true), lines);
252       lines = scm_append (scm_list_2 (lines, scm_vector_to_list (score_lines_[i].lines_)));
253     }
254   
255   //debug helper; ughr
256   int i = 0;
257   for (SCM s = lines; s != SCM_EOL; s = ly_cdr (s))
258     unsmob_paper_line (ly_car (s))->number_ = ++i;
259   return lines;
260 }
261
262 SCM
263 Paper_book::pages ()
264 {
265   init ();
266   Page::page_count_ = 0;
267
268   Output_def *paper = bookpaper_;
269   Page *page = new Page (paper, 1);
270
271   Real text_height = page->text_height ();
272
273   Real copy_height = 0;
274   if (Stencil *s = unsmob_stencil (copyright_))
275     copy_height = s->extent (Y_AXIS).length ();
276
277   Real tag_height = 0;
278   if (Stencil *s = unsmob_stencil (tagline_))
279     tag_height = s->extent (Y_AXIS).length ();
280
281   SCM all = lines ();
282   SCM proc = paper->c_variable ("page-breaking");
283   SCM breaks = scm_apply_0 (proc, scm_list_n (all, scm_make_real (height_),
284                                               scm_make_real (text_height),
285                                               scm_make_real (-copy_height),
286                                               scm_make_real (-tag_height),
287                                               SCM_UNDEFINED));
288
289   /* Copyright on first page.  */
290   if (unsmob_stencil (copyright_))
291     page->copyright_ = copyright_;
292
293   SCM pages = SCM_EOL;
294   int page_count = SCM_VECTOR_LENGTH ((SCM) breaks);
295   int line = 1;
296   for (int i = 0; i < page_count; i++)
297     {
298       if (i)
299         page = new Page (paper, i + 1);
300
301       int next = i + 1 < page_count
302         ? ly_scm2int (scm_vector_ref (breaks, scm_int2num (i))) : 0;
303       while ((!next && all != SCM_EOL) || line <= next)
304         {
305           SCM s = ly_car (all);
306           page->lines_ = ly_snoc (s, page->lines_);
307           page->height_ += unsmob_paper_line (s)->dim ()[Y_AXIS];
308           page->line_count_++;
309           all = ly_cdr (all);
310           line++;
311         }
312       pages = scm_cons (page->self_scm (), pages);
313     }
314
315   /* Tagline on last page.  */
316   if (unsmob_stencil (tagline_))
317     page->tagline_ = tagline_;
318
319   return scm_reverse (pages);
320 }
321
322 static SCM
323 c_ragged_page_breaks (SCM lines, Real book_height, Real text_height,
324                       Real first, Real last)
325 {
326   int page_number = 0;
327   int page_count = int (book_height / text_height + 0.5);
328   SCM breaks = SCM_EOL;
329   Real page_height = text_height + first;
330   Real h = 0;
331   int number = 0;
332   for (SCM s = lines; s != SCM_EOL; s = ly_cdr (s))
333     {
334       Paper_line *pl = unsmob_paper_line (ly_car (s));
335       if (!pl->is_title () && h < page_height)
336         number++;
337       h += pl->dim ()[Y_AXIS];
338       if (!pl->is_title () && h > page_height)
339         {
340           breaks = ly_snoc (scm_int2num (number), breaks);
341           page_number++;
342           page_height = text_height + (page_number == page_count) * last;
343           h = 0;
344         }
345       if (ly_cdr (s) == SCM_EOL)
346         breaks = ly_snoc (scm_int2num (pl->number_), breaks);
347     }
348
349   return scm_vector (breaks);
350 }
351
352 LY_DEFINE (ly_ragged_page_breaks, "ly:ragged-page-breaks",
353            5, 0, 0, (SCM lines, SCM book, SCM text, SCM first, SCM last),
354            "Return a vector with line numbers of page breaks.")
355 {
356   SCM_ASSERT_TYPE (scm_pair_p (lines), lines, SCM_ARG1, __FUNCTION__, "list");
357   SCM_ASSERT_TYPE (ly_c_number_p (book), book, SCM_ARG2, __FUNCTION__, "real");
358   SCM_ASSERT_TYPE (ly_c_number_p (text), text, SCM_ARG2, __FUNCTION__, "real");
359   SCM_ASSERT_TYPE (ly_c_number_p (first), first, SCM_ARG2, __FUNCTION__, "real");
360   SCM_ASSERT_TYPE (ly_c_number_p (last), last, SCM_ARG2, __FUNCTION__, "real");
361
362   return c_ragged_page_breaks (lines,
363                                ly_scm2double (book), ly_scm2double (text),
364                                ly_scm2double (first), ly_scm2double (last));
365 }
366
367 /****************************************************************/
368
369 Score_lines::Score_lines ()
370 {
371   lines_ = SCM_EOL;
372   header_ = SCM_EOL;
373 }
374
375 void
376 Score_lines::gc_mark ()
377 {
378   scm_gc_mark (lines_);
379   scm_gc_mark (header_);
380 }
381