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