1 #include <msp/gl/matrix.h>
2 #include <msp/gl/meshbuilder.h>
3 #include <msp/gl/texture2d.h>
13 struct Text::RenderData
15 GL::PrimitiveBuilder *bld;
18 struct Text::CoordsToGeomData
30 Text::Text(const Style &s, const string &t):
36 void Text::set_style(const Style *s)
42 const GL::Font &font = style->get_font();
43 float font_size = style->get_font_size();
44 for(vector<Line>::iterator i=lines.begin(); i!=lines.end(); ++i)
45 i->width = static_cast<unsigned>(font.get_string_width(text.substr(i->start, i->length))*font_size);
49 unsigned Text::get_width() const
52 for(vector<Line>::const_iterator i=lines.begin(); i!=lines.end(); ++i)
53 width = max(width, i->width);
57 unsigned Text::get_height() const
62 const GL::Font &font = style->get_font();
63 float font_size = style->get_font_size();
64 unsigned line_height = static_cast<unsigned>((font.get_ascent()-font.get_descent())*font_size);
65 unsigned line_spacing = line_height*6/5;
66 return line_height+(lines.size()-1)*line_spacing;
69 void Text::set(const string &t)
75 void Text::erase(unsigned pos, unsigned len)
79 vector<Line>::iterator i;
80 for(i=lines.begin(); (i!=lines.end() && i->start+i->length<pos); ++i) ;
82 if(pos+len>i->start+i->length)
88 for(++i; i!=lines.end(); ++i)
93 void Text::insert(unsigned pos, const string &s)
97 if(s.find('\n')!=string::npos)
101 vector<Line>::iterator i;
102 for(i=lines.begin(); (i!=lines.end() && i->start+i->length<pos); ++i) ;
104 i->length += s.size();
106 for(++i; i!=lines.end(); ++i)
107 i->start += s.size();
111 unsigned Text::get_line_length(unsigned i) const
114 throw out_of_range("Text::get_line_length");
115 return lines[i].length;
118 void Text::offset_to_coords(unsigned offs, unsigned &row, unsigned &col) const
127 for(unsigned i=0; i<lines.size(); ++i)
128 if(offs>=lines[i].start && offs<=lines[i].start+lines[i].length)
131 col = offs-lines[i].start;
136 unsigned Text::coords_to_offset(unsigned row, unsigned col) const
138 if(row>=lines.size())
141 return lines[row].start+min(col, lines[row].length);
144 Geometry Text::coords_to_geometry(const Part &part, const Geometry &parent, unsigned first_row, unsigned row, unsigned col) const
146 if(row>=lines.size())
147 row = lines.size()-1;
148 const Line &line = lines[row];
152 CoordsToGeomData data;
156 process_lines<CoordsToGeomData, &Text::coords_to_geom_line>(part, parent, first_row, data);
161 void Text::build(const Part &part, const Geometry &parent, CachedPart &cache) const
163 build(part, parent, 0, cache);
166 void Text::build(const Part &part, const Geometry &parent, unsigned first_row, CachedPart &cache) const
168 if(!style || lines.empty())
175 GL::MeshBuilder bld(*cache.mesh);
177 const GL::Font &font = style->get_font();
178 bld.color(style->get_font_color());
183 cache.texture = &font.get_texture();
185 process_lines<RenderData, &Text::build_line>(part, parent, first_row, data);
188 Text &Text::operator=(const string &t)
194 void Text::find_lines()
197 float font_size = (style ? style->get_font_size() : 1);
198 string::size_type start = 0;
201 string::size_type newline = text.find('\n', start);
205 line.length = (newline==string::npos ? text.size() : newline)-start;
206 line.width = line.length;
209 string str = text.substr(line.start, line.length);
210 line.width = static_cast<unsigned>(style->get_font().get_string_width(str)*font_size);
212 lines.push_back(line);
214 if(newline==string::npos)
220 template<typename T, void (Text::*func)(unsigned, const Geometry &, T &) const>
221 void Text::process_lines(const Part &part, const Geometry &parent, unsigned first_row, T &data) const
226 const GL::Font &font = style->get_font();
227 float font_size = style->get_font_size();
228 unsigned line_height = static_cast<unsigned>((font.get_ascent()-font.get_descent())*font_size);
229 unsigned line_spacing = static_cast<unsigned>(font_size*6/5);
230 unsigned height = line_height+(lines.size()-1)*line_spacing;
231 int y_offset = static_cast<int>(-font.get_descent()*font_size);
233 const Sides &margin = part.get_margin();
234 unsigned n_lines = min(lines.size(), max((parent.h-margin.top-margin.bottom)/line_spacing, 1U));
235 first_row = min(first_row, lines.size()-n_lines);
237 for(unsigned i=0; i<n_lines; ++i)
239 const Line &line = lines[first_row+i];
242 rgeom.w = line.width;
244 rgeom.y = (n_lines-1-i)*line_spacing+y_offset;
245 part.get_alignment().apply(rgeom, parent, part.get_margin());
247 (this->*func)(first_row+i, rgeom, data);
251 void Text::build_line(unsigned i, const Geometry &rgeom, RenderData &data) const
253 const Line &line = lines[i];
255 GL::MatrixStack::Push _pushm(data.bld->matrix());
256 data.bld->matrix() *= GL::Matrix::translation(rgeom.x, rgeom.y, 0);
257 data.bld->matrix() *= GL::Matrix::scaling(style->get_font_size());
259 style->get_font().build_string(text.substr(line.start, line.length), *data.bld);
262 void Text::coords_to_geom_line(unsigned i, const Geometry &rgeom, CoordsToGeomData &data) const
266 float w = style->get_font().get_string_width(text.substr(lines[i].start, data.col));
268 data.result.x += static_cast<unsigned>(w*style->get_font_size());