33 int width,
int height)
36 cout <<
"DEBUG ps_open_and_init: title=" << title <<
" npages="
37 << npages <<
" width=" << width <<
" height=" << height << endl;
39 ofstream* fps =
new ofstream(filename.
c_str());
41 time_t t = time(NULL);
43 *fps <<
"%!PS-Adobe-2.0" << endl;
44 *fps <<
"%%Creator: Bsoft" << endl;
45 *fps <<
"%%CreationDate: " << asctime(localtime(&t));
46 *fps <<
"%%Title: " << title << endl;
47 *fps <<
"%%DocumentData: Clean7Bit" << endl;
48 *fps <<
"%%LanguageLevel: 2" << endl;
49 *fps <<
"%%Origin: 0 0" << endl;
50 *fps <<
"%%Pages: " << npages << endl;
51 *fps <<
"%%BoundingBox: 0 0 " << width <<
" " << height << endl;
52 *fps <<
"%%EndComments" << endl;
53 *fps <<
"%%BeginSetup" << endl;
54 *fps <<
"%%EndSetup" << endl;
55 *fps <<
"%%EndProlog" << endl;
73 cout <<
"DEBUG ps_open_and_init: title=" << plot->
title() <<
" npages="
76 ofstream* fps =
new ofstream(filename.
c_str());
78 time_t t = time(NULL);
80 *fps <<
"%!PS-Adobe-2.0" << endl;
81 *fps <<
"%%Creator: Bsoft" << endl;
82 *fps <<
"%%CreationDate: " << asctime(localtime(&t));
83 *fps <<
"%%Title: " << plot->
title() << endl;
84 *fps <<
"%%DocumentData: Clean7Bit" << endl;
85 *fps <<
"%%LanguageLevel: 2" << endl;
86 *fps <<
"%%Origin: 0 0" << endl;
87 *fps <<
"%%Pages: " << plot->
pages() << endl;
89 *fps <<
"%%EndComments" << endl;
90 *fps <<
"%%BeginSetup" << endl;
91 *fps <<
"%%EndSetup" << endl;
92 *fps <<
"%%EndProlog" << endl;
108 cout <<
"DEBUG ps_close" << endl;
110 *fps <<
"%%Trailer" << endl;
111 *fps <<
"%%EOF" << endl << endl;
132 cout <<
"Plotting a graph to " << filename << endl << endl;
136 for (
int i=1; i<=plot->
pages(); i++ )
ps_graph(fps, plot, i);
151 if ( filename.
length() < 1 )
return NULL;
154 cout <<
"# Reading Postscript file: " << filename << endl;
158 ifstream fps(filename.
c_str());
159 if ( fps.fail() )
return plot;
163 int ncol(0), nrow(0);
167 while ( fps.getline(aline, 1024) && strncmp(aline,
"%%Title:", 8) ) ;
173 while ( fps.getline(aline, 1024) && strncmp(aline,
"/Data [", 7) ) ;
176 fps.getline(aline, 1024);
182 while ( fps.getline(aline, 1024) && aline[0] !=
']' ) {
184 cout <<
"DEBUG ps_read: aline = \"" << aline <<
"\"" << endl;
188 if ( ncol < temp.size() ) ncol = temp.size();
193 cout <<
"Plot file: " << filename << endl;
194 cout <<
"Columns: " << ncol << endl;
195 cout <<
"Rows: " << nrow << endl;
199 plot =
new Bplot(1, nrow, ncol);
203 for ( i=0, label = labels; i<ncol; i++, label = label->
next ) {
207 cout <<
" " << *label;
211 cout << endl << endl;
218 while ( fps.getline(aline, 1024) && strncmp(aline,
"/Data [", 7) ) ;
221 fps.getline(aline, 1024);
225 while ( fps.getline(aline, 1024) && aline[0] !=
']' ) {
227 cout <<
"DEBUG ps_read: aline = \"" << aline <<
"\"" << endl;
230 for ( j=0; j<ncol; j++ ) (*plot)[j*nrow+i] = temp[j];
249 if ( page_number < 1 ) {
250 cerr <<
"Error in ps_graph: A page number less than 1 is not valid!" << endl;
259 cout <<
"DEBUG ps_graph: starting page " << page.
number() << endl;
262 cerr <<
"Error in ps_graph: At least two columns must be specified for page " << page.
number() << endl;
268 long i, j, left = plot->
left(), bottom = plot->
bottom(), width = plot->
width(), height = plot->
height();
269 long c, ncol(0), nrow(0), x_inverse, y_inverse, y2_inverse;
271 Bstring x_label, y_label, y2_label, *txt;
272 double x_min, x_max, x_inc;
273 double y_min, y_max, y_inc;
274 double y2_min, y2_max, y2_inc;
276 double y_red(0), y_green(0), y_blue(0), y2_red(0), y2_green(0), y2_blue(0);
283 cout <<
"DEBUG ps_graph: page index = " << page_number-1 <<
" columns = " << ncol << endl;
284 cout <<
"DEBUG ps_graph: indices =";
285 for ( i=0; i<ncol; i++ ) cout <<
" " << page.
column(i).
number();
287 cout <<
"DEBUG ps_graph: axes =";
288 for ( i=0; i<ncol; i++ ) cout <<
" " << page.
column(i).
axis();
313 for ( i=0; i<ncol; i++ ) {
315 if ( col.
axis() == 1 ) {
317 }
else if ( col.
axis() == 3 ) {
320 y_green = col.
green();
322 }
else if ( col.
axis() == 4 ) {
323 if ( y2_label.
length() < 1 ) y2_label = col.
label();
325 y2_green = col.
green();
326 y2_blue = col.
blue();
330 long hw = (int) (width*1.0/nrow);
331 if ( hw < 1 ) hw = 1;
334 long x_digits = (long) (3 - log(x_max - x_min)/log(10));
335 long y_digits = (long) (3 - log(y_max - y_min)/log(10));
336 long y2_digits = (long) (3 - log(y2_max - y2_min)/log(10));
338 *fps <<
"%%Page: " << page.
number() <<
" " << page.
number() << endl;
339 *fps <<
"/Helvetica findfont 14 scalefont setfont" << endl;
340 *fps <<
"40 740 moveto (" << page.
title() <<
") show" << endl;
341 *fps <<
"/Left " << left <<
" def" << endl;
342 *fps <<
"/Bottom " << bottom <<
" def" << endl;
343 *fps <<
"/Height " << height <<
" def" << endl;
344 *fps <<
"/Width " << width <<
" def" << endl;
345 *fps << setprecision(4) <<
"/x_Min " << x_min <<
" def" << endl;
346 *fps <<
"/y_Min " << y_min <<
" def" << endl;
347 *fps <<
"/y2_Min " << y2_min <<
" def" << endl;
348 *fps <<
"/v_Min " << v_min <<
" def" << endl;
349 *fps <<
"/x_Max " << x_max <<
" def" << endl;
350 *fps <<
"/y_Max " << y_max <<
" def" << endl;
351 *fps <<
"/y2_Max " << y2_max <<
" def" << endl;
352 *fps <<
"/v_Max " << v_max <<
" def" << endl;
353 *fps <<
"/x_Scale Width x_Max x_Min sub div def" << endl;
354 *fps <<
"/y_Scale Height y_Max y_Min sub div def" << endl;
355 *fps <<
"/y2_Scale Height y2_Max y2_Min sub div def" << endl;
357 *fps <<
"/v_Scale 1 v_Max v_Min sub div def" << endl;
358 *fps <<
"/Ncol " << ncol <<
" def" << endl;
359 *fps <<
"/Frame { newpath 0 0 moveto 0 Height lineto Width Height lineto Width 0 lineto closepath } def" << endl;
361 *fps <<
"/Data [" << endl;
363 for ( j=0; j<ncol; j++ ) {
365 *fps << col.
label() <<
" ";
368 *fps << setprecision(8);
369 for ( i=0; i<nrow && i<max_row; i++ ) {
370 for ( j=0; j<ncol; j++ ) {
373 *fps << (*plot)[c*nrow+i] <<
" ";
377 *fps <<
"] def" << endl;
379 *fps <<
"/Helvetica findfont 12 scalefont setfont" << endl;
380 for ( i=140, txt = page.
text(); txt; txt = txt->
next, i-=18 )
381 *fps <<
"40 " << i <<
" moveto (" << *txt <<
") show" << endl;
383 *fps <<
"/Helvetica findfont 14 scalefont setfont" << endl;
384 *fps <<
"/DeviceRGB setcolorspace" << endl;
385 *fps <<
"0 0 0 setcolor" << endl;
388 *fps << left+(width-6*x_label.
length())/2 <<
" " << bottom-40 <<
389 " moveto (" << x_label <<
") show" << endl;
392 *fps << y_red <<
" " << y_green <<
" " << y_blue <<
" setcolor" << endl;
393 *fps << left-40 <<
" " << bottom+(height-6*y_label.
length())/2 <<
394 " moveto gsave 90 rotate (" << y_label <<
") show grestore" << endl;
397 if ( y2_label.
length() ) {
398 *fps << y2_red <<
" " << y2_green <<
" " << y2_blue <<
" setcolor" << endl;
399 *fps << left+width+35 <<
" " << bottom+(height-6*y2_label.
length())/2 <<
400 " moveto gsave -90 rotate (" << y2_label <<
") show grestore" << endl;
403 *fps <<
"0 0 0 setcolor" << endl;
405 ps_scale(fps, left, bottom, left+width, bottom, x_min, x_max, x_inc,
406 10,
M_PI_2, x_digits, 10, x_inverse);
408 ps_scale(fps, left, bottom, left, bottom+height, y_min, y_max, y_inc,
409 10, 0, y_digits, 10, y_inverse);
412 ps_scale(fps, left+width, bottom, left+width, bottom+height, y2_min, y2_max, y2_inc,
413 10,
M_PI, y2_digits, 10, y2_inverse);
415 *fps <<
"gsave" << endl;
416 *fps <<
" /DeviceRGB setcolorspace" << endl;
417 *fps <<
" Left Bottom translate Frame stroke Frame clip" << endl;
418 *fps <<
" /y 0 y_Min sub y_Scale mul def" << endl;
419 *fps <<
" 0 y moveto Width y lineto stroke" << endl;
420 *fps <<
" /x 0 x_Min sub x_Scale mul def" << endl;
421 *fps <<
" x 0 moveto x Height lineto stroke" << endl;
423 for ( i=0; i<ncol; i++ ) {
425 if ( col.
axis() == 3 || col.
axis() == 4 ) {
426 *fps <<
" " << col.
red() <<
" " << col.
green() <<
" " << col.
blue() <<
" setcolor" << endl;
427 *fps <<
" /x Data 0 get x_Min sub x_Scale mul def" << endl;
428 *fps <<
" /y Data " << i <<
" get y_Min sub y_Scale mul def" << endl;
429 *fps <<
" x y moveto" << endl;
430 *fps <<
" Ncol Ncol Data length Ncol sub {" << endl;
431 *fps <<
" /x_Index exch def" << endl;
432 *fps <<
" /y_Index x_Index " << i <<
" add def" << endl;
433 if ( col.
type() == 3 ) *fps <<
" /py_Index y_Index 1 sub def" << endl;
434 if ( col.
type() == 4 ) *fps <<
" /v_Index y_Index 1 add def" << endl;
435 *fps <<
" /x Data x_Index get x_Min sub x_Scale mul def" << endl;
436 if ( col.
axis() == 3 ) {
437 if ( col.
type() == 3 ) *fps <<
" /y Data y_Index get y_Scale mul def" << endl;
438 else *fps <<
" /y Data y_Index get y_Min sub y_Scale mul def" << endl;
439 }
else *fps <<
" /y Data y_Index get y2_Min sub y2_Scale mul def" << endl;
440 if ( col.
type() == 3 ) *fps <<
" /py Data py_Index get y_Min sub y_Scale mul def" << endl;
441 *fps <<
" /w " << hw <<
" def" << endl;
442 *fps <<
" /xm x " << hhw <<
" sub def" << endl;
444 if ( col.
type() == 4 ) {
445 *fps <<
" /v v_Max Data v_Index get sub v_Scale mul def" << endl;
446 *fps <<
" v setgray" << endl;
448 switch ( col.
type() ) {
449 case 1: *fps <<
" xm 0 w y rectstroke" << endl;
break;
450 case 2: *fps <<
" x y lineto" << endl;
break;
452 *fps <<
" x py y sub moveto 0 y 2 mul rlineto" << endl;
453 *fps <<
" x 3 sub py y sub moveto 6 0 rlineto" << endl;
454 *fps <<
" x 3 sub py y add moveto 6 0 rlineto" << endl;
456 case 4: *fps <<
" x y 5 0 360 arc fill" << endl;
break;
457 default: *fps <<
" x y " << col.
element_size() <<
" 0 360 arc fill" << endl;
459 *fps <<
" } for stroke" << endl;
462 *fps <<
"grestore" << endl;
463 *fps <<
"showpage" << endl;
466 cout <<
"DEBUG ps_graph: finished page " << page.
number() << endl;
488 int ps_scale(ofstream* fps,
double x1,
double y1,
double x2,
double y2,
489 double min,
double max,
double increment,
490 double tick_length,
double tick_angle,
491 int digits,
int fontsize,
int inverse)
493 ios_base::fmtflags fl = fps->flags();
496 double value, showval;
497 double x_tick = tick_length*cos(tick_angle);
498 double y_tick = tick_length*sin(tick_angle);
499 double x_step = (x2-x1)*increment/(max - min);
500 double y_step = (y2-y1)*increment/(max - min);
502 if ( digits < 0 ) digits = 0;
503 if ( fontsize < 2 ) fontsize = 10;
505 long integer_size = (int) (log(fabs(max))/log(10.0) + 1);
506 if ( fabs(max) < fabs(min) ) integer_size = (
int) (log(fabs(min))/log(10.0) + 1);
508 if ( min <= 0 ) integer_size = (int) (log(fabs(10/max))/log(10.0) + 1);
509 else integer_size = (int) (log(fabs(1/min))/log(10.0) + 1);
511 if ( integer_size < 1 ) integer_size = 1;
514 long number_size = integer_size + 1 + digits;
515 double x_offset = x1 - number_size*fontsize/5;
516 double y_offset = y1 - fontsize/3;
517 if ( x2 - x1 > y2 - y1 )
518 y_offset -= fontsize;
520 x_offset -= number_size*fontsize/3;
522 if ( tick_angle > 0.75*
M_PI && tick_angle < 1.25*
M_PI )
523 x_offset = x1 + fontsize/5;
526 cout <<
"DEBUG ps_scale: min=" << min <<
" max=" << max <<
" increment=" << increment << endl;
527 cout <<
"DEBUG ps_scale: number_size=" << number_size <<
" digits=" << digits << endl;
531 *fps <<
"/Helvetica findfont " << fontsize <<
" scalefont setfont " << endl;
532 for ( i=0, value=min; value<max+0.01*increment; value+=increment, i++ ) {
533 *fps << x1+i*x_step <<
" " << y1+i*y_step <<
" moveto " <<
534 x1+i*x_step+x_tick <<
" " << y1+i*y_step+y_tick <<
" lineto stroke" << endl;
536 if ( inverse && value ) {
if ( value ) showval = 1/value;
else showval = 999; }
538 *fps << x_offset + i*x_step <<
" " << y_offset + i*y_step <<
" moveto ("
539 << fixed << setprecision(digits) << right << setw(number_size) << showval <<
") (";
540 for ( j=0; j<integer_size; j++ ) *fps <<
"x";
542 for ( j=0; j<digits; j++ ) *fps <<
"x";
543 *fps <<
") cvs show" << endl;
589 for (
int i=0, j=nrow, k=j+nrow, l=k+nrow; i<nrow; i++, j++, k++, l++ ) {
590 if ( c0 ) (*plot)[i] = c0[i];
591 if ( c1 ) (*plot)[j] = c1[i];
592 if ( c2 ) (*plot)[k] = c2[i];
593 if ( c3 ) (*plot)[l] = c3[i];
604 *fps <<
"/ArrowHeadSize " << 30 <<
" def" << endl;
605 *fps <<
"/ahead {" << endl;
606 *fps <<
" 1 index 4 index sub" << endl;
607 *fps <<
" 1 index 4 index sub" << endl;
608 *fps <<
" exch atan" << endl;
609 *fps <<
" ArrowHeadSize -.8 mul" << endl;
610 *fps <<
" dup" << endl;
611 *fps <<
" 2 index cos mul 4 index add" << endl;
612 *fps <<
" exch" << endl;
613 *fps <<
" 2 index sin mul 3 index add" << endl;
614 *fps <<
" 5 2 roll" << endl;
615 *fps <<
" gsave" << endl;
616 *fps <<
" 3 1 roll" << endl;
617 *fps <<
" translate" << endl;
618 *fps <<
" rotate" << endl;
619 *fps <<
" newpath" << endl;
620 *fps <<
" 0 0 moveto" << endl;
621 *fps <<
" ArrowHeadSize dup neg exch .25 mul" << endl;
622 *fps <<
" 2 copy lineto" << endl;
623 *fps <<
" ArrowHeadSize -.8 mul 0" << endl;
624 *fps <<
" 2 copy" << endl;
625 *fps <<
" 6 4 roll" << endl;
626 *fps <<
" neg curveto" << endl;
627 *fps <<
" closepath fill" << endl;
628 *fps <<
" grestore" << endl;
629 *fps <<
"} bind def" << endl;
630 *fps <<
"/arrowline {" << endl;
631 *fps <<
" ahead" << endl;
632 *fps <<
" moveto" << endl;
633 *fps <<
" lineto" << endl;
634 *fps <<
" stroke" << endl;
635 *fps <<
"} bind def" << endl;