]> git.tdb.fi Git - libs/gltk.git/blob - source/text.h
3387f362953ce87971e8dab4534ae876c922df96
[libs/gltk.git] / source / text.h
1 #ifndef MSP_GLTK_TEXT_H_
2 #define MSP_GLTK_TEXT_H_
3
4 #include <string>
5 #include <vector>
6 #include "mspgltk_api.h"
7 #include "state.h"
8
9 namespace Msp {
10 namespace GLtk {
11
12 class Geometry;
13 class Part;
14 class PartCache;
15 class Style;
16
17 /**
18 Stores and renders text.  Supports multiline text.
19 */
20 class MSPGLTK_API Text
21 {
22 private:
23         struct Line
24         {
25                 std::size_t start = 0;
26                 std::size_t bytes = 0;
27                 std::size_t length = 0;
28                 unsigned width = 0;
29         };
30
31         struct RenderData;
32         struct CoordsToGeomData;
33
34         const Style *style = nullptr;
35         std::string text;
36         std::vector<Line> lines;
37
38 public:
39         Text();
40         Text(const Style &, const std::string &);
41
42         void set_style(const Style *);
43
44         unsigned get_width() const;
45         unsigned get_height() const;
46         void autosize(const Part &, Geometry &) const;
47
48         void set(const std::string &);
49         void erase(std::size_t, std::size_t);
50         void insert(std::size_t, const std::string &);
51         const std::string &get() const { return text; }
52         std::size_t size() const { return text.size(); }
53         std::size_t get_n_lines() const { return lines.size(); }
54         std::size_t get_visible_lines(const Part &, const Geometry &, unsigned *) const;
55         std::size_t get_line_length(std::size_t) const;
56         std::size_t move_offset(std::size_t, std::ptrdiff_t) const;
57         void offset_to_coords(std::size_t, std::size_t &, std::size_t &) const;
58         std::size_t coords_to_offset(std::size_t, std::size_t) const;
59         Geometry coords_to_geometry(const Part &, const Geometry &, std::size_t, std::size_t, std::size_t) const;
60
61         void build(const Part &, State, const Geometry &, PartCache &) const;
62         void build(const Part &, State, const Geometry &, std::size_t, PartCache &) const;
63
64         Text &operator=(const std::string &);
65 private:
66         void find_lines();
67         std::size_t count_characters(std::size_t, std::size_t) const;
68         void check_alignment(std::size_t) const;
69
70         template<typename T>
71         void process_lines(const Part &, const Geometry &, std::size_t, void (Text::*)(std::size_t, const Geometry &, T &) const, T &) const;
72
73         void build_line(std::size_t, const Geometry &, RenderData &) const;
74         void coords_to_geom_line(std::size_t, const Geometry &, CoordsToGeomData &) const;
75 };
76
77 } // namespace GLtk
78 } // namespace Msp
79
80 #endif