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::autosize(const Part &part, Geometry &geom) const
71 const Sides &margin = part.get_margin();
72 geom.w = max(geom.w, get_width()+margin.left+margin.right);
73 geom.h = max(geom.h, get_height()+margin.top+margin.bottom);
76 void Text::set(const string &t)
82 void Text::erase(unsigned pos, unsigned len)
86 vector<Line>::iterator i;
87 for(i=lines.begin(); (i!=lines.end() && i->start+i->length<pos); ++i) ;
89 if(pos+len>i->start+i->length)
95 for(++i; i!=lines.end(); ++i)
100 void Text::insert(unsigned pos, const string &s)
104 if(s.find('\n')!=string::npos)
108 vector<Line>::iterator i;
109 for(i=lines.begin(); (i!=lines.end() && i->start+i->length<pos); ++i) ;
111 i->length += s.size();
113 for(++i; i!=lines.end(); ++i)
114 i->start += s.size();
118 unsigned Text::get_visible_lines(const Part &part, const Geometry &parent, unsigned *fit_height) const
120 const GL::Font &font = style->get_font();
121 float font_size = style->get_font_size();
122 unsigned line_height = static_cast<unsigned>((font.get_ascent()-font.get_descent())*font_size);
123 unsigned line_spacing = static_cast<unsigned>(font_size*6/5);
125 const Sides &margin = part.get_margin();
126 unsigned vmargin = margin.top+margin.bottom+line_height-line_spacing;
127 unsigned n_lines = min<unsigned>(lines.size(), max((max(parent.h, vmargin)-vmargin)/line_spacing, 1U));
129 *fit_height = line_height+(n_lines-1)*line_spacing;
134 unsigned Text::get_line_length(unsigned i) const
137 throw out_of_range("Text::get_line_length");
138 return lines[i].length;
141 void Text::offset_to_coords(unsigned offs, unsigned &row, unsigned &col) const
150 for(unsigned i=0; i<lines.size(); ++i)
151 if(offs>=lines[i].start && offs<=lines[i].start+lines[i].length)
154 col = offs-lines[i].start;
159 unsigned Text::coords_to_offset(unsigned row, unsigned col) const
161 if(row>=lines.size())
164 return lines[row].start+min(col, lines[row].length);
167 Geometry Text::coords_to_geometry(const Part &part, const Geometry &parent, unsigned first_row, unsigned row, unsigned col) const
169 if(row>=lines.size())
170 row = lines.size()-1;
171 const Line &line = lines[row];
175 CoordsToGeomData data;
179 process_lines(part, parent, first_row, &Text::coords_to_geom_line, data);
184 void Text::build(const Part &part, State state, const Geometry &parent, PartCache &cache) const
186 build(part, state, parent, 0, cache);
189 void Text::build(const Part &part, State state, const Geometry &parent, unsigned first_row, PartCache &cache) const
191 if(!style || lines.empty())
194 const GL::Font &font = style->get_font();
195 GL::MeshBuilder bld(cache.create_mesh(part, font.get_texture()));
196 bld.color(style->get_font_color(state));
201 process_lines(part, parent, first_row, &Text::build_line, data);
204 Text &Text::operator=(const string &t)
210 void Text::find_lines()
213 float font_size = (style ? style->get_font_size() : 1);
214 string::size_type start = 0;
217 string::size_type newline = text.find('\n', start);
221 line.length = (newline==string::npos ? text.size() : newline)-start;
222 line.width = line.length;
225 string str = text.substr(line.start, line.length);
226 line.width = static_cast<unsigned>(style->get_font().get_string_width(str)*font_size);
228 lines.push_back(line);
230 if(newline==string::npos)
237 void Text::process_lines(const Part &part, const Geometry &parent, unsigned first_row, void (Text::*func)(unsigned, const Geometry &, T &) const, T &data) const
242 const GL::Font &font = style->get_font();
243 float font_size = style->get_font_size();
244 unsigned line_spacing = static_cast<unsigned>(font_size*6/5);
245 int y_offset = static_cast<int>(-font.get_descent()*font_size);
248 unsigned n_lines = get_visible_lines(part, parent, &fit_height);
249 first_row = min<unsigned>(first_row, lines.size()-n_lines);
251 for(unsigned i=0; i<n_lines; ++i)
253 const Line &line = lines[first_row+i];
256 rgeom.w = line.width;
257 rgeom.h = fit_height;
258 rgeom.y = (n_lines-1-i)*line_spacing+y_offset;
259 part.get_alignment().apply(rgeom, parent, part.get_margin());
261 (this->*func)(first_row+i, rgeom, data);
265 void Text::build_line(unsigned i, const Geometry &rgeom, RenderData &data) const
267 const Line &line = lines[i];
269 GL::MatrixStack::Push _pushm(data.bld->matrix());
270 data.bld->matrix() *= GL::Matrix::translation(rgeom.x, rgeom.y, 0);
271 data.bld->matrix() *= GL::Matrix::scaling(style->get_font_size());
273 style->get_font().build_string(text.substr(line.start, line.length), *data.bld);
276 void Text::coords_to_geom_line(unsigned i, const Geometry &rgeom, CoordsToGeomData &data) const
280 float w = style->get_font().get_string_width(text.substr(lines[i].start, data.col));
282 data.result.x += static_cast<unsigned>(w*style->get_font_size());