Ben Wagner | 37a06c0 | 2018-06-22 21:11:00 +0000 | [diff] [blame] | 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 | |
Mike Reed | ac9f0c9 | 2020-12-23 10:11:33 -0500 | [diff] [blame] | 8 | #include "include/core/SkBitmap.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 9 | #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öttsches | 7646dff | 2022-02-01 20:53:07 +0200 | [diff] [blame] | 13 | #include "include/ports/SkFontMgr_FontConfigInterface.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 14 | #include "include/ports/SkFontMgr_fontconfig.h" |
Dominik Röttsches | 7646dff | 2022-02-01 20:53:07 +0200 | [diff] [blame] | 15 | #include "src/ports/SkFontConfigInterface_direct.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 16 | #include "tests/Test.h" |
| 17 | #include "tools/Resources.h" |
Ben Wagner | 37a06c0 | 2018-06-22 21:11:00 +0000 | [diff] [blame] | 18 | |
Hal Canary | 8a00144 | 2018-09-19 11:31:27 -0400 | [diff] [blame] | 19 | #include <fontconfig/fontconfig.h> |
| 20 | |
Dominik Röttsches | 7646dff | 2022-02-01 20:53:07 +0200 | [diff] [blame] | 21 | namespace { |
| 22 | |
| 23 | bool bitmap_compare(const SkBitmap& ref, const SkBitmap& test) { |
Ben Wagner | 37a06c0 | 2018-06-22 21:11:00 +0000 | [diff] [blame] | 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 | |
Dominik Röttsches | 7646dff | 2022-02-01 20:53:07 +0200 | [diff] [blame] | 36 | FcConfig* build_fontconfig_with_fontfile(const char* fontFilename) { |
Ben Wagner | 37a06c0 | 2018-06-22 21:11:00 +0000 | [diff] [blame] | 37 | FcConfig* config = FcConfigCreate(); |
Ben Wagner | 25e371f | 2019-04-05 15:50:34 -0400 | [diff] [blame] | 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. |
Dominik Röttsches | 7646dff | 2022-02-01 20:53:07 +0200 | [diff] [blame] | 42 | SkString fontFilePath(reinterpret_cast<const char*>(FcConfigGetSysRoot(config))); |
| 43 | fontFilePath += fontFilename; |
| 44 | FcConfigAppFontAddFile(config, reinterpret_cast<const FcChar8*>(fontFilePath.c_str())); |
Ben Wagner | 25e371f | 2019-04-05 15:50:34 -0400 | [diff] [blame] | 45 | |
Ben Wagner | 37a06c0 | 2018-06-22 21:11:00 +0000 | [diff] [blame] | 46 | FcConfigBuildFonts(config); |
Dominik Röttsches | 7646dff | 2022-02-01 20:53:07 +0200 | [diff] [blame] | 47 | return config; |
| 48 | } |
| 49 | |
| 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 | |
| 66 | DEF_TEST(FontMgrFontConfig, reporter) { |
| 67 | FcConfig* config = build_fontconfig_with_fontfile("/fonts/Distortable.ttf"); |
Ben Wagner | 37a06c0 | 2018-06-22 21:11:00 +0000 | [diff] [blame] | 68 | |
| 69 | sk_sp<SkFontMgr> fontMgr(SkFontMgr_New_FontConfig(config)); |
| 70 | sk_sp<SkTypeface> typeface(fontMgr->legacyMakeTypeface("Distortable", SkFontStyle())); |
Ben Wagner | 25e371f | 2019-04-05 15:50:34 -0400 | [diff] [blame] | 71 | if (!typeface) { |
| 72 | ERRORF(reporter, "Could not find typeface. FcVersion: %d", FcGetVersion()); |
| 73 | return; |
| 74 | } |
Ben Wagner | 37a06c0 | 2018-06-22 21:11:00 +0000 | [diff] [blame] | 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 | |
Hal Canary | 3560ea7 | 2019-01-08 13:01:58 -0500 | [diff] [blame] | 86 | SkPaint paint; |
| 87 | paint.setColor(SK_ColorGRAY); |
Ben Wagner | 37a06c0 | 2018-06-22 21:11:00 +0000 | [diff] [blame] | 88 | |
Hal Canary | 3560ea7 | 2019-01-08 13:01:58 -0500 | [diff] [blame] | 89 | constexpr float kTextSize = 20; |
Ben Wagner | 37a06c0 | 2018-06-22 21:11:00 +0000 | [diff] [blame] | 90 | |
| 91 | std::unique_ptr<SkStreamAsset> distortableStream( |
| 92 | GetResourceAsStream("fonts/Distortable.ttf")); |
| 93 | if (!distortableStream) { |
| 94 | return; |
| 95 | } |
| 96 | |
Ben Wagner | 37a06c0 | 2018-06-22 21:11:00 +0000 | [diff] [blame] | 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 | |
Hal Canary | 3560ea7 | 2019-01-08 13:01:58 -0500 | [diff] [blame] | 108 | SkFont fontStream( |
Ben Wagner | 37a06c0 | 2018-06-22 21:11:00 +0000 | [diff] [blame] | 109 | fontMgr->makeFromStream(distortableStream->duplicate(), |
Hal Canary | 3560ea7 | 2019-01-08 13:01:58 -0500 | [diff] [blame] | 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"; |
Ben Wagner | 37a06c0 | 2018-06-22 21:11:00 +0000 | [diff] [blame] | 119 | |
| 120 | canvasStream.drawColor(SK_ColorWHITE); |
Hal Canary | 3560ea7 | 2019-01-08 13:01:58 -0500 | [diff] [blame] | 121 | canvasStream.drawString(text, point.fX, point.fY, fontStream, paint); |
Ben Wagner | 37a06c0 | 2018-06-22 21:11:00 +0000 | [diff] [blame] | 122 | |
| 123 | canvasClone.drawColor(SK_ColorWHITE); |
Hal Canary | 3560ea7 | 2019-01-08 13:01:58 -0500 | [diff] [blame] | 124 | canvasClone.drawString(text, point.fX, point.fY, fontClone, paint); |
Ben Wagner | 37a06c0 | 2018-06-22 21:11:00 +0000 | [diff] [blame] | 125 | |
| 126 | bool success = bitmap_compare(bitmapStream, bitmapClone); |
| 127 | REPORTER_ASSERT(reporter, success); |
| 128 | } |
Hal Canary | 8a00144 | 2018-09-19 11:31:27 -0400 | [diff] [blame] | 129 | } |
Dominik Röttsches | 7646dff | 2022-02-01 20:53:07 +0200 | [diff] [blame] | 130 | |
| 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"); |
Ben Wagner | 88ddcb8 | 2022-02-07 16:14:04 +0000 | [diff] [blame] | 137 | sk_sp<SkFontConfigInterfaceDirect> fciDirect(new SkFontConfigInterfaceDirect(config)); |
Dominik Röttsches | 7646dff | 2022-02-01 20:53:07 +0200 | [diff] [blame] | 138 | |
Ben Wagner | 1f813e4 | 2022-02-09 16:19:11 -0500 | [diff] [blame] | 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 | }; |
Dominik Röttsches | 7646dff | 2022-02-01 20:53:07 +0200 | [diff] [blame] | 156 | |
Ben Wagner | 1f813e4 | 2022-02-09 16:19:11 -0500 | [diff] [blame] | 157 | for (auto&& font_name : family_names) { |
| 158 | for (auto&& [weight, highBitsExpectation] : tests) { |
Dominik Röttsches | 7646dff | 2022-02-01 20:53:07 +0200 | [diff] [blame] | 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( |
Ben Wagner | 1f813e4 | 2022-02-09 16:19:11 -0500 | [diff] [blame] | 165 | font_name, fontStyle, &resultIdentity, &resultFamily, &resultStyle); |
Dominik Röttsches | 7646dff | 2022-02-01 20:53:07 +0200 | [diff] [blame] | 166 | |
| 167 | REPORTER_ASSERT(reporter, r, "Expecting to find a match result."); |
| 168 | REPORTER_ASSERT( |
| 169 | reporter, |
Ben Wagner | 1f813e4 | 2022-02-09 16:19:11 -0500 | [diff] [blame] | 170 | (resultIdentity.fTTCIndex >> 16 > 0) == highBitsExpectation, |
Dominik Röttsches | 7646dff | 2022-02-01 20:53:07 +0200 | [diff] [blame] | 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, |
Ben Wagner | 1f813e4 | 2022-02-09 16:19:11 -0500 | [diff] [blame] | 187 | family_from_typeface == SkString(font_name), |
Dominik Röttsches | 7646dff | 2022-02-01 20:53:07 +0200 | [diff] [blame] | 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 | } |