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