xref: /aosp_15_r20/external/skia/tests/FCITest.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
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/SkFontStyle.h"
11 #include "include/core/SkRefCnt.h"
12 #include "include/core/SkStream.h"
13 #include "include/core/SkString.h"
14 #include "include/core/SkTypeface.h"
15 #include "include/core/SkTypes.h"
16 #include "include/ports/SkFontConfigInterface.h"
17 #include "src/ports/SkFontConfigInterface_direct.h"
18 #include "tests/Test.h"
19 #include "tools/Resources.h"
20 #include "tools/fonts/FontToolUtils.h"
21 
22 #include <fontconfig/fontconfig.h>
23 #include <memory>
24 #include <utility>
25 #include <vector>
26 
27 namespace {
28 
build_fontconfig_with_fontfile(const char * fontFilename)29 FcConfig* 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 
fontmgr_understands_ft_named_instance_bits()43 bool fontmgr_understands_ft_named_instance_bits() {
44     std::unique_ptr<SkStreamAsset> distortable(GetResourceAsStream("fonts/Distortable.ttf"));
45     if (!distortable) {
46         return false;
47     }
48 
49     sk_sp<SkFontMgr> fm = ToolUtils::TestFontMgr();
50     SkFontArguments params;
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 
DEF_TEST(FontConfigInterface_MatchStyleNamedInstance,reporter)59 DEF_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");
65     sk_sp<SkFontConfigInterfaceDirect> fciDirect(new SkFontConfigInterfaceDirect(config));
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) {
87             SkFontStyle fontStyle(weight, SkFontStyle::kNormal_Width, SkFontStyle::kUpright_Slant);
88 
89             SkFontConfigInterface::FontIdentity resultIdentity;
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.");
96             REPORTER_ASSERT(
97                     reporter,
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,
105                                                                ToolUtils::TestFontMgr()).release());
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 
115             REPORTER_ASSERT(reporter,
116                             family_from_typeface == SkString(font_name),
117                             "Matched font's family name should match the request.");
118 
119             SkFontStyle intrinsic_style = typeface->fontStyle();
120             REPORTER_ASSERT(reporter,
121                             intrinsic_style.weight() == weight,
122                             "Matched font's weight should match request.");
123             if (intrinsic_style.weight() != weight) {
124                 ERRORF(reporter,
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 
136             REPORTER_ASSERT(reporter,
137                             coords.size() == 1,
138                             "The font must only have one axis, the weight axis.");
139 
140             REPORTER_ASSERT(reporter,
141                             coords[0].axis == SkSetFourByteTag('w', 'g', 'h', 't'),
142                             "The weight axis must be present and configured.");
143 
144             REPORTER_ASSERT(reporter,
145                             static_cast<int>(coords[0].value) == weight,
146                             "The weight axis must match the weight from the request.");
147         }
148   }
149 
150 }
151