Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
TestFontCollection.cpp
Go to the documentation of this file.
1// Copyright 2019 Google LLC.
3
6#include "src/base/SkUTF.h"
7#include "src/core/SkOSFile.h"
8#include "tools/Resources.h"
9
10#if defined(SK_TYPEFACE_FACTORY_FREETYPE)
12#elif defined(SK_TYPEFACE_FACTORY_CORETEXT)
14#elif defined(SK_TYPEFACE_FACTORY_DIRECTWRITE)
16#endif
17
18#include <memory>
19#include <utility>
20
21namespace skia {
22namespace textlayout {
23
24TestFontCollection::TestFontCollection(const std::string& resourceDir, bool testOnly, bool loadFonts)
25 : fResourceDir(resourceDir)
26 , fFontsFound(0) {
27 if (fDirs == resourceDir) {
28 return;
29 }
30
31 fFontProvider = sk_make_sp<TypefaceFontProvider>();
32
33 if (loadFonts) {
34 SkOSFile::Iter iter(fResourceDir.c_str());
35 SkString path;
36 while (iter.next(&path)) {
37 addFontFromFile(path.c_str());
38 }
39 }
40
41 fFontsFound = fFontProvider->countFamilies();
42 if (testOnly) {
43 this->setTestFontManager(fFontProvider);
44 } else {
45 this->setAssetFontManager(fFontProvider);
46 }
47 this->disableFontFallback();
48 fDirs = resourceDir;
49}
50
51bool TestFontCollection::addFontFromFile(const std::string& path, const std::string& familyName) {
52
53 SkString file_path;
54 file_path.printf("%s/%s", fResourceDir.c_str(), path.c_str());
55
56 std::unique_ptr<SkStreamAsset> file = SkFILEStream::Make(file_path.c_str());
57 if (!file) {
58 return false;
59 }
60#if defined(SK_TYPEFACE_FACTORY_FREETYPE)
63#elif defined(SK_TYPEFACE_FACTORY_CORETEXT)
64 sk_sp<SkTypeface> face = SkTypeface_Mac::MakeFromStream(std::move(file), SkFontArguments());
65#elif defined(SK_TYPEFACE_FACTORY_DIRECTWRITE)
67#else
68 sk_sp<SkTypeface> face = nullptr;
69#endif
70 if (familyName.empty()) {
71 fFontProvider->registerTypeface(std::move(face));
72 } else {
73 fFontProvider->registerTypeface(std::move(face), SkString(familyName.c_str()));
74 }
75
76 return true;
77}
78} // namespace textlayout
79} // namespace skia
static sk_sp< SkTypeface > SK_SPI MakeFromStream(std::unique_ptr< SkStreamAsset >, const SkFontArguments &)
static std::unique_ptr< SkFILEStream > Make(const char path[])
Definition SkStream.h:314
SK_SPI bool next(SkString *name, bool getDir=false)
void printf(const char format[],...) SK_PRINTF_LIKE(2
Definition SkString.cpp:534
const char * c_str() const
Definition SkString.h:133
static sk_sp< SkTypeface > MakeFromStream(std::unique_ptr< SkStreamAsset >, const SkFontArguments &)
void setAssetFontManager(sk_sp< SkFontMgr > fontManager)
void setTestFontManager(sk_sp< SkFontMgr > fontManager)
TestFontCollection(const std::string &resourceDir, bool testOnly=false, bool loadFonts=true)
bool addFontFromFile(const std::string &path, const std::string &familyName="")