lemon/eps.cc
author athos
Mon, 17 Jul 2006 09:31:41 +0000
changeset 2147 63d293ff1bef
parent 2008 0820d8168cbb
child 2207 75a29ac69c19
permissions -rw-r--r--
mip_test file went to mip_demo.
     1 #include <lemon/eps.h>
     2 
     3 namespace lemon {
     4   
     5   void EpsDrawer::defMacros()
     6   {
     7     out << "/clmode true def\n" <<
     8       "/cshowmode false def\n" <<
     9       "/defont (Helvetica) findfont def\n" <<
    10       "/fosi 12 def\n" <<
    11       "\n" <<
    12       "/st { clmode { currentpoint stroke newpath moveto } if } bind def\n" <<
    13       "/str { currentpoint stroke newpath moveto /clmode true def } bind def\n"
    14 	<<
    15       "/fl { currentpoint fill newpath moveto /clmode true def } bind def\n" <<
    16       "/eofl { currentpoint eofill newpath moveto /clmode true def } bind def\n"
    17 	<<
    18       "/cl { currentpoint clip newpath moveto /clmode true def } bind def\n"
    19 	<<
    20       "/eocl { currentpoint eoclip newpath moveto /clmode true def } bind def\n"
    21 	<<
    22       "\n" <<
    23       "/l { moveto lineto st } bind def\n" <<
    24       "/lt { lineto st } bind def\n" <<
    25       "/mt { moveto } bind def\n" <<
    26       "/c { dup 3 index add 2 index moveto 0 360 arc st } bind def\n" <<
    27       "/collect { /clmode false def currentpoint newpath moveto } bind def\n" <<
    28       "\n" <<
    29       "/fontset { defont fosi scalefont setfont } bind def\n" <<
    30       "/stfs { /fosi exch def fontset } bind def\n" <<
    31       "/cshow { dup stringwidth pop\n" <<
    32       "   neg 2 div 0 rmoveto show } bind def\n" <<
    33       "/xshow { cshowmode { cshow } { show } ifelse } def\n" <<
    34       "\n" <<
    35       "fontset\n" <<
    36       "newpath\n" <<
    37       "0 0 moveto\n" <<
    38       "1 setlinecap\n";
    39   }
    40 
    41   void EpsDrawer::init(int x1,int y1,int x2,int y2)
    42   {
    43     out << "%!PS-Adobe-2.0 EPSF-2.0\n" <<
    44       "%%BoundingBox: " << x1 << ' ' << y1 << ' ' << x2 << ' ' << y2 <<
    45       "\n%%EndComments\n";
    46     defMacros();
    47   }
    48 
    49   void EpsDrawer::init(double x1,double y1,double x2,double y2)
    50   {
    51     out << "%!PS-Adobe-2.0\n" <<
    52       "%%HiResBoundingBox: " << 
    53       x1 << ' ' << y1 << ' ' << x2 << ' ' << y2 <<
    54       "\n%%EndComments\n";
    55     defMacros();
    56   }
    57 
    58 
    59   EpsDrawer::EpsDrawer(std::ostream &os,int x,int y) : local_stream(false),
    60 						       out(os)
    61   {
    62     init(0,0,x,y);
    63   }
    64 
    65   EpsDrawer::EpsDrawer(std::ostream &os,int x1,int y1,int x2,int y2) : 
    66     local_stream(false),
    67     out(os)
    68   {
    69     init(x1,y1,x2,y2);
    70   }
    71 
    72   EpsDrawer::EpsDrawer(std::ostream &os,xy<double> s) : local_stream(false),
    73 							out(os)
    74   {
    75     init(0.0,0.0,s.x,s.y);
    76   }
    77 
    78   EpsDrawer::EpsDrawer(std::ostream &os,xy<double> a, xy<double> b) :
    79     local_stream(false),
    80     out(os)
    81   {
    82     init(a.x,a.y,b.x,b.y);
    83   }
    84 
    85 
    86   EpsDrawer::EpsDrawer(const std::string &name,int x,int y) :
    87     local_stream(true),
    88     out(*new std::ofstream(name.c_str()))
    89   {
    90     init(0,0,x,y);
    91   }
    92 
    93   EpsDrawer::EpsDrawer(const std::string &name,int x1,int y1,int x2,int y2) : 
    94     local_stream(true),
    95     out(*new std::ofstream(name.c_str()))
    96   {
    97     init(x1,y1,x2,y2);
    98   }
    99   
   100   EpsDrawer::EpsDrawer(const std::string &name,xy<double> s) :
   101     local_stream(true),
   102     out(*new std::ofstream(name.c_str()))
   103   {
   104     init(0.0,0.0,s.x,s.y);
   105   }
   106 
   107   EpsDrawer::EpsDrawer(const std::string &name,xy<double> a, xy<double> b) :
   108     local_stream(true),
   109     out(*new std::ofstream(name.c_str()))
   110   {
   111     init(a.x,a.y,b.x,b.y);
   112   }
   113 
   114 
   115   EpsDrawer::~EpsDrawer()
   116   {
   117     out << "showpage\n";
   118     if(local_stream) delete &out;
   119   }
   120 
   121   EpsDrawer &EpsDrawer::save()
   122   {
   123     out << "gsave\n";
   124     return *this;  
   125   }
   126 
   127   EpsDrawer &EpsDrawer::restore()
   128   {
   129     out << "grestore\n";
   130     return *this;  
   131   }
   132  
   133   EpsDrawer &EpsDrawer::line(double x1,double y1,double x2,double y2)
   134   {
   135     out << x1 << ' ' << y1 << ' ' << x2 << ' ' << y2 << " l\n";
   136     return *this;
   137   
   138   }
   139 
   140   EpsDrawer &EpsDrawer::lineTo(double x1,double y1)
   141   {
   142     out << x1 << ' ' << y1 << " lt\n";
   143     return *this;
   144   
   145   }
   146 
   147   EpsDrawer &EpsDrawer::moveTo(double x1,double y1)
   148   {
   149     out << x1 << ' ' << y1 << " mt\n";
   150     return *this;  
   151   }
   152 
   153   EpsDrawer &EpsDrawer::circle(double x,double y, double r)
   154   {
   155     out << x << ' ' << y << ' ' << r << " c\n";
   156     return *this;  
   157   }
   158 
   159   EpsDrawer &EpsDrawer::operator<<(const std::string &s)
   160   {
   161     out << "(" << s <<") xshow\n";
   162     return *this;
   163   }
   164 
   165   EpsDrawer &EpsDrawer::operator<<(const char *s)
   166   {
   167     out << "(" << s <<") xshow\n";
   168     return *this;
   169   }
   170 
   171   EpsDrawer &EpsDrawer::operator<<(int i)
   172   {
   173     out << "(" << i <<") xshow\n";
   174     return *this;
   175   }
   176 
   177   EpsDrawer &EpsDrawer::operator<<(double d)
   178   {
   179     out << "(" << d <<") xshow\n";
   180     return *this;
   181   }
   182 
   183   EpsDrawer &EpsDrawer::fontSize(double si)
   184   {
   185     out << si << " stfs\n";
   186     return *this;
   187   }
   188   EpsDrawer &EpsDrawer::font(std::string s)
   189   {
   190     out << "/defont ("<<s<<") findfont def fontset\n";
   191     return *this;
   192   }
   193 
   194 
   195   EpsDrawer &EpsDrawer::collect()
   196   {
   197     out << "collect\n";
   198     return *this;  
   199   }
   200 
   201   EpsDrawer &EpsDrawer::closePath()
   202   {
   203     out << "closepath\n";
   204     return *this;
   205   }
   206 
   207   EpsDrawer &EpsDrawer::stroke()
   208   {
   209     out << "str\n";
   210     return *this;  
   211   }
   212   EpsDrawer &EpsDrawer::fill()
   213   {
   214     out << "fl\n";
   215     return *this;  
   216   }
   217   EpsDrawer &EpsDrawer::eoFill()
   218   {
   219     out << "eofl\n";
   220     return *this;  
   221   }
   222   EpsDrawer &EpsDrawer::clip()
   223   {
   224     out << "cl\n";
   225     return *this;  
   226   }
   227   EpsDrawer &EpsDrawer::eoClip()
   228   {
   229     out << "eocl\n";
   230     return *this;  
   231   }
   232 
   233   EpsDrawer &EpsDrawer::lineWidth(double w)
   234   {
   235     out << w << " setlinewidth\n";
   236     return *this;  
   237   }
   238 
   239   EpsDrawer &EpsDrawer::lineCap(int i)
   240   {
   241     out << i << " setlinecap\n";
   242     return *this;  
   243   }
   244 
   245   EpsDrawer &EpsDrawer::lineJoin(int i)
   246   {
   247     out << i << " setlinejoin\n";
   248     return *this;  
   249   }
   250 
   251   EpsDrawer &EpsDrawer::miterLimit(double w)
   252   {
   253     out << w << " setmiterlimit\n";
   254     return *this;  
   255   }
   256 
   257   EpsDrawer &EpsDrawer::color(double r, double g, double b)
   258   {
   259     out << r << ' ' << g << ' ' << b << " setrgbcolor\n";
   260     return *this;  
   261   }
   262 
   263   EpsDrawer &EpsDrawer::translate(double x,double y)
   264   {
   265     out << x << ' ' << y << " translate\n";
   266     return *this;  
   267   }
   268 
   269   EpsDrawer &EpsDrawer::rotate(double r)
   270   {
   271     out << r << " rotate\n";
   272     return *this;  
   273   }
   274   EpsDrawer &EpsDrawer::scale(double sx, double sy)
   275   {
   276     out << sx << ' ' << sy << " scale\n";
   277     return *this;  
   278   }
   279   
   280   EpsDrawer &EpsDrawer::clear()
   281   {
   282     out << "erasepage\n";
   283     return *this;  
   284   }
   285   
   286   EpsDrawer &EpsDrawer::centerMode(bool m)
   287   {
   288     if(m) out << "/cshowmode true def\n";
   289     else out << "/cshowmode false def\n";
   290 
   291     return *this;  
   292   }
   293   
   294   EpsDrawer &EpsDrawer::flush()
   295   {
   296     out << "flush\n";
   297     //  fflush(fp);
   298     return *this;
   299   }
   300 
   301   EpsDrawer &EpsDrawer::node(NodeShapes t, double x, double y, double r,
   302 			     Color col, Color brd)
   303   {
   304     out << "gsave\n"
   305 	<< brd.red() << ' ' << brd.green() << ' ' << brd.blue() 
   306 	<< " setrgbcolor\n";
   307     switch(t) {
   308     case CIRCLE:
   309       out << "newpath " << x << ' ' << y << ' ' << r 
   310 	  << " dup 3 index add 2 index moveto 0 360 arc fill\n";
   311       break;
   312     case SQUARE:
   313       out << "newpath\n"
   314 	  << x-r << ' ' << y-r << " moveto\n"
   315 	  << x-r << ' ' << y+r << " lineto\n"
   316 	  << x+r << ' ' << y+r << " lineto\n"
   317 	  << x+r << ' ' << y-r << " lineto closepath fill\n";
   318       break;
   319     case DIAMOND:
   320       out << "newpath\n"
   321 	  << x-r << ' ' << y   << " moveto\n"
   322 	  << x   << ' ' << y+r << " lineto\n"
   323 	  << x+r << ' ' << y   << " lineto\n"
   324 	  << x   << ' ' << y-r << " lineto closepath fill\n";
   325       break;
   326     case MALE:
   327       break;
   328     case FEMALE:
   329       break;
   330     }
   331     r/=1.1;
   332     out << col.red() << ' ' << col.green() << ' ' << col.blue() 
   333 	<< " setrgbcolor\n";
   334     switch(t) {
   335     case CIRCLE:
   336       out << "newpath " << x << ' ' << y << ' ' << r 
   337 	  << " dup 3 index add 2 index moveto 0 360 arc fill\n";
   338       break;
   339     case SQUARE:
   340       out << "newpath\n"
   341 	  << x-r << ' ' << y-r << " moveto\n"
   342 	  << x-r << ' ' << y+r << " lineto\n"
   343 	  << x+r << ' ' << y+r << " lineto\n"
   344 	  << x+r << ' ' << y-r << " lineto closepath fill\n";
   345       break;
   346     case DIAMOND:
   347       out << "newpath\n"
   348 	  << x-r << ' ' << y   << " moveto\n"
   349 	  << x   << ' ' << y+r << " lineto\n"
   350 	  << x+r << ' ' << y   << " lineto\n"
   351 	  << x   << ' ' << y-r << " lineto closepath fill\n";
   352       break;
   353     case MALE:
   354       break;
   355     case FEMALE:
   356       break;
   357     }
   358 
   359     out << "grestore\n";
   360     return *this;
   361   }
   362   
   363 }