guit  0.1
 All Classes Functions Variables Typedefs Enumerations Friends
gtextrender.hpp
1 //
2 // Text internals
3 // Guit GUI Toolkit
4 // Copyright © 2019/2020 Eric Lecolinet. All rights reserved.
5 // http://www.telecom-paris.fr/~elc
6 //
7 
8 #ifndef Guit_TextImpl_hpp
9 #define Guit_TextImpl_hpp
10 #include <list>
11 #include <ggraphics.hpp>
12 #include <gfont.hpp>
13 #include <gicon.hpp>
14 namespace guit {
15 
16 
17 class GTextCache {
18 public:
19  struct Row {
20  Row(float y, float h, size_t start) : y_(y), h_(h), start_(start) {}
21  float y_{}, h_{};
22  size_t start_{}, length_{};
23  };
24  ~GTextCache() {delete rows_;}
25  std::vector<Row> *rows_{};
26 };
27 
28 
29 class GTextRender {
30 public:
31  GTextRender(GFontValue const&, bool richtext, GRgba const* color,
32  size_t selbegin, size_t selend, GTextAttributes const*);
33 
34  GDim getSize(GString const& text);
35  void draw(GGraphics&, float x, float y, GString const& text, size_t offset);
36 
37  // lays out and return the size of multiline text.
38  static GDim layoutText(GFontValue const&, bool richtext, GString const& text,
39  float maxwidth, GTextAttributes const*,
40  GTextCache* = {});
41 private:
42  int attrFound(GString const& text, size_t offset);
43  void drawPart(GGraphics&, float x, float y, GString const& text,
44  bool drawlast, bool insel);
45  bool findCurrentAttr(size_t offset);
46  bool processAttr(GGraphics*, float x, float y, int delim,
47  GString const& attr, size_t offset);
48  void processAttrBegin(GGraphics*, float x, float y, GPropList const&) ;
49  void processAttrBegin(GGraphics*, float x, float y, GString const& attr);
50  void processAttrEnd();
51  void processImage(GGraphics*, float x, float y, size_t from, GString const& attr);
52 
53  GDim textsize_;
54  bool htmltags_;
55  size_t pos_, begpart_, selbegin_, selend_;
56  GTextAttributes const* attrlist_{};
57  GTextAttr const* curattr_{};
58  size_t curattrpos_{}; //, offset_{};
59 
60  struct StackNode {
61  StackNode(GFontValue const& font, GRgba const& color, GRgba const& bg)
62  : font_(font), color_(color), bgcolor_(bg) {}
63  GFontValue font_;
64  GRgba color_, bgcolor_;
65  };
66  std::vector<StackNode> stack_;
67  StackNode* current_;
68 };
69 
70 }
71 #endif