Flutter Engine
The Flutter Engine
TestFontDataProvider.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2023 Google LLC
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
9
10#include "include/core/SkData.h"
12#include "src/utils/SkOSPath.h"
14
15const char kTestDataJsonFilename[] = "raster_test.json";
16const char kTestDataBasePath[] = "third_party/externals/googlefonts_testdata/data/";
17
18std::atomic<const char*> gFontTestDataBasePath{kTestDataBasePath};
19
20namespace skiatest {
21static DEFINE_string(fontTestDataPath,
22 "",
23 "Path to extracted data from googlefonts_testdata CIPD file.");
24
26 if (FLAGS_fontTestDataPath.isEmpty()) {
27 return;
28 }
29 if (strlen(FLAGS_fontTestDataPath[0])) {
30 gFontTestDataBasePath = FLAGS_fontTestDataPath[0];
31 }
32}
33} // namespace skiatest
34
35#if defined(SK_DEBUG)
36// Change size when rolling / changing googlefonts_testdata CIPD, see bin/fetch-fonts-testdata.
37constexpr size_t kExpectNumFonts = 51;
38#endif
39
40namespace {
41SkString prefixWithTestDataPath(SkString suffix) {
43}
44
45SkString prefixWithFontsPath(SkString suffix) {
46 SkString fontsPath = prefixWithTestDataPath(SkString("fonts"));
47 return SkOSPath::Join(fontsPath.c_str(), suffix.c_str());
48}
49
50} // namespace
51
52TestFontDataProvider::TestFontDataProvider(const std::string& fontFilterRegexp,
53 const std::string& langFilterRegexp)
54 : fFontFilter(fontFilterRegexp), fLangFilter(langFilterRegexp) {
55 SkString testDataLocation = prefixWithTestDataPath(SkString(kTestDataJsonFilename));
56 sk_sp<SkData> jsonTestData = SkData::MakeFromFileName(testDataLocation.c_str());
57 SkASSERTF(jsonTestData && jsonTestData->size(),
58 "Unable to access font test metadata at location %s, check bin/fetch-fonts-testdata.",
59 testDataLocation.c_str());
60 fJsonDom = std::make_unique<skjson::DOM>(reinterpret_cast<const char*>(jsonTestData->bytes()),
61 jsonTestData->size());
62 const skjson::ObjectValue& root = fJsonDom->root().as<skjson::ObjectValue>();
63 fFonts = root["fonts"];
64 SkASSERT(fFonts);
65 SkASSERTF(fFonts->size() == kExpectNumFonts,
66 "Unable to access all %zu test fonts (only got %zu), check bin/fetch-fonts-testdata.",
67 kExpectNumFonts,
68 fFonts->size());
69 fSamples = root["samples"];
70 SkASSERT(fSamples);
71}
72
74 while (testSet && fFontsIndex < fFonts->size()) {
75 const skjson::ObjectValue* fontsEntry = (*fFonts)[fFontsIndex++];
76 SkASSERT(fontsEntry);
77 const skjson::StringValue* fontName = (*fontsEntry)["name"];
78 SkASSERT(fontName);
79 std::smatch match;
80 std::string fontNameStr(fontName->str());
81 if (std::regex_match(fontNameStr, match, fFontFilter)) {
82 testSet->fontName = SkString(fontNameStr);
83 const skjson::StringValue* fontFilename = (*fontsEntry)["path"];
84 testSet->fontFilename = prefixWithFontsPath(
85 SkString(fontFilename->str().data(), fontFilename->str().size()));
86 testSet->langSamples =
87 getLanguageSamples((*fontsEntry)["languages"].as<skjson::ArrayValue>());
88 return true;
89 }
90 }
91 return false;
92}
93
94void TestFontDataProvider::rewind() { fFontsIndex = 0; }
95
96std::vector<TestFontDataProvider::LangSample> TestFontDataProvider::getLanguageSamples(
97 const skjson::ArrayValue* languages) {
98 std::vector<LangSample> samples;
99 for (size_t i = 0; i < languages->size(); ++i) {
100 const skjson::StringValue* langTag = (*languages)[i];
101 std::string langTagStr(langTag->str());
102 std::smatch match;
103 if (std::regex_match(langTagStr, match, fLangFilter)) {
104 const skjson::ObjectValue* sample = (*fSamples)[langTagStr.c_str()];
105 const skjson::StringValue* shortSample = (*sample)["short_sample"];
106 const skjson::StringValue* longSample = (*sample)["long_sample"];
107 SkString sampleShort(shortSample->str().data(), shortSample->str().size());
108 SkString sampleLong(longSample->str().data(), longSample->str().size());
109 samples.push_back({SkString(langTagStr), sampleShort, sampleLong});
110 }
111 }
112 SkASSERT_RELEASE(samples.size());
113 return samples;
114}
static const ConicPts testSet[]
#define SkASSERT_RELEASE(cond)
Definition: SkAssert.h:100
#define SkASSERT(cond)
Definition: SkAssert.h:116
#define SkASSERTF(cond, fmt,...)
Definition: SkAssert.h:117
std::atomic< const char * > gFontTestDataBasePath
const char kTestDataJsonFilename[]
const char kTestDataBasePath[]
const uint8_t * bytes() const
Definition: SkData.h:43
static sk_sp< SkData > MakeFromFileName(const char path[])
Definition: SkData.cpp:148
size_t size() const
Definition: SkData.h:30
static SkString Join(const char *rootPath, const char *relativePath)
Definition: SkOSPath.cpp:14
const char * c_str() const
Definition: SkString.h:133
bool next(TestSet *testSet)
TestFontDataProvider(const std::string &fontFilterRegexp, const std::string &langFilterRegexp)
std::string_view str() const
Definition: SkJSON.h:327
size_t size() const
Definition: SkJSON.h:262
def match(bench, filt)
Definition: benchmark.py:23
it will be possible to load the file into Perfetto s trace viewer disable asset Prevents usage of any non test fonts unless they were explicitly Loaded via prefetched default font Indicates whether the embedding started a prefetch of the default font manager before creating the engine run In non interactive keep the shell running after the Dart script has completed enable serial On low power devices with low core running concurrent GC tasks on threads can cause them to contend with the UI thread which could potentially lead to jank This option turns off all concurrent GC activities domain network JSON encoded network policy per domain This overrides the DisallowInsecureConnections switch Embedder can specify whether to allow or disallow insecure connections at a domain level old gen heap size
Definition: switches.h:259
string root
Definition: scale_cpu.py:20
static DEFINE_string(fontTestDataPath, "", "Path to extracted data from googlefonts_testdata CIPD file.")
void SetFontTestDataDirectory()