EpsView.cxx
Go to the documentation of this file.
1 
12 #ifdef _MSC_VER
13 #include "msdevstudio/MSconfig.h"
14 #endif
15 
16 #include "EpsView.h"
17 
18 #include "FontBase.h"
19 #include "Color.h"
20 
21 #include "plotters/PlotterBase.h"
22 
23 
24 #ifdef ITERATOR_MEMBER_DEFECT
25 using namespace std;
26 #else
27 using std::endl;
28 using std::ofstream;
29 using std::string;
30 #endif
31 
32 using namespace hippodraw;
33 
34 EpsView::
35 EpsView ( const std::string filename, double x, double y, double w, double h )
36  : DataView(),
37  m_boundingRect ( x, y, w, h )
38 {
39  initPlot ( filename, x, y, w, h );
40 }
41 
44 {
45 }
46 
47 void
49 lineTo ( double x, double y )
50 {
51  m_outfile << x << " " << y << " lineto\n";
52 }
53 
54 void
56 moveTo ( double x, double y )
57 {
58  m_outfile << x << " " << y << " moveto\n";
59 }
60 
61 void
64 {
65  switch (style)
66  {
67  case Line::Solid:
68  m_outfile << "[] 0 setdash\n";
69  break;
70  case Line::Dot:
71  m_outfile << "[3 5] 0 setdash\n";
72  break;
73  case Line::Dash:
74  m_outfile << "[5 3] 0 setdash\n";
75  break;
76  case Line::DashDot:
77  m_outfile << "[5 3 1 3] 0 setdash\n";
78  break;
79  default:
80  break;
81  }
82 }
83 
84 void
86 setLineWidth ( double size )
87 {
88  m_outfile << size << " setlinewidth\n";
89 }
90 
91 void
93 setRgbColor ( int red, int green, int blue )
94 {
95  double inv = 1. / 255.;
96  m_outfile << red * inv << " "
97  << green * inv << " "
98  << blue * inv << " setrgbcolor\n";
99 }
100 
101 void
102 EpsView::
103 setRgbColor ( const Color & color )
104 {
105  setRgbColor ( color.getRed (), color.getGreen (), color.getBlue () );
106 }
111 void EpsView::initPlot ( const std::string & fname,
112  double, double, double w, double h )
113 {
114 
115  const char * fn = fname.c_str();
116  m_outfile.open (fn, std::ios::out);
117 
118  m_outfile << "%%!PS-Adobe-3.0 EPSF-3.0" << endl;
119  m_outfile << "%%Creator: HippoDraw" << endl;
120 
121  double x1 = 0;
122  double y1 = 0;
123  double x2 = x1 + w;
124  double y2 = y1 + h;
125 
126  m_outfile << "%%BoundingBox: "
127  << x1 << " " << y1 << " "
128  << x2 << " " << y2 << endl;
129 
130  m_outfile << "%%EndComments" << endl;
131  m_outfile << endl << endl;
132 
133  m_outfile << "%% Add emulation of selectfont if needed" << endl;
134  m_outfile << "%% taken from PS Lang. Ref. Manual, Appendix D.4" << endl;
135  m_outfile << "/*SF {" << endl;
136  m_outfile << " exch findfont exch" << endl;
137  m_outfile << " dup type /arraytype eq {makefont}{scalefont} ifelse setfont"
138  << endl;
139  m_outfile << "} bind def" << endl;
140  m_outfile << endl;
141  m_outfile << "/languagelevel where" << endl;
142  m_outfile << " {pop languagelevel} {1} ifelse" << endl;
143  m_outfile << "2 lt {/SF /*SF load def}{/SF /selectfont load def} ifelse"
144  << endl;
145 
146  m_outfile << "%%" << endl << "%%" << endl;
147 }
148 
150 {
151  m_outfile << "%%EOF" << endl;
152  m_outfile.close();
153 }
154 
155 void EpsView::drawLines ( const std::vector< double > & x,
156  const std::vector< double > & y,
158  const Color & color,
159  float size )
160 {
161  if ( style == Line::Invisible ) return;
162 
163  m_outfile << "%% drawLines" << endl;
164 
165  m_outfile << "gsave" << endl;
166 
167  setRgbColor ( color );
168 
169  setLineWidth ( size );
170  setDash ( style );
171 
172  for ( unsigned int i = 0; i < x.size(); i = i+2 )
173  {
174 
175  m_outfile << "gsave" << endl << "newpath systemdict begin" << endl;
176  moveTo ( toViewX (x[i]), toViewY (y[i]) );
177  lineTo ( toViewX (x[i+1]), toViewY (y[i+1]) );
178  m_outfile << "end" << endl;
179  m_outfile << "stroke grestore" << endl;
180 
181  }
182 
183  m_outfile << "grestore" << endl;
184 
185 }
186 
187 void EpsView::drawColorLines ( const std::vector< double > & x,
188  const std::vector< double > & y,
190  const std::vector < Color > & colors,
191  float size )
192 {
193  if ( style == Line::Invisible ) return;
194 
195  m_outfile << "%% drawLines" << endl;
196  m_outfile << "gsave" << endl;
197 
198  for (unsigned int i = 0; i < x.size(); i+=2 ){
199 
200  const Color & color = colors[i];
201  setRgbColor ( color );
202  setLineWidth ( size );
203  setDash ( style );
204 
205  m_outfile << "gsave" << endl << "newpath systemdict begin" << endl;
206  moveTo ( toViewX (x[i]), toViewY (y[i]) );
207  lineTo ( toViewX (x[i+1]), toViewY (y[i+1]) );
208  m_outfile << "end" << endl;
209  m_outfile << "stroke grestore" << endl;
210 
211  }
212 
213  m_outfile << "grestore" << endl;
214 
215 }
216 
219 void EpsView::drawViewLines ( const std::vector< double > & x,
220  const std::vector< double > & y,
222  bool, // color,
223  float size )
224 {
225  if ( style == Line::Invisible ) return;
226 
227  m_outfile << "%% drawViewLines1" << endl;
228 
229  m_outfile << "gsave" << endl;
230 
231  setLineWidth ( size );
232  setDash ( style );
233 
234  for ( unsigned int i = 0; i < x.size(); i = i+2 )
235  {
236 
237  m_outfile << "gsave" << endl << "newpath systemdict begin" << endl;
238  moveTo ( toX(x[i]), toY(y[i]) );
239  lineTo ( toX(x[i+1]), toY(y[i+1]) );
240  m_outfile << "end" << endl;
241  m_outfile << "stroke grestore" << endl;
242 
243  }
244 
245  m_outfile << "grestore" << endl;
246 
247 }
248 
249 void EpsView::drawViewLines ( const std::vector< double > & x,
250  const std::vector< double > & y,
252  const Color & color,
253  float size )
254 {
255  if ( style == Line::Invisible ) return;
256 
257  m_outfile << "%% drawViewLines2" << endl;
258 
259  m_outfile << "gsave" << endl;
260 
261  setRgbColor ( color );
262  setLineWidth ( size );
263  setDash ( style );
264 
265  for ( unsigned int i = 0; i < x.size(); i = i+2 )
266  {
267 
268  m_outfile << "gsave" << endl << "newpath systemdict begin" << endl;
269  moveTo ( toX(x[i]), toY(y[i]) );
270  lineTo ( toX(x[i+1]), toY(y[i+1]) );
271  m_outfile << "end" << endl;
272  m_outfile << "stroke grestore" << endl;
273 
274  }
275 
276  m_outfile << "grestore" << endl;
277 
278 }
279 
280 void EpsView::drawPolyLine ( const std::vector< double > & xpoints,
281  const std::vector< double > & ypoints,
283  const Color & color,
284  float size )
285 {
286  if ( style == Line::Invisible ) return;
287 
288  m_outfile << "%% drawPolyLine" << endl;
289 
290  m_outfile << "gsave" << endl;
291 
292  setRgbColor ( color );
293  setLineWidth ( size );
294  setDash ( style );
295 
296  m_outfile << "gsave" << endl << "newpath systemdict begin" << endl;
297  moveTo ( toViewX ( xpoints[0] ), toViewY ( ypoints[0] ) );
298 
299  for ( unsigned int i = 1; i < xpoints.size(); i++ )
300  {
301  double view_x = toViewX ( xpoints[i] );
302  double view_y = toViewY ( ypoints[i] );
303  lineTo ( view_x, view_y );
304  }
305 
306  m_outfile << "end" << endl;
307 
308  m_outfile << "stroke grestore" << endl;
309 
310  m_outfile << "grestore" << endl;
311 
312 }
313 
314 void
315 EpsView::
316 drawSquare ( double x1, double y1, double x2, double y2,
317  int red, int green, int blue )
318 {
319  m_outfile << "%% drawSquareRGB" << endl;
320 
321  m_outfile << "gsave" << endl;
322 
323  setRgbColor ( red, green, blue );
324  m_outfile << "newpath" << endl;
325  moveTo ( toViewX ((double)(x1)), toViewY ((double)(y1)) );
326  lineTo ( toViewX ((double)(x2)), toViewY ((double)(y1)) );
327  lineTo ( toViewX ((double)(x2)), toViewY ((double)(y2)) );
328  lineTo ( toViewX ((double)(x1)), toViewY ((double)(y2)) );
329  m_outfile << "closepath" << endl;
330 
331  m_outfile << "fill" << endl;
332 
333  m_outfile << "grestore" << endl;
334 
335 }
336 
337 void EpsView::drawViewSquare ( float x1, float y1, float x2, float y2,
338  int red, int green, int blue )
339 {
340  m_outfile << "%% drawSquareRGB" << endl;
341 
342  m_outfile << "gsave" << endl;
343 
344  setRgbColor ( red, green, blue );
345  m_outfile << "newpath" << endl;
346  moveTo ( toX ((double)(x1)), toY ((double)(y1)) );
347  lineTo ( toX ((double)(x2)), toY ((double)(y1)) );
348  lineTo ( toX ((double)(x2)), toY ((double)(y2)) );
349  lineTo ( toX ((double)(x1)), toY ((double)(y2)) );
350  m_outfile << "closepath" << endl;
351 
352  m_outfile << "fill" << endl;
353 
354  m_outfile << "grestore" << endl;
355 
356 }
357 
358 void
359 EpsView::
361  double x, double y, float sym_size )
362 {
363  switch ( type )
364  {
365  case Symbol::SQUARE:
366  case Symbol::SOLIDSQUARE:
367 
368  moveTo ( toViewX (x)-(sym_size/2), toViewY (y)-(sym_size/2) );
369  lineTo ( toViewX (x)+(sym_size/2), toViewY (y)-(sym_size/2) );
370  lineTo ( toViewX (x)+(sym_size/2), toViewY (y)+(sym_size/2) );
371  lineTo ( toViewX (x)-(sym_size/2), toViewY (y)+(sym_size/2) );
372 
373  m_outfile << "closepath" << endl;
374 
375  break;
376 
377  case Symbol::TRIANGLE:
379  moveTo ( toViewX (x), toViewY (y) + (sym_size/2) );
380  lineTo ( toViewX (x) + (sym_size/2), toViewY (y)-(sym_size/2) );
381  lineTo ( toViewX (x) - (sym_size/2), toViewY (y)-(sym_size/2) );
382  m_outfile << "closepath" << endl;
383  break;
384 
385  case Symbol::CIRCLE:
387  moveTo ( toViewX (x) + (sym_size/2), toViewY (y) );
388  m_outfile << toViewX (x) << " "
389  << toViewY (y) << " "
390  << sym_size/2 << " "
391  << "0.0 360 arc" << endl;
392  break;
393 
394  case Symbol::PLUS:
395  moveTo ( toViewX (x)-(sym_size/2), toViewY (y) );
396  lineTo ( toViewX (x)+(sym_size/2), toViewY (y) );
397  moveTo ( toViewX (x), toViewY (y)-(sym_size/2) );
398  lineTo ( toViewX (x), toViewY (y)+(sym_size/2) );
399  break;
400 
401  case Symbol::TIMES:
402  moveTo ( toViewX (x)-(sym_size/2), toViewY (y)-(sym_size/2) );
403  lineTo ( toViewX (x)+(sym_size/2), toViewY (y)+(sym_size/2) );
404  lineTo ( toViewX (x)+(sym_size/2), toViewY (y)-(sym_size/2) );
405  lineTo ( toViewX (x)-(sym_size/2), toViewY (y)+(sym_size/2) );
406  break;
407 
408  default:
409  break;
410  }
411 }
412 
413 void EpsView::drawPoints ( const std::vector<double> & x,
414  const std::vector<double> & y,
416  float sym_size,
417  const Color & color )
418 {
419  m_outfile << "%% drawPoints" << endl;
420 
421  m_outfile << "gsave" << endl;
422 
423  setRgbColor ( color );
424 
425  bool filled = false;
426 
427  if ( type == Symbol::SOLIDSQUARE ||
428  type == Symbol::FILLED_TRIANGLE ||
429  type == Symbol::FILLED_CIRCLE ) filled = true;
430 
431  m_outfile << "gsave" << endl;
432  m_outfile << "newpath systemdict begin" << endl;
433 
434  for (unsigned int i = 0; i < x.size(); i++) {
435  drawSymbol ( type, x[i], y[i], sym_size );
436  }
437 
438  m_outfile << "end" << endl;
439 
440  if(filled)
441  {
442  m_outfile << "fill grestore" << endl;
443  }
444  else
445  {
446  m_outfile << "stroke grestore" << endl;
447  }
448 
449  m_outfile << "grestore" << endl;
450 
451 }
452 
453 void
454 EpsView::
455 drawPoints ( const std::vector< double > & x,
456  const std::vector< double > & y,
457  const std::vector< Color > & colors,
459  float sym_size )
460 {
461  m_outfile << "%% drawPoints2" << endl;
462 
463  m_outfile << "gsave" << endl;
464 
465  bool filled = false;
466 
467  if ( type == Symbol::SOLIDSQUARE ||
468  type == Symbol::FILLED_TRIANGLE ||
469  type == Symbol::FILLED_CIRCLE ) filled = true;
470 
471  for (unsigned int i = 0; i < x.size(); i++)
472  {
473  m_outfile << "gsave" << endl;
474 
475  const Color & color = colors[i];
476  setRgbColor ( color );
477 
478  m_outfile << "newpath systemdict begin" << endl;
479  drawSymbol ( type, x[i], y[i], sym_size );
480 
481  m_outfile << "end" << endl;
482 
483  if ( filled ) {
484  m_outfile << "fill grestore" << endl;
485  }
486  else {
487  m_outfile << "stroke grestore" << endl;
488  }
489  }
490 
491  m_outfile << "grestore" << endl;
492 
493 }
494 
497 void EpsView::draw_Text ( const std::string &s, float x, float y,
498  float fontsize, float angle,
499  char xp, char yp, const FontBase * font )
500 {
501  float xStep = 0.0, yStep = 0.0;
502 
503  switch (yp)
504  {
505  case 'c':
506  yStep = 0.4f;
507  break;
508 
509  case 't':
510  yStep = 0.8f;
511  break;
512 
513  default:
514  break;
515  }
516 
517  switch (xp)
518  {
519 
520  case 'c':
521  xStep = 0.5f;
522  break;
523 
524  case 'r':
525  xStep = 1.0f;
526  break;
527 
528  default:
529  break;
530  }
531 
532  m_outfile << "gsave" << endl;
533 
534  int size = 0;
535  if ( font == 0 ) {
536  m_outfile << "(Helvetica) " << fontsize << " SF" << endl;
537  size = static_cast < int > ( fontsize );
538  }
539  else {
540  string family ( "(" );
541  family += font -> family ();
542  family += ") ";
543  size = font -> pointSize ();
544  m_outfile << family << size << " SF" << endl;
545  }
546  m_outfile << x << " " << y << " translate " << angle << " rotate" << endl;
547 
548  m_outfile << "(" << s << ") stringwidth pop" << endl;
549  m_outfile << xStep << " neg mul " << -size*yStep << " moveto" << endl;
550  m_outfile << "(" << s << ") show" << endl;
551  m_outfile << "grestore" << endl;
552 
553 }
554 
558 void EpsView::drawText ( const std::string &s, float x, float y,
559  float fontsize, float angle,
560  char xp, char yp, bool, // resize,
561  const FontBase * font,
562  const Color * color )
563 {
564  // Resize ignored as the view size is always just bounding the contents
565  m_outfile << "%% drawText" << endl;
566 
567  draw_Text ( s, toX(x), toY(y), fontsize, angle, xp, yp, font );
568 }
569 
570 void EpsView:: update ( const Observable * )
571 {
572 }
573 
576 float EpsView::userToDrawX ( double x ) const
577 {
578  return userToMarginX ( x );
579 }
580 
581 float EpsView::userToDrawXAutoInv ( double x ) const
582 {
583  if (m_plotter -> isReverse())
584  return userToInvertedMarginX( x );
585  else
586  return userToMarginX( x );
587 }
588 
591 float EpsView::userToDrawY ( double y ) const
592 {
593  return userToInvertedMarginY( y );
594 }
595 
598 float EpsView::userToDrawColor ( double c ) const
599 {
600  return userToMarginColor( c );
601 }
602 
604 {
605  return m_draw_rect;
606 }
607 
608 void EpsView::setDrawRect ( float x, float y, float w, float h )
609 {
610  m_draw_rect.setRect ( x, y, w, h );
611 }
612 
613 float EpsView::toViewX ( double datX ) const
614 {
615  double view_x = ( m_draw_rect.getX()
616  + userToMarginX ( datX )
617  - m_boundingRect.getX() );
618 
619  return view_x;
620 }
621 
622 float EpsView::toViewY ( double datY ) const
623 {
624  /* If you remove the s1 and the s2 from the return statements, you get
625  the plots that look OK, but a plot on top in the canvas appears at the
626  bottom in the EPSF file. Subtracting from s2 vertically inverts each
627  plot, and then subtracting from s1 vertically inverts the entire EPSF
628  file. The result is what is desired - Sanket. */
629 
630  double s1 = m_boundingRect.getY() + m_boundingRect.getHeight();
631 
632  double s2 = m_draw_rect.getY() + m_draw_rect.getHeight();
633 
634  const Rect & margin_rect = getMarginRect ();
635 
636  double mar_y = userToMarginY ( datY );
637  return ( s1 - ( s2 - ( mar_y
638  - 2 * margin_rect.getY()
640  - margin_rect.getHeight()
641  )
642  )
643  );
644 }
645 
646 float EpsView::toX ( double x ) const
647 {
648  return static_cast < float > ( m_draw_rect.getX()
649  + x
650  - m_boundingRect.getX() );
651 }
652 
653 float EpsView::toY ( double y ) const
654 {
655  float s1 = m_boundingRect.getY() + m_boundingRect.getHeight();
656 
657  return static_cast < float > ( s1 - ( m_draw_rect.getY() + y ) );
658 }

Generated for HippoDraw Class Library by doxygen