g4tools  5.4.0
text_valop
Go to the documentation of this file.
1 // Copyright (C) 2010, Guy Barrand. All rights reserved.
2 // See the file tools.license for terms.
3 
4 #ifndef tools_sg_text_valop
5 #define tools_sg_text_valop
6 
7 #include "base_text"
8 #include "valop2sg"
9 #include "nodekit"
10 #include "base_freetype"
11 #include "mnmx"
12 #include "../nostream"
13 
14 namespace tools {
15 namespace sg {
16 
17 class text_valop : public base_text {
18 public:
20 public:
24 public:
25  virtual const desc_fields& node_desc_fields() const {
27  static const desc_fields s_v(parent::node_desc_fields(),3, //WARNING : take care of count.
35  );
36  return s_v;
37  }
38 private:
39  void add_fields(){
41  add_field(&font);
43  }
44 public:
45  virtual void render(render_action& a_action) {
46  if(touched()) {
47  update_sg(a_action.out());
48  reset_touched();
49  }
50  m_sep.render(a_action);
51  }
52  virtual void pick(pick_action& a_action) {
53  if(touched()) {
54  update_sg(a_action.out());
55  reset_touched();
56  }
57  nodekit_pick(a_action,m_sep,this);
58  }
59  virtual void bbox(bbox_action& a_action) {
60  if(touched()) {
61  update_sg(a_action.out());
62  reset_touched();
63  }
64  m_sep.bbox(a_action);
65  }
66  virtual void search(search_action& a_action) {
67  if(touched()) {
68  update_sg(a_action.out());
69  reset_touched();
70  }
71  parent::search(a_action);
72  if(a_action.done()) return;
73  if(a_action.do_path()) a_action.path_push(this);
74  m_sep.search(a_action);
75  if(a_action.done()) return;
76  if(a_action.do_path()) a_action.path_pop();
77  }
78 public:
79  text_valop(const base_freetype& a_ttf)
80  :parent()
82  ,font(font_hershey())
84  ,m_ttf(a_ttf)
85  {
86  add_fields();
87  }
88  virtual ~text_valop(){}
89 public:
90  text_valop(const text_valop& a_from)
91  :parent(a_from)
92  ,encoding(a_from.encoding)
93  ,font(a_from.font)
95  ,m_ttf(a_from.m_ttf)
96  {
97  add_fields();
98  }
99  text_valop& operator=(const text_valop& a_from){
100  parent::operator=(a_from);
101  encoding = a_from.encoding;
102  font = a_from.font;
103  font_modeling = a_from.font_modeling;
104  return *this;
105  }
106 public: //sg::base_text :
107  virtual void get_bounds(float /*a_height*/,
108  float& a_mn_x,float& a_mn_y,float& a_mn_z,
109  float& a_mx_x,float& a_mx_y,float& a_mx_z) const {
110  text_valop& self = const_cast<text_valop&>(*this);
111  if(self.touched()) {
112  self.update_sg(self.m_out);
113  self.reset_touched();
114  }
115  vec3f mn,mx;
116  mnmx(self.m_out,self.m_sep,mn,mx);
117  a_mn_x = mn[0];a_mn_y = mn[1];a_mn_z = mn[2];
118  a_mx_x = mx[0];a_mx_y = mx[1];a_mx_z = mx[2];
119  //::printf("debug : get_bounds : %g %g %g : %g %g %g\n",a_mn_x,a_mn_y,a_mn_z,a_mx_x,a_mx_y,a_mx_z);
120  }
121  virtual float ascent(float) const {return 1;}
122  virtual float y_advance(float) const {return 1;}
123  virtual float descent(float) const {return 0;}
124  virtual bool truncate(const std::string&,float,float,std::string&) const {return false;}
125 protected:
126  void update_sg(std::ostream& a_out) {
127  m_sep.clear();
128  matrix* tsf = new matrix;
129  m_sep.add(tsf);
130  tools_vforcit(std::string,strings.values(),it) {
131  valop* _valop = new valop(valop::STRING,*it);
132  valop2sg v(a_out,m_sep,m_ttf);
133  if(!v.visit(*_valop)) {
134  a_out << "tools::sg::text_valop::upate_sg : valop2sg.visit() failed." << std::endl;
135  m_sep.clear();
136  delete _valop;
137  return;
138  }
139  delete _valop;
140  }
141  vec3f mn,mx;
142  mnmx(a_out,m_sep,mn,mx);
143  float h = mx[1]-mn[1];
144  if(!h) {
145  a_out << "tools::sg::text_valop::upate_sg : valop has null height." << std::endl;
146  m_sep.clear();
147  return;
148  }
149  float scale = height.value()/h;
150  tsf->set_scale(scale,scale,1);
151  }
152 protected:
156 };
157 
158 }}
159 
160 #endif
tools::nostream
Definition: nostream:16
tools::sg::text_valop::ascent
virtual float ascent(float) const
Definition: text_valop:121
tools::sg::font_pixmap
@ font_pixmap
Definition: enums:94
tools::mnmx
bool mnmx(std::ostream &a_out, sg::node &a_node, vec3f &a_mn, vec3f &a_mx)
Definition: mnmx:12
valop2sg
tools::sg::group::search
virtual void search(search_action &a_action)
Definition: group:51
tools::sg::text_valop::get_bounds
virtual void get_bounds(float, float &a_mn_x, float &a_mn_y, float &a_mn_z, float &a_mx_x, float &a_mx_y, float &a_mx_z) const
Definition: text_valop:107
tools::sg::base_text
Definition: base_text:17
tools::sg::text_valop::m_out
nostream m_out
Definition: text_valop:155
tools::sg::text_valop::text_valop
text_valop(const text_valop &a_from)
Definition: text_valop:90
tools::sg::text_valop::search
virtual void search(search_action &a_action)
Definition: text_valop:66
tools::sg::text_valop::truncate
virtual bool truncate(const std::string &, float, float, std::string &) const
Definition: text_valop:124
TOOLS_ARG_FIELD_DESC_ENUMS_BEG
#define TOOLS_ARG_FIELD_DESC_ENUMS_BEG(a__field, a__num)
Definition: field:77
tools::sg::base_text::height
sf< float > height
Definition: base_text:30
tools::sg::node
Definition: node:28
tools::sg::bbox_action
Definition: bbox_action:15
TOOLS_NODE
#define TOOLS_NODE(a__class, a__sclass, a__parent)
Definition: node:324
tools::sg::search_action::done
bool done() const
Definition: search_action:68
tools::mn
T mn(const T &a, const T &b)
Definition: mnmx:10
tools::valop
Definition: valop:11
tools::mx
T mx(const T &a, const T &b)
Definition: mnmx:13
tools::sg::text_valop::encoding
sf_string encoding
Definition: text_valop:21
tools::sg::base_freetype
Definition: base_freetype:16
tools::sg::search_action::path_push
void path_push(sg::node *a_v)
Definition: search_action:90
tools::sg::text_valop::render
virtual void render(render_action &a_action)
Definition: text_valop:45
tools::sg::desc_fields
Definition: field_desc:148
tools::sg::node::operator=
node & operator=(const node &)
Definition: node:124
tools::sg::bmf::values
const std::vector< T > & values() const
Definition: bmf:71
tools::sg::pick_action
Definition: pick_action:59
tools::sg::separator::bbox
virtual void bbox(bbox_action &a_action)
Definition: separator:38
tools::sg::text_valop::update_sg
void update_sg(std::ostream &a_out)
Definition: text_valop:126
tools::sg::nodekit_pick
void nodekit_pick(pick_action &a_action, node &a_sg, node *a_node)
Definition: nodekit:13
tools::valop2sg
Definition: valop2sg:20
tools::sg::text_valop::bbox
virtual void bbox(bbox_action &a_action)
Definition: text_valop:59
tools::sg::matrix::set_scale
void set_scale(float a_x, float a_y, float a_z)
Definition: matrix:93
base_freetype
tools::sg::search_action::do_path
bool do_path() const
Definition: search_action:98
tools::valop_visitor::visit
bool visit(const valop &a_valop)
Definition: valop:419
tools::sg::text_valop::font
sf_string font
Definition: text_valop:22
tools::sg::sf_enum< sg::font_modeling >
tools::sg::text_valop::node_desc_fields
virtual const desc_fields & node_desc_fields() const
Definition: text_valop:25
tools::sg::search_action::path_pop
void path_pop()
Definition: search_action:91
tools::sg::font_modeling
font_modeling
Definition: enums:91
tools::sg::group::add
void add(node *a_node)
Definition: group:96
tools::sg::text_valop::y_advance
virtual float y_advance(float) const
Definition: text_valop:122
tools::sg::font_outline
@ font_outline
Definition: enums:92
mnmx
tools::sg::search_action
Definition: search_action:19
tools::sg::encoding_none
const std::string & encoding_none()
Definition: strings:38
tools::vec3f
Definition: vec3f:13
tools::sg::node::search
virtual void search(search_action &a_action)
Definition: node:56
tools::sg::render_action
Definition: render_action:24
tools::sg::text_valop::m_ttf
const base_freetype & m_ttf
Definition: text_valop:153
tools::sg::separator
Definition: separator:15
tools
inlined C code : ///////////////////////////////////
Definition: aida_ntuple:26
tools::sg::node::node_desc_fields
virtual const desc_fields & node_desc_fields() const
Definition: node:48
tools::sg::separator::render
virtual void render(render_action &a_action)
Definition: separator:18
tools::sg::text_valop::text_valop
text_valop(const base_freetype &a_ttf)
Definition: text_valop:79
tools::sg::node::add_field
void add_field(field *a_field)
Definition: node:128
tools::sg::matrix
Definition: matrix:19
tools::sg::text_valop::font_modeling
sf_enum< sg::font_modeling > font_modeling
Definition: text_valop:23
tools::sg::base_text::strings
mf_string strings
Definition: base_text:29
tools::sg::text_valop
Definition: text_valop:17
TOOLS_FIELD_DESC_NODE_CLASS
#define TOOLS_FIELD_DESC_NODE_CLASS(a__class)
Definition: field:68
tools::sg::text_valop::~text_valop
virtual ~text_valop()
Definition: text_valop:88
tools::valop::STRING
@ STRING
Definition: valop:37
TOOLS_ARG_ENUM
#define TOOLS_ARG_ENUM(a__value)
Definition: field:85
tools::sg::group::clear
void clear()
Definition: group:235
tools::sg::sf_string
Definition: sf_string:15
tools::sg::bsf::value
T & value()
Definition: bsf:98
tools::sg::action::out
std::ostream & out() const
Definition: action:51
nodekit
tools::sg::font_hershey
const std::string & font_hershey()
Definition: strings:186
tools_vforcit
#define tools_vforcit(a__T, a__v, a__it)
Definition: forit:7
tools::sg::font_filled
@ font_filled
Definition: enums:93
tools::sg::text_valop::descent
virtual float descent(float) const
Definition: text_valop:123
tools::sg::text_valop::operator=
text_valop & operator=(const text_valop &a_from)
Definition: text_valop:99
base_text
tools::sg::node::touched
virtual bool touched()
Definition: node:96
tools::sg::text_valop::m_sep
separator m_sep
Definition: text_valop:154
TOOLS_ARG_FIELD_DESC
#define TOOLS_ARG_FIELD_DESC(a__field)
Definition: field:71
tools::sg::text_valop::pick
virtual void pick(pick_action &a_action)
Definition: text_valop:52
tools::sg::node::reset_touched
virtual void reset_touched()
Definition: node:102
TOOLS_ARG_FIELD_DESC_ENUMS_END
#define TOOLS_ARG_FIELD_DESC_ENUMS_END
Definition: field:83