Pulp 1.88.0
Cross-platform audio plugin framework
Loading...
Searching...
No Matches
text_font_context.hpp
Go to the documentation of this file.
1// text_font_context.hpp — owns the per-context typeface registry used by
2// Canvas2D `fillText/strokeText` (via SkShaper fallback runs) and by the
3// `TextShaper` measurement path (via SkParagraph FontCollection).
4//
5// Why this exists:
6// - HarfBuzz can only shape a multi-codepoint emoji cluster (ZWJ, flag,
7// keycap, modifier, tag) if the whole cluster lands on one font run.
8// `emoji_segmenter` pre-segments the text, and `TextFontContext`
9// supplies the typeface for each role (Default vs Emoji).
10// - A process-wide singleton would let one plugin overwrite another's
11// fallback registration in the same host process. Instead each
12// `AssetManager` / `Canvas` owns a context; a `shared()` default is
13// available for plugin authors who haven't wired their own.
14// - `SkParagraph::ParagraphBuilder` needs a `FontCollection` for label
15// measurement; we cache one per context so it isn't reconstructed
16// every call.
17//
18// This header is Skia-aware and intentionally only ships under
19// `PULP_HAS_SKIA`. Non-Skia callers should use the API stubs in
20// `font_registry_stubs.cpp`.
21
22#pragma once
23
26
27#include <cstdint>
28#include <memory>
29#include <mutex>
30#include <string>
31#include <vector>
32
33#ifdef PULP_HAS_SKIA
34
35#include "include/core/SkFontMgr.h"
36#include "include/core/SkFontStyle.h"
37#include "include/core/SkRefCnt.h"
38#include "include/core/SkTypeface.h"
39
40namespace skia { namespace textlayout {
41class FontCollection;
42class TypefaceFontProvider;
43} }
44
45namespace pulp::canvas {
46
47class TextFontContext {
48public:
49 TextFontContext();
50 ~TextFontContext();
51
52 TextFontContext(const TextFontContext&) = delete;
53 TextFontContext& operator=(const TextFontContext&) = delete;
54
58 static std::shared_ptr<TextFontContext> shared();
59
62 sk_sp<SkFontMgr> font_manager() const { return font_mgr_; }
63
66 sk_sp<skia::textlayout::FontCollection> font_collection() const;
67
71 std::unique_lock<std::mutex> lock_font_collection_use() const {
72 return std::unique_lock<std::mutex>(font_collection_use_mutex_);
73 }
74
79 std::string emoji_family_name() const;
80
83 void set_emoji_typeface(sk_sp<SkTypeface> face);
84
86 sk_sp<SkTypeface> emoji_typeface() const;
87
90 bool has_emoji_typeface() const;
91
97 sk_sp<SkTypeface> typeface_for_run(FontRunRole role,
98 const sk_sp<SkTypeface>& primary) const;
99
100private:
101 sk_sp<SkFontMgr> font_mgr_;
102 sk_sp<SkTypeface> emoji_typeface_;
103 std::string emoji_family_name_;
104
105 mutable std::mutex mutex_;
106 mutable std::mutex font_collection_use_mutex_;
107 mutable sk_sp<skia::textlayout::FontCollection> font_collection_;
108 mutable sk_sp<skia::textlayout::TypefaceFontProvider> typeface_provider_;
109 mutable std::uint64_t collection_generation_ = 0;
110};
111
112// ── Public registration API ────────────────────────────────────────────
113
117bool register_emoji_fallback(sk_sp<SkTypeface> face);
118
123bool register_emoji_fallback(const std::string& family);
124
132
138
144
145} // namespace pulp::canvas
146
147#else // PULP_HAS_SKIA
148
149namespace pulp::canvas {
150// Non-Skia stubs so plugin startup code can call these unconditionally.
151inline bool register_emoji_fallback(...) { return false; }
152inline bool register_platform_emoji_fallback() { return false; }
153inline bool register_bundled_noto_color_emoji() { return false; }
154inline bool register_best_available_emoji_fallback() { return false; }
155} // namespace pulp::canvas
156
157#endif // PULP_HAS_SKIA
@ shared
Definition device.hpp:32
Definition affine_transform.hpp:24
bool register_bundled_noto_color_emoji()
Definition text_font_context.hpp:153
bool register_emoji_fallback(...)
Definition text_font_context.hpp:151
bool register_best_available_emoji_fallback()
Definition text_font_context.hpp:154
bool register_platform_emoji_fallback()
Definition text_font_context.hpp:152
@ skia
CPU Skia raster (SkSurfaces::Raster).
Definition screenshot.hpp:15