blob: bed4dbc2ceb88e2c7d55f1870aba4189c4e90b69 [file] [log] [blame]
Ben Wagner37a06c02018-06-22 21:11:00 +00001/*
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
Mike Reedac9f0c92020-12-23 10:11:33 -05008#include "include/core/SkBitmap.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -05009#include "include/core/SkCanvas.h"
10#include "include/core/SkFont.h"
11#include "include/core/SkFontMgr.h"
12#include "include/core/SkTypeface.h"
Dominik Röttsches7646dff2022-02-01 20:53:07 +020013#include "include/ports/SkFontMgr_FontConfigInterface.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "include/ports/SkFontMgr_fontconfig.h"
Dominik Röttsches7646dff2022-02-01 20:53:07 +020015#include "src/ports/SkFontConfigInterface_direct.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "tests/Test.h"
17#include "tools/Resources.h"
Ben Wagner37a06c02018-06-22 21:11:00 +000018
Hal Canary8a001442018-09-19 11:31:27 -040019#include <fontconfig/fontconfig.h>
20
Dominik Röttsches7646dff2022-02-01 20:53:07 +020021namespace {
22
23bool bitmap_compare(const SkBitmap& ref, const SkBitmap& test) {
Ben Wagner37a06c02018-06-22 21:11:00 +000024 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
Dominik Röttsches7646dff2022-02-01 20:53:07 +020036FcConfig* build_fontconfig_with_fontfile(const char* fontFilename) {
Ben Wagner37a06c02018-06-22 21:11:00 +000037 FcConfig* config = FcConfigCreate();
Ben Wagner25e371f2019-04-05 15:50:34 -040038
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.
Dominik Röttsches7646dff2022-02-01 20:53:07 +020042 SkString fontFilePath(reinterpret_cast<const char*>(FcConfigGetSysRoot(config)));
43 fontFilePath += fontFilename;
44 FcConfigAppFontAddFile(config, reinterpret_cast<const FcChar8*>(fontFilePath.c_str()));
Ben Wagner25e371f2019-04-05 15:50:34 -040045
Ben Wagner37a06c02018-06-22 21:11:00 +000046 FcConfigBuildFonts(config);
Dominik Röttsches7646dff2022-02-01 20:53:07 +020047 return config;
48}
49
50bool 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
66DEF_TEST(FontMgrFontConfig, reporter) {
67 FcConfig* config = build_fontconfig_with_fontfile("/fonts/Distortable.ttf");
Ben Wagner37a06c02018-06-22 21:11:00 +000068
69 sk_sp<SkFontMgr> fontMgr(SkFontMgr_New_FontConfig(config));
70 sk_sp<SkTypeface> typeface(fontMgr->legacyMakeTypeface("Distortable", SkFontStyle()));
Ben Wagner25e371f2019-04-05 15:50:34 -040071 if (!typeface) {
72 ERRORF(reporter, "Could not find typeface. FcVersion: %d", FcGetVersion());
73 return;
74 }
Ben Wagner37a06c02018-06-22 21:11:00 +000075
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
Hal Canary3560ea72019-01-08 13:01:58 -050086 SkPaint paint;
87 paint.setColor(SK_ColorGRAY);
Ben Wagner37a06c02018-06-22 21:11:00 +000088
Hal Canary3560ea72019-01-08 13:01:58 -050089 constexpr float kTextSize = 20;
Ben Wagner37a06c02018-06-22 21:11:00 +000090
91 std::unique_ptr<SkStreamAsset> distortableStream(
92 GetResourceAsStream("fonts/Distortable.ttf"));
93 if (!distortableStream) {
94 return;
95 }
96
Ben Wagner37a06c02018-06-22 21:11:00 +000097 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
Hal Canary3560ea72019-01-08 13:01:58 -0500108 SkFont fontStream(
Ben Wagner37a06c02018-06-22 21:11:00 +0000109 fontMgr->makeFromStream(distortableStream->duplicate(),
Hal Canary3560ea72019-01-08 13:01:58 -0500110 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";
Ben Wagner37a06c02018-06-22 21:11:00 +0000119
120 canvasStream.drawColor(SK_ColorWHITE);
Hal Canary3560ea72019-01-08 13:01:58 -0500121 canvasStream.drawString(text, point.fX, point.fY, fontStream, paint);
Ben Wagner37a06c02018-06-22 21:11:00 +0000122
123 canvasClone.drawColor(SK_ColorWHITE);
Hal Canary3560ea72019-01-08 13:01:58 -0500124 canvasClone.drawString(text, point.fX, point.fY, fontClone, paint);
Ben Wagner37a06c02018-06-22 21:11:00 +0000125
126 bool success = bitmap_compare(bitmapStream, bitmapClone);
127 REPORTER_ASSERT(reporter, success);
128 }
Hal Canary8a001442018-09-19 11:31:27 -0400129}
Dominik Röttsches7646dff2022-02-01 20:53:07 +0200130
131DEF_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");
Ben Wagner88ddcb82022-02-07 16:14:04 +0000137 sk_sp<SkFontConfigInterfaceDirect> fciDirect(new SkFontConfigInterfaceDirect(config));
Dominik Röttsches7646dff2022-02-01 20:53:07 +0200138
Ben Wagner1f813e42022-02-09 16:19:11 -0500139 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 };
Dominik Röttsches7646dff2022-02-01 20:53:07 +0200156
Ben Wagner1f813e42022-02-09 16:19:11 -0500157 for (auto&& font_name : family_names) {
158 for (auto&& [weight, highBitsExpectation] : tests) {
Dominik Röttsches7646dff2022-02-01 20:53:07 +0200159 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(
Ben Wagner1f813e42022-02-09 16:19:11 -0500165 font_name, fontStyle, &resultIdentity, &resultFamily, &resultStyle);
Dominik Röttsches7646dff2022-02-01 20:53:07 +0200166
167 REPORTER_ASSERT(reporter, r, "Expecting to find a match result.");
168 REPORTER_ASSERT(
169 reporter,
Ben Wagner1f813e42022-02-09 16:19:11 -0500170 (resultIdentity.fTTCIndex >> 16 > 0) == highBitsExpectation,
Dominik Röttsches7646dff2022-02-01 20:53:07 +0200171 "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,
Ben Wagner1f813e42022-02-09 16:19:11 -0500187 family_from_typeface == SkString(font_name),
Dominik Röttsches7646dff2022-02-01 20:53:07 +0200188 "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}