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:
EmbeddedSystemFontLoader(const SkEmbeddedResourceHeader * header)21 EmbeddedSystemFontLoader(const SkEmbeddedResourceHeader* header) : fHeader(header) { }
22
loadSystemFonts(const SkTypeface_FreeType::Scanner & scanner,SkFontMgr_Custom::Families * families) const23 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:
DataFontLoader(sk_sp<SkData> * datas,int n)44 DataFontLoader(sk_sp<SkData>* datas, int n) : fDatas(datas), fNum(n) { }
45
loadSystemFonts(const SkTypeface_FreeType::Scanner & scanner,SkFontMgr_Custom::Families * families) const46 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
find_family(SkFontMgr_Custom::Families & families,const char familyName[])65 static SkFontStyleSet_Custom* find_family(SkFontMgr_Custom::Families& families,
66 const char familyName[])
67 {
68 for (int i = 0; i < families.size(); ++i) {
69 if (families[i]->getFamilyName().equals(familyName)) {
70 return families[i].get();
71 }
72 }
73 return nullptr;
74 }
75
load_font_from_data(const SkTypeface_FreeType::Scanner & scanner,std::unique_ptr<SkMemoryStream> stream,int index,SkFontMgr_Custom::Families * families)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_FreeTypeStream>(
105 std::move(data), realname, style, isFixedPitch));
106 }
107 }
108
SkFontMgr_New_Custom_Embedded(const SkEmbeddedResourceHeader * header)109 sk_sp<SkFontMgr> SkFontMgr_New_Custom_Embedded(const SkEmbeddedResourceHeader* header) {
110 return sk_make_sp<SkFontMgr_Custom>(EmbeddedSystemFontLoader(header));
111 }
112
113 // SkFontMgr_New_Custom_Data expects to be called with the data for n font files.
SkFontMgr_New_Custom_Data(sk_sp<SkData> * datas,int n)114 sk_sp<SkFontMgr> SkFontMgr_New_Custom_Data(sk_sp<SkData>* datas, int n) {
115 SkASSERT(datas != nullptr);
116 SkASSERT(n > 0);
117 return sk_make_sp<SkFontMgr_Custom>(DataFontLoader(datas, n));
118 }
119