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->bytes))*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)
85 check_alignment(pos+len);
88 vector<Line>::iterator i;
89 for(i=lines.begin(); (i!=lines.end() && i->start+i->bytes<pos); ++i) ;
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 vector<Line>::iterator i;
113 for(i=lines.begin(); (i!=lines.end() && i->start+i->bytes<pos); ++i) ;
115 i->bytes += s.size();
116 i->length = count_characters(i->start, i->bytes);
118 for(++i; i!=lines.end(); ++i)
119 i->start += s.size();
123 unsigned Text::get_visible_lines(const Part &part, const Geometry &parent, unsigned *fit_height) const
125 const GL::Font &font = style->get_font();
126 float font_size = style->get_font_size();
127 unsigned line_height = static_cast<unsigned>((font.get_ascent()-font.get_descent())*font_size);
128 unsigned line_spacing = static_cast<unsigned>(font_size*6/5);
130 const Sides &margin = part.get_margin();
131 unsigned vmargin = margin.top+margin.bottom;
132 unsigned free_height = max(parent.h, vmargin)-vmargin+line_spacing-line_height;
133 unsigned n_lines = min<unsigned>(lines.size(), max(free_height/line_spacing, 1U));
135 *fit_height = line_height+(n_lines-1)*line_spacing;
140 unsigned Text::get_line_length(unsigned i) const
143 throw out_of_range("Text::get_line_length");
144 return lines[i].length;
147 unsigned Text::move_offset(unsigned offs, int change) const
149 check_alignment(offs);
153 StringCodec::Utf8::Decoder dec(StringCodec::IGNORE_ERRORS);
154 string::const_iterator i = text.begin()+offs;
157 for(; change>0; --change)
158 dec.decode_char(text, i);
162 while(change<0 && i!=text.begin())
165 string::const_iterator j = i;
166 if(dec.decode_char(text, j)!=-1)
170 return i-text.begin();
173 void Text::offset_to_coords(unsigned offs, unsigned &row, unsigned &col) const
182 for(unsigned i=0; i<lines.size(); ++i)
183 if(offs>=lines[i].start && offs<=lines[i].start+lines[i].bytes)
186 if(lines[i].length==lines[i].bytes)
187 col = offs-lines[i].start;
189 col = count_characters(lines[i].start, offs-lines[i].start);
194 unsigned Text::coords_to_offset(unsigned row, unsigned col) const
196 if(row>=lines.size())
198 const Line &line = lines[row];
202 if(line.length==line.bytes)
203 return line.start+col;
206 StringCodec::Utf8::Decoder dec;
207 string::const_iterator i = text.begin()+line.start;
208 for(col=min(col, line.length); col; --col)
209 dec.decode_char(text, i);
210 return i-text.begin();
214 Geometry Text::coords_to_geometry(const Part &part, const Geometry &parent, unsigned first_row, unsigned row, unsigned col) const
216 if(row>=lines.size())
217 row = lines.size()-1;
218 const Line &line = lines[row];
222 CoordsToGeomData data;
226 process_lines(part, parent, first_row, &Text::coords_to_geom_line, data);
231 void Text::build(const Part &part, State state, const Geometry &parent, PartCache &cache) const
233 build(part, state, parent, 0, cache);
236 void Text::build(const Part &part, State state, const Geometry &parent, unsigned first_row, PartCache &cache) const
238 if(!style || lines.empty())
241 const GL::Font &font = style->get_font();
242 GL::MeshBuilder bld(cache.create_mesh(part, font.get_texture()));
243 bld.color(style->get_font_color(state));
248 process_lines(part, parent, first_row, &Text::build_line, data);
251 Text &Text::operator=(const string &t)
257 void Text::find_lines()
260 float font_size = (style ? style->get_font_size() : 1);
261 string::size_type start = 0;
264 string::size_type newline = text.find('\n', start);
268 line.bytes = (newline==string::npos ? text.size() : newline)-start;
269 line.length = count_characters(line.start, line.bytes);
270 line.width = line.length;
273 string str = text.substr(line.start, line.bytes);
274 line.width = static_cast<unsigned>(style->get_font().get_string_width(str)*font_size);
276 lines.push_back(line);
278 if(newline==string::npos)
284 unsigned Text::count_characters(unsigned start, unsigned bytes) const
286 StringCodec::Utf8::Decoder dec;
287 string::const_iterator i = text.begin()+start;
288 string::const_iterator end = i+bytes;
290 for(; i<end; dec.decode_char(text, i))
295 void Text::check_alignment(unsigned offs) const
297 StringCodec::Utf8::Decoder dec;
298 string::const_iterator i = text.begin()+offs;
299 dec.decode_char(text, i);
303 void Text::process_lines(const Part &part, const Geometry &parent, unsigned first_row, void (Text::*func)(unsigned, const Geometry &, T &) const, T &data) const
308 const GL::Font &font = style->get_font();
309 float font_size = style->get_font_size();
310 unsigned line_spacing = static_cast<unsigned>(font_size*6/5);
311 int y_offset = static_cast<int>(-font.get_descent()*font_size);
314 unsigned n_lines = get_visible_lines(part, parent, &fit_height);
315 first_row = min<unsigned>(first_row, lines.size()-n_lines);
317 for(unsigned i=0; i<n_lines; ++i)
319 const Line &line = lines[first_row+i];
322 rgeom.w = line.width;
323 rgeom.h = fit_height;
324 rgeom.y = (n_lines-1-i)*line_spacing+y_offset;
325 part.get_alignment().apply(rgeom, parent, part.get_margin());
327 (this->*func)(first_row+i, rgeom, data);
331 void Text::build_line(unsigned i, const Geometry &rgeom, RenderData &data) const
333 const Line &line = lines[i];
335 GL::MatrixStack::Push _pushm(data.bld->matrix());
336 data.bld->matrix() *= GL::Matrix::translation(rgeom.x, rgeom.y, 0);
337 data.bld->matrix() *= GL::Matrix::scaling(style->get_font_size());
339 style->get_font().build_string(text.substr(line.start, line.bytes), *data.bld);
342 void Text::coords_to_geom_line(unsigned i, const Geometry &rgeom, CoordsToGeomData &data) const
346 string::const_iterator begin = text.begin()+lines[i].start;
347 string::const_iterator j = begin;
348 if(lines[i].length==lines[i].bytes)
352 StringCodec::Utf8::Decoder dec;
353 for(unsigned c=data.col; c; --c)
354 dec.decode_char(text, j);
356 float w = style->get_font().get_string_width(string(begin, j));
358 data.result.x += static_cast<unsigned>(w*style->get_font_size());