Coverage Report

Created: 2024-05-20 07:14

/src/skia/modules/skshaper/utils/FactoryHelpers.h
Line
Count
Source (jump to first uncovered line)
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_HARFBUZZ_AVAILABLE) && defined(SK_SHAPER_UNICODE_AVAILABLE)
14
#include "modules/skshaper/include/SkShaper_harfbuzz.h"
15
#include "modules/skshaper/include/SkShaper_skunicode.h"
16
#include "modules/skunicode/include/SkUnicode.h"
17
#endif
18
19
#if defined(SK_SHAPER_CORETEXT_AVAILABLE)
20
#include "modules/skshaper/include/SkShaper_coretext.h"
21
#endif
22
23
#if defined(SK_UNICODE_ICU_IMPLEMENTATION)
24
#include "modules/skunicode/include/SkUnicode_icu.h"
25
#endif
26
27
#if defined(SK_UNICODE_LIBGRAPHEME_IMPLEMENTATION)
28
#include "modules/skunicode/include/SkUnicode_libgrapheme.h"
29
#endif
30
31
#if defined(SK_UNICODE_ICU4X_IMPLEMENTATION)
32
#include "modules/skunicode/include/SkUnicode_icu4x.h"
33
#endif
34
35
namespace SkShapers {
36
#if defined(SK_SHAPER_HARFBUZZ_AVAILABLE) && defined(SK_SHAPER_UNICODE_AVAILABLE)
37
class HarfbuzzFactory final : public Factory {
38
public:
39
9.10k
    HarfbuzzFactory() {
40
9.10k
#if defined(SK_UNICODE_ICU_IMPLEMENTATION)
41
9.10k
        fUnicode = SkUnicodes::ICU::Make();
42
9.10k
#endif
43
#if defined(SK_UNICODE_ICU4X_IMPLEMENTATION)
44
        if (!fUnicode) {
45
            fUnicode = SkUnicodes::ICU4X::Make();
46
        }
47
#endif
48
#if defined(SK_UNICODE_LIBGRAPHEME_IMPLEMENTATION)
49
        if (!fUnicode) {
50
            fUnicode = SkUnicodes::Libgrapheme::Make();
51
        }
52
#endif
53
9.10k
    }
54
0
    std::unique_ptr<SkShaper> makeShaper(sk_sp<SkFontMgr> fallback) override {
55
0
        return SkShapers::HB::ShaperDrivenWrapper(fUnicode, fallback);
56
0
    }
57
58
    std::unique_ptr<SkShaper::BiDiRunIterator> makeBidiRunIterator(const char* utf8,
59
                                                                size_t utf8Bytes,
60
0
                                                                uint8_t bidiLevel) override {
61
0
        return SkShapers::unicode::BidiRunIterator(fUnicode, utf8, utf8Bytes, bidiLevel);
62
0
    }
63
64
    std::unique_ptr<SkShaper::ScriptRunIterator> makeScriptRunIterator(const char* utf8,
65
                                                                 size_t utf8Bytes,
66
0
                                                                 SkFourByteTag script) override {
67
0
        return SkShapers::HB::ScriptRunIterator(utf8, utf8Bytes, script);
68
0
    }
69
70
0
    SkUnicode* getUnicode() override { return fUnicode.get(); }
71
72
private:
73
    sk_sp<SkUnicode> fUnicode;
74
};
75
#endif  // defined(SK_SHAPER_HARFBUZZ_AVAILABLE) && defined(SK_SHAPER_UNICODE_AVAILABLE)
76
77
#if defined(SK_SHAPER_CORETEXT_AVAILABLE)
78
class CoreTextFactory final : public Factory {
79
    std::unique_ptr<SkShaper> makeShaper(sk_sp<SkFontMgr>) override {
80
        return SkShapers::CT::CoreText();
81
    }
82
    std::unique_ptr<SkShaper::BiDiRunIterator> makeBidiRunIterator(const char* utf8,
83
                                                                size_t utf8Bytes,
84
                                                                uint8_t bidiLevel) override {
85
        return std::make_unique<SkShaper::TrivialBiDiRunIterator>(0, 0);
86
    }
87
    std::unique_ptr<SkShaper::ScriptRunIterator> makeScriptRunIterator(const char* utf8,
88
                                                                 size_t utf8Bytes,
89
                                                                 SkFourByteTag script) override {
90
        return std::make_unique<SkShaper::TrivialScriptRunIterator>(0, 0);
91
    }
92
    SkUnicode* getUnicode() override { return nullptr; }
93
};
94
#endif  // defined(SK_SHAPER_CORETEXT_AVAILABLE)
95
96
// This convenience function will return a set of callbacks that has the "best" text shaping
97
// depending on what parts of Skia the client has compiled in. For example, if the clients
98
// have compiled in SkShaper and a version of SkUnicode, callbacks which produce the
99
// appropriate types will be returned.
100
//
101
// This must be inline (and defined in this header) because the *client* has to compile this code
102
// with all defines set by *their* dependencies (which may include defines from SkShaper and
103
// SkUnicode modules).
104
9.14k
inline sk_sp<Factory> BestAvailable() {
105
#if defined(SK_SHAPER_HARFBUZZ_AVAILABLE) && defined(SK_SHAPER_UNICODE_AVAILABLE)
106
    return sk_make_sp<SkShapers::HarfbuzzFactory>();
107
#elif defined(SK_SHAPER_CORETEXT_AVAILABLE)
108
    return sk_make_sp<SkShapers::CoreTextFactory>();
109
#else
110
9.14k
    return SkShapers::Primitive::Factory();
111
9.14k
#endif
112
9.14k
}
113
114
};  // namespace SkShapers
115
116
#endif  // SkShaperFactoryHelpers_DEFINED