]> git.tdb.fi Git - libs/gltk.git/blob - source/text.cpp
Handle line data when inserting/erasing text
[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         find_lines();
42 }
43
44 void Text::erase(unsigned pos, unsigned len)
45 {
46         text.erase(pos, len);
47
48         vector<Line>::iterator i;
49         for(i=lines.begin(); (i!=lines.end() && i->start+i->length<pos); ++i) ;
50
51         if(pos+len>i->start+i->length)
52                 find_lines();
53         else
54         {
55                 i->length-=len;
56
57                 for(++i; i!=lines.end(); ++i)
58                         i->start-=len;
59         }
60 }
61
62 void Text::insert(unsigned pos, const string &s)
63 {
64         text.insert(pos, s);
65
66         if(s.find('\n')!=string::npos)
67                 find_lines();
68         else
69         {
70                 vector<Line>::iterator i;
71                 for(i=lines.begin(); (i!=lines.end() && i->start+i->length<pos); ++i) ;
72
73                 i->length+=s.size();
74
75                 for(++i; i!=lines.end(); ++i)
76                         i->start+=s.size();
77         }
78 }
79
80 void Text::render(const Part &part, const Geometry &geom) const
81 {
82         if(lines.empty())
83                 return;
84
85         const GL::Font *font=style->get_font();
86         float font_size=font->get_default_size();
87         unsigned line_height=static_cast<unsigned>((font->get_ascent()-font->get_descent())*font_size);
88         unsigned line_spacing=font_size*6/5;
89         unsigned height=line_height+(lines.size()-1)*line_spacing;
90         int y_offset=static_cast<int>(-font->get_descent()*font_size);
91
92         const GL::Color &color=style->get_font_color();
93         GL::Immediate imm((GL::COLOR4_UBYTE, GL::TEXCOORD2, GL::VERTEX2));
94         imm.color(color.r, color.g, color.b);
95         for(unsigned i=0; i<lines.size(); ++i)
96         {
97                 const Line &line=lines[i];
98
99                 Geometry rgeom;
100                 rgeom.w=line.width;
101                 rgeom.h=height;
102                 rgeom.y=(lines.size()-1-i)*line_spacing+y_offset;
103                 part.get_alignment().apply(rgeom, geom, part.get_margin());
104
105                 GL::push_matrix();
106                 GL::translate(rgeom.x, rgeom.y, 0);
107                 GL::scale_uniform(font_size);
108
109                 font->draw_string(text.substr(line.start, line.length), imm);
110
111                 GL::pop_matrix();
112         }
113 }
114
115 Text &Text::operator=(const string &t)
116 {
117         set(t);
118         return *this;
119 }
120
121 void Text::find_lines()
122 {
123         lines.clear();
124         float font_size=style->get_font()->get_default_size();
125         string::size_type start=0;
126         while(1)
127         {
128                 string::size_type newline=text.find('\n', start);
129
130                 Line line;
131                 line.start=start;
132                 line.length=(newline==string::npos ? text.size() : newline)-start;
133                 line.width=static_cast<unsigned>(style->get_font()->get_string_width(text.substr(line.start, line.length))*font_size);
134                 lines.push_back(line);
135
136                 if(newline==string::npos)
137                         break;
138                 start=newline+1;
139         }
140 }
141
142 } // namespace GLtk
143 } // namespace Msp