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