]> git.donarmstrong.com Git - lilypond.git/blob - lily/paper-book.cc
Do header and footer.
[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 <stdio.h>
10
11 #include "ly-module.hh"
12 #include "main.hh"
13 #include "paper-book.hh"
14 #include "paper-def.hh"
15 #include "paper-outputter.hh"
16 #include "paper-score.hh"
17 #include "stencil.hh"
18
19 static SCM
20 stencil2line (Stencil* stil)
21 {
22   static SCM z = ly_offset2scm (Offset (0, 0));
23   Offset dim = Offset (stil->extent (X_AXIS).length (),
24                        stil->extent (Y_AXIS).length ());
25   return scm_cons (ly_offset2scm (dim),
26                    scm_list_1 (scm_cons (z, stil->smobbed_copy ())));
27 }
28
29 static SCM
30 height2line (Real h)
31 {
32   static SCM z = ly_offset2scm (Offset (0, 0));
33   return scm_cons (ly_offset2scm (Offset (0, h)),
34                    scm_list_1 (scm_cons (z, SCM_EOL)));
35 }
36
37 // WIP -- simplistic page interface
38 // Do we need this at all?  SCM, smob?
39 class Page
40 {
41 public:
42   Paper_def *paper_;
43   int number_;
44
45   SCM lines_;
46   SCM protect_;
47   Stencil *header_;
48   Stencil *footer_;
49
50   /* actual height filled with text.  */
51   Real height_;
52   
53   //HMMM all this size stuff to paper/paper-outputter?
54   Real hsize_;
55   Real vsize_;
56   Real top_margin_;
57   Real bottom_margin_;
58   Real foot_sep_;
59   Real head_sep_;
60
61   /* available area for text.  */
62   Real text_height ();
63
64   Page (Paper_def*, int);
65   void output (Paper_outputter*, bool);
66 };
67
68 Page::Page (Paper_def *paper, int number)
69 {
70   paper_ = paper;
71   number_ = number;
72   height_ = 0;
73   lines_ = SCM_EOL;
74   
75   hsize_ = paper->get_realvar (ly_symbol2scm ("hsize"));
76   vsize_ = paper->get_realvar (ly_symbol2scm ("vsize"));
77   top_margin_ = paper->get_realvar (ly_symbol2scm ("top-margin"));
78   bottom_margin_ = paper->get_realvar (ly_symbol2scm ("bottom-margin"));
79   head_sep_ = paper->get_realvar (ly_symbol2scm ("head-sep"));
80   foot_sep_ = paper->get_realvar (ly_symbol2scm ("foot-sep"));
81   
82   SCM make_header = scm_primitive_eval (ly_symbol2scm ("make-header"));
83   SCM make_footer = scm_primitive_eval (ly_symbol2scm ("make-footer"));
84
85   header_ = unsmob_stencil (scm_call_2 (make_header, paper_->smobbed_copy (),
86                                         scm_int2num (number_)));
87   protect_ = SCM_EOL;
88   // ugh, how to protect a Stencil from the outside?
89   protect_ = scm_cons (header_->get_expr (), protect_);
90   if (header_)
91     header_->align_to (Y_AXIS, UP);
92     
93   // FIXME: tagline/copyright
94   footer_ = unsmob_stencil (scm_call_2 (make_footer, paper_->smobbed_copy (),
95                                         scm_int2num (number_)));
96   // ugh, how to protect a Stencil from the outside?
97   protect_ = scm_cons (footer_->get_expr (), protect_);
98   if (footer_)
99     footer_->align_to (Y_AXIS, UP);
100 }
101
102 void
103 Page::output (Paper_outputter *out, bool is_last)
104 {
105   if (header_)
106     {
107       out->output_line (stencil2line (header_), false);
108       out->output_line (height2line (head_sep_), false);
109     }
110   out->output_scheme (scm_list_1 (ly_symbol2scm ("start-page")));
111   for (SCM s = lines_; gh_pair_p (s); s = ly_cdr (s))
112     out->output_line (ly_car (s), is_last && gh_pair_p (ly_cdr (s)));
113   out->output_scheme (scm_list_2 (ly_symbol2scm ("stop-page"),
114                                   gh_bool2scm (is_last && !footer_)));
115   if (footer_)
116     {
117       out->output_line (height2line (foot_sep_), false);
118       out->output_line (stencil2line (footer_), is_last);
119     }
120 }
121
122 Real
123 Page::text_height ()
124 {
125   Real h = vsize_ - top_margin_ - bottom_margin_;
126   if (header_)
127     h -= header_->extent (Y_AXIS).length () + head_sep_;
128   if (footer_)
129     h -= footer_->extent (Y_AXIS).length () + foot_sep_;
130   return h;
131 }
132
133 /****************************************************************/
134
135 /* Current global paper book.  Gives default_rendering access from
136    input-file-results.  */
137 Paper_book *paper_book;
138
139 Paper_book::Paper_book ()
140 {
141   smobify_self ();
142 }
143
144 Paper_book::~Paper_book ()
145 {
146 }
147
148 void
149 Paper_book::output (String outname)
150 {
151   /* Generate all stencils to trigger font loads.  */
152   Link_array<Page> *pages = get_pages ();
153
154   Paper_def *paper = papers_[0];
155   Paper_outputter *out = paper->get_paper_outputter (outname);
156   out->output_metadata (get_scopes (0), paper);
157   out->output_header (paper);
158
159   int page_count = pages->size ();
160   for (int i = 0; i < page_count; i++)
161     (*pages)[i]->output (out, i + 1 == page_count);
162
163   out->output_scheme (scm_list_1 (ly_symbol2scm ("end-output")));
164   progress_indication ("\n");
165 }
166
167 SCM
168 Paper_book::get_scopes (int i)
169 {
170   SCM scopes = SCM_EOL;
171   if (headers_[i])
172     scopes = scm_cons (headers_[i], scopes);
173   if (global_headers_[i] && global_headers_[i] != headers_[i])
174     scopes = scm_cons (global_headers_[i], scopes);
175   return scopes;
176 }
177
178 Stencil*
179 Paper_book::get_title (int i)
180 {
181   SCM user_title = scm_primitive_eval (ly_symbol2scm ("user-title"));
182   SCM book_title = scm_primitive_eval (ly_symbol2scm ("book-title"));
183   SCM score_title = scm_primitive_eval (ly_symbol2scm ("score-title"));
184   SCM field = (i == 0 ? ly_symbol2scm ("bookTitle")
185                : ly_symbol2scm ("scoreTitle"));
186
187   Stencil *title = 0;
188   SCM scopes = get_scopes (i);
189   SCM s = ly_modules_lookup (scopes, field);
190   if (s != SCM_UNDEFINED && scm_variable_bound_p (s) == SCM_BOOL_T)
191     title = unsmob_stencil (scm_call_2 (user_title,
192                                         papers_[0]->self_scm (),
193                                         scm_variable_ref (s)));
194   else
195     title = unsmob_stencil (scm_call_2 (i == 0 ? book_title : score_title,
196                                         papers_[0]->self_scm (),
197                                         scopes));
198   if (title)
199     title->align_to (Y_AXIS, UP);
200   
201   return title;
202 }
203
204 /* TODO:
205    - decent page breaking algorithm
206    - top/bottom & inter-line filling
207    - footer: tagline/copyright
208    - override: # pages, or pageBreakLines= #'(3 3 4), ?  */
209 Link_array<Page>*
210 Paper_book::get_pages ()
211 {
212   Link_array<Page> *pages = new Link_array<Page>;
213   int score_count = scores_.size ();
214
215   /* Calculate the full book height.  Hmm, can't we cache system
216      heights while making stencils?  */
217   Real book_height = 0;
218   for (int i = 0; i < score_count; i++)
219     {
220       Stencil *title = get_title (i);
221       if (title)
222         book_height += title->extent (Y_AXIS).length ();
223
224       int line_count = SCM_VECTOR_LENGTH ((SCM) scores_[i]);
225       for (int j = 0; j < line_count; j++)
226         {
227           SCM line = scm_vector_ref ((SCM) scores_[i], scm_int2num (j));
228           book_height += ly_scm2offset (ly_car (line))[Y_AXIS];
229         }
230     }
231
232   Paper_def *paper = papers_[0];
233   int page_number = 1;
234   Page *page = new Page (paper, page_number++);
235   fprintf (stderr, "book_height: %f\n", book_height);
236   fprintf (stderr, "vsize: %f\n", page->vsize_);
237   fprintf (stderr, "pages: %f\n", book_height / page->text_height ());
238
239   /* Simplistic page breaking.  */
240   Real text_height = page->text_height ();
241   for (int i = 0; i < score_count; i++)
242     {
243       Stencil *title = get_title (i);
244       if (title)
245         book_height += title->extent (Y_AXIS).length ();
246       Real h = 0;
247       if (title)
248         h = title->extent (Y_AXIS).length ();
249
250       int line_count = SCM_VECTOR_LENGTH ((SCM) scores_[i]);
251       for (int j = 0; j < line_count; j++)
252         {
253           SCM line = scm_vector_ref ((SCM) scores_[i], scm_int2num (j));
254           h += ly_scm2offset (ly_car (line))[Y_AXIS];
255           if (page->height_ + h > text_height)
256             {
257               pages->push (page);
258               page = new Page (paper, page_number++);
259             }
260           if (page->height_ + h <= text_height || page->height_ == 0)
261             {
262               if (j == 0 && title)
263                 page->lines_ = ly_snoc (stencil2line (title), page->lines_);
264               page->lines_ = ly_snoc (line, page->lines_);
265               page->height_ += h;
266               h = 0;
267             }
268         }
269     }
270   
271   pages->push (page);
272   return pages;
273 }
274
275 void
276 Paper_book::classic_output (String outname)
277 {
278   Paper_outputter *out = papers_.top ()->get_paper_outputter (outname);
279   int count = scores_.size ();
280   
281   out->output_metadata (get_scopes (count - 1), papers_.top ());
282   out->output_header (papers_.top ());
283
284   int line_count = SCM_VECTOR_LENGTH ((SCM) scores_.top ());
285   for (int i = 0; i < line_count; i++)
286     out->output_line (scm_vector_ref ((SCM) scores_.top (), scm_int2num (i)),
287                       i == line_count - 1);
288   
289   out->output_scheme (scm_list_1 (ly_symbol2scm ("end-output")));
290   progress_indication ("\n");
291 }
292
293
294 #include "ly-smobs.icc"
295
296 IMPLEMENT_DEFAULT_EQUAL_P (Paper_book);
297 IMPLEMENT_SMOBS (Paper_book)
298 IMPLEMENT_TYPE_P (Paper_book, "ly:paper-book?")
299
300 SCM
301 Paper_book::mark_smob (SCM smob)
302 {
303   Paper_book *pb = (Paper_book*) SCM_CELL_WORD_1 (smob);
304   for (int i = 0; i < pb->headers_.size (); i++)
305     scm_gc_mark (pb->headers_[i]);
306   for (int i = 0; i < pb->global_headers_.size (); i++)
307     scm_gc_mark (pb->global_headers_[i]);
308   for (int i = 0; i < pb->papers_.size (); i++)
309     scm_gc_mark (pb->papers_[i]->self_scm ());
310   for (int i = 0; i < pb->scores_.size (); i++)
311     scm_gc_mark (pb->scores_[i]);
312   return SCM_EOL;
313 }
314
315 int
316 Paper_book::print_smob (SCM smob, SCM port, scm_print_state*)
317 {
318   Paper_book *b = (Paper_book*) ly_cdr (smob);
319      
320   scm_puts ("#<", port);
321   scm_puts (classname (b), port);
322   scm_puts (" ", port);
323   //scm_puts (b->, port);
324   scm_puts (">", port);
325   return 1;
326 }
327