00001
00002
00003
00004
00005 #include "PSdraw.h"
00006 #include "gui/DisplayRep.h"
00007
00008
00009 #include <iostream>
00010 #include <iomanip>
00011
00012 namespace gui {
00013
00014 static int markerSize = 10;
00015
00016
00017 inline int PSdraw::xwin(float x)
00018 {
00019 return (int)((x+1)*currentQuad.dx/2) + currentQuad.x;
00020 }
00021
00022 inline int PSdraw::ywin(float y)
00023 {
00024 return (int)((y+1)*currentQuad.dy/2) + currentQuad.y;
00025 }
00026
00027
00028 struct PSdraw::PageFormat
00029 {
00030 const char *format;
00031 int ptXsize;
00032 int ptYsize;
00033
00034 static const PageFormat *liste;
00035 static void lookUp (const char *_format, int *_ptXsize, int *_ptYsize);
00036 };
00037
00038
00039 void PSdraw::PageFormat::lookUp (const char *_format, int *_ptXsize, int *_ptYsize)
00040 {
00041
00042 for (int i=0; liste[i].format; i++)
00043 {
00044
00045 if (strcmp(liste[i].format, _format)==0)
00046 {
00047 *_ptXsize = liste[i].ptXsize;
00048 *_ptYsize = liste[i].ptYsize;
00049 return;
00050 }
00051 }
00052 *_ptXsize = liste[0].ptXsize;
00053 *_ptYsize = liste[0].ptYsize;
00054 return;
00055 }
00056
00057
00058 static const PSdraw::PageFormat formatListe[] =
00059 {
00060 { "DinA4", 2100, 2970 },
00061 { "DinA4-quer", 2970, 2100 },
00062 { 0, 0, 0 }
00063 };
00064 const PSdraw::PageFormat *PSdraw::PageFormat::liste = formatListe;
00065
00066
00067 static const char *psInitString =
00068 "%!PS-Adobe-2.0\n"
00069 "%%Title: gui-Display (Portrait A 4)\n"
00070 "%%Pages: atend\n"
00071 "%%Creator: Hardy's PS-Printer 08.15\n"
00072 "%%CreationDate: 94/03/28 10.13\n"
00073 "%%EndComments\n"
00074 "/s {stroke} def /l {lineto} def /m {moveto} def /t {translate} def\n"
00075 "/sw {stringwidth} def /r {rlineto} def /rl {roll} def\n"
00076 "/gr {grestore} def\n"
00077 "/c {setrgbcolor} def /lw {setlinewidth} def /sd {setdash} def\n"
00078 "/cl {closepath} def /sf {scalefont setfont} def\n"
00079 "/rgb {setrgbcolor} def\n"
00080 "\n"
00081 "/Helvetica findfont\n"
00082 "30 scalefont\n"
00083 "setfont\n"
00084 "0.28346456 0.28346456 scale\n"
00085 "1 setlinewidth\n"
00086 "gsave\n";
00087
00088
00089 static const char *psFlushString =
00090 "";
00091
00092 static const char *psCloseString =
00093 "\ngrestore\n"
00094 "showpage gr\n"
00095 "%%Trailer\n"
00096 "%%Pages: 1\n"
00097 "gr gr\n"
00098 "%%EOF\n";
00099
00100
00101
00102
00103 PSdraw::PSdraw (const char *filename, const char *page_format)
00104 : psfile( filename )
00105 {
00106
00107 psfile << psInitString;
00108
00109
00110
00111 PageFormat::lookUp (page_format, &screenWidth, &screenHeight);
00112
00113
00114
00115 flSingle = 1;
00116 resize(screenWidth, screenHeight);
00117
00118
00119 isStrokePending = 0;
00120 }
00121
00122
00123 PSdraw::~PSdraw()
00124 {
00125 flush ();
00126 psfile << psCloseString;
00127 psfile.flush();
00128 }
00129
00130
00131 void PSdraw::draw_string (float x, float y, const char *string, int size)
00132 {
00133 move_to (x,y);
00134 draw_string (string, size);
00135 }
00136
00137
00138 void PSdraw::draw_string (const char* string, int )
00139 {
00140 psfile << "(" << string << ") show\n";
00141 }
00142
00143
00144 void PSdraw::move_to (float x, float y)
00145 {
00146 lastx = xwin(x);
00147 lasty = ywin(y);
00148
00149 if (isStrokePending)
00150 {
00151 psfile << " s\n";
00152 isStrokePending = 0;
00153 }
00154
00155 psfile << " " << lastx << " " << lasty << " m";
00156 }
00157
00158
00159 void PSdraw::line_to (float x, float y)
00160 {
00161 int thisx = xwin(x);
00162 int thisy = ywin(y);
00163
00164 psfile << " " << thisx << " " << thisy << " l";
00165 isStrokePending = 1;
00166
00167 lastx = thisx;
00168 lasty = thisy;
00169 }
00170
00171
00172 void PSdraw::flush ()
00173 {
00174 if (isStrokePending)
00175 {
00176 psfile << " s\n";
00177 isStrokePending = 0;
00178 }
00179
00180 psfile << psFlushString;
00181 psfile.flush ();
00182 }
00183
00184
00185 void PSdraw::draw_marker (float x, float y)
00186 {
00187 if (isStrokePending)
00188 {
00189 psfile << " s\n";
00190 isStrokePending = 0;
00191 }
00192
00193 int thisx = xwin(x);
00194 int thisy = ywin(y);
00195
00196 psfile << " " << thisx-markerSize << " " << thisy+markerSize << " m";
00197 psfile << " " << thisx+markerSize << " " << thisy-markerSize << " l";
00198 psfile << " " << thisx-markerSize << " " << thisy-markerSize << " m";
00199 psfile << " " << thisx+markerSize << " " << thisy+markerSize << " l";
00200 psfile << " " << lastx << " " << lasty << " m";
00201
00202 }
00203
00204
00205 void PSdraw::set_quad (int quadnr, const char * title, int )
00206 {
00207 if (quadnr==0)
00208 {
00209 flSingle = 1;
00210 currentQuad = screen;
00211 }
00212 else
00213 {
00214 flSingle = 0;
00215 currentQuad = quadrant [selected = quadnr-1];
00216 }
00217
00218
00219
00220
00221 psfile << "\ngrestore "
00222 << currentQuad.x << ' ' << currentQuad.y << " m "
00223 << currentQuad.dx << ' ' << 0 << " r "
00224 << 0 << ' ' << currentQuad.dy << " r "
00225 << -currentQuad.dx << ' ' << 0 << " r "
00226 << " closepath stroke\n";
00227
00228
00229 psfile << "gsave "
00230 << currentQuad.x << ' ' << currentQuad.y << " m "
00231 << currentQuad.dx << ' ' << 0 << " r "
00232 << 0 << ' ' << currentQuad.dy << " r "
00233 << -currentQuad.dx << ' ' << 0 << " r "
00234 << " closepath clip\n";
00235
00236 psfile << xwin(-0.9f) << ' ' << ywin(-0.9f) << " m\n";
00237 psfile << '('<<title << ") show\n";
00238
00239
00240 }
00241
00242
00243 void PSdraw::resize (int xsize, int ysize)
00244 {
00245 int xoffset = 0;
00246 int yoffset = 0;
00247
00248 if (ysize > xsize)
00249 {
00250 yoffset = (ysize-xsize) / 2;
00251 ysize = xsize;
00252 }
00253 else
00254 {
00255 xoffset = (xsize-ysize) / 2;
00256 xsize = ysize;
00257 }
00258
00259 screen.x = 0; screen.y = 0; screen.dx = xsize; screen.dy = ysize;
00260 quadrant[0].x = 0; quadrant[0].y = 0; quadrant[0].dx = xsize/2; quadrant[0].dy = ysize/2;
00261 quadrant[1].x = xsize/2+1; quadrant[1].y = 0; quadrant[1].dx = xsize/2; quadrant[1].dy = ysize/2;
00262 quadrant[2].x = 0; quadrant[2].y = ysize/2+1; quadrant[2].dx = xsize/2; quadrant[2].dy = ysize/2;
00263 quadrant[3].x = xsize/2+1; quadrant[3].y = ysize/2+1; quadrant[3].dx = xsize/2; quadrant[3].dy = ysize/2;
00264
00265 screen.x += xoffset; screen.y += yoffset;
00266 quadrant[0].x += xoffset; quadrant[0].y += yoffset;
00267 quadrant[1].x += xoffset; quadrant[1].y += yoffset;
00268 quadrant[2].x += xoffset; quadrant[2].y += yoffset;
00269 quadrant[3].x += xoffset; quadrant[3].y += yoffset;
00270
00271
00272 if (flSingle)
00273 currentQuad = screen;
00274 else
00275 currentQuad = quadrant[selected];
00276 }
00277
00278
00279 void PSdraw::set_defaults ()
00280 {
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290 }
00291
00292
00293 void PSdraw::set_line_style (int )
00294 {
00295
00296
00297
00298
00299
00300
00301 }
00302
00303
00304
00305 void PSdraw::set_rgb(float r,float g,float b)
00306 {
00307 psfile << std::setprecision(3)
00308 << ' ' << r/255. << ' ' << g/255. << ' ' << b/255. << " rgb\n";
00309 }
00310 void PSdraw::set_col_index(int i)
00311 {
00312 DisplayRep::ColorInfo& cinfo = DisplayRep::pallete[i];
00313 set_rgb(cinfo.r, cinfo.g, cinfo.b );
00314
00315 }
00316
00317
00318
00319 }