Debian Codegraph @

modules/skshaper/utils/FactoryHelpers.h

raw
1 /*
2 * Copyright 2024 Google LLC
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7 #ifndef SkShaperFactoryHelpers_DEFINED
8 #define SkShaperFactoryHelpers_DEFINED
9
10 #include "modules/skshaper/include/SkShaper.h"
11 #include "modules/skshaper/include/SkShaper_factory.h"
12
13 #if defined(SK_SHAPER_UNICODE_AVAILABLE)
14 #include "modules/skunicode/include/SkUnicode.h"
15 #if defined(SK_SHAPER_HARFBUZZ_AVAILABLE)
16 #include "modules/skshaper/include/SkShaper_harfbuzz.h"
17 #include "modules/skshaper/include/SkShaper_skunicode.h"
18 #endif // defined(SK_SHAPER_HARFBUZZ_AVAILABLE)
19 #endif // defined(SK_SHAPER_UNICODE_AVAILABLE)
20
21 #if defined(SK_SHAPER_CORETEXT_AVAILABLE)
22 #include "modules/skshaper/include/SkShaper_coretext.h"
23 #endif
24
25 #if defined(SK_UNICODE_ICU_IMPLEMENTATION)
26 #include "modules/skunicode/include/SkUnicode_icu.h"
27 #endif
28
29 #if defined(SK_UNICODE_LIBGRAPHEME_IMPLEMENTATION)
30 #include "modules/skunicode/include/SkUnicode_libgrapheme.h"
31 #endif
32
33 #if defined(SK_UNICODE_ICU4X_IMPLEMENTATION)
34 #include "modules/skunicode/include/SkUnicode_icu4x.h"
35 #endif
36
37 namespace SkShapers {
38 #if defined(SK_SHAPER_UNICODE_AVAILABLE)
39 static inline sk_sp<SkUnicode> BestAvailableUnicode() {
40 sk_sp<SkUnicode> uc;
41 #if defined(SK_UNICODE_ICU_IMPLEMENTATION)
42 uc = SkUnicodes::ICU::Make();
43 #endif
44 #if defined(SK_UNICODE_ICU4X_IMPLEMENTATION)
45 if (!uc) {
46 uc = SkUnicodes::ICU4X::Make();
47 }
48 #endif
49 #if defined(SK_UNICODE_LIBGRAPHEME_IMPLEMENTATION)
50 if (!uc) {
51 uc = SkUnicodes::Libgrapheme::Make();
52 }
53 #endif
54 return uc;
55 }
56
57 #if defined(SK_SHAPER_HARFBUZZ_AVAILABLE)
58 class HarfbuzzFactory final : public Factory {
59 public:
60 explicit HarfbuzzFactory(sk_sp<SkUnicode> uc = nullptr)
61 : fUnicode(uc ? std::move(uc) : BestAvailableUnicode()) {}
62
63 std::unique_ptr<SkShaper> makeShaper(sk_sp<SkFontMgr> fallback) override {
64 return SkShapers::HB::ShaperDrivenWrapper(fUnicode, fallback);
65 }
66
67 std::unique_ptr<SkShaper::BiDiRunIterator> makeBidiRunIterator(const char* utf8,
68 size_t utf8Bytes,
69 uint8_t bidiLevel) override {
70 return SkShapers::unicode::BidiRunIterator(fUnicode, utf8, utf8Bytes, bidiLevel);
71 }
72
73 std::unique_ptr<SkShaper::ScriptRunIterator> makeScriptRunIterator(const char* utf8,
74 size_t utf8Bytes,
75 SkFourByteTag script) override {
76 return SkShapers::HB::ScriptRunIterator(utf8, utf8Bytes, script);
77 }
78
79 SkUnicode* getUnicode() override { return fUnicode.get(); }
80
81 private:
82 sk_sp<SkUnicode> fUnicode;
83 };
84 #endif // defined(SK_SHAPER_HARFBUZZ_AVAILABLE)
85 #endif // defined(SK_SHAPER_UNICODE_AVAILABLE)
86
87 #if defined(SK_SHAPER_CORETEXT_AVAILABLE)
88 class CoreTextFactory final : public Factory {
89 std::unique_ptr<SkShaper> makeShaper(sk_sp<SkFontMgr>) override {
90 return SkShapers::CT::CoreText();
91 }
92 std::unique_ptr<SkShaper::BiDiRunIterator> makeBidiRunIterator(const char* utf8,
93 size_t utf8Bytes,
94 uint8_t bidiLevel) override {
95 return std::make_unique<SkShaper::TrivialBiDiRunIterator>(0, 0);
96 }
97 std::unique_ptr<SkShaper::ScriptRunIterator> makeScriptRunIterator(const char* utf8,
98 size_t utf8Bytes,
99 SkFourByteTag script) override {
100 return std::make_unique<SkShaper::TrivialScriptRunIterator>(0, 0);
101 }
102 SkUnicode* getUnicode() override { return nullptr; }
103 };
104 #endif // defined(SK_SHAPER_CORETEXT_AVAILABLE)
105
106 // This convenience function will return a set of callbacks that has the "best" text shaping
107 // depending on what parts of Skia the client has compiled in. For example, if the clients
108 // have compiled in SkShaper and a version of SkUnicode, callbacks which produce the
109 // appropriate types will be returned.
110 //
111 // This must be inline (and defined in this header) because the *client* has to compile this code
112 // with all defines set by *their* dependencies (which may include defines from SkShaper and
113 // SkUnicode modules).
114 inline sk_sp<Factory> BestAvailable() {
115 #if defined(SK_SHAPER_HARFBUZZ_AVAILABLE) && defined(SK_SHAPER_UNICODE_AVAILABLE)
116 return sk_make_sp<SkShapers::HarfbuzzFactory>();
117 #elif defined(SK_SHAPER_CORETEXT_AVAILABLE)
118 return sk_make_sp<SkShapers::CoreTextFactory>();
119 #else
120 return SkShapers::Primitive::Factory();
121 #endif
122 }
123
124 } // namespace SkShapers
125
126 #endif // SkShaperFactoryHelpers_DEFINED