Warning, /include/Geant4/tools/sg/base_freetype is written in an unsupported language. File is not indexed.
0001 // Copyright (C) 2010, Guy Barrand. All rights reserved.
0002 // See the file tools.license for terms.
0003
0004 #ifndef tools_sg_base_freetype
0005 #define tools_sg_base_freetype
0006
0007 #include "base_text"
0008 #include "gstos"
0009 #include "sf_string"
0010 #include <map>
0011
0012 // abstract class to handle freetype text.
0013
0014 namespace tools {
0015 namespace sg {
0016
0017 class base_freetype : public base_text, public gstos {
0018 TOOLS_HEADER(base_freetype,tools::sg::base_freetype,base_text)
0019 public:
0020 sf_string font;
0021 sf_enum<winding_type> front_face; //default is ccw.
0022 sf_enum<font_modeling> modeling;
0023 typedef unsigned int unichar;
0024 typedef std::vector<unichar> uniline;
0025 mf_std_vec<unichar> unitext;
0026 //mf_string font_dirs; //do we want that ? It is costly. We should have a sg::font node.
0027 public:
0028 virtual const desc_fields& node_desc_fields() const {
0029 TOOLS_FIELD_DESC_NODE_CLASS(tools::sg::base_freetype)
0030 static const desc_fields s_v(parent::node_desc_fields(),4, //WARNING : take care of count.
0031 TOOLS_ARG_FIELD_DESC(font),
0032 TOOLS_ARG_FIELD_DESC(front_face),
0033
0034 TOOLS_ARG_FIELD_DESC_ENUMS_BEG(modeling,3)
0035 TOOLS_ARG_ENUM(font_outline),
0036 TOOLS_ARG_ENUM(font_filled),
0037 TOOLS_ARG_ENUM(font_pixmap)
0038 TOOLS_ARG_FIELD_DESC_ENUMS_END,
0039
0040 TOOLS_ARG_FIELD_DESC(unitext)
0041 //TOOLS_ADD_FIELD_DESC(font_dirs)
0042 );
0043 return s_v;
0044 }
0045 private:
0046 void add_fields(){
0047 add_field(&font);
0048 add_field(&front_face);
0049 add_field(&modeling);
0050 add_field(&unitext);
0051 //add_field(&font_dirs);
0052 }
0053 public:
0054 base_freetype()
0055 :parent()
0056 ,gstos()
0057
0058 ,font("")
0059 ,front_face(winding_ccw)
0060 ,modeling(font_filled)
0061 ,unitext()
0062 //,font_dirs()
0063 {
0064 add_fields();
0065 }
0066 virtual ~base_freetype(){}
0067 public:
0068 base_freetype(const base_freetype& a_from)
0069 :parent(a_from)
0070 ,gstos(a_from)
0071
0072 ,font(a_from.font)
0073 ,front_face(a_from.front_face)
0074 ,modeling(a_from.modeling)
0075 ,unitext(a_from.unitext)
0076 //,font_dirs(a_from.font_dirs)
0077 ,m_fonts(a_from.m_fonts)
0078 ,m_finders(a_from.m_finders)
0079 {
0080 add_fields();
0081 }
0082
0083 base_freetype& operator=(const base_freetype& a_from){
0084 parent::operator=(a_from);
0085 gstos::operator=(a_from);
0086 font = a_from.font;
0087 front_face = a_from.front_face;
0088 modeling = a_from.modeling;
0089 unitext = a_from.unitext;
0090 //font_dirs = a_from.font_dirs;
0091 m_fonts = a_from.m_fonts;
0092 m_finders = a_from.m_finders;
0093 return *this;
0094 }
0095
0096 public:
0097 static base_freetype* create(const base_freetype& a_ttf) {
0098 return safe_cast<node,base_freetype>(*(a_ttf.copy()));
0099 }
0100 public:
0101 void dump_unitext(std::ostream& a_out) {
0102 //unitext.values().size()
0103 a_out << "unitext size : " << unitext.values().size() << std::endl;
0104 std::vector<uniline>::const_iterator vit;
0105 for(vit=unitext.values().begin();vit!=unitext.values().end();++vit) {
0106 const uniline& line = *vit;
0107 a_out << "beg line :" << std::endl;
0108 //a_out << line << std::endl;
0109 std::vector<unichar>::const_iterator it;
0110 for(it=line.begin();it!=line.end();++it) {
0111 a_out << ((unsigned int)*it) << std::endl;
0112 }
0113 a_out << "end line." << std::endl;
0114 }
0115 }
0116 public:
0117 typedef const unsigned char*(*font_getter)(unsigned int&);
0118 void add_embedded_font(const std::string& a_name,font_getter a_getter) {
0119 unsigned int size;
0120 const unsigned char* buffer = a_getter(size);
0121 m_fonts[a_name] = embedded_font(size,buffer); //warning: we do not take ownership of buffer.
0122 }
0123 typedef std::pair<unsigned int,const unsigned char*> embedded_font;
0124 void add_embedded_font(const std::string& a_name,unsigned int a_size,const unsigned char* a_buffer) {
0125 //warning: we do not take ownership of a_buffer.
0126 m_fonts[a_name] = embedded_font(a_size,a_buffer);
0127 }
0128 bool find_embedded_font(const std::string& a_name,unsigned int& a_size,const unsigned char*& a_buffer) {
0129 std::map<std::string,embedded_font>::const_iterator it = m_fonts.find(a_name);
0130 if(it==m_fonts.end()) {a_size = 0;a_buffer = 0;return false;}
0131 a_size = (*it).second.first;
0132 a_buffer = (*it).second.second;
0133 return true;
0134 }
0135 public:
0136 typedef bool(*font_finder)(const std::string&,std::string&);
0137 void add_font_finder(font_finder a_finder) {m_finders.push_back(a_finder);}
0138 bool find_font_with_finders(const std::string& a_name,std::string& a_font) {
0139 tools_vforcit(font_finder,m_finders,it) {
0140 if((*it)(a_name,a_font)) return true;
0141 }
0142 a_font.clear();
0143 return false;
0144 }
0145 protected:
0146 std::map<std::string,embedded_font> m_fonts;
0147 std::vector<font_finder> m_finders;
0148 };
0149
0150 }}
0151
0152 #endif