]> git.donarmstrong.com Git - lilypond.git/blobdiff - lily/p-score.cc
release: 1.0.1
[lilypond.git] / lily / p-score.cc
index 0e093c9693a55ec6e849ec987ea503aef74c517a..bd7ec060e049239b06322958e4018f8400776c38 100644 (file)
 /*
-  p-score.cc -- implement PScore
+  p-score.cc -- implement Paper_score
 
   source file of the GNU LilyPond music typesetter
 
-  (c) 1996, 1997 Han-Wen Nienhuys <hanwen@stack.nl>
+  (c) 1996,  1997--1998 Han-Wen Nienhuys <hanwen@cs.uu.nl>
 */
 
-#include "super-elem.hh"
+#include "main.hh"
+#include "super-element.hh"
 #include "debug.hh"
 #include "lookup.hh"
 #include "spanner.hh"
 #include "paper-def.hh"
-#include "scoreline.hh"
+#include "line-of-score.hh"
+#include "pcursor.hh"
+#include "plist.hh"
+#include "p-col.hh"
 #include "p-score.hh"
 #include "tex-stream.hh"
-#include "word-wrap.hh"
 #include "p-col.hh"
+#include "header.hh"
+#include "word-wrap.hh"
+#include "gourlay-breaking.hh"
+#include "tex-outputter.hh"
+#include "file-results.hh"
+#include "misc.hh"
 
-PScore::PScore(Paper_def*p)
-{
-    paper_l_ = p;
-    super_elem_l_   = new Super_elem;
-    typeset_element(super_elem_l_);
-}
+// sucking Cygnus egcs - w32
+#include "list.tcc"
+#include "cursor.tcc"
 
-PScore::~PScore()
+Paper_score::Paper_score ()
 {
-    super_elem_l_->unlink_all();
+  outputter_l_ =0;
+  super_elem_l_   = new Super_elem;
+  typeset_element (super_elem_l_);
 }
 
-void
-PScore::typeset_element(Score_elem * elem_p)
+Paper_score::~Paper_score ()
 {
-    elem_p_list_.bottom().add(elem_p);
-    elem_p->pscore_l_ = this;
-
-    elem_p->add_processing();
+  super_elem_l_->unlink_all ();
+  for (PCursor<Score_element*> i(elem_p_list_.top()); i.ok(); i++)
+    assert(!i->linked_b());
 }
 
 void
-PScore::typeset_item(Item *i, PCol *c, int breakstat)
+Paper_score::typeset_element (Score_element * elem_p)
 {
-    assert(c && i);
-
-    if (breakstat == -1) {
-       typeset_item(i, c->prebreak_p_);
-       return;
-    }
-
-    if (breakstat == 1) {
-       typeset_item(i, c->postbreak_p_);
-       return;
-    }
+  elem_p_list_.bottom ().add (elem_p);
+  elem_p->pscore_l_ = this;
 
-    c->add(i);
-    typeset_element(i);
+  elem_p->add_processing ();
 }
 
 void
-PScore::typeset_broken_spanner(Spanner*span_p)
+Paper_score::typeset_broken_spanner (Spanner*span_p)
 {
-    span_p->left_col_l_->starters.bottom().add (span_p);
-    span_p->right_col_l_->stoppers.bottom().add(span_p);
-    assert(span_p->left_col_l_->line_l_ == span_p->right_col_l_->line_l_);
-
-    typeset_element(span_p);
+  typeset_element (span_p);
 }
 
 
 void
-PScore::typeset_unbroken_spanner(Spanner*span_p)
+Paper_score::typeset_unbroken_spanner (Spanner*span_p)
 {
-    span_p_list_.bottom().add(span_p);
-    span_p->pscore_l_=this;
+  span_p_list_.bottom ().add (span_p);
+  span_p->pscore_l_=this;
 
-    if (span_p->left_col_l_) 
-       span_p->left_col_l_->used_b_ = true;
-    if ( span_p->right_col_l_)
-       span_p->right_col_l_->used_b_ = true;
-    
-    // do not init start/stop fields. These are for broken spans only.
-    span_p->add_processing();
+  // do not init start/stop fields. These are for broken spans only.
+  span_p->add_processing ();
 }
 
 
 void
-PScore::clean_cols()
-{
-    int rank_i = 0;
-    for (iter_top(col_p_list_,c); c.ok(); )
-       if (!c->used_b()) {
-           delete c.remove_p();
-       } else {
-           c->set_rank(rank_i++);
-           c++;
-       }
-}
-
-void
-PScore::add(PCol *p)
+Paper_score::clean_cols ()
 {
-    p->pscore_l_ = this;
-    if (p->breakable_b()){
-       p->prebreak_p_->pscore_l_ = this;
-       p->postbreak_p_->pscore_l_ = this;
+  int rank_i = 0;
+  for (iter_top (col_p_list_,c); c.ok ();)
+    {
+      c->set_rank (rank_i++);
+      c++;
     }
-    col_p_list_.bottom().add(p);
 }
 
 void
-PScore::output(Tex_stream &ts)
+Paper_score::add_column (Paper_column *p)
 {
-    ts << "\n "<<  paper_l_->lookup_l()->texsetting << "%(Tex id)\n";
-    ts<< super_elem_l_->TeX_string();
-    ts << "\n\\EndLilyPondOutput";
+  col_p_list_.bottom ().add (p);
+  typeset_element(p);
 }
 
 
+
 void
-PScore::OK()const
+Paper_score::print () const
 {
-#ifndef NDEBUG
-    for (iter_top(col_p_list_,cc); cc.ok(); cc++)
-       cc->OK();
+#ifndef NPRINT
+  if (!check_debug)
+    return ;
+  DOUT << "Paper_score { ";
+  DOUT << "\n elements: ";
+  for (iter_top (elem_p_list_,cc); cc.ok (); cc++)
+    cc->print ();
+  DOUT << "\n unbroken spanners: ";
+  for (iter (span_p_list_.top (), i); i.ok  (); i++)
+    i->print ();
+
+  DOUT << "}\n";
 #endif
 }
 
-void
-PScore::print() const
-{    
-#ifndef NPRINT
-    mtor << "PScore { ";
-    paper_l_->print();
-    mtor << "\n elements: ";
-    for (iter_top(elem_p_list_,cc); cc.ok(); cc++)     
-       cc->print();
-    mtor << "\n unbroken spanners: ";
-    for (iter(span_p_list_.top(), i); i.ok(); i++)
-       i->print();
-    mtor << "\ncolumns: ";
-     for (iter_top(col_p_list_,cc); cc.ok(); cc++)
-       cc->print();
-    
-    mtor << "}\n";
-#endif 
+PCursor<Paper_column *>
+Paper_score::find_col (Paper_column const *c) const
+{
+  Paper_column const *what = c;
+
+  return col_p_list_.find ((Paper_column*)what);
 }
 
+
 void
-PScore::preprocess()
+Paper_score::set_breaking (Array<Column_x_positions> const &breaking)
 {
-    super_elem_l_->breakable_col_processing();
-    super_elem_l_->pre_processing();
+  super_elem_l_->line_of_score_l_->set_breaking (breaking);
+  super_elem_l_->break_processing ();
+
+  for (iter (span_p_list_.top (),i); i.ok  ();)
+    {
+      Spanner *span_p = i.remove_p ();
+      if (span_p->broken_b ()
+         || !((Score_element*)span_p)->line_l ())
+       {
+         span_p->unlink ();
+#if 0
+         int sz = span_p->class_size();
+         set_frobnify (span_p,sz);
+#endif
+         delete span_p;
+       }
+      else 
+       {
+         typeset_broken_spanner (span_p);
+       }
+    }
+  for (iter (elem_p_list_.top (),i); i.ok  () ;)
+    {
+      Item *i_l =i->access_Item ();
+      if (i_l && !i_l->line_l ())
+       {
+         i_l->unlink ();
+         Score_element * item_p= i.remove_p ();
+#if 0
+         int sz = item_p->class_size ();
+         set_frobnify (item_p, sz);
+#endif
+         delete item_p;
+       }
+      else
+       i++;
+    }
 }
 
 void
-PScore::postprocess()
+Paper_score::calc_breaking ()
 {
-    super_elem_l_->post_processing();
-    super_elem_l_->molecule_processing();
+  Break_algorithm *algorithm_p=0;
+  Array<Column_x_positions> sol;
+  bool try_wrap = ! paper_l_->get_var ("castingalgorithm");
+
+  if (!try_wrap)
+    {
+      algorithm_p = new Gourlay_breaking ;
+      algorithm_p->set_pscore (this);
+      sol = algorithm_p->solve ();
+      delete algorithm_p;
+      if (! sol.size ())
+       {
+         warning (_ ("Can't solve this casting problem exactly; revert to Word_wrap"));
+         try_wrap = true;
+       }
+    }
+  if  (try_wrap)
+    {
+      algorithm_p = new Word_wrap;
+      algorithm_p->set_pscore (this);
+      sol = algorithm_p->solve ();
+      delete algorithm_p;
+    }
+  set_breaking (sol);
 }
 
-PCursor<PCol *>
-PScore::find_col(PCol const *c)const
+void
+Paper_score::process ()
 {
-    PCol const *what = c;
-    if (what->daddy_l_ )
-       what = what->daddy_l_;
-    
-    return col_p_list_.find((PCol*)what);
+  clean_cols ();
+  print ();
+  *mlog << _ ("Preprocessing elements...") << " " << flush;
+  super_elem_l_->breakable_col_processing ();
+  super_elem_l_->pre_processing ();
+  
+  *mlog << '\n' << _ ("Calculating column positions...") << " " << flush;
+  super_elem_l_->space_processing ();
+  calc_breaking ();
+  print ();
+  *mlog << _ ("Postprocessing elements...") << " " << endl;
+  super_elem_l_->post_processing ();
+  tex_output ();
 }
 
 
 void
-PScore::set_breaking(Array<Col_hpositions> const &breaking)
+Paper_score::tex_output ()
 {
-    super_elem_l_->line_of_score_l_->set_breaking( breaking);
-    super_elem_l_->break_processing();
-
-
-    for (iter(span_p_list_.top(),i); i.ok(); ) {
-       Spanner *span_p = i.remove_p();
-       if (span_p->broken_b()) {
-           span_p->unlink();
-           delete span_p;
-       }else{
-           typeset_broken_spanner(span_p);
+  // output
+  String base_outname = paper_l_->outfile_str_ ;
+  if (base_outname.empty_b ())
+    {
+      base_outname = default_outname_base_global;
+      int def = paper_l_->get_next_default_count ();
+      if (def)
+       {
+         base_outname += "-" + to_str (def);
        }
     }
-    for (iter(elem_p_list_.top(),i ); i.ok() ;) {
-       Item *i_l =i->item();
-       if ( i_l && !i_l->pcol_l_->line_l_) {
-           i_l->unlink();
-           delete i.remove_p();
-       } else
-           i++;
-    }
 
-    for (iter_top(col_p_list_, i); i.ok(); i++)
-       i->clean_breakable_items();
-}
+  String outname = base_outname;
+  if (outname != "-")
+     outname += ".tex";
+  target_str_global_array.push (outname);
 
-void
-PScore::calc_breaking()
-{
-    Word_wrap w(*this);
+  *mlog << _f ("TeX output to %s...", 
+    outname == "-" ? String ("<stdout>") : outname ) << endl;
 
-    set_breaking(w.solve());
-}
+  Tex_stream tex_out (outname);
+  Tex_outputter interfees (&tex_out);
 
-void
-PScore::process()
-{
-    clean_cols();
-    print();
-    *mlog << "Preprocessing elements... " <<flush;
-    preprocess();
-    *mlog << "\nCalculating column positions ... " <<flush;
-    calc_breaking();
-    *mlog << "\nPostprocessing elements..." << endl;
-    postprocess();
+  outputter_l_ = &interfees;
+
+  if (header_global_p)
+    {
+      tex_out << header_global_p->TeX_string ();
+    }
+    
+  
+  tex_out << _ ("% outputting Score, defined at: ") << origin_str_ << '\n';
+
+  if (header_l_)
+    {
+      tex_out << header_l_->TeX_string();
+    }
+  tex_out << paper_l_->TeX_output_settings_str ();
+  
+
+  if (experimental_features_global_b)
+    tex_out << "\\turnOnExperimentalFeatures%\n";
+
+  tex_out << "\\turnOnPostScript%\n";
+  super_elem_l_->output_all ();
+  tex_out << "\n\\EndLilyPondOutput";
+  outputter_l_ = 0;
 }
 
 /** Get all breakable columns between l and r, (not counting l and r).  */
-Link_array<PCol>
-PScore::breakable_col_range(PCol*l,PCol*r)const
+Link_array<Paper_column>
+Paper_score::breakable_col_range (Paper_column*l, Paper_column*r) const
 {
-    Link_array<PCol> ret;
-
-    PCursor<PCol*> start(l ? find_col(l)+1 : col_p_list_.top() );
-    PCursor<PCol*> stop(r ? find_col(r) : col_p_list_.bottom());
-
-    /*
-      ugh! windows-suck-suck-suck.
-     */
-    while ( PCursor<PCol*>::compare(start,stop) < 0 ) {
-       if (start->breakable_b())
-           ret.push(start);
-       start++;
+  Link_array<Paper_column> ret;
+
+  PCursor<Paper_column*> start (l ? find_col (l)+1 : col_p_list_.top ());
+  PCursor<Paper_column*> stop (r ? find_col (r) : col_p_list_.bottom ());
+
+  /*
+    ugh! windows-suck-suck-suck.
+    */
+  while (PCursor<Paper_column*>::compare (start,stop) < 0)
+    {
+      if (start->breakable_b_)
+       ret.push (start);
+      start++;
     }
 
-    return ret;
+  return ret;
 }
-Link_array<PCol>
-PScore::col_range(PCol*l,PCol*r)const
+
+
+Link_array<Paper_column>
+Paper_score::col_range (Paper_column*l, Paper_column*r) const
 {
-    Link_array<PCol> ret;
-    
-    PCursor<PCol*> start(l ? find_col(l)+1 : col_p_list_.top() );
-    PCursor<PCol*> stop(r ? find_col(r) : col_p_list_.bottom());
-    ret.push(l);
-    
-    /*
-      ugh! windows-suck-suck-suck.
-     */
-    while ( PCursor<PCol*>::compare(start,stop) < 0 )
-       ret.push(start++);
-    ret.push(r);
-    return ret;
+  Link_array<Paper_column> ret;
+
+  PCursor<Paper_column*> start (l ? find_col (l)+1 : col_p_list_.top ());
+  PCursor<Paper_column*> stop (r ? find_col (r) : col_p_list_.bottom ());
+  ret.push (l);
+
+  /*
+    ugh! windows-suck-suck-suck.
+    */
+  while (PCursor<Paper_column*>::compare (start,stop) < 0)
+    ret.push (start++);
+  ret.push (r);
+  return ret;
 }
 
-Link_array<PCol>
-PScore::broken_col_range(PCol*l,PCol*r)const
+Link_array<Item>
+Paper_score::broken_col_range (Item const*l_item_l, Item const*r_item_l) const
 {
-    Link_array<PCol> ret;
-
-    PCursor<PCol*> start(l ? find_col(l)+1 : col_p_list_.top() );
-    PCursor<PCol*> stop(r ? find_col(r) : col_p_list_.bottom());
-  
-    /*
-      ugh! windows-suck-suck-suck.
-      */
-    while ( PCursor<PCol*>::compare(start,stop) < 0 ) {
-       if (start->breakable_b() && !start->line_l_ )
-           ret.push(start);
-       start++;
+  Link_array<Item> ret;
+  Item const*l=l_item_l;
+  Item const*r=r_item_l;
+
+  while (! l->is_type_b(Paper_column::static_name ()))
+    l = l->axis_group_l_a_[X_AXIS]->access_Score_element ()->access_Item ();
+
+  while (! r->is_type_b(Paper_column::static_name ()))
+    r = r->axis_group_l_a_[X_AXIS]->access_Score_element ()->access_Item ();
+
+  PCursor<Paper_column*> start (l ? find_col ((Paper_column*)l)+1 : col_p_list_.top ());
+  PCursor<Paper_column*> stop (r ? find_col ((Paper_column*)r) : col_p_list_.bottom ());
+
+  /*
+    ugh! windows-suck-suck-suck.
+    */
+  while (PCursor<Paper_column*>::compare (start,stop) < 0)
+    {
+      if (start->breakable_b_ && !start->line_l_)
+       ret.push (start);
+      start++;
     }
 
-    return ret;
+  return ret;
 }