]> git.tdb.fi Git - libs/gltk.git/blob - source/text.cpp
Use size_t to represent counts and indices
[libs/gltk.git] / source / text.cpp
1 #include <msp/core/algorithm.h>
2 #include <msp/gl/matrix.h>
3 #include <msp/gl/meshbuilder.h>
4 #include <msp/gl/texture2d.h>
5 #include "partcache.h"
6 #include "style.h"
7 #include "text.h"
8
9 using namespace std;
10
11 namespace Msp {
12 namespace GLtk {
13
14 struct Text::RenderData
15 {
16         GL::PrimitiveBuilder *bld;
17 };
18
19 struct Text::CoordsToGeomData
20 {
21         size_t row;
22         size_t col;
23         Geometry result;
24 };
25
26
27 Text::Text():
28         lines(1)
29 { }
30
31 Text::Text(const Style &s, const string &t):
32         style(&s)
33 {
34         set(t);
35 }
36
37 void Text::set_style(const Style *s)
38 {
39         style = s;
40
41         if(style)
42         {
43                 const GL::Font &font = style->get_font();
44                 float font_size = style->get_font_size();
45                 for(Line &l: lines)
46                         l.width = static_cast<unsigned>(font.get_string_width(text.substr(l.start, l.bytes))*font_size);
47         }
48 }
49
50 unsigned Text::get_width() const
51 {
52         unsigned width = 0;
53         for(const Line &l: lines)
54                 width = max(width, l.width);
55         return width;
56 }
57
58 unsigned Text::get_height() const
59 {
60         if(!style)
61                 return lines.size();
62
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;
68 }
69
70 void Text::autosize(const Part &part, Geometry &geom) const
71 {
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);
75 }
76
77 void Text::set(const string &t)
78 {
79         text = t;
80         find_lines();
81 }
82
83 void Text::erase(size_t pos, size_t len)
84 {
85         check_alignment(pos);
86         check_alignment(pos+len);
87         text.erase(pos, len);
88
89         auto i = find_if(lines, [pos](const Line &l){ return l.start+l.bytes>=pos; });
90
91         if(pos+len>i->start+i->bytes)
92                 find_lines();
93         else
94         {
95                 i->bytes -= len;
96                 i->length = count_characters(i->start, i->bytes);
97
98                 for(++i; i!=lines.end(); ++i)
99                         i->start -= len;
100         }
101 }
102
103 void Text::insert(size_t pos, const string &s)
104 {
105         check_alignment(pos);
106         text.insert(pos, s);
107
108         if(s.find('\n')!=string::npos)
109                 find_lines();
110         else
111         {
112                 auto i = find_if(lines, [pos](const Line &l){ return l.start+l.bytes>=pos; });
113
114                 i->bytes += s.size();
115                 i->length = count_characters(i->start, i->bytes);
116
117                 for(++i; i!=lines.end(); ++i)
118                         i->start += s.size();
119         }
120 }
121
122 size_t Text::get_visible_lines(const Part &part, const Geometry &parent, unsigned *fit_height) const
123 {
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);
128
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         size_t n_lines = min<size_t>(lines.size(), max(free_height/line_spacing, 1U));
133         if(fit_height)
134                 *fit_height = line_height+(n_lines-1)*line_spacing;
135
136         return n_lines;
137 }
138
139 size_t Text::get_line_length(size_t i) const
140 {
141         if(i>=lines.size())
142                 throw out_of_range("Text::get_line_length");
143         return lines[i].length;
144 }
145
146 size_t Text::move_offset(size_t offs, ptrdiff_t change) const
147 {
148         check_alignment(offs);
149         if(!change)
150                 return offs;
151
152         StringCodec::Utf8::Decoder dec(StringCodec::IGNORE_ERRORS);
153         auto i = text.begin()+offs;
154         if(change>0)
155         {
156                 for(; change>0; --change)
157                         dec.decode_char(text, i);
158         }
159         else
160         {
161                 while(change<0 && i!=text.begin())
162                 {
163                         --i;
164                         auto j = i;
165                         if(dec.decode_char(text, j)!=-1)
166                                 ++change;
167                 }
168         }
169         return i-text.begin();
170 }
171
172 void Text::offset_to_coords(size_t offs, size_t &row, size_t &col) const
173 {
174         if(lines.empty())
175         {
176                 row = 0;
177                 col = 0;
178                 return;
179         }
180
181         for(size_t i=0; i<lines.size(); ++i)
182                 if(offs>=lines[i].start && offs<=lines[i].start+lines[i].bytes)
183                 {
184                         row = i;
185                         if(lines[i].length==lines[i].bytes)
186                                 col = offs-lines[i].start;
187                         else
188                                 col = count_characters(lines[i].start, offs-lines[i].start);
189                         return;
190                 }
191 }
192
193 size_t Text::coords_to_offset(size_t row, size_t col) const
194 {
195         if(row>=lines.size())
196                 return text.size();
197         const Line &line = lines[row];
198         if(col>line.length)
199                 col = line.length;
200
201         if(line.length==line.bytes)
202                 return line.start+col;
203         else
204         {
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();
210         }
211 }
212
213 Geometry Text::coords_to_geometry(const Part &part, const Geometry &parent, size_t first_row, size_t row, size_t col) const
214 {
215         if(row>=lines.size())
216                 row = lines.size()-1;
217         const Line &line = lines[row];
218         if(col>line.length)
219                 col = line.length;
220
221         CoordsToGeomData data;
222         data.row = row;
223         data.col = col;
224
225         process_lines(part, parent, first_row, &Text::coords_to_geom_line, data);
226
227         return data.result;
228 }
229
230 void Text::build(const Part &part, State state, const Geometry &parent, PartCache &cache) const
231 {
232         build(part, state, parent, 0, cache);
233 }
234
235 void Text::build(const Part &part, State state, const Geometry &parent, size_t first_row, PartCache &cache) const
236 {
237         if(!style || lines.empty())
238                 return;
239
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));
243
244         RenderData data;
245         data.bld = &bld;
246
247         process_lines(part, parent, first_row, &Text::build_line, data);
248 }
249
250 Text &Text::operator=(const string &t)
251 {
252         set(t);
253         return *this;
254 }
255
256 void Text::find_lines()
257 {
258         lines.clear();
259         float font_size = (style ? style->get_font_size() : 1);
260         string::size_type start = 0;
261         while(1)
262         {
263                 string::size_type newline = text.find('\n', start);
264
265                 Line line;
266                 line.start = 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;
270                 if(style)
271                 {
272                         string str = text.substr(line.start, line.bytes);
273                         line.width = static_cast<unsigned>(style->get_font().get_string_width(str)*font_size);
274                 }
275                 lines.push_back(line);
276
277                 if(newline==string::npos)
278                         break;
279                 start = newline+1;
280         }
281 }
282
283 size_t Text::count_characters(size_t start, size_t bytes) const
284 {
285         StringCodec::Utf8::Decoder dec;
286         auto i = text.begin()+start;
287         auto end = i+bytes;
288         size_t count = 0;
289         for(; i<end; dec.decode_char(text, i))
290                 ++count;
291         return count;
292 }
293
294 void Text::check_alignment(size_t offs) const
295 {
296         StringCodec::Utf8::Decoder dec;
297         auto i = text.begin()+offs;
298         dec.decode_char(text, i);
299 }
300
301 template<typename T>
302 void Text::process_lines(const Part &part, const Geometry &parent, size_t first_row, void (Text::*func)(size_t, const Geometry &, T &) const, T &data) const
303 {
304         if(!style)
305                 return;
306
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);
311
312         unsigned fit_height;
313         size_t n_lines = get_visible_lines(part, parent, &fit_height);
314         first_row = min(first_row, lines.size()-n_lines);
315
316         for(size_t i=0; i<n_lines; ++i)
317         {
318                 const Line &line = lines[first_row+i];
319
320                 Geometry rgeom;
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());
325
326                 (this->*func)(first_row+i, rgeom, data);
327         }
328 }
329
330 void Text::build_line(size_t i, const Geometry &rgeom, RenderData &data) const
331 {
332         const Line &line = lines[i];
333
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()));
337
338         style->get_font().build_string(text.substr(line.start, line.bytes), *data.bld);
339 }
340
341 void Text::coords_to_geom_line(size_t i, const Geometry &rgeom, CoordsToGeomData &data) const
342 {
343         if(i==data.row)
344         {
345                 auto begin = text.begin()+lines[i].start;
346                 auto j = begin;
347                 if(lines[i].length==lines[i].bytes)
348                         j += data.col;
349                 else
350                 {
351                         StringCodec::Utf8::Decoder dec;
352                         for(size_t c=data.col; c; --c)
353                                 dec.decode_char(text, j);
354                 }
355                 float w = style->get_font().get_string_width(string(begin, j));
356                 data.result = rgeom;
357                 data.result.x += static_cast<unsigned>(w*style->get_font_size());
358         }
359 }
360
361 } // namespace GLtk
362 } // namespace Msp