contrib/brl/bbas/bxml/bsvg/bsvg_document.cxx
Go to the documentation of this file.
00001 #include "bsvg_document.h"
00002 //:
00003 // \file
00004 // \author Ozge C. Ozcanli (Brown)
00005 // \date   April 21, 2009
00006 
00007 #include <vcl_iostream.h>
00008 #include <vcl_sstream.h>
00009 
00010 bsvg_document::bsvg_document(float w, float h) : w_(w), h_(h)
00011 {
00012   bxml_element* root = new bxml_element("svg");
00013   this->set_root_element(root);
00014 
00015   root->set_attribute("xmlns", "http://www.w3.org/2000/svg");
00016 
00017   vcl_stringstream ss_w; ss_w << w;
00018   root->set_attribute("width", ss_w.str());
00019 
00020   vcl_stringstream ss_h; ss_h << h;
00021   root->set_attribute("height", ss_h.str());
00022 
00023   //: viewBox is by default, the same size as width and height
00024   vcl_string v_box = "0,0,"+ss_w.str()+","+ss_h.str();
00025   root->set_attribute("viewBox", v_box);
00026   root->append_text("\n");
00027 }
00028 
00029 bsvg_document::bsvg_document(float w, float h, float viewBox_x, float viewBox_y, float viewBox_w, float viewBox_h) : w_(w), h_(h)
00030 {
00031   bxml_element* root = new bxml_element("svg");
00032   this->set_root_element(root);
00033 
00034   root->set_attribute("xmlns", "http://www.w3.org/2000/svg");
00035 
00036   vcl_stringstream ss_w; ss_w << w;
00037   root->set_attribute("width", ss_w.str());
00038 
00039   vcl_stringstream ss_h; ss_h << h;
00040   root->set_attribute("height", ss_h.str());
00041 
00042   vcl_stringstream ss_x; ss_x << viewBox_x;
00043   vcl_stringstream ss_y; ss_y << viewBox_y;
00044   vcl_stringstream ss_Bw; ss_Bw << viewBox_w;
00045   vcl_stringstream ss_Bh; ss_Bh << viewBox_h;
00046 
00047   vcl_string v_box = ss_x.str()+","+ss_y.str()+","+ss_Bw.str()+","+ss_Bh.str();
00048   root->set_attribute("viewBox", v_box);
00049   root->append_text("\n");
00050 }
00051 
00052 bool bsvg_document::add_description(const vcl_string& d)
00053 {
00054   if (!this->root_element()) {
00055     vcl_cout << "Error: Root element of SVG document has not been created!\n";
00056     return false;
00057   }
00058 
00059   bxml_element* root = dynamic_cast<bxml_element*>(this->root_element().ptr());
00060 
00061   bxml_element* desc = new bxml_element("desc");
00062   desc->append_text(d);
00063   root->append_data(desc);
00064   root->append_text("\n");
00065 
00066   return true;
00067 }
00068 
00069 bool bsvg_document::add_element(const bxml_data_sptr& element)
00070 {
00071   if (!this->root_element()) {
00072     vcl_cout << "Error: Root element of SVG document has not been created!\n";
00073     return false;
00074   }
00075 
00076   bxml_element* root = dynamic_cast<bxml_element*>(this->root_element().ptr());
00077   root->append_data(element);
00078   root->append_text("\n");
00079 
00080   return true;
00081 }
00082