]> git.donarmstrong.com Git - lilypond.git/blobdiff - lily/p-score.cc
release: 1.1.28
[lilypond.git] / lily / p-score.cc
index 184e90db3f4fe166d7e0f05033f18ea838c3860f..f7b4238de813b3a9a109178e72d999c89b499e40 100644 (file)
-#include "idealspacing.hh"
+/*
+  p-score.cc -- implement Paper_score
+
+  source file of the GNU LilyPond music typesetter
+
+  (c) 1996,  1997--1998 Han-Wen Nienhuys <hanwen@cs.uu.nl>
+*/
+
+#include "main.hh"
 #include "debug.hh"
 #include "lookup.hh"
 #include "spanner.hh"
 #include "paper-def.hh"
-#include "molecule.hh"
-#include "dimen.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 "item.hh"
-#include "break.hh"
+#include "p-col.hh"
+#include "scope.hh"
+#include "word-wrap.hh"
+#include "gourlay-breaking.hh"
+#include "paper-stream.hh"
+#include "paper-outputter.hh"
+#include "file-results.hh"
+#include "misc.hh"
+
+#if 1                  // has w32 advanced?
+                       // nope (cygwin-b20)
+
+// sucking Cygnus egcs - w32
+#include "list.tcc"
+#include "cursor.tcc"
 
-Idealspacing*
-PScore::get_spacing(PCol*l, PCol*r)
-{
-    assert(l!=r);
+#endif
 
-    Idealspacing*i_p =new Idealspacing(l,r);
-    suz.bottom().add(i_p);
+Paper_score::Paper_score ()
+{
+  outputter_l_ =0;
+  Line_of_score * line_p = new Line_of_score;
+  typeset_unbroken_spanner (line_p);
 
-    return i_p;
+  line_l_ = line_p;
 }
 
+Paper_score::~Paper_score ()
+{
+}
 
 void
-PScore::clean_cols()
+Paper_score::typeset_element (Score_element * elem_p)
 {
-    int rank_i = 0;
-    for (iter_top(cols,c); c.ok(); )
-       if (!c->used_b()) {
-           delete c.remove_p();
-       } else {
-           c->set_rank(rank_i++);
-           c++;
-       }
-    
+  elem_p_list_.bottom ().add (elem_p);
+  elem_p->pscore_l_ = this;
+  elem_p->add_processing ();
 }
 
-
 void
-PScore::add(PStaff *s)
+Paper_score::typeset_broken_spanner (Spanner*span_p)
 {
-    assert(s->pscore_l_ == this);
-    staffs.bottom().add(s);
+  typeset_element (span_p);
 }
 
 
 void
-PScore::do_connect(PCol *c1, PCol *c2, Real d, Real h)
+Paper_score::typeset_unbroken_spanner (Spanner*span_p)
 {
-    if (!c1 || !c2 )
-       return;
-    Idealspacing*s_l=get_spacing(c1,c2);
+  span_p_list_.bottom ().add (span_p);
+  span_p->pscore_l_=this;
 
-    
-    s_l->hooke = h;
-    s_l->space =d;
+  // do not init start/stop fields. These are for broken spans only.
+  span_p->add_processing ();
 }
 
-void
-PScore::connect(PCol* c1, PCol *c2, Real d, Real h)
-{
-    do_connect(c1,c2,d,h);
-    do_connect(c1->postbreak_p_, c2,d,h);
-    do_connect(c1, c2->prebreak_p_,d,h);
-    do_connect(c1->postbreak_p_, c2->prebreak_p_,d,h);
-}
 
 void
-PScore::typeset_item(Item *i, PCol *c, PStaff *s, int breakstat)
+Paper_score::clean_cols ()
 {
-    assert(c && i && s);
-
-    if (breakstat == 0) {
-       typeset_item(i, c->prebreak_p_, s);
-       return;
+  int rank_i = 0;
+  for (iter_top (col_p_list_,c); c.ok ();)
+    {
+      c->set_rank (rank_i++);
+      c++;
     }
-
-    if (breakstat == 2) {
-       typeset_item(i, c->postbreak_p_, s);
-       return;
-    }
-
-
-    its.bottom().add(i);
-    s->add(i);
-    c->add(i);
-
-    /* first do this, because i->width() may follow the 0-pointer */
-    i->add_processing();    
 }
 
 void
-PScore::typeset_spanner(Spanner*span_p, PStaff*ps)
+Paper_score::add_column (Paper_column *p)
 {
-    span_p->pstaff_l_ = ps;
-    spanners.bottom().add(span_p);
-    ps->spans.bottom().add(span_p);
-
-    // do not init start/stop fields. These are for broken spans only.
-    span_p->add_processing();
+  col_p_list_.bottom ().add (p);
+  typeset_element(p);
 }
 
 
+
 void
-PScore::add(PCol *p)
+Paper_score::print () const
 {
-    p->pscore_l_ = this;
-    if (p->breakable_b()){
-       p->prebreak_p_->pscore_l_ = this;
-       p->postbreak_p_->pscore_l_ = this;
-    }
-    cols.bottom().add(p);
+#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
 }
 
-PScore::PScore( Paper_def*p)
+PCursor<Paper_column *>
+Paper_score::find_col (Paper_column const *c) const
 {
-    paper_l_ = p;
-}
+  Paper_column const *what = c;
 
-void
-PScore::output(Tex_stream &ts)
-{
-    int l=1;
-
-    ts << "\n "<<  paper_l_->lookup_l()->texsetting << "%(Tex id)\n";
-    for (iter_top(lines,lic); lic.ok(); lic++) {
-       ts << "% line of score no. " << l++ <<"\n";
-       ts << lic->TeXstring();
-       if ((lic+1).ok())
-           ts << "\\interscoreline\n";
-    }  
-    ts << "\n\\EndLilyPondOutput";
+  return col_p_list_.find ((Paper_column*)what);
 }
 
 
-Array<Item*>
-PScore::select_items(PStaff*ps, PCol*pc)
+
+
+Array<Column_x_positions>
+Paper_score::calc_breaking ()
 {
-    Array<Item*> ret;
-    assert(ps && pc);
-    for (iter_top(pc->its,i); i.ok(); i++){
-       if (i->pstaff_l_ == ps)
-           ret.push((Item*)(Item const *)i);
+  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;
     }
-    return ret;
+  return sol;
 }
 
-void
-PScore::OK()const
-{
-#ifndef NDEBUG
-    for (iter_top(cols,cc); cc.ok(); cc++)
-       cc->OK();
-    for (iter_top(suz,i); i.ok(); i++)
-       i->OK();
-#endif
-}
 
-void
-PScore::print() const
-{    
-#ifndef NPRINT
-    mtor << "PScore { ";
-    paper_l_->print();
-    mtor << "\ncolumns: ";
-    for (iter_top(cols,cc); cc.ok(); cc++)
-       cc->print();
-    
-    mtor << "\nideals: ";
-    for (iter_top(suz,i); i.ok(); i++)
-       i->print();
-    mtor << "}\n";
-#endif 
-}
 
 void
-PScore::preprocess()
+Paper_score::process ()
 {
-    for (iter_top(spanners,i); i.ok(); i++) {
-       i->pre_processing();
+  clean_cols ();
+  print ();
+  *mlog << _ ("Preprocessing elements...") << " " << flush;
+      line_l_->breakable_col_processing ();
+      line_l_->pre_processing ();
+  
+      *mlog << '\n' << _ ("Calculating column positions...") << " " << flush;
+      line_l_->space_processing ();
+
+  Array<Column_x_positions> breaking = calc_breaking ();
+
+  Paper_stream* paper_stream_p = paper_l_->paper_stream_p ();
+  outputter_l_ = paper_l_->paper_outputter_p (paper_stream_p, header_l_, origin_str_);
+
+  Link_array<Line_of_score> lines;
+  for (int i=0; i < breaking.size (); i++)
+    {
+      Line_of_score *line_l = line_l_->set_breaking (breaking, i);
+      lines.push (line_l);
+      if (line_l != line_l_)
+       typeset_broken_spanner (line_l);
+      
     }
-    for (iter_top(its,i); i.ok(); i++){
-       i->pre_processing();
+
+  *mlog << "\nLine ... ";
+  for (int i=0; i < lines.size (); i++)
+    {
+      *mlog << '[' << flush;
+      
+      Line_of_score *line_l = lines[i];
+      line_l->break_processing ();
+      line_l->post_processing ();
+       *mlog << i << flush;
+      line_l->output_all ();
+       *mlog << ']' << flush;
+      remove_line (line_l);
     }
+  
+  // huh?
+  delete outputter_l_;
+  delete paper_stream_p;
+  outputter_l_ = 0;
+
+  *mlog << '\n' << flush;
 }
 
 void
-PScore::postprocess()
+Paper_score::remove_line (Line_of_score *l)
 {
-    for (iter_top(broken_spans,i); i.ok(); i++) { // could chase spans as well.
-       i->post_processing();
-    }
-    for (iter_top(its,i); i.ok(); i++){
-       i->post_processing();
-    }
-    
-    for (iter_top(broken_spans,i); i.ok(); i++) {
-       i->molecule_processing();
-    }
-    for (iter_top(its,i); i.ok(); i++){
-       i->molecule_processing();
+  Link_array<Score_element> to_remove;
+  for (PCursor<Score_element*> i(elem_p_list_.top ()); i.ok (); )
+    {
+      if (i->line_l () == l)
+       to_remove.push (i.remove_p ());
+      else
+       i++;
     }
 
-    for (iter_top(lines,i); i.ok(); i++)
-       i->process();
+  for (PCursor<Spanner*> i (span_p_list_.top ()); i.ok (); )
+    {
+      Score_element *e = i.ptr ();
+      if (e->line_l () == l)
+       to_remove.push (i.remove_p ());
+      else
+       i++;
+    }
 
+  //  l->unlink_all ();
+  for (int i=0; i < to_remove.size (); i++)
+    {
+      to_remove[i]->unlink ();
+      assert (!to_remove[i]->linked_b ());
+      delete to_remove [i];
+    }
 }
 
-PCursor<PCol *>
-PScore::find_col(PCol const *c)const
+/** Get all breakable columns between l and r, (not counting l and r).  */
+Link_array<Paper_column>
+Paper_score::breakable_col_range (Paper_column*l, Paper_column*r) const
 {
-    PCol const *what = c;
-    if (what->daddy_l_ )
-       what = what->daddy_l_;
-    
-    return cols.find((PCol*)what);
-}
+  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++;
+    }
 
-void
-PScore::add_broken(Spanner*s)
-{
-    assert(s->left_col_l_->line_l_ == s->right_col_l_->line_l_);
-    broken_spans.bottom().add(s);
-    s->left_col_l_->starters.bottom().add (s);
-    s->right_col_l_->stoppers.bottom().add (s);
+  return ret;
 }
 
-void
-PScore::set_breaking(Array<Col_hpositions> breaking)
-{
-    for (int j=0; j < breaking.size(); j++) {
-       Array<PCol*> &curline(breaking[j].cols);
-       Array<Real> &config(breaking[j].config);
-       
-       Line_of_score *s_p = new Line_of_score(curline,this);
-       lines.bottom().add(s_p);        
-       for (int i=0; i < curline.size(); i++){
-           curline[i]->hpos = config[i];
-       }
-    }
-}
 
-void
-PScore::calc_breaking()
+Link_array<Paper_column>
+Paper_score::col_range (Paper_column*l, Paper_column*r) const
 {
-    Word_wrap w(*this);
-    set_breaking(w.solve());
+  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;
 }
 
-void
-PScore::process()
+Link_array<Item>
+Paper_score::broken_col_range (Item const*l_item_l, Item const*r_item_l) const
 {
-    clean_cols();
-    print();
-    *mlog << "Preprocessing elements... " <<flush;
-    preprocess();
-    *mlog << "\nCalculating column positions ... " <<flush;
-    calc_breaking();
-    *mlog << "\nPostprocessing elements..." << endl;
-    postprocess();
+  Link_array<Item> ret;
+  Item const*l=l_item_l;
+  Item const*r=r_item_l;
+
+  // huh? see Item::left_right_compare ()
+  /*
+  while (! (dynamic_cast<Paper_column const *> (l)))
+    l = dynamic_cast<Item*> (l->axis_group_l_a_[X_AXIS]);
+
+  while (! (dynamic_cast<Paper_column const *> (r)))
+    r = dynamic_cast<Item*>(r->axis_group_l_a_[X_AXIS]);
+  */
+  l = l->column_l ();
+  r = r->column_l ();
+  
+  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;
 }