]> git.tdb.fi Git - libs/gltk.git/blob - source/text.cpp
Improve widget part caching
[libs/gltk.git] / source / text.cpp
1 #include <msp/gl/matrix.h>
2 #include <msp/gl/meshbuilder.h>
3 #include <msp/gl/texture2d.h>
4 #include "partcache.h"
5 #include "style.h"
6 #include "text.h"
7
8 using namespace std;
9
10 namespace Msp {
11 namespace GLtk {
12
13 struct Text::RenderData
14 {
15         GL::PrimitiveBuilder *bld;
16 };
17
18 struct Text::CoordsToGeomData
19 {
20         unsigned row;
21         unsigned col;
22         Geometry result;
23 };
24
25
26 Text::Text():
27         style(0)
28 { }
29
30 Text::Text(const Style &s, const string &t):
31         style(&s)
32 {
33         set(t);
34 }
35
36 void Text::set_style(const Style *s)
37 {
38         style = s;
39
40         if(style)
41         {
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);
46         }
47 }
48
49 unsigned Text::get_width() const
50 {
51         unsigned width = 0;
52         for(vector<Line>::const_iterator i=lines.begin(); i!=lines.end(); ++i)
53                 width = max(width, i->width);
54         return width;
55 }
56
57 unsigned Text::get_height() const
58 {
59         if(!style)
60                 return lines.size();
61
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;
67 }
68
69 void Text::set(const string &t)
70 {
71         text = t;
72         find_lines();
73 }
74
75 void Text::erase(unsigned pos, unsigned len)
76 {
77         text.erase(pos, len);
78
79         vector<Line>::iterator i;
80         for(i=lines.begin(); (i!=lines.end() && i->start+i->length<pos); ++i) ;
81
82         if(pos+len>i->start+i->length)
83                 find_lines();
84         else
85         {
86                 i->length -= len;
87
88                 for(++i; i!=lines.end(); ++i)
89                         i->start -= len;
90         }
91 }
92
93 void Text::insert(unsigned pos, const string &s)
94 {
95         text.insert(pos, s);
96
97         if(s.find('\n')!=string::npos)
98                 find_lines();
99         else
100         {
101                 vector<Line>::iterator i;
102                 for(i=lines.begin(); (i!=lines.end() && i->start+i->length<pos); ++i) ;
103
104                 i->length += s.size();
105
106                 for(++i; i!=lines.end(); ++i)
107                         i->start += s.size();
108         }
109 }
110
111 unsigned Text::get_line_length(unsigned i) const
112 {
113         if(i>=lines.size())
114                 throw out_of_range("Text::get_line_length");
115         return lines[i].length;
116 }
117
118 void Text::offset_to_coords(unsigned offs, unsigned &row, unsigned &col) const
119 {
120         if(lines.empty())
121         {
122                 row = 0;
123                 col = 0;
124                 return;
125         }
126
127         for(unsigned i=0; i<lines.size(); ++i)
128                 if(offs>=lines[i].start && offs<=lines[i].start+lines[i].length)
129                 {
130                         row = i;
131                         col = offs-lines[i].start;
132                         return;
133                 }
134 }
135
136 unsigned Text::coords_to_offset(unsigned row, unsigned col) const
137 {
138         if(row>=lines.size())
139                 return text.size();
140
141         return lines[row].start+min(col, lines[row].length);
142 }
143
144 Geometry Text::coords_to_geometry(const Part &part, const Geometry &parent, unsigned first_row, unsigned row, unsigned col) const
145 {
146         if(row>=lines.size())
147                 row = lines.size()-1;
148         const Line &line = lines[row];
149         if(col>line.length)
150                 col = line.length;
151
152         CoordsToGeomData data;
153         data.row = row;
154         data.col = col;
155
156         process_lines<CoordsToGeomData, &Text::coords_to_geom_line>(part, parent, first_row, data);
157
158         return data.result;
159 }
160
161 void Text::build(const Part &part, const Geometry &parent, PartCache &cache) const
162 {
163         build(part, parent, 0, cache);
164 }
165
166 void Text::build(const Part &part, const Geometry &parent, unsigned first_row, PartCache &cache) const
167 {
168         if(!style || lines.empty())
169                 return;
170
171         const GL::Font &font = style->get_font();
172         GL::MeshBuilder bld(cache.create_mesh(part, font.get_texture()));
173         bld.color(style->get_font_color());
174
175         RenderData data;
176         data.bld = &bld;
177
178         process_lines<RenderData, &Text::build_line>(part, parent, first_row, data);
179 }
180
181 Text &Text::operator=(const string &t)
182 {
183         set(t);
184         return *this;
185 }
186
187 void Text::find_lines()
188 {
189         lines.clear();
190         float font_size = (style ? style->get_font_size() : 1);
191         string::size_type start = 0;
192         while(1)
193         {
194                 string::size_type newline = text.find('\n', start);
195
196                 Line line;
197                 line.start = start;
198                 line.length = (newline==string::npos ? text.size() : newline)-start;
199                 line.width = line.length;
200                 if(style)
201                 {
202                         string str = text.substr(line.start, line.length);
203                         line.width = static_cast<unsigned>(style->get_font().get_string_width(str)*font_size);
204                 }
205                 lines.push_back(line);
206
207                 if(newline==string::npos)
208                         break;
209                 start = newline+1;
210         }
211 }
212
213 template<typename T, void (Text::*func)(unsigned, const Geometry &, T &) const>
214 void Text::process_lines(const Part &part, const Geometry &parent, unsigned first_row, T &data) const
215 {
216         if(!style)
217                 return;
218
219         const GL::Font &font = style->get_font();
220         float font_size = style->get_font_size();
221         unsigned line_height = static_cast<unsigned>((font.get_ascent()-font.get_descent())*font_size);
222         unsigned line_spacing = static_cast<unsigned>(font_size*6/5);
223         unsigned height = line_height+(lines.size()-1)*line_spacing;
224         int y_offset = static_cast<int>(-font.get_descent()*font_size);
225
226         const Sides &margin = part.get_margin();
227         unsigned n_lines = min<unsigned>(lines.size(), max((parent.h-margin.top-margin.bottom)/line_spacing, 1U));
228         first_row = min<unsigned>(first_row, lines.size()-n_lines);
229
230         for(unsigned i=0; i<n_lines; ++i)
231         {
232                 const Line &line = lines[first_row+i];
233
234                 Geometry rgeom;
235                 rgeom.w = line.width;
236                 rgeom.h = height;
237                 rgeom.y = (n_lines-1-i)*line_spacing+y_offset;
238                 part.get_alignment().apply(rgeom, parent, part.get_margin());
239
240                 (this->*func)(first_row+i, rgeom, data);
241         }
242 }
243
244 void Text::build_line(unsigned i, const Geometry &rgeom, RenderData &data) const
245 {
246         const Line &line = lines[i];
247
248         GL::MatrixStack::Push _pushm(data.bld->matrix());
249         data.bld->matrix() *= GL::Matrix::translation(rgeom.x, rgeom.y, 0);
250         data.bld->matrix() *= GL::Matrix::scaling(style->get_font_size());
251
252         style->get_font().build_string(text.substr(line.start, line.length), *data.bld);
253 }
254
255 void Text::coords_to_geom_line(unsigned i, const Geometry &rgeom, CoordsToGeomData &data) const
256 {
257         if(i==data.row)
258         {
259                 float w = style->get_font().get_string_width(text.substr(lines[i].start, data.col));
260                 data.result = rgeom;
261                 data.result.x += static_cast<unsigned>(w*style->get_font_size());
262         }
263 }
264
265 } // namespace GLtk
266 } // namespace Msp