libpromeki 1.0.0-alpha
PROfessional MEdia toolKIt
 
Loading...
Searching...
No Matches
fastfont.h
Go to the documentation of this file.
1
8#pragma once
9
10
11#include <promeki/config.h>
12#if PROMEKI_ENABLE_PROAV
13#include <promeki/font.h>
14#include <promeki/list.h>
15#include <promeki/map.h>
17#include <promeki/buffer.h>
18#include <promeki/uniqueptr.h>
19
20PROMEKI_NAMESPACE_BEGIN
21
80class FastFont : public Font {
81 public:
83 using UPtr = UniquePtr<FastFont>;
84
96 struct DrawStyle {
97 Color foreground;
98 Color background;
99 bool bold = false;
100 bool italic = false;
101 bool underline = false;
102 };
103
108 FastFont(const PaintEngine &pe);
109
111 ~FastFont() override;
112
113 bool drawText(const String &text, int x, int y) override;
114 int measureText(const String &text) override;
115 int lineHeight() override;
116 int ascender() override;
117 int descender() override;
118
129 bool drawText(const String &text, int x, int y, const DrawStyle &style);
130
140 int measureText(const String &text, const DrawStyle &style);
141
142 protected:
143 void onStateChanged() override;
146 void onColorChanged() override;
147
148 private:
149 struct GlyphKey {
150 uint32_t codepoint = 0;
151 uint32_t fgRGBA = 0;
152 uint32_t bgRGBA = 0;
153 uint8_t styleFlags = 0;
154 bool operator<(const GlyphKey &o) const {
155 if (codepoint != o.codepoint) return codepoint < o.codepoint;
156 if (fgRGBA != o.fgRGBA) return fgRGBA < o.fgRGBA;
157 if (bgRGBA != o.bgRGBA) return bgRGBA < o.bgRGBA;
158 return styleFlags < o.styleFlags;
159 }
160 bool operator==(const GlyphKey &o) const {
161 return codepoint == o.codepoint && fgRGBA == o.fgRGBA
162 && bgRGBA == o.bgRGBA && styleFlags == o.styleFlags;
163 }
164 };
165
166 struct CachedGlyph {
167 UncompressedVideoPayload::Ptr payload;
168 int advanceX = 0;
173 int faceIndex = 0;
174 };
175
179 struct LoadedFace {
180 void *face = nullptr;
181 Buffer data;
182 String path;
183 };
184
185 bool ensureFontLoaded();
186 bool loadFace(const String &path, LoadedFace &out);
187 const CachedGlyph *getGlyph(uint32_t codepoint, const GlyphKey &key, const Color &fg, const Color &bg);
188 void invalidateGlyphs();
189 void invalidateFont();
190 void invalidateAll();
191
195 GlyphKey makeKey(uint32_t codepoint, const DrawStyle &style, Color &outFg, Color &outBg) const;
196
198 static uint32_t colorKey(const Color &c);
199
205 int resolveFaceIndex(uint32_t codepoint) const;
206
207 void *_ftLibrary = nullptr;
208 List<LoadedFace> _ftFaces;
209 int _ascender = 0;
210 int _descender = 0;
211 int _lineHeight = 0;
213 int _underlineThickness = 1;
215 int _underlinePosition = 0;
223 int _alignX = 1;
225 int _alignY = 1;
226 Map<GlyphKey, CachedGlyph> _glyphCache;
227};
228
229PROMEKI_NAMESPACE_END
230
231#endif // PROMEKI_ENABLE_PROAV