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
35 lines.push_back(line);
38 Text::Text(const Style &s, const string &t):
44 void Text::set_style(const Style *s)
50 const GL::Font &font = style->get_font();
51 float font_size = style->get_font_size();
53 l.width = static_cast<unsigned>(font.get_string_width(text.substr(l.start, l.bytes))*font_size);
57 unsigned Text::get_width() const
60 for(const Line &l: lines)
61 width = max(width, l.width);
65 unsigned Text::get_height() const
70 const GL::Font &font = style->get_font();
71 float font_size = style->get_font_size();
72 unsigned line_height = static_cast<unsigned>((font.get_ascent()-font.get_descent())*font_size);
73 unsigned line_spacing = line_height*6/5;
74 return line_height+(lines.size()-1)*line_spacing;
77 void Text::autosize(const Part &part, Geometry &geom) const
79 const Sides &margin = part.get_margin();
80 geom.w = max(geom.w, get_width()+margin.left+margin.right);
81 geom.h = max(geom.h, get_height()+margin.top+margin.bottom);
84 void Text::set(const string &t)
90 void Text::erase(unsigned pos, unsigned len)
93 check_alignment(pos+len);
96 auto i = find_if(lines, [pos](const Line &l){ return l.start+l.bytes>=pos; });
98 if(pos+len>i->start+i->bytes)
103 i->length = count_characters(i->start, i->bytes);
105 for(++i; i!=lines.end(); ++i)
110 void Text::insert(unsigned pos, const string &s)
112 check_alignment(pos);
115 if(s.find('\n')!=string::npos)
119 auto i = find_if(lines, [pos](const Line &l){ return l.start+l.bytes>=pos; });
121 i->bytes += s.size();
122 i->length = count_characters(i->start, i->bytes);
124 for(++i; i!=lines.end(); ++i)
125 i->start += s.size();
129 unsigned Text::get_visible_lines(const Part &part, const Geometry &parent, unsigned *fit_height) const
131 const GL::Font &font = style->get_font();
132 float font_size = style->get_font_size();
133 unsigned line_height = static_cast<unsigned>((font.get_ascent()-font.get_descent())*font_size);
134 unsigned line_spacing = static_cast<unsigned>(font_size*6/5);
136 const Sides &margin = part.get_margin();
137 unsigned vmargin = margin.top+margin.bottom;
138 unsigned free_height = max(parent.h, vmargin)-vmargin+line_spacing-line_height;
139 unsigned n_lines = min<unsigned>(lines.size(), max(free_height/line_spacing, 1U));
141 *fit_height = line_height+(n_lines-1)*line_spacing;
146 unsigned Text::get_line_length(unsigned i) const
149 throw out_of_range("Text::get_line_length");
150 return lines[i].length;
153 unsigned Text::move_offset(unsigned offs, int change) const
155 check_alignment(offs);
159 StringCodec::Utf8::Decoder dec(StringCodec::IGNORE_ERRORS);
160 auto i = text.begin()+offs;
163 for(; change>0; --change)
164 dec.decode_char(text, i);
168 while(change<0 && i!=text.begin())
172 if(dec.decode_char(text, j)!=-1)
176 return i-text.begin();
179 void Text::offset_to_coords(unsigned offs, unsigned &row, unsigned &col) const
188 for(unsigned i=0; i<lines.size(); ++i)
189 if(offs>=lines[i].start && offs<=lines[i].start+lines[i].bytes)
192 if(lines[i].length==lines[i].bytes)
193 col = offs-lines[i].start;
195 col = count_characters(lines[i].start, offs-lines[i].start);
200 unsigned Text::coords_to_offset(unsigned row, unsigned col) const
202 if(row>=lines.size())
204 const Line &line = lines[row];
208 if(line.length==line.bytes)
209 return line.start+col;
212 StringCodec::Utf8::Decoder dec;
213 auto i = text.begin()+line.start;
214 for(col=min(col, line.length); col; --col)
215 dec.decode_char(text, i);
216 return i-text.begin();
220 Geometry Text::coords_to_geometry(const Part &part, const Geometry &parent, unsigned first_row, unsigned row, unsigned col) const
222 if(row>=lines.size())
223 row = lines.size()-1;
224 const Line &line = lines[row];
228 CoordsToGeomData data;
232 process_lines(part, parent, first_row, &Text::coords_to_geom_line, data);
237 void Text::build(const Part &part, State state, const Geometry &parent, PartCache &cache) const
239 build(part, state, parent, 0, cache);
242 void Text::build(const Part &part, State state, const Geometry &parent, unsigned first_row, PartCache &cache) const
244 if(!style || lines.empty())
247 const GL::Font &font = style->get_font();
248 GL::MeshBuilder bld(cache.create_mesh(part, font.get_texture()));
249 bld.color(style->get_font_color(state));
254 process_lines(part, parent, first_row, &Text::build_line, data);
257 Text &Text::operator=(const string &t)
263 void Text::find_lines()
266 float font_size = (style ? style->get_font_size() : 1);
267 string::size_type start = 0;
270 string::size_type newline = text.find('\n', start);
274 line.bytes = (newline==string::npos ? text.size() : newline)-start;
275 line.length = count_characters(line.start, line.bytes);
276 line.width = line.length;
279 string str = text.substr(line.start, line.bytes);
280 line.width = static_cast<unsigned>(style->get_font().get_string_width(str)*font_size);
282 lines.push_back(line);
284 if(newline==string::npos)
290 unsigned Text::count_characters(unsigned start, unsigned bytes) const
292 StringCodec::Utf8::Decoder dec;
293 auto i = text.begin()+start;
296 for(; i<end; dec.decode_char(text, i))
301 void Text::check_alignment(unsigned offs) const
303 StringCodec::Utf8::Decoder dec;
304 auto i = text.begin()+offs;
305 dec.decode_char(text, i);
309 void Text::process_lines(const Part &part, const Geometry &parent, unsigned first_row, void (Text::*func)(unsigned, const Geometry &, T &) const, T &data) const
314 const GL::Font &font = style->get_font();
315 float font_size = style->get_font_size();
316 unsigned line_spacing = static_cast<unsigned>(font_size*6/5);
317 int y_offset = static_cast<int>(-font.get_descent()*font_size);
320 unsigned n_lines = get_visible_lines(part, parent, &fit_height);
321 first_row = min<unsigned>(first_row, lines.size()-n_lines);
323 for(unsigned i=0; i<n_lines; ++i)
325 const Line &line = lines[first_row+i];
328 rgeom.w = line.width;
329 rgeom.h = fit_height;
330 rgeom.y = (n_lines-1-i)*line_spacing+y_offset;
331 part.get_alignment().apply(rgeom, parent, part.get_margin());
333 (this->*func)(first_row+i, rgeom, data);
337 void Text::build_line(unsigned i, const Geometry &rgeom, RenderData &data) const
339 const Line &line = lines[i];
341 GL::VertexBuilder::PushMatrix _pushm(*data.bld);
342 data.bld->transform(GL::Matrix::translation(rgeom.x, rgeom.y, 0));
343 data.bld->transform(GL::Matrix::scaling(style->get_font_size()));
345 style->get_font().build_string(text.substr(line.start, line.bytes), *data.bld);
348 void Text::coords_to_geom_line(unsigned i, const Geometry &rgeom, CoordsToGeomData &data) const
352 auto begin = text.begin()+lines[i].start;
354 if(lines[i].length==lines[i].bytes)
358 StringCodec::Utf8::Decoder dec;
359 for(unsigned c=data.col; c; --c)
360 dec.decode_char(text, j);
362 float w = style->get_font().get_string_width(string(begin, j));
364 data.result.x += static_cast<unsigned>(w*style->get_font_size());