1 #include <msp/gl/immediate.h>
2 #include <msp/gl/matrix.h>
11 struct Text::RenderData
13 GL::PrimitiveBuilder *bld;
16 struct Text::CoordsToGeomData
28 Text::Text(const Style &s, const string &t):
34 void Text::set_style(const Style *s)
40 float font_size = style->get_font()->get_default_size();
41 for(vector<Line>::iterator i=lines.begin(); i!=lines.end(); ++i)
42 i->width = static_cast<unsigned>(style->get_font()->get_string_width(text.substr(i->start, i->length))*font_size);
46 unsigned Text::get_width() const
49 for(vector<Line>::const_iterator i=lines.begin(); i!=lines.end(); ++i)
50 width = max(width, i->width);
54 unsigned Text::get_height() const
59 const GL::Font *font = style->get_font();
60 float font_size = font->get_default_size();
61 unsigned line_height = static_cast<unsigned>((font->get_ascent()-font->get_descent())*font_size);
62 unsigned line_spacing = line_height*6/5;
63 return line_height+(lines.size()-1)*line_spacing;
66 void Text::set(const string &t)
72 void Text::erase(unsigned pos, unsigned len)
76 vector<Line>::iterator i;
77 for(i=lines.begin(); (i!=lines.end() && i->start+i->length<pos); ++i) ;
79 if(pos+len>i->start+i->length)
85 for(++i; i!=lines.end(); ++i)
90 void Text::insert(unsigned pos, const string &s)
94 if(s.find('\n')!=string::npos)
98 vector<Line>::iterator i;
99 for(i=lines.begin(); (i!=lines.end() && i->start+i->length<pos); ++i) ;
101 i->length += s.size();
103 for(++i; i!=lines.end(); ++i)
104 i->start += s.size();
108 unsigned Text::get_line_length(unsigned i) const
111 throw InvalidParameterValue("Invalid line number");
112 return lines[i].length;
115 void Text::offset_to_coords(unsigned offs, unsigned &row, unsigned &col) const
124 for(unsigned i=0; i<lines.size(); ++i)
125 if(offs>=lines[i].start && offs<=lines[i].start+lines[i].length)
128 col = offs-lines[i].start;
133 unsigned Text::coords_to_offset(unsigned row, unsigned col) const
135 if(row>=lines.size())
138 return lines[row].start+min(col, lines[row].length);
141 Geometry Text::coords_to_geometry(const Part &part, const Geometry &parent, unsigned first_row, unsigned row, unsigned col) const
143 if(row>=lines.size())
144 row = lines.size()-1;
145 const Line &line = lines[row];
149 CoordsToGeomData data;
153 process_lines<CoordsToGeomData, &Text::coords_to_geom_line>(part, parent, first_row, data);
158 void Text::render(const Part &part, const Geometry &parent, unsigned first_row) const
160 if(!style || lines.empty())
163 const GL::Color &color = style->get_font_color();
164 GL::Immediate imm((GL::COLOR4_UBYTE, GL::TEXCOORD2, GL::VERTEX2));
165 imm.color(color.r, color.g, color.b);
170 process_lines<RenderData, &Text::render_line>(part, parent, first_row, data);
173 Text &Text::operator=(const string &t)
179 void Text::find_lines()
182 float font_size = (style ? style->get_font()->get_default_size() : 1);
183 string::size_type start = 0;
186 string::size_type newline = text.find('\n', start);
190 line.length = (newline==string::npos ? text.size() : newline)-start;
191 line.width = line.length;
194 string str = text.substr(line.start, line.length);
195 line.width = static_cast<unsigned>(style->get_font()->get_string_width(str)*font_size);
197 lines.push_back(line);
199 if(newline==string::npos)
205 template<typename T, void (Text::*func)(unsigned, const Geometry &, T &) const>
206 void Text::process_lines(const Part &part, const Geometry &parent, unsigned first_row, T &data) const
211 const GL::Font *font = style->get_font();
212 float font_size = font->get_default_size();
213 unsigned line_height = static_cast<unsigned>((font->get_ascent()-font->get_descent())*font_size);
214 unsigned line_spacing = static_cast<unsigned>(font_size*6/5);
215 unsigned height = line_height+(lines.size()-1)*line_spacing;
216 int y_offset = static_cast<int>(-font->get_descent()*font_size);
218 const Sides &margin = part.get_margin();
219 unsigned n_lines = min(lines.size(), max((parent.h-margin.top-margin.bottom)/line_spacing, 1U));
220 first_row = min(first_row, lines.size()-n_lines);
222 for(unsigned i=0; i<n_lines; ++i)
224 const Line &line = lines[first_row+i];
227 rgeom.w = line.width;
229 rgeom.y = (n_lines-1-i)*line_spacing+y_offset;
230 part.get_alignment().apply(rgeom, parent, part.get_margin());
232 (this->*func)(first_row+i, rgeom, data);
236 void Text::render_line(unsigned i, const Geometry &rgeom, RenderData &data) const
238 const Line &line = lines[i];
239 const GL::Font *font = style->get_font();
241 GL::PushMatrix _pushm;
242 GL::translate(rgeom.x, rgeom.y, 0);
243 GL::scale_uniform(font->get_default_size());
245 font->draw_string(text.substr(line.start, line.length), *data.bld);
248 void Text::coords_to_geom_line(unsigned i, const Geometry &rgeom, CoordsToGeomData &data) const
252 const Line &line = lines[i];
253 const GL::Font *font = style->get_font();
256 data.result.x += static_cast<unsigned>(font->get_string_width(text.substr(line.start, data.col))*font->get_default_size());