2 paper-book.cc -- implement Paper_book
4 source file of the GNU LilyPond music typesetter
6 (c) 2004 Jan Nieuwenhuizen <janneke@gnu.org>
9 #include "ly-module.hh"
12 #include "paper-book.hh"
13 #include "paper-def.hh"
14 #include "paper-outputter.hh"
15 #include "paper-line.hh"
16 #include "paper-score.hh"
19 #include "book-paper-def.hh"
23 stencil2line (Stencil stil, bool is_title = false)
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 (),
32 -10001 * is_title, is_title);
34 return scm_gc_unprotect_object (pl->self_scm ());
38 Paper_book::Paper_book ()
46 Paper_book::~Paper_book ()
50 #include "ly-smobs.icc"
52 IMPLEMENT_DEFAULT_EQUAL_P (Paper_book);
53 IMPLEMENT_SMOBS (Paper_book)
54 IMPLEMENT_TYPE_P (Paper_book, "ly:paper-book?")
57 Paper_book::mark_smob (SCM smob)
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 ();
63 scm_gc_mark (b->copyright_);
65 scm_gc_mark (b->bookpaper_->self_scm ());
71 Paper_book::print_smob (SCM smob, SCM port, scm_print_state*)
73 Paper_book *b = (Paper_book*) ly_cdr (smob);
75 scm_puts ("#<", port);
76 scm_puts (classname (b), port);
78 //scm_puts (b->, port);
84 Paper_book::output (String outname)
86 if (!score_lines_.size ())
87 // FIXME: no end-output?
90 /* Generate all stencils to trigger font loads. */
91 SCM pages = this->pages ();
93 Paper_def *paper = score_lines_[0].paper_;
94 Paper_outputter *out = paper->get_paper_outputter (outname);
95 int page_count = scm_ilength (pages);
96 out->output_header (paper->bookpaper_, scopes (0), page_count, false);
98 for (SCM s = pages; s != SCM_EOL; s = ly_cdr (s))
100 Page *p = unsmob_page (ly_car (s));
101 progress_indication ("[" + to_string (p->number_));
102 out->output_page (p, ly_cdr (s) == SCM_EOL);
103 progress_indication ("]");
106 out->output_scheme (scm_list_1 (ly_symbol2scm ("end-output")));
107 progress_indication ("\n");
111 Paper_book::scopes (int i)
113 return score_lines_[i].scopes ();
118 Paper_book::title (int i)
120 SCM user_title = bookpaper_->lookup_variable (ly_symbol2scm ("user-title"));
121 SCM book_title = bookpaper_->lookup_variable (ly_symbol2scm ("book-title"));
122 SCM score_title = bookpaper_->lookup_variable (ly_symbol2scm ("score-title"));
123 SCM field = (i == 0 ? ly_symbol2scm ("bookTitle")
124 : ly_symbol2scm ("scoreTitle"));
127 SCM scopes = this->scopes (i);
128 SCM s = ly_modules_lookup (scopes, field);
129 if (s != SCM_UNDEFINED && scm_variable_bound_p (s) == SCM_BOOL_T)
130 title = *unsmob_stencil (scm_call_2 (user_title,
131 score_lines_[0].paper_->self_scm (),
132 scm_variable_ref (s)));
134 title = *unsmob_stencil (scm_call_2 (i == 0 ? book_title : score_title,
135 score_lines_[0].paper_->self_scm (),
137 if (!title.is_empty ())
138 title.align_to (Y_AXIS, UP);
144 Paper_book::classic_output (String outname)
146 int count = score_lines_.size ();
147 Paper_def * p = score_lines_.top ().paper_;
148 Paper_outputter *out = p->get_paper_outputter (outname);
149 out->output_header (p->bookpaper_, scopes (count - 1), 0, true);
151 SCM top_lines = score_lines_.top ().lines_;
152 Paper_line *first = unsmob_paper_line (scm_vector_ref (top_lines,
154 Offset o (0, -0.5 * first->dim ()[Y_AXIS]);
155 int line_count = SCM_VECTOR_LENGTH (top_lines);
156 for (int i = 0; i < line_count; i++)
158 /* In classic compatibility TeX tracks how large things are, and
159 advances the Y pos for us. If we advance it too, we get too
162 FIXME: vague... why is TeX is different from other ouput
163 backends, why not fix the TeX backend? -- jcn */
164 if (output_format_global == "tex")
167 out->output_line (scm_vector_ref (top_lines, scm_int2num (i)),
168 &o, i == line_count - 1);
171 out->output_scheme (scm_list_1 (ly_symbol2scm ("end-output")));
172 progress_indication ("\n");
175 /* calculate book height, #lines, stencils. */
179 int score_count = score_lines_.size ();
181 /* Calculate the full book height. Hmm, can't we cache system
182 heights while making stencils? */
184 for (int i = 0; i < score_count; i++)
186 Stencil title = this->title (i);
187 if (!title.is_empty ())
188 height_ += title.extent (Y_AXIS).length ();
190 int line_count = SCM_VECTOR_LENGTH (score_lines_[i].lines_);
191 for (int j = 0; j < line_count; j++)
193 SCM s = scm_vector_ref ((SCM) score_lines_[i].lines_, scm_int2num (j));
194 height_ += unsmob_paper_line (s)->dim ()[Y_AXIS];
198 Paper_def *paper = score_lines_[0].paper_;
199 SCM scopes = this->scopes (0);
201 SCM make_tagline = paper->c_variable ("make-tagline");
202 tagline_ = scm_call_2 (make_tagline, paper->self_scm (), scopes);
204 if (Stencil *s = unsmob_stencil (tagline_))
205 tag_height = s->extent (Y_AXIS).length ();
206 height_ += tag_height;
208 SCM make_copyright = paper->c_variable ("make-copyright");
209 copyright_ = scm_call_2 (make_copyright, paper->self_scm (), scopes);
210 Real copy_height = 0;
211 if (Stencil *s = unsmob_stencil (copyright_))
212 copy_height = s->extent (Y_AXIS).length ();
213 height_ += copy_height;
219 int score_count = score_lines_.size ();
221 for (int i = 0; i < score_count; i++)
223 Stencil title = this->title (i);
224 if (!title.is_empty ())
225 lines = ly_snoc (stencil2line (title, true), lines);
226 lines = scm_append (scm_list_2 (lines, scm_vector_to_list (score_lines_[i].lines_)));
231 for (SCM s = lines; s != SCM_EOL; s = ly_cdr (s))
232 unsmob_paper_line (ly_car (s))->number_ = ++i;
240 Page::page_count_ = 0;
241 Paper_def *paper = score_lines_[0].paper_;
242 Page *page = new Page (paper, 1);
244 Real text_height = page->text_height ();
246 Real copy_height = 0;
247 if (Stencil *s = unsmob_stencil (copyright_))
248 copy_height = s->extent (Y_AXIS).length ();
251 if (Stencil *s = unsmob_stencil (tagline_))
252 tag_height = s->extent (Y_AXIS).length ();
255 SCM proc = paper->c_variable ("page-breaking");
256 SCM breaks = scm_apply_0 (proc, scm_list_n (all, scm_make_real (height_),
257 scm_make_real (text_height),
258 scm_make_real (-copy_height),
259 scm_make_real (-tag_height),
262 /* Copyright on first page. */
263 if (unsmob_stencil (copyright_))
264 page->copyright_ = copyright_;
267 int page_count = SCM_VECTOR_LENGTH ((SCM) breaks);
269 for (int i = 0; i < page_count; i++)
272 page = new Page (paper, i + 1);
274 int next = i + 1 < page_count
275 ? ly_scm2int (scm_vector_ref (breaks, scm_int2num (i))) : 0;
276 while ((!next && all != SCM_EOL) || line <= next)
278 SCM s = ly_car (all);
279 page->lines_ = ly_snoc (s, page->lines_);
280 page->height_ += unsmob_paper_line (s)->dim ()[Y_AXIS];
285 pages = scm_cons (page->self_scm (), pages);
288 /* Tagline on last page. */
289 if (unsmob_stencil (tagline_))
290 page->tagline_ = tagline_;
292 return scm_reverse (pages);
296 c_ragged_page_breaks (SCM lines, Real book_height, Real text_height,
297 Real first, Real last)
300 int page_count = int (book_height / text_height + 0.5);
301 SCM breaks = SCM_EOL;
302 Real page_height = text_height + first;
305 for (SCM s = lines; s != SCM_EOL; s = ly_cdr (s))
307 Paper_line *pl = unsmob_paper_line (ly_car (s));
308 if (!pl->is_title () && h < page_height)
310 h += pl->dim ()[Y_AXIS];
311 if (!pl->is_title () && h > page_height)
313 breaks = ly_snoc (scm_int2num (number), breaks);
315 page_height = text_height + (page_number == page_count) * last;
318 if (ly_cdr (s) == SCM_EOL)
319 breaks = ly_snoc (scm_int2num (pl->number_), breaks);
322 return scm_vector (breaks);
325 LY_DEFINE (ly_ragged_page_breaks, "ly:ragged-page-breaks",
326 5, 0, 0, (SCM lines, SCM book, SCM text, SCM first, SCM last),
327 "Return a vector with line numbers of page breaks.")
329 SCM_ASSERT_TYPE (scm_pair_p (lines), lines, SCM_ARG1, __FUNCTION__, "list");
330 SCM_ASSERT_TYPE (ly_c_number_p (book), book, SCM_ARG2, __FUNCTION__, "real");
331 SCM_ASSERT_TYPE (ly_c_number_p (text), text, SCM_ARG2, __FUNCTION__, "real");
332 SCM_ASSERT_TYPE (ly_c_number_p (first), first, SCM_ARG2, __FUNCTION__, "real");
333 SCM_ASSERT_TYPE (ly_c_number_p (last), last, SCM_ARG2, __FUNCTION__, "real");
335 return c_ragged_page_breaks (lines,
336 ly_scm2double (book), ly_scm2double (text),
337 ly_scm2double (first), ly_scm2double (last));
340 /****************************************************************/
342 Score_lines::Score_lines ()
345 global_header_ = SCM_EOL;
351 Score_lines::gc_mark ()
353 scm_gc_mark (lines_);
354 scm_gc_mark (global_header_);
355 scm_gc_mark (header_);
357 scm_gc_mark (paper_->self_scm ());
362 Score_lines::scopes ()
364 SCM scopes = SCM_EOL;
366 scopes = scm_cons (header_, scopes);
367 if (SCM_MODULEP(global_header_)
368 && global_header_ != header_)
369 scopes = scm_cons (global_header_, scopes);