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