1 #include <msp/core/algorithm.h>
2 #include <msp/gl/matrix.h>
3 #include <msp/gl/meshbuilder.h>
4 #include <msp/gl/texture2d.h>
14 struct Text::RenderData
16 GL::PrimitiveBuilder *bld;
19 struct Text::CoordsToGeomData
31 Text::Text(const Style &s, const string &t):
37 void Text::set_style(const Style *s)
43 const GL::Font &font = style->get_font();
44 float font_size = style->get_font_size();
46 l.width = static_cast<unsigned>(font.get_string_width(text.substr(l.start, l.bytes))*font_size);
50 unsigned Text::get_width() const
53 for(const Line &l: lines)
54 width = max(width, l.width);
58 unsigned Text::get_height() const
63 const GL::Font &font = style->get_font();
64 float font_size = style->get_font_size();
65 unsigned line_height = static_cast<unsigned>((font.get_ascent()-font.get_descent())*font_size);
66 unsigned line_spacing = line_height*6/5;
67 return line_height+(lines.size()-1)*line_spacing;
70 void Text::autosize(const Part &part, Geometry &geom) const
72 const Sides &margin = part.get_margin();
73 geom.w = max(geom.w, get_width()+margin.left+margin.right);
74 geom.h = max(geom.h, get_height()+margin.top+margin.bottom);
77 void Text::set(const string &t)
83 void Text::erase(unsigned pos, unsigned len)
86 check_alignment(pos+len);
89 auto i = find_if(lines, [pos](const Line &l){ return l.start+l.bytes>=pos; });
91 if(pos+len>i->start+i->bytes)
96 i->length = count_characters(i->start, i->bytes);
98 for(++i; i!=lines.end(); ++i)
103 void Text::insert(unsigned pos, const string &s)
105 check_alignment(pos);
108 if(s.find('\n')!=string::npos)
112 auto i = find_if(lines, [pos](const Line &l){ return l.start+l.bytes>=pos; });
114 i->bytes += s.size();
115 i->length = count_characters(i->start, i->bytes);
117 for(++i; i!=lines.end(); ++i)
118 i->start += s.size();
122 unsigned Text::get_visible_lines(const Part &part, const Geometry &parent, unsigned *fit_height) const
124 const GL::Font &font = style->get_font();
125 float font_size = style->get_font_size();
126 unsigned line_height = static_cast<unsigned>((font.get_ascent()-font.get_descent())*font_size);
127 unsigned line_spacing = static_cast<unsigned>(font_size*6/5);
129 const Sides &margin = part.get_margin();
130 unsigned vmargin = margin.top+margin.bottom;
131 unsigned free_height = max(parent.h, vmargin)-vmargin+line_spacing-line_height;
132 unsigned n_lines = min<unsigned>(lines.size(), max(free_height/line_spacing, 1U));
134 *fit_height = line_height+(n_lines-1)*line_spacing;
139 unsigned Text::get_line_length(unsigned i) const
142 throw out_of_range("Text::get_line_length");
143 return lines[i].length;
146 unsigned Text::move_offset(unsigned offs, int change) const
148 check_alignment(offs);
152 StringCodec::Utf8::Decoder dec(StringCodec::IGNORE_ERRORS);
153 auto i = text.begin()+offs;
156 for(; change>0; --change)
157 dec.decode_char(text, i);
161 while(change<0 && i!=text.begin())
165 if(dec.decode_char(text, j)!=-1)
169 return i-text.begin();
172 void Text::offset_to_coords(unsigned offs, unsigned &row, unsigned &col) const
181 for(unsigned i=0; i<lines.size(); ++i)
182 if(offs>=lines[i].start && offs<=lines[i].start+lines[i].bytes)
185 if(lines[i].length==lines[i].bytes)
186 col = offs-lines[i].start;
188 col = count_characters(lines[i].start, offs-lines[i].start);
193 unsigned Text::coords_to_offset(unsigned row, unsigned col) const
195 if(row>=lines.size())
197 const Line &line = lines[row];
201 if(line.length==line.bytes)
202 return line.start+col;
205 StringCodec::Utf8::Decoder dec;
206 auto i = text.begin()+line.start;
207 for(col=min(col, line.length); col; --col)
208 dec.decode_char(text, i);
209 return i-text.begin();
213 Geometry Text::coords_to_geometry(const Part &part, const Geometry &parent, unsigned first_row, unsigned row, unsigned col) const
215 if(row>=lines.size())
216 row = lines.size()-1;
217 const Line &line = lines[row];
221 CoordsToGeomData data;
225 process_lines(part, parent, first_row, &Text::coords_to_geom_line, data);
230 void Text::build(const Part &part, State state, const Geometry &parent, PartCache &cache) const
232 build(part, state, parent, 0, cache);
235 void Text::build(const Part &part, State state, const Geometry &parent, unsigned first_row, PartCache &cache) const
237 if(!style || lines.empty())
240 const GL::Font &font = style->get_font();
241 GL::MeshBuilder bld(cache.create_mesh(part, font.get_texture()));
242 bld.color(style->get_font_color(state));
247 process_lines(part, parent, first_row, &Text::build_line, data);
250 Text &Text::operator=(const string &t)
256 void Text::find_lines()
259 float font_size = (style ? style->get_font_size() : 1);
260 string::size_type start = 0;
263 string::size_type newline = text.find('\n', start);
267 line.bytes = (newline==string::npos ? text.size() : newline)-start;
268 line.length = count_characters(line.start, line.bytes);
269 line.width = line.length;
272 string str = text.substr(line.start, line.bytes);
273 line.width = static_cast<unsigned>(style->get_font().get_string_width(str)*font_size);
275 lines.push_back(line);
277 if(newline==string::npos)
283 unsigned Text::count_characters(unsigned start, unsigned bytes) const
285 StringCodec::Utf8::Decoder dec;
286 auto i = text.begin()+start;
289 for(; i<end; dec.decode_char(text, i))
294 void Text::check_alignment(unsigned offs) const
296 StringCodec::Utf8::Decoder dec;
297 auto i = text.begin()+offs;
298 dec.decode_char(text, i);
302 void Text::process_lines(const Part &part, const Geometry &parent, unsigned first_row, void (Text::*func)(unsigned, const Geometry &, T &) const, T &data) const
307 const GL::Font &font = style->get_font();
308 float font_size = style->get_font_size();
309 unsigned line_spacing = static_cast<unsigned>(font_size*6/5);
310 int y_offset = static_cast<int>(-font.get_descent()*font_size);
313 unsigned n_lines = get_visible_lines(part, parent, &fit_height);
314 first_row = min<unsigned>(first_row, lines.size()-n_lines);
316 for(unsigned i=0; i<n_lines; ++i)
318 const Line &line = lines[first_row+i];
321 rgeom.w = line.width;
322 rgeom.h = fit_height;
323 rgeom.y = (n_lines-1-i)*line_spacing+y_offset;
324 part.get_alignment().apply(rgeom, parent, part.get_margin());
326 (this->*func)(first_row+i, rgeom, data);
330 void Text::build_line(unsigned i, const Geometry &rgeom, RenderData &data) const
332 const Line &line = lines[i];
334 GL::VertexBuilder::PushMatrix _pushm(*data.bld);
335 data.bld->transform(GL::Matrix::translation(rgeom.x, rgeom.y, 0));
336 data.bld->transform(GL::Matrix::scaling(style->get_font_size()));
338 style->get_font().build_string(text.substr(line.start, line.bytes), *data.bld);
341 void Text::coords_to_geom_line(unsigned i, const Geometry &rgeom, CoordsToGeomData &data) const
345 auto begin = text.begin()+lines[i].start;
347 if(lines[i].length==lines[i].bytes)
351 StringCodec::Utf8::Decoder dec;
352 for(unsigned c=data.col; c; --c)
353 dec.decode_char(text, j);
355 float w = style->get_font().get_string_width(string(begin, j));
357 data.result.x += static_cast<unsigned>(w*style->get_font_size());