3 This file is part of libmspgltk
4 Copyright © 2009-2010 Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
8 #include <msp/gl/immediate.h>
9 #include <msp/gl/matrix.h>
18 struct Text::RenderData
20 GL::PrimitiveBuilder *bld;
23 struct Text::CoordsToGeomData
35 Text::Text(const Style &s, const string &t):
41 void Text::set_style(const Style *s)
45 float font_size = style->get_font()->get_default_size();
46 for(vector<Line>::iterator i=lines.begin(); i!=lines.end(); ++i)
47 i->width = static_cast<unsigned>(style->get_font()->get_string_width(text.substr(i->start, i->length))*font_size);
50 unsigned Text::get_width() const
53 for(vector<Line>::const_iterator i=lines.begin(); i!=lines.end(); ++i)
54 width = max(width, i->width);
58 unsigned Text::get_height() const
60 const GL::Font *font = style->get_font();
61 float font_size = font->get_default_size();
62 unsigned line_height = static_cast<unsigned>((font->get_ascent()-font->get_descent())*font_size);
63 unsigned line_spacing = line_height*6/5;
64 return line_height+(lines.size()-1)*line_spacing;
67 void Text::set(const string &t)
73 void Text::erase(unsigned pos, unsigned len)
77 vector<Line>::iterator i;
78 for(i=lines.begin(); (i!=lines.end() && i->start+i->length<pos); ++i) ;
80 if(pos+len>i->start+i->length)
86 for(++i; i!=lines.end(); ++i)
91 void Text::insert(unsigned pos, const string &s)
95 if(s.find('\n')!=string::npos)
99 vector<Line>::iterator i;
100 for(i=lines.begin(); (i!=lines.end() && i->start+i->length<pos); ++i) ;
102 i->length += s.size();
104 for(++i; i!=lines.end(); ++i)
105 i->start += s.size();
109 unsigned Text::get_line_length(unsigned i) const
112 throw InvalidParameterValue("Invalid line number");
113 return lines[i].length;
116 void Text::offset_to_coords(unsigned offs, unsigned &row, unsigned &col) const
125 for(unsigned i=0; i<lines.size(); ++i)
126 if(offs>=lines[i].start && offs<=lines[i].start+lines[i].length)
129 col = offs-lines[i].start;
134 unsigned Text::coords_to_offset(unsigned row, unsigned col) const
136 if(row>=lines.size())
139 return lines[row].start+min(col, lines[row].length);
142 Geometry Text::coords_to_geometry(const Part &part, const Geometry &parent, unsigned first_row, unsigned row, unsigned col) const
144 if(row>=lines.size())
145 row = lines.size()-1;
146 const Line &line = lines[row];
150 CoordsToGeomData data;
154 process_lines<CoordsToGeomData, &Text::coords_to_geom_line>(part, parent, first_row, data);
159 void Text::render(const Part &part, const Geometry &parent, unsigned first_row) const
164 const GL::Color &color = style->get_font_color();
165 GL::Immediate imm((GL::COLOR4_UBYTE, GL::TEXCOORD2, GL::VERTEX2));
166 imm.color(color.r, color.g, color.b);
171 process_lines<RenderData, &Text::render_line>(part, parent, first_row, data);
174 Text &Text::operator=(const string &t)
180 void Text::find_lines()
183 float font_size = style->get_font()->get_default_size();
184 string::size_type start = 0;
187 string::size_type newline = text.find('\n', start);
191 line.length = (newline==string::npos ? text.size() : newline)-start;
192 line.width = static_cast<unsigned>(style->get_font()->get_string_width(text.substr(line.start, line.length))*font_size);
193 lines.push_back(line);
195 if(newline==string::npos)
201 template<typename T, void (Text::*func)(unsigned, const Geometry &, T &) const>
202 void Text::process_lines(const Part &part, const Geometry &parent, unsigned first_row, T &data) const
204 const GL::Font *font = style->get_font();
205 float font_size = font->get_default_size();
206 unsigned line_height = static_cast<unsigned>((font->get_ascent()-font->get_descent())*font_size);
207 unsigned line_spacing = static_cast<unsigned>(font_size*6/5);
208 unsigned height = line_height+(lines.size()-1)*line_spacing;
209 int y_offset = static_cast<int>(-font->get_descent()*font_size);
211 const Sides &margin = part.get_margin();
212 unsigned n_lines = max(min(lines.size(), (parent.h-margin.top-margin.bottom)/line_spacing), 1U);
213 first_row = min(first_row, lines.size()-n_lines);
215 for(unsigned i=0; i<n_lines; ++i)
217 const Line &line = lines[first_row+i];
220 rgeom.w = line.width;
222 rgeom.y = (n_lines-1-i)*line_spacing+y_offset;
223 part.get_alignment().apply(rgeom, parent, part.get_margin());
225 (this->*func)(first_row+i, rgeom, data);
229 void Text::render_line(unsigned i, const Geometry &rgeom, RenderData &data) const
231 const Line &line = lines[i];
232 const GL::Font *font = style->get_font();
234 GL::PushMatrix _pushm;
235 GL::translate(rgeom.x, rgeom.y, 0);
236 GL::scale_uniform(font->get_default_size());
238 font->draw_string(text.substr(line.start, line.length), *data.bld);
241 void Text::coords_to_geom_line(unsigned i, const Geometry &rgeom, CoordsToGeomData &data) const
245 const Line &line = lines[i];
246 const GL::Font *font = style->get_font();
249 data.result.x += static_cast<unsigned>(font->get_string_width(text.substr(line.start, data.col))*font->get_default_size());