]> git.donarmstrong.com Git - lilypond.git/blobdiff - lily/paper-book.cc
* lily/ly-module.cc (LY_DEFINE): bugfix.
[lilypond.git] / lily / paper-book.cc
index c4cc819c96c8fcde7eb0b32cb1a69ada330385ec..dfbb55d6e5799df91d4208aa7ac4bb8a8b83de9c 100644 (file)
 #include "main.hh"
 #include "page.hh"
 #include "paper-book.hh"
-#include "paper-def.hh"
+#include "output-def.hh"
 #include "paper-outputter.hh"
 #include "paper-line.hh"
 #include "paper-score.hh"
 #include "stencil.hh"
 #include "warn.hh"
-#include "book-paper-def.hh"
+
 
 // JUNKME
 SCM
@@ -37,8 +37,12 @@ stencil2line (Stencil stil, bool is_title = false)
 
 Paper_book::Paper_book ()
 {
+  pages_ = SCM_EOL;
+  lines_ = SCM_EOL;
   copyright_ = SCM_EOL;
   tagline_ = SCM_EOL;
+  header_ = SCM_EOL;
+  
   bookpaper_ = 0;
   smobify_self ();
 }
@@ -63,7 +67,9 @@ Paper_book::mark_smob (SCM smob)
   scm_gc_mark (b->copyright_);
   if (b->bookpaper_)
     scm_gc_mark (b->bookpaper_->self_scm ());
-
+  scm_gc_mark (b->header_);
+  scm_gc_mark (b->pages_);
+  scm_gc_mark (b->lines_);
   return b->tagline_;
 }
 
@@ -80,6 +86,45 @@ Paper_book::print_smob (SCM smob, SCM port, scm_print_state*)
   return 1;
 }
 
+Array<String>
+split_string (String s, char c)
+{
+  Array<String> rv; 
+  while (s.length ())
+    {
+      int i = s.index (c);
+      
+      if (i == 0)
+       {
+         s = s.nomid_string (0, 1);
+         continue;
+       }
+      
+      if (i < 0)
+       i = s.length () ;
+
+      rv.push (s.cut_string (0, i));
+      s = s.nomid_string (0, i);
+    }
+
+  return rv;
+}
+
+SCM
+dump_fields ()
+{
+  SCM fields = SCM_EOL;
+  for (int i = dump_header_fieldnames_global.size (); i--; )
+    fields
+      = scm_cons (ly_symbol2scm (dump_header_fieldnames_global[i].to_str0 ()),
+                 fields);
+  return fields;
+}
+
+/*
+  TODO: there is too much code dup, and the interface is not
+  clear. FIXME.
+ */
 void
 Paper_book::output (String outname)
 {
@@ -88,90 +133,170 @@ Paper_book::output (String outname)
     return;
     
   /* Generate all stencils to trigger font loads.  */
-  SCM pages = this->pages ();
+  pages ();
 
-  Paper_def *paper = score_lines_[0].paper_;
-  Paper_outputter *out = paper->get_paper_outputter (outname);
-  int page_count = scm_ilength (pages);
-  out->output_header (paper->bookpaper_, scopes (0), page_count, false);
+  Array<String> output_formats = split_string (output_format_global, ',');
 
-  for (SCM s = pages; s != SCM_EOL; s = ly_cdr (s))
+  for (int i = 0; i < output_formats.size (); i++)
     {
-      Page *p = unsmob_page (ly_car (s));
-      progress_indication ("[" + to_string (p->number_));
-      out->output_page (p, ly_cdr (s) == SCM_EOL);
-      progress_indication ("]");
+      String format = output_formats[i];
+      Paper_outputter *out = get_paper_outputter (outname + "." + output_formats[i], format);
+      
+      
+
+  
+      SCM scopes = SCM_EOL;
+      if (ly_c_module_p (header_))
+       scopes = scm_cons (header_, scopes);
+  
+      String func_nm = output_format_global;
+      func_nm = "output-framework-" + func_nm;
+       
+      SCM func = ly_scheme_function (func_nm.to_str0 ());
+      scm_apply_0 (func, scm_list_n (out->self_scm (),
+                                    self_scm (),
+                                    scopes,
+                                    dump_fields (),
+                                    scm_makfrom0str (outname.to_str0 ()),
+                                    SCM_UNDEFINED
+                                    )) ;
+
+      scm_gc_unprotect_object (out->self_scm ());
     }
+}
+
+
+void
+Paper_book::classic_output (String outname)
+{
+  String format = "tex";
+  Paper_outputter *out = get_paper_outputter (outname + "." + format, format);
+
+  /* Generate all stencils to trigger font loads.  */
+  lines ();
+
+
+  // ugh code dup
+  SCM scopes = SCM_EOL;
+  if (ly_c_module_p (header_))
+    scopes = scm_cons (header_, scopes);
+
+  if (ly_c_module_p (score_lines_[0].header_))
+    scopes = scm_cons (score_lines_[0].header_, scopes);
+  //end ugh
+  
+  String func_nm = output_format_global;
+  func_nm = "output-classic-framework-" + func_nm;
+      
+  SCM func = ly_scheme_function (func_nm.to_str0 ());
+  scm_apply_0 (func, scm_list_n (out->self_scm (),
+                                self_scm (),
+                                scopes,
+                                dump_fields (),
+                                scm_makfrom0str (outname.to_str0 ()),
+                                SCM_UNDEFINED
+                                )) ;
 
-  out->output_scheme (scm_list_1 (ly_symbol2scm ("end-output")));
   progress_indication ("\n");
 }
 
-SCM
-Paper_book::scopes (int i)
+
+
+
+LY_DEFINE(ly_paper_book_pages, "ly:paper-book-pages",
+         1,0,0,
+         (SCM pb),
+         "Return pages in book PB.")
 {
-  return score_lines_[i].scopes ();
+  return unsmob_paper_book(pb)->pages ();
 }
 
 
-Stencil
-Paper_book::title (int i)
+LY_DEFINE(ly_paper_book_lines, "ly:paper-book-lines",
+         1,0,0,
+         (SCM pb),
+         "Return lines in book PB.")
+{
+  return unsmob_paper_book (pb)->lines ();
+}
+
+
+LY_DEFINE(ly_paper_book_book_paper, "ly:paper-book-book-paper",
+         1,0,0,
+         (SCM pb),
+         "Return pages in book PB.")
 {
-  SCM user_title = bookpaper_->lookup_variable (ly_symbol2scm ("user-title"));
-  SCM book_title = bookpaper_->lookup_variable (ly_symbol2scm ("book-title"));
-  SCM score_title = bookpaper_->lookup_variable (ly_symbol2scm ("score-title"));
-  SCM field = (i == 0 ? ly_symbol2scm ("bookTitle")
-              : ly_symbol2scm ("scoreTitle"));
+  return unsmob_paper_book(pb)->bookpaper_->self_scm ();
+}
+
+/*
+
+TODO: resurrect more complex user-tweaks for titling .
 
+*/
+Stencil
+Paper_book::book_title ()
+{
+  SCM title_func = bookpaper_->lookup_variable (ly_symbol2scm ("book-title"));
   Stencil title;
-  SCM scopes = this->scopes (i);
-  SCM s = ly_modules_lookup (scopes, field);
-  if (s != SCM_UNDEFINED && scm_variable_bound_p (s) == SCM_BOOL_T)
-    title = *unsmob_stencil (scm_call_2 (user_title,
-                                        score_lines_[0].paper_->self_scm (),
-                                       scm_variable_ref (s)));
-  else
-    title = *unsmob_stencil (scm_call_2 (i == 0 ? book_title : score_title,
-                                        score_lines_[0].paper_->self_scm (),
-                                       scopes));
+
+  SCM scopes = SCM_EOL;
+  if (ly_c_module_p (header_))
+    scopes = scm_cons (header_, scopes);
+
+  SCM tit = SCM_EOL;
+  if (ly_c_procedure_p (title_func))
+    tit = scm_call_2 (title_func,
+                    bookpaper_->self_scm (),
+                    scopes);
+
+  if (unsmob_stencil (tit))
+    title = *unsmob_stencil (tit);
+
   if (!title.is_empty ())
     title.align_to (Y_AXIS, UP);
   
   return title;
 }
 
-void
-Paper_book::classic_output (String outname)
+  
+
+Stencil
+Paper_book::score_title (int i)
 {
-  int count = score_lines_.size ();
-  Paper_def * p = score_lines_.top ().paper_;
-  Paper_outputter *out = p->get_paper_outputter (outname);
-  out->output_header (p->bookpaper_, scopes (count - 1), 0, true);
-
-  SCM top_lines = score_lines_.top ().lines_;
-  Paper_line *first = unsmob_paper_line (scm_vector_ref (top_lines,
-                                                        scm_int2num (0)));
-  Offset o (0, -0.5 * first->dim ()[Y_AXIS]);
-  int line_count = SCM_VECTOR_LENGTH (top_lines);
-  for (int i = 0; i < line_count; i++)
-    {
-      /* In classic compatibility TeX tracks how large things are, and
-        advances the Y pos for us.  If we advance it too, we get too
-        much space.
+  SCM title_func = bookpaper_->lookup_variable (ly_symbol2scm ("score-title"));
 
-        FIXME: vague... why is TeX is different from other ouput
-               backends, why not fix the TeX backend? -- jcn */
-      if (output_format_global == "tex")
-       o = Offset (0, 0);
+  Stencil title;
 
-      out->output_line (scm_vector_ref (top_lines, scm_int2num (i)),
-                       &o, i == line_count - 1);
-    }
+  // ugh code dup
+  SCM scopes = SCM_EOL;
+  if (ly_c_module_p (header_))
+    scopes = scm_cons (header_, scopes);
+
+  if (ly_c_module_p (score_lines_[i].header_))
+    scopes = scm_cons (score_lines_[i].header_, scopes);
+  //end ugh
+
+  SCM tit = SCM_EOL;
+  if (ly_c_procedure_p (title_func))
+    tit =scm_call_2 (title_func,
+                    bookpaper_->self_scm (),
+                    scopes);
+
+  if (unsmob_stencil (tit))
+    title = *unsmob_stencil (tit);
+
+
+  if (!title.is_empty ())
+    title.align_to (Y_AXIS, UP);
   
-  out->output_scheme (scm_list_1 (ly_symbol2scm ("end-output")));
-  progress_indication ("\n");
+  return title;
 }
 
+  
+
+
 /* calculate book height, #lines, stencils.  */
 void
 Paper_book::init ()
@@ -181,9 +306,13 @@ Paper_book::init ()
   /* Calculate the full book height.  Hmm, can't we cache system
      heights while making stencils?  */
   height_ = 0;
+  Stencil btitle = book_title ();
+  if (!btitle.is_empty ())
+    height_ += btitle.extent (Y_AXIS).length ();
+  
   for (int i = 0; i < score_count; i++)
     {
-      Stencil title = this->title (i);
+      Stencil title = score_title (i);
       if (!title.is_empty ())
        height_ += title.extent (Y_AXIS).length ();
 
@@ -195,8 +324,11 @@ Paper_book::init ()
        }
     }
 
-  Paper_def *paper = score_lines_[0].paper_;
-  SCM scopes = this->scopes (0);
+  Output_def *paper = bookpaper_;
+  
+  SCM scopes = SCM_EOL;
+  if (ly_c_module_p (header_))
+    scopes = scm_cons (header_, scopes);
 
   SCM make_tagline = paper->c_variable ("make-tagline");
   tagline_ = scm_call_2 (make_tagline, paper->self_scm (), scopes);
@@ -216,29 +348,40 @@ Paper_book::init ()
 SCM
 Paper_book::lines ()
 {
+  if (ly_c_pair_p (lines_))
+    return lines_;
+
+  Stencil title = book_title ();      
+  if (!title.is_empty ())
+    lines_ = scm_cons (stencil2line (title, true), lines_);
+  
   int score_count = score_lines_.size ();
-  SCM lines = SCM_EOL;
   for (int i = 0; i < score_count; i++)
     {
-      Stencil title = this->title (i);      
+      Stencil title = score_title (i);      
       if (!title.is_empty ())
-       lines = ly_snoc (stencil2line (title, true), lines);
-      lines = scm_append (scm_list_2 (lines, scm_vector_to_list (score_lines_[i].lines_)));
+       lines_ = scm_cons (stencil2line (title, true), lines_);
+
+      SCM line_list = scm_vector_to_list (score_lines_[i].lines_); // guh.
+      lines_ = scm_append (scm_list_2 (scm_reverse (line_list), lines_));
     }
   
-  //debug helper; ughr
+  lines_ = scm_reverse (lines_);
+
   int i = 0;
-  for (SCM s = lines; s != SCM_EOL; s = ly_cdr (s))
+  for (SCM s = lines_; s != SCM_EOL; s = ly_cdr (s))
     unsmob_paper_line (ly_car (s))->number_ = ++i;
-  return lines;
+  return lines_;
 }
 
 SCM
 Paper_book::pages ()
 {
+  if (ly_c_pair_p (pages_))
+    return pages_;
+
   init ();
-  Page::page_count_ = 0;
-  Paper_def *paper = score_lines_[0].paper_;
+  Output_def *paper = bookpaper_;
   Page *page = new Page (paper, 1);
 
   Real text_height = page->text_height ();
@@ -263,9 +406,9 @@ Paper_book::pages ()
   if (unsmob_stencil (copyright_))
     page->copyright_ = copyright_;
 
-  SCM pages = SCM_EOL;
   int page_count = SCM_VECTOR_LENGTH ((SCM) breaks);
   int line = 1;
+
   for (int i = 0; i < page_count; i++)
     {
       if (i)
@@ -282,14 +425,18 @@ Paper_book::pages ()
          all = ly_cdr (all);
          line++;
        }
-      pages = scm_cons (page->self_scm (), pages);
+      if (i == page_count-1)
+       page->is_last_ = true;
+      
+      pages_ = scm_cons (page->self_scm (), pages_);
     }
 
   /* Tagline on last page.  */
   if (unsmob_stencil (tagline_))
     page->tagline_ = tagline_;
 
-  return scm_reverse (pages);
+  pages_ =  scm_reverse (pages_);
+  return pages_;
 }
 
 static SCM
@@ -342,31 +489,13 @@ LY_DEFINE (ly_ragged_page_breaks, "ly:ragged-page-breaks",
 Score_lines::Score_lines ()
 {
   lines_ = SCM_EOL;
-  global_header_ = SCM_EOL;
   header_ = SCM_EOL;
-  paper_ = 0;
 }
 
 void
 Score_lines::gc_mark ()
 {
   scm_gc_mark (lines_);
-  scm_gc_mark (global_header_);
   scm_gc_mark (header_);
-  if (paper_)
-    scm_gc_mark (paper_->self_scm ());
 }
 
-
-SCM 
-Score_lines::scopes ()
-{
-  SCM scopes = SCM_EOL;
-  if (header_)
-    scopes = scm_cons (header_, scopes);
-  if (SCM_MODULEP(global_header_)
-      && global_header_ != header_)
-    scopes = scm_cons (global_header_, scopes);
-
-  return scopes;
-}