src/lemon/graph_to_eps.h
changeset 1110 ba28dfbea5f2
parent 1107 d972653c89d5
child 1164 80bb73097736
equal deleted inserted replaced
8:e95d3521892e 9:f437ee16a485
    14  *
    14  *
    15  */
    15  */
    16 
    16 
    17 #ifndef LEMON_GRAPH_TO_EPS_H
    17 #ifndef LEMON_GRAPH_TO_EPS_H
    18 #define LEMON_GRAPH_TO_EPS_H
    18 #define LEMON_GRAPH_TO_EPS_H
       
    19 
       
    20 #include <sys/time.h>
       
    21 #include <time.h>
    19 
    22 
    20 #include<iostream>
    23 #include<iostream>
    21 #include<fstream>
    24 #include<fstream>
    22 #include<sstream>
    25 #include<sstream>
    23 #include<algorithm>
    26 #include<algorithm>
   117   
   120   
   118   bool _undir;
   121   bool _undir;
   119   bool _pleaseRemoveOsStream;
   122   bool _pleaseRemoveOsStream;
   120 
   123 
   121   bool _scaleToA4;
   124   bool _scaleToA4;
       
   125 
       
   126   std::string _title;
       
   127   std::string _copyright;
   122   
   128   
   123   ///Constructor
   129   ///Constructor
   124 
   130 
   125   ///Constructor
   131   ///Constructor
   126   ///\param _g is a reference to the graph to be printed
   132   ///\param _g is a reference to the graph to be printed
   151 ///Helper class to implement the named parameters of \ref graphToEps()
   157 ///Helper class to implement the named parameters of \ref graphToEps()
   152 ///\todo Is 'helper class' a good name for this?
   158 ///\todo Is 'helper class' a good name for this?
   153 ///
   159 ///
   154 ///\todo Follow PostScript's DSC.
   160 ///\todo Follow PostScript's DSC.
   155 /// Use own dictionary.
   161 /// Use own dictionary.
   156 ///\todo Provide a way to set %%Title: and %%Copyright:. Set %%CreationDate:
       
   157 ///\todo Useful new features.
   162 ///\todo Useful new features.
   158 /// - Linestyles: dotted, dashed etc.
   163 /// - Linestyles: dotted, dashed etc.
   159 /// - A second color and percent value for the lines.
   164 /// - A second color and percent value for the lines.
   160 template<class T> class GraphToEps : public T 
   165 template<class T> class GraphToEps : public T 
   161 {
   166 {
   447 
   452 
   448   ///Sets whether the the graph is directed.
   453   ///Sets whether the the graph is directed.
   449   ///Use it to show the undirected edges as a pair of directed ones.
   454   ///Use it to show the undirected edges as a pair of directed ones.
   450   GraphToEps<T> &bidir(bool b=true) {_undir=!b;return *this;}
   455   GraphToEps<T> &bidir(bool b=true) {_undir=!b;return *this;}
   451 
   456 
       
   457   ///Sets the title.
       
   458 
       
   459   ///Sets the title of the generated image,
       
   460   ///namely it inserts a <tt>%%Title:</tt> DSC field to the header of
       
   461   ///the EPS file.
       
   462   GraphToEps<T> &title(const std::string &t) {_title=t;return *this;}
       
   463   ///Sets the copyright statement.
       
   464 
       
   465   ///Sets the copyright statement of the generated image,
       
   466   ///namely it inserts a <tt>%%Copyright:</tt> DSC field to the header of
       
   467   ///the EPS file.
       
   468   ///\todo Multiline copyright notice could be supported.
       
   469   GraphToEps<T> &copyright(const std::string &t) {_copyright=t;return *this;}
       
   470 
   452 protected:
   471 protected:
   453   bool isInsideNode(xy<double> p, double r,int t) 
   472   bool isInsideNode(xy<double> p, double r,int t) 
   454   {
   473   {
   455     switch(t) {
   474     switch(t) {
   456     case CIRCLE:
   475     case CIRCLE:
   474   ///it draws the graph.
   493   ///it draws the graph.
   475   void run() {
   494   void run() {
   476     if(dontPrint) return;
   495     if(dontPrint) return;
   477     
   496     
   478     os << "%!PS-Adobe-2.0 EPSF-2.0\n";
   497     os << "%!PS-Adobe-2.0 EPSF-2.0\n";
   479     os << "%%Title: LEMON GraphToEps figure\n" ///\todo setTitle() is needed
   498     if(_title.size()>0) os << "%%Title: " << _title << '\n';
       
   499      if(_copyright.size()>0) os << "%%Copyright: " << _copyright << '\n';
   480 //        << "%%Copyright: XXXX\n"
   500 //        << "%%Copyright: XXXX\n"
   481        << "%%Creator: LEMON GraphToEps function\n"
   501     os << "%%Creator: LEMON GraphToEps function\n";
   482 //        << "%%CreationDate: XXXXXXX\n"
   502     
   483       ;
   503     {
       
   504       char cbuf[50];
       
   505       timeval tv;
       
   506       gettimeofday(&tv, 0);
       
   507       ctime_r(&tv.tv_sec,cbuf);
       
   508       os << "%%CreationDate: " << cbuf;
       
   509     }
   484     ///\todo: Chech whether the graph is empty.
   510     ///\todo: Chech whether the graph is empty.
   485     BoundingBox<double> bb;
   511     BoundingBox<double> bb;
   486     for(NodeIt n(g);n!=INVALID;++n) {
   512     for(NodeIt n(g);n!=INVALID;++n) {
   487       double ns=_nodeSizes[n]*_nodeScale;
   513       double ns=_nodeSizes[n]*_nodeScale;
   488       xy<double> p(ns,ns);
   514       xy<double> p(ns,ns);
   489       bb+=p+_coords[n];
   515       bb+=p+_coords[n];
   490       bb+=-p+_coords[n];
   516       bb+=-p+_coords[n];
   491       }
   517       }
   492     if(!_scaleToA4) os << "%%BoundingBox: "
   518     if(_scaleToA4)
   493 		      << bb.left()*  _scale-_xBorder << ' '
   519       os <<"%%BoundingBox: 0 0 596 842\n%%DocumentPaperSizes: a4\n";
   494 		      << bb.bottom()*_scale-_yBorder << ' '
   520     else os << "%%BoundingBox: "
   495 		      << bb.right()* _scale+_xBorder << ' '
   521 	    << bb.left()*  _scale-_xBorder << ' '
   496 		      << bb.top()*   _scale+_yBorder << '\n';
   522 	    << bb.bottom()*_scale-_yBorder << ' '
   497 
   523 	    << bb.right()* _scale+_xBorder << ' '
       
   524 	    << bb.top()*   _scale+_yBorder << '\n';
       
   525     
   498     os << "%%EndComments\n";
   526     os << "%%EndComments\n";
   499     
   527     
   500     //x1 y1 x2 y2 x3 y3 cr cg cb w
   528     //x1 y1 x2 y2 x3 y3 cr cg cb w
   501     os << "/lb { setlinewidth setrgbcolor newpath moveto\n"
   529     os << "/lb { setlinewidth setrgbcolor newpath moveto\n"
   502        << "      4 2 roll 1 index 1 index curveto stroke } bind def\n";
   530        << "      4 2 roll 1 index 1 index curveto stroke } bind def\n";