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