1 #pragma once
2 #include "Fontstash.h"
3 #include "Color.h"
4 #include "Geometry.h"
5 #include "Cairo++.h"
6 #include <cstdint>
7 
8 struct Font;
9 
10 ///
11 class FontEngine {
12 public:
13     FontEngine();
14     FontEngine(unsigned width, unsigned height);
15     ~FontEngine();
16 
17     bool addFont(const char *name, const uint8_t *data, unsigned size);
18 
19     enum Align {
20         AlignCenter = 0,
21         AlignTop = 1,
22         AlignBottom = 2,
23         AlignLeft = 4,
24         AlignRight = 8,
25         AlignInside = 16,
26         AlignTopLeft = AlignTop | AlignLeft,
27         AlignTopRight = AlignTop | AlignRight,
28         AlignBottomLeft = AlignBottom | AlignLeft,
29         AlignBottomRight = AlignBottom | AlignRight,
30     };
31 
32     void draw(cairo_t *cr, const char *text, const Font &font, double x, double y);
33     void drawInBox(cairo_t *cr, const char *text, const Font &font, const Rect &box, int align);
34     void drawInBox(cairo_t *cr, const char *text, const Font &font, const RectF &box, int align);
35 
36 private:
37     static int renderCreate(void *uptr, int width, int height);
38     static int renderResize(void *uptr, int width, int height);
39     static void renderUpdate(void *uptr, int *rect, const unsigned char *data);
40     static void renderDraw(void *uptr, const FONSquad *quads, const unsigned int *colors, int nquads);
41     static void renderDelete(void *uptr);
42 
43 private:
44     FONScontext_u fContext;
45     cairo_surface_u fAtlas;
46     cairo_t *fDrawingContext = nullptr;
47 };
48 
49 ///
50 struct Font {
51     std::string name = "default";
52     float size = 12.0;
53     ColorRGBA8 color = {0x00, 0x00, 0x00, 0xff};
54     float spacing = 0.0;
55     float blur = 0;
56 };
57 
58 inline bool operator==(const Font &a, const Font &b)
59 {
60     return a.name == b.name && a.size == b.size && a.color == b.color &&
61         a.spacing == b.spacing && a.blur == b.blur;
62 }
63 
64 inline bool operator!=(const Font &a, const Font &b)
65 {
66     return !operator==(a, b);
67 }
68