Logo Search packages:      
Sourcecode: passepartout version File versions  Download package

void Document::print ( std::ostream &  out,
int  first_page,
int  last_page,
bool  eps = false,
bool  include_fonts = false,
bool  grayscale = false 
) const

throws Error::Print

Definition at line 542 of file document.cc.

References os::fullname(), font::FontManager::getFontFile(), os::hostname(), os::machine(), os::release(), os::sysname(), font::FontManager::unalias(), and os::username().

{
  const font::FontManager &fm = font::FontManager::instance();
  
  // merge required fonts from all streams
  font::Fonts used_fonts;
  for(StreamVec::const_iterator j = text_streams.begin();
      j != text_streams.end();
      j++) {
    const font::Fonts &fonts = (*j)->get_used_fonts();
    for(font::Fonts::const_iterator i = fonts.begin();
      i != fonts.end();
      i++) {
      used_fonts.insert(*i);
    }
  }

  using std::endl;
  
  time_t the_time = std::time(0);

  if(!(first_page >= first_page_num 
       && first_page <= last_page
       && last_page <= first_page_num + int(get_num_of_pages()) - 1))
    throw Error::Print("Bad page interval");

  // ignore request to print multible-page EPS 
  eps = eps && first_page == last_page;

  int w = int(get_width() + 0.5);
  int h = int(get_height() + 0.5);
  
  if(eps) {
    out << "%!PS-Adobe-3.0 EPSF-3.0\n"
      << "%%BoundingBox: 0 0 " << w << " " << h << '\n';
  } else {
    out << "%!PS-Adobe-3.0\n";
  }

  
  //out << "%%DocumentData: Clean8Bit" << endl
  // <<"%%LanguageLevel: 2"<<endl
  // actually, we don't really know much about the eps files ...
  if(!eps) {
    // eps images should always be upright
    out << "%%Orientation: "
        << (orientation == Papers::PORTRAIT ? "Portrait\n" : "Landscape\n");
  }
  out << "%%Pages: " << get_num_of_pages() << '\n'
      << "%%PageOrder: Ascend\n"
    //      << "%%Title: " << basename(filename) << '\n'
      << "%%CreationDate: " << std::ctime(&the_time)
    // ctime seems to add a newline
      << "%%Creator: Passepartout " << std::string(VERSION)
      << " by Fredrik Arnerup & Rasmus Kaj\n";

  if(true) // perhaps the user is into cloak and dagger stuff ...
    out << "%%For: " << os::fullname()
      << " <" << os::username() << "@" << os::hostname() << ">"
      << " (" << os::machine() << ", " << os::sysname()
      << " " << os::release() << ")\n";

  out << "%%EndComments\n\n"
      << "%%BeginProlog\n";
  xml2ps::PsStream::psProlog(out);
  out << "%%EndProlog\n\n"
      << "%%BeginSetup" << endl;
  
  // Resource comments
  int line = 0;
  for(font::Fonts::const_iterator i = used_fonts.begin();
      i != used_fonts.end();
      i++) {
    out << (line++ ? "%%+ "
          : (include_fonts 
             ? "%%DocumentSuppliedResources: " 
             : "%%DocumentNeededResources: "))
      << "font " << fm.unalias(*i) << std::endl;
  }

  // %%IncludeResource comments
  if(!include_fonts) {
    for(font::Fonts::const_iterator i = used_fonts.begin();
      i != used_fonts.end();
      i++) {
      out << "%%IncludeResource: font " << fm.unalias(*i) << std::endl;
    }
  } else { // include fonts
    for(font::Fonts::const_iterator i = used_fonts.begin();
      i != used_fonts.end();
      i++) {
      std::string fontfile = fm.getFontFile(*i);
      if(fontfile.empty()) {
      warning << "Couldn't find font file for " << *i << std::endl;
      continue;
      }
      std::ifstream in(fontfile.c_str());
      if(!in) {
      warning << "Couldn't read " << fontfile << std::endl;
      continue;
      }
      out << "%%BeginResource: font " << fm.unalias(*i) << std::endl;
      std::string ext = suffix(fontfile);
      if(ext == "pfa") { // ascii
      out << in.rdbuf();
      
      } else if(ext == "pfb") {
      try {
        PS::pfb2pfa(in, out);
      } catch(const std::runtime_error &e) {
        warning << "error in " << fontfile 
              << " : " << e.what() << std::endl;
      }

      } else if(ext == "ttf") {
        PS::truetype2type42(fontfile, out);

      } else {
        out << "% bad fontfile: " << fontfile << std::endl;
        warning << "unknown font format \"" << ext << "\" in "
                << fontfile << std::endl;
      }
      out << "%%EndResource" << std::endl;
    }
  }

  out << "%%EndSetup" << endl
      << endl;
  
  int page_num = first_page_num;
  for(PageVec::const_iterator i = pages.begin(); i != pages.end(); i++) {
    if(page_num >= first_page && page_num <= last_page) {
      out << endl << "%%Page: \""
        <<(*i)->get_name()<<"\" "<<page_num<<endl;

      if(!eps && orientation == Papers::LANDSCAPE)
        out << "90 rotate 0 " << -h << " translate\n";

      // If we have a bounding box, we may not draw outside of it.
      // Shouldn't really be a problem, but one never knows ...
      if(eps)
      out << "\ngsave\n"
          << "0 0 moveto "            << w << " 0 rlineto\n"
          << "0 " << h <<" rlineto "  << -w << " 0 rlineto\n"
          << "closepath clip newpath\n\n";
      //clip doesn't make implicit newpath

      (*i)->print(out, grayscale);

      if(eps)
      out << "\ngrestore\n";
    }
    page_num++;
  }
  out << "%%EOF" << endl;
}


Generated by  Doxygen 1.6.0   Back to index