Flutter Engine
The Flutter Engine
FCITest.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2014 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
18#include "tests/Test.h"
19#include "tools/Resources.h"
21
22#include <fontconfig/fontconfig.h>
23#include <memory>
24#include <utility>
25#include <vector>
26
27namespace {
28
29FcConfig* build_fontconfig_with_fontfile(const char* fontFilename) {
30 FcConfig* config = FcConfigCreate();
31
32 // FontConfig may modify the passed path (make absolute or other).
33 FcConfigSetSysRoot(config, reinterpret_cast<const FcChar8*>(GetResourcePath("").c_str()));
34 // FontConfig will lexically compare paths against its version of the sysroot.
35 SkString fontFilePath(reinterpret_cast<const char*>(FcConfigGetSysRoot(config)));
36 fontFilePath += fontFilename;
37 FcConfigAppFontAddFile(config, reinterpret_cast<const FcChar8*>(fontFilePath.c_str()));
38
39 FcConfigBuildFonts(config);
40 return config;
41}
42
43bool fontmgr_understands_ft_named_instance_bits() {
44 std::unique_ptr<SkStreamAsset> distortable(GetResourceAsStream("fonts/Distortable.ttf"));
45 if (!distortable) {
46 return false;
47 }
48
51 // The first named variation position in Distortable is 'Thin'.
52 params.setCollectionIndex(0x00010000);
53 sk_sp<SkTypeface> typeface = fm->makeFromStream(std::move(distortable), params);
54 return !!typeface;
55}
56
57} // namespace
58
59DEF_TEST(FontConfigInterface_MatchStyleNamedInstance, reporter) {
60 if (!fontmgr_understands_ft_named_instance_bits()) {
61 return;
62 }
63
64 FcConfig* config = build_fontconfig_with_fontfile("/fonts/NotoSansCJK-VF-subset.otf.ttc");
66
67 static constexpr const char* family_names[]{"Noto Sans CJK JP",
68 "Noto Sans CJK HK",
69 "Noto Sans CJK SC",
70 "Noto Sans CJK TC",
71 "Noto Sans CJK KR"};
72 static constexpr const struct Test {
73 int weight;
74 bool highBitsExpectation;
75 } tests[] {
76 {100, false},
77 {300, true },
78 {350, true },
79 {400, true },
80 {500, true },
81 {700, true },
82 {900, true },
83 };
84
85 for (auto&& font_name : family_names) {
86 for (auto&& [weight, highBitsExpectation] : tests) {
88
90 SkFontStyle resultStyle;
91 SkString resultFamily;
92 const bool r = fciDirect->matchFamilyName(
93 font_name, fontStyle, &resultIdentity, &resultFamily, &resultStyle);
94
95 REPORTER_ASSERT(reporter, r, "Expecting to find a match result.");
98 (resultIdentity.fTTCIndex >> 16 > 0) == highBitsExpectation,
99 "Expected to have the ttcIndex' upper 16 bits refer to a named instance.");
100
101 // Intentionally go through manually creating the typeface so that SkFontStyle is
102 // derived from data inside the font, not from the FcPattern that is the FontConfig
103 // match result, see https://crbug.com/skia/12881
104 sk_sp<SkTypeface> typeface(fciDirect->makeTypeface(resultIdentity,
106
107 if (!typeface) {
108 ERRORF(reporter, "Could not instantiate typeface, FcVersion: %d", FcGetVersion());
109 return;
110 }
111
112 SkString family_from_typeface;
113 typeface->getFamilyName(&family_from_typeface);
114
116 family_from_typeface == SkString(font_name),
117 "Matched font's family name should match the request.");
118
119 SkFontStyle intrinsic_style = typeface->fontStyle();
121 intrinsic_style.weight() == weight,
122 "Matched font's weight should match request.");
123 if (intrinsic_style.weight() != weight) {
125 "Matched font had weight: %d, expected %d, family: %s",
126 intrinsic_style.weight(),
127 weight,
128 family_from_typeface.c_str());
129 }
130
131 int numAxes = typeface->getVariationDesignPosition(nullptr, 0);
132 std::vector<SkFontArguments::VariationPosition::Coordinate> coords;
133 coords.resize(numAxes);
134 typeface->getVariationDesignPosition(coords.data(), numAxes);
135
137 coords.size() == 1,
138 "The font must only have one axis, the weight axis.");
139
141 coords[0].axis == SkSetFourByteTag('w', 'g', 'h', 't'),
142 "The weight axis must be present and configured.");
143
145 static_cast<int>(coords[0].value) == weight,
146 "The weight axis must match the weight from the request.");
147 }
148 }
149
150}
static BlurTest tests[]
Definition: BlurTest.cpp:84
DEF_TEST(FontConfigInterface_MatchStyleNamedInstance, reporter)
Definition: FCITest.cpp:59
reporter
Definition: FontMgrTest.cpp:39
std::unique_ptr< SkStreamAsset > GetResourceAsStream(const char *resource, bool useFileStream)
Definition: Resources.cpp:31
SkString GetResourcePath(const char *resource)
Definition: Resources.cpp:23
static constexpr SkFourByteTag SkSetFourByteTag(char a, char b, char c, char d)
Definition: SkTypes.h:167
#define REPORTER_ASSERT(r, cond,...)
Definition: Test.h:286
#define ERRORF(r,...)
Definition: Test.h:293
sk_sp< SkTypeface > makeFromStream(std::unique_ptr< SkStreamAsset >, int ttcIndex=0) const
Definition: SkFontMgr.cpp:127
int weight() const
Definition: SkFontStyle.h:62
const char * c_str() const
Definition: SkString.h:133
void getFamilyName(SkString *name) const
Definition: SkTypeface.cpp:459
SkFontStyle fontStyle() const
Definition: SkTypeface.h:55
int getVariationDesignPosition(SkFontArguments::VariationPosition::Coordinate coordinates[], int coordinateCount) const
Definition: SkTypeface.cpp:289
T * release()
Definition: SkRefCnt.h:324
const EmbeddedViewParams * params
uint8_t value
sk_sp< SkFontMgr > TestFontMgr()