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