2 paper-book.cc -- implement Paper_book
4 source file of the GNU LilyPond music typesetter
6 (c) 2004 Jan Nieuwenhuizen <janneke@gnu.org>
11 #include "ly-module.hh"
13 #include "paper-book.hh"
14 #include "paper-def.hh"
15 #include "paper-outputter.hh"
16 #include "paper-score.hh"
20 stencil2line (Stencil* stil)
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 ())));
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)));
37 // WIP -- simplistic page interface
38 // Do we need this at all? SCM, smob?
50 /* actual height filled with text. */
53 //HMMM all this size stuff to paper/paper-outputter?
61 /* available area for text. */
64 Page (Paper_def*, int);
65 void output (Paper_outputter*, bool);
68 Page::Page (Paper_def *paper, int number)
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"));
82 SCM make_header = scm_primitive_eval (ly_symbol2scm ("make-header"));
83 SCM make_footer = scm_primitive_eval (ly_symbol2scm ("make-footer"));
85 header_ = unsmob_stencil (scm_call_2 (make_header, paper_->smobbed_copy (),
86 scm_int2num (number_)));
88 // ugh, how to protect a Stencil from the outside?
89 protect_ = scm_cons (header_->get_expr (), protect_);
91 header_->align_to (Y_AXIS, UP);
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_);
99 footer_->align_to (Y_AXIS, UP);
103 Page::output (Paper_outputter *out, bool is_last)
107 out->output_line (stencil2line (header_), false);
108 out->output_line (height2line (head_sep_), false);
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_)));
117 out->output_line (height2line (foot_sep_), false);
118 out->output_line (stencil2line (footer_), is_last);
125 Real h = vsize_ - top_margin_ - bottom_margin_;
127 h -= header_->extent (Y_AXIS).length () + head_sep_;
129 h -= footer_->extent (Y_AXIS).length () + foot_sep_;
133 /****************************************************************/
135 /* Current global paper book. Gives default_rendering access from
136 input-file-results. */
137 Paper_book *paper_book;
139 Paper_book::Paper_book ()
144 Paper_book::~Paper_book ()
149 Paper_book::output (String outname)
151 /* Generate all stencils to trigger font loads. */
152 Link_array<Page> *pages = get_pages ();
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);
159 int page_count = pages->size ();
160 for (int i = 0; i < page_count; i++)
161 (*pages)[i]->output (out, i + 1 == page_count);
163 out->output_scheme (scm_list_1 (ly_symbol2scm ("end-output")));
164 progress_indication ("\n");
168 Paper_book::get_scopes (int i)
170 SCM scopes = SCM_EOL;
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);
179 Paper_book::get_title (int i)
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"));
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)));
195 title = unsmob_stencil (scm_call_2 (i == 0 ? book_title : score_title,
196 papers_[0]->self_scm (),
199 title->align_to (Y_AXIS, UP);
205 - decent page breaking algorithm
206 - top/bottom & inter-line filling
207 - footer: tagline/copyright
208 - override: # pages, or pageBreakLines= #'(3 3 4), ? */
210 Paper_book::get_pages ()
212 Link_array<Page> *pages = new Link_array<Page>;
213 int score_count = scores_.size ();
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++)
220 Stencil *title = get_title (i);
222 book_height += title->extent (Y_AXIS).length ();
224 int line_count = SCM_VECTOR_LENGTH ((SCM) scores_[i]);
225 for (int j = 0; j < line_count; j++)
227 SCM line = scm_vector_ref ((SCM) scores_[i], scm_int2num (j));
228 book_height += ly_scm2offset (ly_car (line))[Y_AXIS];
232 Paper_def *paper = papers_[0];
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 ());
239 /* Simplistic page breaking. */
240 Real text_height = page->text_height ();
241 for (int i = 0; i < score_count; i++)
243 Stencil *title = get_title (i);
245 book_height += title->extent (Y_AXIS).length ();
248 h = title->extent (Y_AXIS).length ();
250 int line_count = SCM_VECTOR_LENGTH ((SCM) scores_[i]);
251 for (int j = 0; j < line_count; j++)
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)
258 page = new Page (paper, page_number++);
260 if (page->height_ + h <= text_height || page->height_ == 0)
263 page->lines_ = ly_snoc (stencil2line (title), page->lines_);
264 page->lines_ = ly_snoc (line, page->lines_);
276 Paper_book::classic_output (String outname)
278 Paper_outputter *out = papers_.top ()->get_paper_outputter (outname);
279 int count = scores_.size ();
281 out->output_metadata (get_scopes (count - 1), papers_.top ());
282 out->output_header (papers_.top ());
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);
289 out->output_scheme (scm_list_1 (ly_symbol2scm ("end-output")));
290 progress_indication ("\n");
294 #include "ly-smobs.icc"
296 IMPLEMENT_DEFAULT_EQUAL_P (Paper_book);
297 IMPLEMENT_SMOBS (Paper_book)
298 IMPLEMENT_TYPE_P (Paper_book, "ly:paper-book?")
301 Paper_book::mark_smob (SCM smob)
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]);
316 Paper_book::print_smob (SCM smob, SCM port, scm_print_state*)
318 Paper_book *b = (Paper_book*) ly_cdr (smob);
320 scm_puts ("#<", port);
321 scm_puts (classname (b), port);
322 scm_puts (" ", port);
323 //scm_puts (b->, port);
324 scm_puts (">", port);