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/SkBitmap.h"
9 #include "include/core/SkCanvas.h"
10 #include "include/core/SkFont.h"
11 #include "include/core/SkFontMgr.h"
12 #include "include/core/SkTypeface.h"
13 #include "include/ports/SkFontMgr_FontConfigInterface.h"
14 #include "include/ports/SkFontMgr_fontconfig.h"
15 #include "src/ports/SkFontConfigInterface_direct.h"
16 #include "tests/Test.h"
17 #include "tools/Resources.h"
18
19 #include <fontconfig/fontconfig.h>
20
21 namespace {
22
bitmap_compare(const SkBitmap & ref,const SkBitmap & test)23 bool bitmap_compare(const SkBitmap& ref, const SkBitmap& test) {
24 for (int y = 0; y < test.height(); ++y) {
25 for (int x = 0; x < test.width(); ++x) {
26 SkColor testColor = test.getColor(x, y);
27 SkColor refColor = ref.getColor(x, y);
28 if (refColor != testColor) {
29 return false;
30 }
31 }
32 }
33 return true;
34 }
35
build_fontconfig_with_fontfile(const char * fontFilename)36 FcConfig* build_fontconfig_with_fontfile(const char* fontFilename) {
37 FcConfig* config = FcConfigCreate();
38
39 // FontConfig may modify the passed path (make absolute or other).
40 FcConfigSetSysRoot(config, reinterpret_cast<const FcChar8*>(GetResourcePath("").c_str()));
41 // FontConfig will lexically compare paths against its version of the sysroot.
42 SkString fontFilePath(reinterpret_cast<const char*>(FcConfigGetSysRoot(config)));
43 fontFilePath += fontFilename;
44 FcConfigAppFontAddFile(config, reinterpret_cast<const FcChar8*>(fontFilePath.c_str()));
45
46 FcConfigBuildFonts(config);
47 return config;
48 }
49
fontmgr_understands_ft_named_instance_bits()50 bool fontmgr_understands_ft_named_instance_bits() {
51 std::unique_ptr<SkStreamAsset> distortable(GetResourceAsStream("fonts/Distortable.ttf"));
52 if (!distortable) {
53 return false;
54 }
55
56 sk_sp<SkFontMgr> fm = SkFontMgr::RefDefault();
57 SkFontArguments params;
58 // The first named variation position in Distortable is 'Thin'.
59 params.setCollectionIndex(0x00010000);
60 sk_sp<SkTypeface> typeface = fm->makeFromStream(std::move(distortable), params);
61 return !!typeface;
62 }
63
64 } // namespace
65
DEF_TEST(FontMgrFontConfig,reporter)66 DEF_TEST(FontMgrFontConfig, reporter) {
67 FcConfig* config = build_fontconfig_with_fontfile("/fonts/Distortable.ttf");
68
69 sk_sp<SkFontMgr> fontMgr(SkFontMgr_New_FontConfig(config));
70 sk_sp<SkTypeface> typeface(fontMgr->legacyMakeTypeface("Distortable", SkFontStyle()));
71 if (!typeface) {
72 ERRORF(reporter, "Could not find typeface. FcVersion: %d", FcGetVersion());
73 return;
74 }
75
76 SkBitmap bitmapStream;
77 bitmapStream.allocN32Pixels(64, 64);
78 SkCanvas canvasStream(bitmapStream);
79 canvasStream.drawColor(SK_ColorWHITE);
80
81 SkBitmap bitmapClone;
82 bitmapClone.allocN32Pixels(64, 64);
83 SkCanvas canvasClone(bitmapClone);
84 canvasStream.drawColor(SK_ColorWHITE);
85
86 SkPaint paint;
87 paint.setColor(SK_ColorGRAY);
88
89 constexpr float kTextSize = 20;
90
91 std::unique_ptr<SkStreamAsset> distortableStream(
92 GetResourceAsStream("fonts/Distortable.ttf"));
93 if (!distortableStream) {
94 return;
95 }
96
97 SkPoint point = SkPoint::Make(20.0f, 20.0f);
98 SkFourByteTag tag = SkSetFourByteTag('w', 'g', 'h', 't');
99
100 for (int i = 0; i < 10; ++i) {
101 SkScalar styleValue =
102 SkDoubleToScalar(0.5 + i * ((2.0 - 0.5) / 10));
103 SkFontArguments::VariationPosition::Coordinate
104 coordinates[] = {{tag, styleValue}};
105 SkFontArguments::VariationPosition
106 position = {coordinates, SK_ARRAY_COUNT(coordinates)};
107
108 SkFont fontStream(
109 fontMgr->makeFromStream(distortableStream->duplicate(),
110 SkFontArguments().setVariationDesignPosition(position)),
111 kTextSize);
112 fontStream.setEdging(SkFont::Edging::kSubpixelAntiAlias);
113
114 SkFont fontClone(
115 typeface->makeClone(SkFontArguments().setVariationDesignPosition(position)), kTextSize);
116 fontClone.setEdging(SkFont::Edging::kSubpixelAntiAlias);
117
118 constexpr char text[] = "abc";
119
120 canvasStream.drawColor(SK_ColorWHITE);
121 canvasStream.drawString(text, point.fX, point.fY, fontStream, paint);
122
123 canvasClone.drawColor(SK_ColorWHITE);
124 canvasClone.drawString(text, point.fX, point.fY, fontClone, paint);
125
126 bool success = bitmap_compare(bitmapStream, bitmapClone);
127 REPORTER_ASSERT(reporter, success);
128 }
129 }
130
DEF_TEST(FontConfigInterface_MatchStyleNamedInstance,reporter)131 DEF_TEST(FontConfigInterface_MatchStyleNamedInstance, reporter) {
132 if (!fontmgr_understands_ft_named_instance_bits()) {
133 return;
134 }
135
136 FcConfig* config = build_fontconfig_with_fontfile("/fonts/NotoSansCJK-VF-subset.otf.ttc");
137 sk_sp<SkFontConfigInterfaceDirect> fciDirect(new SkFontConfigInterfaceDirect(config));
138
139 static constexpr const char* family_names[]{"Noto Sans CJK JP",
140 "Noto Sans CJK HK",
141 "Noto Sans CJK SC",
142 "Noto Sans CJK TC",
143 "Noto Sans CJK KR"};
144 static constexpr const struct Test {
145 int weight;
146 bool highBitsExpectation;
147 } tests[] {
148 {100, false},
149 {300, true },
150 {350, true },
151 {400, true },
152 {500, true },
153 {700, true },
154 {900, true },
155 };
156
157 for (auto&& font_name : family_names) {
158 for (auto&& [weight, highBitsExpectation] : tests) {
159 SkFontStyle fontStyle(weight, SkFontStyle::kNormal_Width, SkFontStyle::kUpright_Slant);
160
161 SkFontConfigInterface::FontIdentity resultIdentity;
162 SkFontStyle resultStyle;
163 SkString resultFamily;
164 const bool r = fciDirect->matchFamilyName(
165 font_name, fontStyle, &resultIdentity, &resultFamily, &resultStyle);
166
167 REPORTER_ASSERT(reporter, r, "Expecting to find a match result.");
168 REPORTER_ASSERT(
169 reporter,
170 (resultIdentity.fTTCIndex >> 16 > 0) == highBitsExpectation,
171 "Expected to have the ttcIndex' upper 16 bits refer to a named instance.");
172
173 // Intentionally go through manually creating the typeface so that SkFontStyle is
174 // derived from data inside the font, not from the FcPattern that is the FontConfig
175 // match result, see https://crbug.com/skia/12881
176 sk_sp<SkTypeface> typeface(fciDirect->makeTypeface(resultIdentity).release());
177
178 if (!typeface) {
179 ERRORF(reporter, "Could not instantiate typeface, FcVersion: %d", FcGetVersion());
180 return;
181 }
182
183 SkString family_from_typeface;
184 typeface->getFamilyName(&family_from_typeface);
185
186 REPORTER_ASSERT(reporter,
187 family_from_typeface == SkString(font_name),
188 "Matched font's family name should match the request.");
189
190 SkFontStyle intrinsic_style = typeface->fontStyle();
191 REPORTER_ASSERT(reporter,
192 intrinsic_style.weight() == weight,
193 "Matched font's weight should match request.");
194 if (intrinsic_style.weight() != weight) {
195 ERRORF(reporter,
196 "Matched font had weight: %d, expected %d, family: %s",
197 intrinsic_style.weight(),
198 weight,
199 family_from_typeface.c_str());
200 }
201
202 int numAxes = typeface->getVariationDesignPosition(nullptr, 0);
203 std::vector<SkFontArguments::VariationPosition::Coordinate> coords;
204 coords.resize(numAxes);
205 typeface->getVariationDesignPosition(coords.data(), numAxes);
206
207 REPORTER_ASSERT(reporter,
208 coords.size() == 1,
209 "The font must only have one axis, the weight axis.");
210
211 REPORTER_ASSERT(reporter,
212 coords[0].axis == SkSetFourByteTag('w', 'g', 'h', 't'),
213 "The weight axis must be present and configured.");
214
215 REPORTER_ASSERT(reporter,
216 static_cast<int>(coords[0].value) == weight,
217 "The weight axis must match the weight from the request.");
218 }
219 }
220
221 }
222