blob: 224b21a2a747e040419f336626042491063642d2 [file] [log] [blame]
Julia Lavrova6e6333f2019-06-17 10:34:10 -04001// Copyright 2019 Google LLC.
2#ifndef FontResolver_DEFINED
3#define FontResolver_DEFINED
4
5#include "src/core/SkSpan.h"
6#include <memory>
7#include <set>
8#include "include/core/SkFontMgr.h"
9#include "include/core/SkRefCnt.h"
10#include "include/private/SkTHash.h"
11#include "modules/skparagraph/include/FontCollection.h"
12#include "modules/skparagraph/include/TextStyle.h"
13
14namespace skia {
15namespace textlayout {
16
17class FontResolver {
18public:
19 FontResolver(sk_sp<FontCollection> fontCollection);
20 ~FontResolver() = default;
21
22 void findAllFontsForStyledBlock(const TextStyle& style, SkSpan<const char> text);
23 bool findFirst(const char* codepoint, SkFont* font, SkScalar* height);
24 bool findNext(const char* codepoint, SkFont* font, SkScalar* height);
25
26private:
27 std::pair<SkFont, SkScalar> makeFont(sk_sp<SkTypeface> typeface, SkScalar size,
28 SkScalar height);
29
30 size_t resolveAllCharactersByFont(std::pair<SkFont, SkScalar> font);
31 void addResolvedWhitespacesToMapping();
32
33 struct Hash {
34 uint32_t operator()(const std::pair<SkFont, SkScalar>& key) const {
35 return SkTypeface::UniqueID(key.first.getTypeface()) +
36 SkScalarCeilToInt(key.first.getSize()) + SkScalarCeilToInt(key.second);
37 }
38 };
39
40 SkUnichar firstUnresolved();
41
42 sk_sp<FontCollection> fFontCollection;
43
44 SkTHashMap<const char*, std::pair<SkFont, SkScalar>> fFontMapping;
45 SkTHashSet<std::pair<SkFont, SkScalar>, Hash> fResolvedFonts;
46 std::pair<SkFont, SkScalar> fFirstResolvedFont;
47
48 SkTArray<SkUnichar> fCodepoints;
49 SkTArray<const char*> fCharacters;
50 SkTArray<size_t> fUnresolvedIndexes;
51 SkTArray<SkUnichar> fUnresolvedCodepoints;
52 SkTHashMap<size_t, std::pair<SkFont, SkScalar>> fWhitespaces;
53 size_t fUnresolved;
54};
55} // namespace textlayout
56} // namespace skia
57
58#endif // FontResolver_DEFINED