Debian Codegraph @

tools/TestFontDataProvider.cpp

raw
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
8 #include "tools/TestFontDataProvider.h"
9
10 #include "include/core/SkData.h"
11 #include "include/private/base/SkDebug.h"
12 #include "src/utils/SkOSPath.h"
13 #include "tools/flags/CommandLineFlags.h"
14
15 const char kTestDataJsonFilename[] = "raster_test.json";
16 const char kTestDataBasePath[] = "third_party/externals/googlefonts_testdata/data/";
17
18 std::atomic<const char*> gFontTestDataBasePath{kTestDataBasePath};
19
20 namespace skiatest {
21 static DEFINE_string(fontTestDataPath,
22 "",
23 "Path to extracted data from googlefonts_testdata CIPD file.");
24
25 void SetFontTestDataDirectory() {
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 // Change size when rolling / changing googlefonts_testdata CIPD, see bin/fetch-fonts-testdata.
36 constexpr size_t kExpectNumFonts = 51;
37
38 namespace {
39 SkString prefixWithTestDataPath(SkString suffix) {
40 return SkOSPath::Join(gFontTestDataBasePath, suffix.c_str());
41 }
42
43 SkString prefixWithFontsPath(SkString suffix) {
44 SkString fontsPath = prefixWithTestDataPath(SkString("fonts"));
45 return SkOSPath::Join(fontsPath.c_str(), suffix.c_str());
46 }
47
48 } // namespace
49
50 TestFontDataProvider::TestFontDataProvider(const std::string& fontFilterRegexp,
51 const std::string& langFilterRegexp)
52 : fFontFilter(fontFilterRegexp), fLangFilter(langFilterRegexp) {
53 SkString testDataLocation = prefixWithTestDataPath(SkString(kTestDataJsonFilename));
54 sk_sp<SkData> jsonTestData = SkData::MakeFromFileName(testDataLocation.c_str());
55 if (!jsonTestData || !jsonTestData->size()) {
56 SkDebugf("Unable to access font test data at %s, check bin/fetch-fonts-testdata.\n",
57 testDataLocation.c_str());
58 return;
59 }
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 if (!fFonts) {
65 SkDebugf("Unable to read font test data.\n");
66 return;
67 }
68 if (fFonts->size() != kExpectNumFonts) {
69 SkDebugf("Unable to access all %zu test fonts (got %zu), check bin/fetch-fonts-testdata.\n",
70 kExpectNumFonts, fFonts->size());
71 return;
72 }
73 fSamples = root["samples"];
74 if (!fSamples || !fSamples->size()) {
75 SkDebugf("Unable to read font test data samples.\n");
76 return;
77 }
78 }
79
80 bool TestFontDataProvider::next(TestSet* testSet) {
81 while (testSet && fFonts && fFontsIndex < fFonts->size()) {
82 const skjson::ObjectValue* fontsEntry = (*fFonts)[fFontsIndex++];
83 SkASSERT(fontsEntry);
84 const skjson::StringValue* fontName = (*fontsEntry)["name"];
85 SkASSERT(fontName);
86 std::smatch match;
87 std::string fontNameStr(fontName->str());
88 if (std::regex_match(fontNameStr, match, fFontFilter)) {
89 testSet->fontName = SkString(fontNameStr);
90 const skjson::StringValue* fontFilename = (*fontsEntry)["path"];
91 testSet->fontFilename = prefixWithFontsPath(
92 SkString(fontFilename->str().data(), fontFilename->str().size()));
93 testSet->langSamples =
94 getLanguageSamples((*fontsEntry)["languages"].as<skjson::ArrayValue>());
95 return true;
96 }
97 }
98 return false;
99 }
100
101 void TestFontDataProvider::rewind() { fFontsIndex = 0; }
102
103 std::vector<TestFontDataProvider::LangSample> TestFontDataProvider::getLanguageSamples(
104 const skjson::ArrayValue* languages) {
105 std::vector<LangSample> samples;
106 for (size_t i = 0; i < languages->size(); ++i) {
107 const skjson::StringValue* langTag = (*languages)[i];
108 std::string langTagStr(langTag->str());
109 std::smatch match;
110 if (std::regex_match(langTagStr, match, fLangFilter)) {
111 const skjson::ObjectValue* sample = (*fSamples)[langTagStr.c_str()];
112 const skjson::StringValue* shortSample = (*sample)["short_sample"];
113 const skjson::StringValue* longSample = (*sample)["long_sample"];
114 SkString sampleShort(shortSample->str().data(), shortSample->str().size());
115 SkString sampleLong(longSample->str().data(), longSample->str().size());
116 samples.push_back({SkString(langTagStr), sampleShort, sampleLong});
117 }
118 }
119 SkASSERT_RELEASE(samples.size());
120 return samples;
121 }