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 ())));
30 title2line (Stencil* stil)
32 SCM s = stencil2line (stil);
33 /* whugh, add marker recognise titles. */
34 return scm_cons (scm_cons (ly_symbol2scm ("title"), ly_car (s)), ly_cdr (s));
37 /* Simplistic page interface */
42 static int page_count_;
47 Protected_scm header_;
48 Protected_scm footer_;
49 Protected_scm copyright_;
50 Protected_scm tagline_;
52 Stencil *get_header () { return unsmob_stencil (header_); }
53 Stencil *get_copyright () { return unsmob_stencil (copyright_); }
54 Stencil *get_tagline () { return unsmob_stencil (tagline_); }
55 Stencil *get_footer () { return unsmob_stencil (footer_); }
57 /* actual height filled with text. */
60 // HMMM all this size stuff to paper/paper-outputter?
70 /* available area for text. */
73 Page (Paper_def*, int);
74 void output (Paper_outputter*, bool);
77 int Page::page_count_ = 0;
79 Page::Page (Paper_def *paper, int number)
89 hsize_ = paper->get_realvar (ly_symbol2scm ("hsize"));
90 vsize_ = paper->get_realvar (ly_symbol2scm ("vsize"));
91 top_margin_ = paper->get_realvar (ly_symbol2scm ("top-margin"));
92 bottom_margin_ = paper->get_realvar (ly_symbol2scm ("bottom-margin"));
93 head_sep_ = paper->get_realvar (ly_symbol2scm ("head-sep"));
94 foot_sep_ = paper->get_realvar (ly_symbol2scm ("foot-sep"));
95 text_width_ = paper->get_realvar (ly_symbol2scm ("linewidth"));
96 left_margin_ = (hsize_ - text_width_) / 2;
101 SCM make_header = scm_primitive_eval (ly_symbol2scm ("make-header"));
102 SCM make_footer = scm_primitive_eval (ly_symbol2scm ("make-footer"));
104 header_ = scm_call_2 (make_header, paper_->smobbed_copy (),
105 scm_int2num (number_));
106 // FIXME: why does this (generates Stencil) not trigger font load?
108 get_header ()->align_to (Y_AXIS, UP);
110 footer_ = scm_call_2 (make_footer, paper_->smobbed_copy (),
111 scm_int2num (number_));
113 get_footer ()->align_to (Y_AXIS, UP);
117 Page::output (Paper_outputter *out, bool is_last)
119 progress_indication ("[" + to_string (number_));
120 out->output_scheme (scm_list_1 (ly_symbol2scm ("start-page")));
121 Offset o (left_margin_, top_margin_);
122 Real vfill = line_count_ > 1 ? (text_height () - height_) / (line_count_ - 1)
126 out->output_line (stencil2line (get_header ()), &o, false);
127 o[Y_AXIS] += head_sep_;
129 for (SCM s = lines_; gh_pair_p (s); s = ly_cdr (s))
131 SCM line = ly_car (s);
132 SCM offset = ly_car (line);
133 if (ly_car (offset) == ly_symbol2scm ("title"))
134 line = scm_cons (ly_cdr (offset), ly_cdr (line));
135 out->output_line (line, &o,
136 is_last && gh_pair_p (ly_cdr (s)) && !get_copyright ()
137 && !get_tagline () && !get_footer ());
138 if (gh_pair_p (ly_cdr (s)) && ly_car (offset) != ly_symbol2scm ("title"))
141 if (get_copyright () || get_tagline () || get_footer ())
142 o[Y_AXIS] += foot_sep_;
143 if (get_copyright ())
144 out->output_line (stencil2line (get_copyright ()), &o,
145 is_last && !get_tagline () && !get_footer ());
147 out->output_line (stencil2line (get_tagline ()), &o,
148 is_last && !get_footer ());
150 out->output_line (stencil2line (get_footer ()), &o, is_last);
151 out->output_scheme (scm_list_2 (ly_symbol2scm ("stop-page"),
152 gh_bool2scm (is_last && !get_footer ())));
153 progress_indication ("]");
159 Real h = vsize_ - top_margin_ - bottom_margin_;
161 h -= get_header ()->extent (Y_AXIS).length () + head_sep_;
162 if (get_copyright () || get_tagline () || get_footer ())
164 if (get_copyright ())
165 h -= get_copyright ()->extent (Y_AXIS).length ();
167 h -= get_tagline ()->extent (Y_AXIS).length ();
169 h -= get_footer ()->extent (Y_AXIS).length ();
173 /****************************************************************/
175 /* Current global paper book. Gives default_rendering access from
176 input-file-results. */
177 Paper_book *paper_book;
179 Paper_book::Paper_book ()
184 Paper_book::~Paper_book ()
189 Paper_book::output (String outname)
191 /* Generate all stencils to trigger font loads. */
192 Link_array<Page> *pages = get_pages ();
194 Paper_def *paper = papers_[0];
195 Paper_outputter *out = paper->get_paper_outputter (outname);
196 out->output_metadata (get_scopes (0), paper);
197 out->output_header (paper);
199 int page_count = pages->size ();
200 for (int i = 0; i < page_count; i++)
201 (*pages)[i]->output (out, i + 1 == page_count);
203 out->output_scheme (scm_list_1 (ly_symbol2scm ("end-output")));
204 progress_indication ("\n");
208 Paper_book::get_scopes (int i)
210 SCM scopes = SCM_EOL;
212 scopes = scm_cons (headers_[i], scopes);
213 if (global_headers_[i] && global_headers_[i] != headers_[i])
214 scopes = scm_cons (global_headers_[i], scopes);
219 Paper_book::get_title (int i)
221 SCM user_title = scm_primitive_eval (ly_symbol2scm ("user-title"));
222 SCM book_title = scm_primitive_eval (ly_symbol2scm ("book-title"));
223 SCM score_title = scm_primitive_eval (ly_symbol2scm ("score-title"));
224 SCM field = (i == 0 ? ly_symbol2scm ("bookTitle")
225 : ly_symbol2scm ("scoreTitle"));
228 SCM scopes = get_scopes (i);
229 SCM s = ly_modules_lookup (scopes, field);
230 if (s != SCM_UNDEFINED && scm_variable_bound_p (s) == SCM_BOOL_T)
231 title = unsmob_stencil (scm_call_2 (user_title,
232 papers_[0]->self_scm (),
233 scm_variable_ref (s)));
235 title = unsmob_stencil (scm_call_2 (i == 0 ? book_title : score_title,
236 papers_[0]->self_scm (),
239 title->align_to (Y_AXIS, UP);
245 - real page breaking algorithm (Gourlay?)
246 - override: # pages, or pageBreakLines= #'(3 3 4), ? */
248 Paper_book::get_pages ()
250 Link_array<Page> *pages = new Link_array<Page>;
251 int score_count = scores_.size ();
253 /* Calculate the full book height. Hmm, can't we cache system
254 heights while making stencils? */
255 Real book_height = 0;
256 for (int i = 0; i < score_count; i++)
258 Stencil *title = get_title (i);
260 book_height += title->extent (Y_AXIS).length ();
262 int line_count = SCM_VECTOR_LENGTH ((SCM) scores_[i]);
263 for (int j = 0; j < line_count; j++)
265 SCM line = scm_vector_ref ((SCM) scores_[i], scm_int2num (j));
266 book_height += ly_scm2offset (ly_car (line))[Y_AXIS];
270 Paper_def *paper = papers_[0];
271 SCM scopes = get_scopes (0);
273 SCM make_tagline = scm_primitive_eval (ly_symbol2scm ("make-tagline"));
274 SCM tagline = scm_call_2 (make_tagline, paper->smobbed_copy (), scopes);
276 if (Stencil *s = unsmob_stencil (tagline))
277 tag_height = s->extent (Y_AXIS).length ();
278 book_height += tag_height;
280 SCM make_copyright = scm_primitive_eval (ly_symbol2scm ("make-copyright"));
281 SCM copyright = scm_call_2 (make_copyright, paper->smobbed_copy (), scopes);
282 Real copy_height = 0;
283 if (Stencil *s = unsmob_stencil (copyright))
284 copy_height = s->extent (Y_AXIS).length ();
285 book_height += copy_height;
287 Page::page_count_ = 0;
289 Page *page = new Page (paper, ++page_number);
290 int page_count = int (book_height / page->text_height () + 0.5);
291 if (unsmob_stencil (copyright))
292 page->copyright_ = copyright;
293 if (unsmob_stencil (tagline) && page_number == page_count)
294 page->tagline_ = tagline;
297 fprintf (stderr, "book_height: %f\n", book_height);
298 fprintf (stderr, "vsize: %f\n", page->vsize_);
299 fprintf (stderr, "pages: %d\n", page_count);
302 /* Simplistic page breaking. */
303 Real text_height = page->text_height ();
304 for (int i = 0; i < score_count; i++)
307 Stencil *title = get_title (i);
309 h = title->extent (Y_AXIS).length ();
311 int line_count = SCM_VECTOR_LENGTH ((SCM) scores_[i]);
312 for (int j = 0; j < line_count; j++)
314 SCM line = scm_vector_ref ((SCM) scores_[i], scm_int2num (j));
315 h += ly_scm2offset (ly_car (line))[Y_AXIS];
316 if (page->height_ + h > text_height)
319 page = new Page (paper, ++page_number);
320 if (unsmob_stencil (tagline) && page_number == page_count)
321 page->tagline_ = tagline;
322 text_height = page->text_height ();
324 if (page->height_ + h <= text_height || page->height_ == 0)
328 = ly_snoc (title2line (title), page->lines_);
329 page->lines_ = ly_snoc (line, page->lines_);
342 Paper_book::classic_output (String outname)
344 Paper_outputter *out = papers_.top ()->get_paper_outputter (outname);
345 int count = scores_.size ();
347 out->output_metadata (get_scopes (count - 1), papers_.top ());
348 out->output_header (papers_.top ());
350 int line_count = SCM_VECTOR_LENGTH ((SCM) scores_.top ());
351 for (int i = 0; i < line_count; i++)
352 out->output_line (scm_vector_ref ((SCM) scores_.top (), scm_int2num (i)),
353 0, i == line_count - 1);
355 out->output_scheme (scm_list_1 (ly_symbol2scm ("end-output")));
356 progress_indication ("\n");
360 #include "ly-smobs.icc"
362 IMPLEMENT_DEFAULT_EQUAL_P (Paper_book);
363 IMPLEMENT_SMOBS (Paper_book)
364 IMPLEMENT_TYPE_P (Paper_book, "ly:paper-book?")
367 Paper_book::mark_smob (SCM smob)
369 Paper_book *pb = (Paper_book*) SCM_CELL_WORD_1 (smob);
370 for (int i = 0; i < pb->headers_.size (); i++)
371 scm_gc_mark (pb->headers_[i]);
372 for (int i = 0; i < pb->global_headers_.size (); i++)
373 scm_gc_mark (pb->global_headers_[i]);
374 for (int i = 0; i < pb->papers_.size (); i++)
375 scm_gc_mark (pb->papers_[i]->self_scm ());
376 for (int i = 0; i < pb->scores_.size (); i++)
377 scm_gc_mark (pb->scores_[i]);
382 Paper_book::print_smob (SCM smob, SCM port, scm_print_state*)
384 Paper_book *b = (Paper_book*) ly_cdr (smob);
386 scm_puts ("#<", port);
387 scm_puts (classname (b), port);
388 scm_puts (" ", port);
389 //scm_puts (b->, port);
390 scm_puts (">", port);