]> git.tdb.fi Git - libs/gltk.git/blob - source/text.cpp
Add Text class with multiline support
[libs/gltk.git] / source / text.cpp
1 #include <msp/gl/immediate.h>
2 #include <msp/gl/matrix.h>
3 #include "style.h"
4 #include "text.h"
5
6 using namespace std;
7
8 namespace Msp {
9 namespace GLtk {
10
11 Text::Text(const Style *const &s):
12         style(s)
13 { }
14
15 Text::Text(const Style *const &s, const string &t):
16         style(s)
17 {
18         set(t);
19 }
20
21 unsigned Text::get_width() const
22 {
23         unsigned width=0;
24         for(vector<Line>::const_iterator i=lines.begin(); i!=lines.end(); ++i)
25                 width=max(width, i->width);
26         return width;
27 }
28
29 unsigned Text::get_height() const
30 {
31         const GL::Font *font=style->get_font();
32         float font_size=font->get_default_size();
33         unsigned line_height=static_cast<unsigned>((font->get_ascent()-font->get_descent())*font_size);
34         unsigned line_spacing=line_height*6/5;
35         return line_height+(lines.size()-1)*line_spacing;
36 }
37
38 void Text::set(const string &t)
39 {
40         text=t;
41         lines.clear();
42         float font_size=style->get_font()->get_default_size();
43         unsigned start=0;
44         while(1)
45         {
46                 unsigned newline=text.find('\n', start);
47
48                 Line line;
49                 line.start=start;
50                 line.length=(newline==string::npos ? text.size() : newline)-start;
51                 line.width=static_cast<unsigned>(style->get_font()->get_string_width(text.substr(line.start, line.length))*font_size);
52                 lines.push_back(line);
53
54                 if(newline==string::npos)
55                         break;
56                 start=newline+1;
57         }
58 }
59
60 void Text::erase(unsigned pos, unsigned len)
61 {
62         text.erase(pos, len);
63 }
64
65 void Text::insert(unsigned pos, const string &s)
66 {
67         text.insert(pos, s);
68 }
69
70 void Text::render(const Part &part, const Geometry &geom) const
71 {
72         if(lines.empty())
73                 return;
74
75         const GL::Font *font=style->get_font();
76         float font_size=font->get_default_size();
77         unsigned line_height=static_cast<unsigned>((font->get_ascent()-font->get_descent())*font_size);
78         unsigned line_spacing=font_size*6/5;
79         unsigned height=line_height+(lines.size()-1)*line_spacing;
80         int y_offset=static_cast<int>(-font->get_descent()*font_size);
81
82         const GL::Color &color=style->get_font_color();
83         GL::Immediate imm((GL::COLOR4_UBYTE, GL::TEXCOORD2, GL::VERTEX2));
84         imm.color(color.r, color.g, color.b);
85         for(unsigned i=0; i<lines.size(); ++i)
86         {
87                 const Line &line=lines[i];
88
89                 Geometry rgeom;
90                 rgeom.w=line.width;
91                 rgeom.h=height;
92                 rgeom.y=(lines.size()-1-i)*line_spacing+y_offset;
93                 part.get_alignment().apply(rgeom, geom, part.get_margin());
94
95                 GL::push_matrix();
96                 GL::translate(rgeom.x, rgeom.y, 0);
97                 GL::scale_uniform(font_size);
98
99                 font->draw_string(text.substr(line.start, line.length), imm);
100
101                 GL::pop_matrix();
102         }
103 }
104
105 Text &Text::operator=(const string &t)
106 {
107         set(t);
108         return *this;
109 }
110
111 } // namespace GLtk
112 } // namespace Msp