EpsView.cxx

Go to the documentation of this file.
00001 
00012 #ifdef _MSC_VER
00013 #include "msdevstudio/MSconfig.h"
00014 #endif
00015 
00016 #include "EpsView.h"
00017 
00018 #include "FontBase.h"
00019 #include "Color.h"
00020 
00021 #include "plotters/PlotterBase.h"
00022 
00023 
00024 #ifdef ITERATOR_MEMBER_DEFECT
00025 using namespace std;
00026 #else
00027 using std::endl;
00028 using std::ofstream;
00029 using std::string;
00030 #endif
00031 
00032 using namespace hippodraw;
00033 
00034 EpsView::
00035 EpsView ( const std::string filename, double x, double y, double w, double h )
00036   : DataView(),
00037     m_boundingRect ( x, y, w, h )
00038 {
00039   initPlot ( filename, x, y, w, h );
00040 }
00041 
00042 EpsView::
00043 ~EpsView()
00044 {
00045 }
00046 
00047 void
00048 EpsView::
00049 lineTo ( double x, double y )
00050 {
00051   m_outfile << x << " " << y << " lineto\n";
00052 }
00053 
00054 void
00055 EpsView::
00056 moveTo ( double x, double y )
00057 {
00058   m_outfile << x << " " << y << " moveto\n";
00059 }
00060 
00061 void
00062 EpsView::
00063 setDash ( Line::Style style )
00064 {
00065   switch (style)
00066     {
00067     case Line::Solid:
00068       m_outfile << "[] 0 setdash\n";
00069       break;
00070     case Line::Dot:
00071       m_outfile << "[3 5] 0 setdash\n";
00072       break;
00073     case Line::Dash:
00074       m_outfile << "[5 3] 0 setdash\n";
00075       break;
00076     case Line::DashDot:
00077       m_outfile << "[5 3 1 3] 0 setdash\n";
00078       break;
00079     default:
00080       break;
00081     }
00082 }
00083 
00084 void
00085 EpsView::
00086 setLineWidth ( double size )
00087 {
00088   m_outfile << size << " setlinewidth\n";
00089 }
00090 
00091 void
00092 EpsView::
00093 setRgbColor ( int red, int green, int blue )
00094 {
00095   double inv = 1. / 255.;
00096   m_outfile << red   * inv << " "
00097             << green * inv << " "
00098             << blue  * inv << " setrgbcolor\n";
00099 }
00100 
00101 void
00102 EpsView::
00103 setRgbColor ( const Color & color )
00104 {
00105   setRgbColor ( color.getRed (), color.getGreen (), color.getBlue () );
00106 }
00111 void EpsView::initPlot ( const std::string & fname, 
00112                          double, double, double w, double h )
00113 {
00114 
00115   const char * fn = fname.c_str();
00116   m_outfile.open (fn, std::ios::out);
00117 
00118   m_outfile << "%%!PS-Adobe-3.0 EPSF-3.0" << endl;
00119   m_outfile << "%%Creator: HippoDraw" << endl;
00120 
00121   double x1 = 0;
00122   double y1 = 0;
00123   double x2 = x1 + w;
00124   double y2 = y1 + h;
00125 
00126   m_outfile << "%%BoundingBox: " 
00127           << x1 << " " << y1 << " " 
00128           << x2 << " " << y2 << endl; 
00129   
00130   m_outfile << "%%EndComments" << endl;
00131   m_outfile << endl << endl;
00132 
00133   m_outfile << "%% Add emulation of selectfont if needed" << endl;
00134   m_outfile << "%%   taken from PS Lang. Ref. Manual, Appendix D.4" << endl;
00135   m_outfile << "/*SF {" << endl;
00136   m_outfile << "  exch findfont exch" << endl;
00137   m_outfile << "  dup type /arraytype eq {makefont}{scalefont} ifelse setfont" 
00138             << endl;
00139   m_outfile << "} bind def" << endl;
00140   m_outfile << endl;
00141   m_outfile << "/languagelevel where" << endl;
00142   m_outfile << " {pop languagelevel} {1} ifelse" << endl;
00143   m_outfile << "2 lt {/SF /*SF load def}{/SF /selectfont load def} ifelse"
00144           << endl;
00145 
00146   m_outfile << "%%" << endl << "%%" << endl;
00147 }
00148 
00149 void EpsView::closeFile ()
00150 {
00151   m_outfile << "%%EOF" << endl;
00152   m_outfile.close();
00153 }
00154 
00155 void EpsView::drawLines ( const std::vector< double > & x,
00156                           const std::vector< double > & y,
00157                           hippodraw::Line::Style style,
00158                           const Color & color,
00159                           float size )
00160 {
00161   if ( style == Line::Invisible ) return;
00162 
00163   m_outfile << "%% drawLines" << endl;
00164 
00165   m_outfile << "gsave" << endl;
00166 
00167   setRgbColor ( color );
00168 
00169   setLineWidth ( size );
00170   setDash ( style );
00171   
00172   for ( unsigned int i = 0; i < x.size(); i = i+2 )
00173     {
00174       
00175       m_outfile << "gsave" << endl << "newpath systemdict begin" << endl;
00176       moveTo ( toViewX (x[i]), toViewY (y[i]) );
00177       lineTo ( toViewX (x[i+1]), toViewY (y[i+1]) );
00178       m_outfile << "end" << endl;
00179       m_outfile << "stroke grestore" << endl;
00180       
00181     }
00182 
00183   m_outfile << "grestore" << endl;
00184 
00185 }
00186 
00187 void EpsView::drawColorLines ( const std::vector< double > & x,
00188                                const std::vector< double > & y,
00189                                hippodraw::Line::Style style,
00190                                const std::vector < Color > & colors,
00191                                float size )
00192 {
00193   if ( style == Line::Invisible ) return;
00194 
00195   m_outfile << "%% drawLines" << endl;
00196   m_outfile << "gsave" << endl;
00197 
00198   for (unsigned int i = 0; i < x.size(); i+=2 ){
00199     
00200     const Color & color = colors[i];
00201     setRgbColor ( color );
00202     setLineWidth ( size );
00203     setDash ( style );
00204   
00205     m_outfile << "gsave" << endl << "newpath systemdict begin" << endl;
00206     moveTo ( toViewX (x[i]),   toViewY (y[i]) );
00207     lineTo ( toViewX (x[i+1]), toViewY (y[i+1]) );
00208     m_outfile << "end" << endl;
00209     m_outfile << "stroke grestore" << endl;
00210     
00211   }
00212   
00213   m_outfile << "grestore" << endl;
00214 
00215 }
00216 
00219 void EpsView::drawViewLines ( const std::vector< double > & x,
00220                               const std::vector< double > & y,
00221                               hippodraw::Line::Style style,
00222                               bool, // color,
00223                               float size )
00224 {
00225   if ( style == Line::Invisible ) return;
00226 
00227   m_outfile << "%% drawViewLines1" << endl;
00228   
00229   m_outfile << "gsave" << endl;
00230 
00231   setLineWidth ( size );
00232   setDash ( style );
00233 
00234     for ( unsigned int i = 0; i < x.size(); i = i+2 )
00235     {
00236     
00237       m_outfile << "gsave" << endl << "newpath systemdict begin" << endl;
00238       moveTo ( toX(x[i]),   toY(y[i])   );
00239       lineTo ( toX(x[i+1]), toY(y[i+1]) );
00240       m_outfile << "end" << endl;
00241       m_outfile << "stroke grestore" << endl;
00242       
00243     }
00244 
00245     m_outfile << "grestore" << endl;
00246 
00247 }
00248  
00249 void EpsView::drawViewLines ( const std::vector< double > & x,
00250                               const std::vector< double > & y,
00251                               hippodraw::Line::Style style,
00252                               const Color & color,
00253                               float size )
00254 {
00255   if ( style == Line::Invisible ) return;
00256 
00257   m_outfile << "%% drawViewLines2" << endl;
00258 
00259   m_outfile << "gsave" << endl;
00260 
00261   setRgbColor ( color );
00262   setLineWidth ( size );
00263   setDash ( style );
00264   
00265   for ( unsigned int i = 0; i < x.size(); i = i+2 )
00266     {
00267       
00268       m_outfile << "gsave" << endl << "newpath systemdict begin" << endl;
00269       moveTo ( toX(x[i]),   toY(y[i])   );
00270       lineTo ( toX(x[i+1]), toY(y[i+1]) );
00271       m_outfile << "end" << endl;
00272       m_outfile << "stroke grestore" << endl;
00273       
00274     }
00275   
00276   m_outfile << "grestore" << endl;
00277   
00278 }
00279  
00280 void EpsView::drawPolyLine ( const std::vector< double > & xpoints,
00281                              const std::vector< double > & ypoints, 
00282                              hippodraw::Line::Style style,
00283                              const Color & color,
00284                              float size )
00285 {
00286   if ( style == Line::Invisible ) return;
00287 
00288   m_outfile << "%% drawPolyLine" << endl;
00289 
00290   m_outfile << "gsave" << endl;
00291 
00292   setRgbColor ( color );
00293   setLineWidth ( size );
00294   setDash ( style );
00295   
00296   m_outfile << "gsave" << endl << "newpath systemdict begin" << endl;
00297   moveTo ( toViewX ( xpoints[0] ), toViewY ( ypoints[0] ) );
00298 
00299   for ( unsigned int i = 1; i < xpoints.size(); i++ )
00300     {
00301       double view_x = toViewX ( xpoints[i] );
00302       double view_y = toViewY ( ypoints[i] );
00303       lineTo ( view_x, view_y );
00304     }
00305      
00306   m_outfile << "end" << endl;
00307   
00308   m_outfile << "stroke grestore" << endl;
00309 
00310   m_outfile << "grestore" << endl;
00311 
00312 }
00313 
00314 void
00315 EpsView::
00316 drawSquare ( double x1, double y1, double x2, double y2,
00317              int red, int green, int blue )
00318 {
00319   m_outfile << "%% drawSquareRGB" << endl;
00320 
00321   m_outfile << "gsave" << endl;
00322   
00323   setRgbColor ( red, green, blue );
00324   m_outfile << "newpath" << endl; 
00325   moveTo ( toViewX ((double)(x1)), toViewY ((double)(y1)) );
00326   lineTo ( toViewX ((double)(x2)), toViewY ((double)(y1)) );
00327   lineTo ( toViewX ((double)(x2)), toViewY ((double)(y2)) );
00328   lineTo ( toViewX ((double)(x1)), toViewY ((double)(y2)) );
00329   m_outfile << "closepath" << endl;
00330 
00331   m_outfile << "fill" << endl;
00332 
00333   m_outfile << "grestore" << endl;
00334 
00335 }
00336 
00337 void EpsView::drawViewSquare ( float x1, float y1, float x2, float y2,
00338                                int red, int green, int blue )
00339 {
00340   m_outfile << "%% drawSquareRGB" << endl;
00341 
00342   m_outfile << "gsave" << endl;
00343   
00344   setRgbColor ( red, green, blue );  
00345   m_outfile << "newpath" << endl; 
00346   moveTo ( toX ((double)(x1)), toY ((double)(y1)) );
00347   lineTo ( toX ((double)(x2)), toY ((double)(y1)) );
00348   lineTo ( toX ((double)(x2)), toY ((double)(y2)) );
00349   lineTo ( toX ((double)(x1)), toY ((double)(y2)) );
00350   m_outfile << "closepath" << endl;
00351 
00352   m_outfile << "fill" << endl;
00353 
00354   m_outfile << "grestore" << endl;
00355 
00356 }
00357 
00358 void
00359 EpsView::
00360 drawSymbol ( hippodraw::Symbol::Type type, 
00361              double x, double  y, float sym_size )
00362 {
00363   switch ( type )
00364     {
00365     case Symbol::SQUARE:
00366     case Symbol::SOLIDSQUARE:
00367           
00368       moveTo ( toViewX (x)-(sym_size/2), toViewY (y)-(sym_size/2) );
00369       lineTo ( toViewX (x)+(sym_size/2), toViewY (y)-(sym_size/2) );
00370       lineTo ( toViewX (x)+(sym_size/2), toViewY (y)+(sym_size/2) );
00371       lineTo ( toViewX (x)-(sym_size/2), toViewY (y)+(sym_size/2) );
00372 
00373       m_outfile << "closepath" << endl;
00374           
00375       break;
00376           
00377     case Symbol::TRIANGLE:
00378     case Symbol::FILLED_TRIANGLE:
00379       moveTo ( toViewX (x),                toViewY (y) + (sym_size/2) );
00380       lineTo ( toViewX (x) + (sym_size/2), toViewY (y)-(sym_size/2) );
00381       lineTo ( toViewX (x) - (sym_size/2), toViewY (y)-(sym_size/2) );
00382       m_outfile << "closepath" << endl;
00383       break;
00384           
00385     case Symbol::CIRCLE:
00386     case Symbol::FILLED_CIRCLE:
00387       moveTo ( toViewX (x) + (sym_size/2), toViewY (y) );
00388       m_outfile << toViewX (x) << " "
00389                 << toViewY (y) << " "
00390                 << sym_size/2 << " "
00391                 << "0.0 360 arc" << endl;
00392       break;
00393 
00394     case Symbol::PLUS:
00395       moveTo ( toViewX (x)-(sym_size/2), toViewY (y) );
00396       lineTo ( toViewX (x)+(sym_size/2), toViewY (y) );
00397       moveTo ( toViewX (x),              toViewY (y)-(sym_size/2) );
00398       lineTo ( toViewX (x),              toViewY (y)+(sym_size/2) );
00399       break;
00400           
00401     case Symbol::TIMES:
00402       moveTo ( toViewX (x)-(sym_size/2), toViewY (y)-(sym_size/2) );
00403       lineTo ( toViewX (x)+(sym_size/2), toViewY (y)+(sym_size/2) );
00404       lineTo ( toViewX (x)+(sym_size/2), toViewY (y)-(sym_size/2) );
00405       lineTo ( toViewX (x)-(sym_size/2), toViewY (y)+(sym_size/2) );
00406       break;
00407 
00408     default:
00409       break;
00410     }
00411 }
00412 
00413 void EpsView::drawPoints ( const std::vector<double> & x,
00414                            const std::vector<double> & y,
00415                            hippodraw::Symbol::Type type, 
00416                            float sym_size, 
00417                            const Color & color )
00418 {
00419   m_outfile << "%% drawPoints" << endl;
00420 
00421   m_outfile << "gsave" << endl;
00422 
00423   setRgbColor ( color );
00424 
00425   bool filled = false;
00426 
00427   if ( type == Symbol::SOLIDSQUARE ||
00428        type == Symbol::FILLED_TRIANGLE ||
00429        type == Symbol::FILLED_CIRCLE ) filled = true;
00430 
00431   m_outfile << "gsave" << endl;
00432   m_outfile << "newpath systemdict begin" << endl;
00433   
00434   for (unsigned int i = 0; i < x.size(); i++) {
00435       drawSymbol ( type, x[i], y[i], sym_size );
00436   }
00437 
00438   m_outfile << "end" << endl;
00439   
00440   if(filled)
00441     {
00442       m_outfile << "fill grestore" << endl;
00443     }
00444   else
00445     {
00446       m_outfile << "stroke grestore" << endl;
00447     }
00448 
00449   m_outfile << "grestore" << endl;
00450   
00451 }
00452 
00453 void
00454 EpsView::
00455 drawPoints ( const std::vector< double > & x,
00456              const std::vector< double > & y, 
00457              const std::vector< Color > & colors,
00458              hippodraw::Symbol::Type type, 
00459              float sym_size )
00460 {
00461   m_outfile << "%% drawPoints2" << endl;
00462 
00463   m_outfile << "gsave" << endl;
00464   
00465   bool filled = false;
00466 
00467   if ( type == Symbol::SOLIDSQUARE ||
00468        type == Symbol::FILLED_TRIANGLE ||
00469        type == Symbol::FILLED_CIRCLE ) filled = true;
00470 
00471   for (unsigned int i = 0; i < x.size(); i++)
00472     {
00473       m_outfile << "gsave" << endl;
00474 
00475       const Color & color = colors[i];
00476       setRgbColor ( color );
00477 
00478       m_outfile << "newpath systemdict begin" << endl;
00479       drawSymbol ( type, x[i], y[i], sym_size );
00480 
00481       m_outfile << "end" << endl;
00482       
00483       if ( filled ) {
00484         m_outfile << "fill grestore" << endl;
00485       }
00486       else {
00487         m_outfile << "stroke grestore" << endl;
00488       }
00489     }
00490   
00491   m_outfile << "grestore" << endl;
00492 
00493 }
00494 
00497 void EpsView::draw_Text ( const std::string &s, float x, float y,
00498                          float fontsize, float angle,
00499                          char xp, char yp, const FontBase * font )
00500 {
00501   float xStep = 0.0, yStep = 0.0;
00502      
00503   switch (yp)
00504     {
00505     case 'c':
00506       yStep = 0.4f;
00507       break;
00508           
00509     case 't':
00510       yStep = 0.8f;
00511       break;
00512 
00513     default:
00514       break;
00515     }
00516   
00517   switch (xp)
00518     {
00519 
00520     case 'c':
00521       xStep = 0.5f;
00522       break;
00523 
00524     case 'r':
00525       xStep = 1.0f;
00526       break;
00527 
00528     default:
00529       break;
00530     }
00531      
00532   m_outfile << "gsave" << endl;
00533 
00534   int size = 0;
00535   if ( font == 0 ) {
00536     m_outfile << "(Helvetica) " << fontsize << " SF" << endl;
00537     size = static_cast < int > ( fontsize );
00538   }
00539   else {
00540     string family ( "(" );
00541     family += font -> family ();
00542     family += ") ";
00543     size = font -> pointSize ();
00544     m_outfile << family << size << " SF" << endl;
00545   }
00546   m_outfile << x << " " << y << " translate " << angle << " rotate" << endl;
00547      
00548   m_outfile << "(" << s << ") stringwidth pop" << endl;
00549   m_outfile << xStep << " neg mul " << -size*yStep << " moveto" << endl;
00550   m_outfile << "(" << s << ") show" << endl;
00551   m_outfile << "grestore" << endl;
00552 
00553 }
00554 
00558 void EpsView::drawText ( const std::string &s, float x, float y,
00559                          float fontsize, float angle,
00560                          char xp, char yp, bool, // resize,
00561                          const FontBase * font,
00562                          const Color * color )
00563 {
00564   // Resize ignored as the view size is always just bounding the contents
00565   m_outfile << "%% drawText" << endl;
00566      
00567   draw_Text ( s, toX(x), toY(y), fontsize, angle, xp, yp, font );
00568 }
00569 
00570 void EpsView:: update ( const Observable * )
00571 {
00572 }
00573 
00576 float EpsView::userToDrawX ( double x ) const
00577 {
00578   return userToMarginX ( x );
00579 }
00580 
00581 float EpsView::userToDrawXAutoInv ( double x ) const
00582 {
00583   if (m_plotter -> isReverse())
00584     return userToInvertedMarginX( x );
00585   else 
00586     return userToMarginX( x );
00587 }
00588 
00591 float EpsView::userToDrawY ( double y ) const
00592 {
00593   return userToInvertedMarginY( y );
00594 }
00595 
00598 float EpsView::userToDrawColor ( double c ) const
00599 {
00600   return userToMarginColor( c );
00601 }
00602 
00603 Rect EpsView::getDrawRect() const
00604 {
00605   return m_draw_rect;
00606 }
00607 
00608 void EpsView::setDrawRect ( float x, float y, float w, float h )
00609 {
00610   m_draw_rect.setRect ( x, y, w, h );
00611 }
00612 
00613 float EpsView::toViewX ( double datX ) const
00614 {
00615   double view_x =  ( m_draw_rect.getX() 
00616            + userToMarginX ( datX )
00617            - m_boundingRect.getX() );
00618 
00619   return view_x;
00620 }
00621 
00622 float EpsView::toViewY ( double datY ) const
00623 {
00624   /* If you remove the s1 and the s2 from the return statements, you get
00625      the plots that look OK, but a plot on top in the canvas appears at the
00626      bottom in the EPSF file. Subtracting from s2 vertically inverts each
00627      plot, and then subtracting from s1 vertically inverts the entire EPSF
00628      file. The result is what is desired - Sanket. */
00629 
00630   double s1 = m_boundingRect.getY() + m_boundingRect.getHeight();
00631 
00632   double s2 = m_draw_rect.getY() + m_draw_rect.getHeight();
00633 
00634   const Rect & margin_rect = getMarginRect ();
00635 
00636   double mar_y = userToMarginY ( datY );
00637   return ( s1 - ( s2 -  ( mar_y
00638                           - 2 * margin_rect.getY()
00639                           + m_draw_rect.getHeight()
00640                           - margin_rect.getHeight()
00641                           )
00642                   )
00643            );
00644 }
00645 
00646 float EpsView::toX ( double x ) const
00647 {  
00648   return static_cast < float > ( m_draw_rect.getX() 
00649                                  + x
00650                                  - m_boundingRect.getX() );
00651 }
00652 
00653 float EpsView::toY ( double y ) const
00654 {
00655   float s1 = m_boundingRect.getY() + m_boundingRect.getHeight();
00656   
00657   return static_cast < float > ( s1 - ( m_draw_rect.getY() + y ) );
00658 }

Generated for HippoDraw Class Library by doxygen