hex::svg::Document Class Reference

#include <hexsvg.h>

List of all members.


Detailed Description

Definition at line 60 of file hexsvg.h.

Public Member Functions

 Document (const Grid &grid)
Point T (const Point &p) const
 Transform Point p into the document's co-ordinate system.
std::string header (void) const
std::string footer (void) const
std::string draw_simple_area (const Area &a, float bias=0.0)
 Draws the boundary around an area, without worrying about voids within.
std::string draw_complex_area (const Area &a, float bias=0.0)
 Draws the boundary around an area, taking account of voids within.
std::string draw_skeleton (const Area &a, bool include_boundary=true)
std::string draw_boundary (const Boundary &b, float bias=0.0)
 Draws a boundary line.
std::string draw_path (const Path &p)
 Draws a path line.
std::string draw_poly (std::list< Point > points, bool closed, const Identity *identity=NULL)
 Series of points, rendered as an SVG polygon, or polyline, depending upon whether they are closed or not.

Public Attributes

Point p0
 Lower left corner of the document.
Point p1
 Upper right corner of the document.
std::list< std::string > stylesheets
 List of stylesheets to import.
std::list< std::string > defs
 Fragments insert into the <defs> element.


Constructor & Destructor Documentation

hex::svg::Document::Document ( const Grid grid  ) 

Definition at line 341 of file svg.cc.

00342   : _grid(grid),
00343     p0(0.0,0.0),
00344     p1(grid.width(),grid.height()), 
00345     stylesheets(),
00346     defs()
00347 {}


Member Function Documentation

Point hex::svg::Document::T ( const Point p  )  const

Transform Point p into the document's co-ordinate system.

Definition at line 146 of file svg.cc.

References p0, p1, hex::Point::x, and hex::Point::y.

Referenced by draw_poly(), draw_skeleton(), and hex::svg::output_path_data().

00147 {
00148   return Point( p.x - p0.x, p1.y - p.y );
00149 }

std::string hex::svg::Document::header ( void   )  const

Definition at line 153 of file svg.cc.

References defs, p0, p1, stylesheets, hex::Point::x, and hex::Point::y.

Referenced by main().

00154 {
00155   std::ostringstream os;
00156   os<<
00157     "<?xml version=\"1.0\" standalone=\"no\"?>\n"
00158     "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\" "
00159       "\"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n";
00160 #if defined(HEX_EXTERNAL_CSS) && HEX_EXTERNAL_CSS!=0
00161   for(std::list<std::string>::const_iterator s =this->stylesheets.begin();
00162                                              s!=this->stylesheets.end();
00163                                            ++s)
00164   {
00165     os<<"<?xml-stylesheet href=\""<<(*s)<<"\" type=\"text/css\"?>\n";
00166   }
00167 #endif
00168   Point extent =p1-p0;
00169   os<<
00170     "<svg width=\"100%\" height=\"100%\" viewBox=\""
00171     << 0L <<" "<< 0L << " " << extent.x <<" "<< extent.y <<
00172     "\" version=\"1.1\""
00173       " xmlns=\"http://www.w3.org/2000/svg\""
00174       " xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n"
00175 
00176     "<defs>\n";
00177 #if !defined(HEX_EXTERNAL_CSS) || HEX_EXTERNAL_CSS==0
00178   if(!this->stylesheets.empty())
00179   {
00180     os<<"<style type=\"text/css\"><![CDATA[\n";
00181     for(std::list<std::string>::const_iterator s =this->stylesheets.begin();
00182                                                s!=this->stylesheets.end();
00183                                              ++s)
00184     {
00185       std::ifstream css(s->c_str());
00186 
00187       // Copy the css file to os.
00188       if(css)
00189       {
00190         char buf[1024];
00191         std::streamsize total =0;
00192         while(css.good())
00193         {
00194           std::streamsize bytes =css.readsome(buf,sizeof(buf));
00195           if(!bytes)
00196             break;
00197           total+=bytes;
00198           os.write(buf,bytes);
00199         }
00200       }
00201     }
00202     os<<"]]></style>\n";
00203   }
00204 #endif
00205   os<<
00206     "<marker id=\"Triangle\""
00207     " viewBox=\"0 0 10 10\" refX=\"0\" refY=\"5\" "
00208     " markerUnits=\"strokeWidth\""
00209     " markerWidth=\"4\" markerHeight=\"3\""
00210     " orient=\"auto\">\n"
00211     "<path d=\"M 0 0 L 10 5 L 0 10 z\" />\n"
00212     "</marker>\n";
00213   for(std::list<std::string>::const_iterator d =this->defs.begin();
00214                                              d!=this->defs.end();
00215                                            ++d)
00216   {
00217     os<<(*d)<<"\n";
00218   }
00219   os<<"</defs>\n";
00220   return os.str();
00221 }

std::string hex::svg::Document::footer ( void   )  const

Definition at line 225 of file svg.cc.

Referenced by main().

00226 {
00227   return "</svg>\n";
00228 }

std::string hex::svg::Document::draw_simple_area ( const Area a,
float  bias = 0.0 
)

Draws the boundary around an area, without worrying about voids within.

Definition at line 235 of file svg.cc.

References hex::Area::boundary(), draw_poly(), and hex::Boundary::stroke().

Referenced by main().

00236 {
00237   return draw_poly(a.boundary().stroke(bias),true,&a);
00238 }

std::string hex::svg::Document::draw_complex_area ( const Area a,
float  bias = 0.0 
)

Draws the boundary around an area, taking account of voids within.

Definition at line 242 of file svg.cc.

References hex::svg::Identity::attributes(), hex::Area::boundary(), hex::Area::enclosed_areas(), hex::svg::output_path_data(), and hex::Boundary::stroke().

Referenced by main().

00243 {
00244   using namespace std;
00245   std::ostringstream os;
00246   os<<"<path fill-rule=\"nonzero\""<<a.attributes()<<" d=\"";
00247   const std::list<Point> apoints =a.boundary().stroke(bias);
00248   output_path_data(*this,os,apoints.begin(),apoints.end());
00249   std::list<Area> voids =a.enclosed_areas();
00250   for(list<Area>::const_iterator v=voids.begin(); v!=voids.end(); ++v)
00251   {
00252     os<<" ";
00253     const std::list<Point> vpoints =v->boundary().stroke(-bias);
00254     output_path_data(*this,os,vpoints.rbegin(),vpoints.rend());
00255   }
00256   os<<"\"/>\n";
00257   return os.str();
00258 }

std::string hex::svg::Document::draw_skeleton ( const Area a,
bool  include_boundary = true 
)

Definition at line 262 of file svg.cc.

References hex::svg::Identity::attributes(), hex::svg::path_append(), hex::Area::skeleton(), and T().

Referenced by main().

00263 {
00264   std::ostringstream os;
00265   os<<"<path"<<a.attributes()<<" d=\"";
00266   const std::list<Boundary> bb =a.skeleton(include_boundary);
00267   Point curr =T( bb.front().edges().front()->start_point() );
00268   os<<"M "<<curr;
00269   char cmd ='\0';
00270   for(std::list<Boundary>::const_iterator b=bb.begin(); b!=bb.end(); ++b)
00271   {
00272     const std::list<Edge*> edges =b->edges();
00273     assert(!edges.empty());
00274     os<<path_append(cmd,curr,'m',T( edges.front()->start_point() ));
00275     for(std::list<Edge*>::const_iterator e=edges.begin(); e!=edges.end(); ++e)
00276     {
00277       os<<path_append(cmd,curr,'l',T( (**e).end_point() ));
00278     }
00279   }
00280   os<<"\"/>\n";
00281   return os.str();
00282 }

std::string hex::svg::Document::draw_boundary ( const Boundary b,
float  bias = 0.0 
)

Draws a boundary line.

Definition at line 286 of file svg.cc.

References draw_poly(), hex::Boundary::is_closed(), and hex::Boundary::stroke().

00287 {
00288   return draw_poly(b.stroke(bias), b.is_closed(), &b);
00289 }

std::string hex::svg::Document::draw_path ( const Path p  ) 

Draws a path line.

Definition at line 293 of file svg.cc.

References draw_poly(), and hex::Path::hexes().

Referenced by main().

00294 {
00295   std::ostringstream os;
00296   const std::list<Hex*>& hexes =p.hexes();
00297   assert(!hexes.empty());
00298   if(hexes.size()>1) // Nothing to draw if there is only one hex in the path.
00299   {
00300     std::list<Point> points;
00301     for(std::list<Hex*>::const_iterator h =hexes.begin(); h!=hexes.end(); ++h)
00302         points.push_back( (**h).centre() );
00303     bool is_closed =( hexes.front()==hexes.back() );
00304     os<<draw_poly(points,is_closed,&p);
00305   }
00306   return os.str();
00307 }

std::string hex::svg::Document::draw_poly ( std::list< Point points,
bool  closed,
const Identity identity = NULL 
)

Series of points, rendered as an SVG polygon, or polyline, depending upon whether they are closed or not.

Definition at line 311 of file svg.cc.

References hex::svg::Identity::attributes(), and T().

Referenced by draw_boundary(), draw_path(), and draw_simple_area().

00316 {
00317   assert(!points.empty());
00318   std::ostringstream os;
00319   if(closed)
00320       points.pop_back();
00321   if(closed)
00322       os<<"<polygon";
00323   else
00324       os<<"<polyline";
00325   if(identity)
00326       os<<identity->attributes();
00327   os<<" points=\"";
00328   for(std::list<Point>::const_iterator p=points.begin(); p!=points.end(); ++p)
00329   {
00330     if(p!=points.begin())
00331        os<<" ";
00332     os<<T(*p);
00333   }
00334   os<<"\"/>\n";
00335   return os.str();
00336 }


Member Data Documentation

Point hex::svg::Document::p0

Lower left corner of the document.

Definition at line 64 of file hexsvg.h.

Referenced by header(), main(), and T().

Point hex::svg::Document::p1

Upper right corner of the document.

Definition at line 65 of file hexsvg.h.

Referenced by header(), main(), and T().

std::list<std::string> hex::svg::Document::stylesheets

List of stylesheets to import.

Definition at line 66 of file hexsvg.h.

Referenced by header().

std::list<std::string> hex::svg::Document::defs

Fragments insert into the <defs> element.

Definition at line 67 of file hexsvg.h.

Referenced by header().


The documentation for this class was generated from the following files:
Generated on Thu Feb 21 00:00:55 2008 for libhex by  doxygen 1.5.1