# HG changeset patch # User alpar # Date 1104920077 0 # Node ID 4ebe32765b48f1a5b8e619f9a5f9ebcc3a0a9842 # Parent bcc0766a7b8674c2a6624bf0b315ed0de7cc1e61 graphToEps is now able to write to any ostream. diff -r bcc0766a7b86 -r 4ebe32765b48 src/work/alpar/graph_to_eps.cc --- a/src/work/alpar/graph_to_eps.cc Tue Jan 04 22:16:46 2005 +0000 +++ b/src/work/alpar/graph_to_eps.cc Wed Jan 05 10:14:37 2005 +0000 @@ -1,4 +1,6 @@ +#include #include + #include #include #include @@ -49,6 +51,9 @@ const Graph &g; + + std::ostream& os; + ConstMap > _coords; ConstMap _nodeSizes; @@ -70,9 +75,12 @@ ///Constructor ///Constructor - ///\param _g is a reference to the underlying graph - DefaultGraphToEpsTraits(const G &_g) : - g(_g), _coords(xy(1,1)), _nodeSizes(1.0), + ///\param _g is a reference to the graph to be printed + ///\param _os is a reference to the output stream. + ///By default it is std::cout + DefaultGraphToEpsTraits(const G &_g,std::ostream& _os=std::cout) : + g(_g), os(_os), + _coords(xy(1,1)), _nodeSizes(1.0), _nodeColors(Color(1,1,1)), _edgeColors(Color(0,0,0)), _edgeWidths(1), _edgeWidthScale(0.3), _nodeScale(1.0), _xBorder(10), _yBorder(10), _scale(1.0), @@ -214,7 +222,7 @@ { if(dontPrint) return; - cout << "%!PS-Adobe-2.0 EPSF-2.0\n"; + os << "%!PS-Adobe-2.0 EPSF-2.0\n"; //\todo: Chech whether the graph is empty. BoundingBox bb; for(NodeIt n(g);n!=INVALID;++n) { @@ -223,26 +231,26 @@ bb+=p+_coords[n]; bb+=-p+_coords[n]; } - cout << "%%BoundingBox: " + os << "%%BoundingBox: " << bb.left()* _scale-_xBorder << ' ' << bb.bottom()*_scale-_yBorder << ' ' << bb.right()* _scale+_xBorder << ' ' << bb.top()* _scale+_yBorder << '\n'; //x1 y1 x2 y2 cr cg cb w - cout << "/l { setlinewidth setrgbcolor newpath moveto lineto stroke } bind def\n"; - cout << "/c { newpath dup 3 index add 2 index moveto 0 360 arc } bind def\n"; + os << "/l { setlinewidth setrgbcolor newpath moveto lineto stroke } bind def\n"; + os << "/c { newpath dup 3 index add 2 index moveto 0 360 arc } bind def\n"; // x y r cr cg cb - cout << "/n { setrgbcolor 2 index 2 index 2 index c fill\n" + os << "/n { setrgbcolor 2 index 2 index 2 index c fill\n" << " 0 0 0 setrgbcolor dup " << _nodeBorderQuotient << " mul setlinewidth " << 1+_nodeBorderQuotient/2 << " div c stroke\n" << " } bind def\n"; - cout << "/arrl " << _arrowLength << " def\n"; - cout << "/arrw " << _arrowWidth << " def\n"; + os << "/arrl " << _arrowLength << " def\n"; + os << "/arrw " << _arrowWidth << " def\n"; // l dx_norm dy_norm - cout << "/lrl { 2 index mul exch 2 index mul exch rlineto pop} bind def\n"; + os << "/lrl { 2 index mul exch 2 index mul exch rlineto pop} bind def\n"; //len w dx_norm dy_norm x1 y1 cr cg cb - cout << "/arr { setrgbcolor /y1 exch def /x1 exch def /dy exch def /dx exch def\n" + os << "/arr { setrgbcolor /y1 exch def /x1 exch def /dy exch def /dx exch def\n" << " /w exch def /len exch def\n" // << " 0.1 setlinewidth x1 y1 moveto dx len mul dy len mul rlineto stroke" << " newpath x1 dy w 2 div mul add y1 dx w 2 div mul sub moveto\n" @@ -255,9 +263,9 @@ << " arrw dy dx neg lrl\n" << " len w sub arrl sub neg dx dy lrl\n" << " closepath fill } bind def\n"; - cout << "\ngsave\n"; - if(_scale!=1.0) cout << _scale << " dup scale\n"; - cout << "%Edges:\ngsave\n"; + os << "\ngsave\n"; + if(_scale!=1.0) os << _scale << " dup scale\n"; + os << "%Edges:\ngsave\n"; for(NodeIt n(g);n!=INVALID;++n) for(OutEdgeIt e(g,n);e!=INVALID;++e) if(_drawArrows) { @@ -266,7 +274,7 @@ d/=l; xy x1(d*_nodeScale*_nodeSizes[g.source(e)]+ _coords[g.source(e)]); - cout << l-(_nodeSizes[g.source(e)]+ + os << l-(_nodeSizes[g.source(e)]+ _nodeSizes[g.target(e)])*_nodeScale << ' ' << _edgeWidths[e]*_edgeWidthScale << ' ' << d.x << ' ' << d.y << ' ' @@ -275,7 +283,7 @@ << _edgeColors[e].getG() << ' ' << _edgeColors[e].getB() << " arr\n"; } - else cout << _coords[g.source(e)].x << ' ' + else os << _coords[g.source(e)].x << ' ' << _coords[g.source(e)].y << ' ' << _coords[g.target(e)].x << ' ' << _coords[g.target(e)].y << ' ' @@ -283,14 +291,14 @@ << _edgeColors[e].getG() << ' ' << _edgeColors[e].getB() << ' ' << _edgeWidths[e]*_edgeWidthScale << " l\n"; - cout << "grestore\n%Nodes:\ngsave\n"; + os << "grestore\n%Nodes:\ngsave\n"; for(NodeIt n(g);n!=INVALID;++n) - cout << _coords[n].x << ' ' << _coords[n].y << ' ' + os << _coords[n].x << ' ' << _coords[n].y << ' ' << _nodeSizes[n]*_nodeScale << ' ' << _nodeColors[n].getR() << ' ' << _nodeColors[n].getG() << ' ' << _nodeColors[n].getB() << " n\n"; - cout << "grestore\ngrestore\n"; + os << "grestore\ngrestore\n"; } }; @@ -299,8 +307,11 @@ ///\ingroup misc ///Generates an EPS file from a graph. +///\param g is a reference to the graph to be printed +///\param os is a reference to the output stream. +///By default it is std::cout /// -///This function has a lot of \ref named-templ-param "named parameters", +///This function also has a lot of \ref named-templ-param "named parameters", ///they are declared as the members of class \ref GraphToEps. The following ///example shows how to use these parameters. ///\code @@ -310,7 +321,7 @@ ///\endcode ///\sa GraphToEps template -GraphToEps > graphToEps(G &g) +GraphToEps > graphToEps(G &g,std::ostream& os=std::cout) { return GraphToEps >(DefaultGraphToEpsTraits(g)); }