Update rive-cpp to 2.0 version
[platform/core/uifw/rive-tizen.git] / submodule / skia / src / ports / SkFontMgr_custom_embedded.cpp
1 /*
2  * Copyright 2017 Google Inc.
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
8 #include "include/core/SkStream.h"
9 #include "src/core/SkFontDescriptor.h"
10 #include "src/ports/SkFontMgr_custom.h"
11
12 struct SkEmbeddedResource { const uint8_t* data; size_t size; };
13 struct SkEmbeddedResourceHeader { const SkEmbeddedResource* entries; int count; };
14
15 static void load_font_from_data(const SkTypeface_FreeType::Scanner& scanner,
16                                 std::unique_ptr<SkMemoryStream> stream, int index,
17                                 SkFontMgr_Custom::Families* families);
18
19 class EmbeddedSystemFontLoader : public SkFontMgr_Custom::SystemFontLoader {
20 public:
21     EmbeddedSystemFontLoader(const SkEmbeddedResourceHeader* header) : fHeader(header) { }
22
23     void loadSystemFonts(const SkTypeface_FreeType::Scanner& scanner,
24                          SkFontMgr_Custom::Families* families) const override
25     {
26         for (int i = 0; i < fHeader->count; ++i) {
27             const SkEmbeddedResource& fontEntry = fHeader->entries[i];
28             auto stream = std::make_unique<SkMemoryStream>(fontEntry.data, fontEntry.size, false);
29             load_font_from_data(scanner, std::move(stream), i, families);
30         }
31
32         if (families->empty()) {
33             SkFontStyleSet_Custom* family = new SkFontStyleSet_Custom(SkString());
34             families->push_back().reset(family);
35             family->appendTypeface(sk_make_sp<SkTypeface_Empty>());
36         }
37     }
38
39     const SkEmbeddedResourceHeader* fHeader;
40 };
41
42 class DataFontLoader : public SkFontMgr_Custom::SystemFontLoader {
43 public:
44     DataFontLoader(sk_sp<SkData>* datas, int n) : fDatas(datas), fNum(n) { }
45
46     void loadSystemFonts(const SkTypeface_FreeType::Scanner& scanner,
47                          SkFontMgr_Custom::Families* families) const override
48     {
49         for (int i = 0; i < fNum; ++i) {
50             auto stream = std::make_unique<SkMemoryStream>(fDatas[i]);
51             load_font_from_data(scanner, std::move(stream), i, families);
52         }
53
54         if (families->empty()) {
55             SkFontStyleSet_Custom* family = new SkFontStyleSet_Custom(SkString());
56             families->push_back().reset(family);
57             family->appendTypeface(sk_make_sp<SkTypeface_Empty>());
58         }
59     }
60
61     const sk_sp<SkData>* fDatas;
62     const int fNum;
63 };
64
65 static SkFontStyleSet_Custom* find_family(SkFontMgr_Custom::Families& families,
66                                           const char familyName[])
67 {
68    for (int i = 0; i < families.count(); ++i) {
69         if (families[i]->getFamilyName().equals(familyName)) {
70             return families[i].get();
71         }
72     }
73     return nullptr;
74 }
75
76 static void load_font_from_data(const SkTypeface_FreeType::Scanner& scanner,
77                                 std::unique_ptr<SkMemoryStream> stream, int index,
78                                 SkFontMgr_Custom::Families* families)
79 {
80     int numFaces;
81     if (!scanner.recognizedFont(stream.get(), &numFaces)) {
82         SkDebugf("---- failed to open <%d> as a font\n", index);
83         return;
84     }
85
86     for (int faceIndex = 0; faceIndex < numFaces; ++faceIndex) {
87         bool isFixedPitch;
88         SkString realname;
89         SkFontStyle style = SkFontStyle(); // avoid uninitialized warning
90         if (!scanner.scanFont(stream.get(), faceIndex,
91                               &realname, &style, &isFixedPitch, nullptr))
92         {
93             SkDebugf("---- failed to open <%d> <%d> as a font\n", index, faceIndex);
94             return;
95         }
96
97         SkFontStyleSet_Custom* addTo = find_family(*families, realname.c_str());
98         if (nullptr == addTo) {
99             addTo = new SkFontStyleSet_Custom(realname);
100             families->push_back().reset(addTo);
101         }
102         auto data = std::make_unique<SkFontData>(stream->duplicate(), faceIndex, 0,
103                                                  nullptr, 0, nullptr, 0);
104         addTo->appendTypeface(sk_make_sp<SkTypeface_Stream>(std::move(data),
105                                                             style, isFixedPitch,
106                                                             true, realname));
107     }
108 }
109
110 sk_sp<SkFontMgr> SkFontMgr_New_Custom_Embedded(const SkEmbeddedResourceHeader* header) {
111     return sk_make_sp<SkFontMgr_Custom>(EmbeddedSystemFontLoader(header));
112 }
113
114 // SkFontMgr_New_Custom_Data expects to be called with the data for n font files.
115 sk_sp<SkFontMgr> SkFontMgr_New_Custom_Data(sk_sp<SkData>* datas, int n) {
116     SkASSERT(datas != nullptr);
117     SkASSERT(n > 0);
118     return sk_make_sp<SkFontMgr_Custom>(DataFontLoader(datas, n));
119 }