1// Copyright 2019 Google LLC.
2#include "modules/skparagraph/src/ParagraphImpl.h"
3#include "modules/skparagraph/utils/TestFontCollection.h"
4#include "src/base/SkUTF.h"
5#include "src/core/SkOSFile.h"
6#include "tools/Resources.h"
7
8namespace skia {
9namespace textlayout {
10
11TestFontCollection::TestFontCollection(const std::string& resourceDir, bool testOnly, bool loadFonts)
12  : fResourceDir(resourceDir)
13  , fFontsFound(0) {
14    if (fDirs == resourceDir) {
15      return;
16    }
17
18    fFontProvider = sk_make_sp<TypefaceFontProvider>();
19
20    if (loadFonts) {
21        SkOSFile::Iter iter(fResourceDir.c_str());
22        SkString path;
23        while (iter.next(&path)) {
24            addFontFromFile(path.c_str());
25        }
26    }
27
28    fFontsFound = fFontProvider->countFamilies();
29    if (testOnly) {
30        this->setTestFontManager(fFontProvider);
31    } else {
32        this->setAssetFontManager(fFontProvider);
33    }
34    this->disableFontFallback();
35    fDirs = resourceDir;
36}
37
38bool TestFontCollection::addFontFromFile(const std::string& path, const std::string& familyName) {
39
40    SkString file_path;
41    file_path.printf("%s/%s", fResourceDir.c_str(), path.c_str());
42
43    auto data = SkData::MakeFromFileName(file_path.c_str());
44    if (!data) {
45        return false;
46    }
47    if (familyName.empty()) {
48        fFontProvider->registerTypeface(SkTypeface::MakeFromData(data));
49    } else {
50        fFontProvider->registerTypeface(SkTypeface::MakeFromData(data), SkString(familyName.c_str()));
51    }
52
53    return true;
54}
55}  // namespace textlayout
56}  // namespace skia
57