Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
FontMgrFontConfigTest.cpp
Go to the documentation of this file.
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
11#include "include/core/SkFont.h"
24#include "tests/Test.h"
25#include "tools/Resources.h"
26
27#include <fontconfig/fontconfig.h>
28
29#include <array>
30#include <memory>
31
32namespace {
33
34bool bitmap_compare(const SkBitmap& ref, const SkBitmap& test) {
35 for (int y = 0; y < test.height(); ++y) {
36 for (int x = 0; x < test.width(); ++x) {
37 SkColor testColor = test.getColor(x, y);
38 SkColor refColor = ref.getColor(x, y);
39 if (refColor != testColor) {
40 return false;
41 }
42 }
43 }
44 return true;
45}
46
47FcConfig* build_fontconfig_with_fontfile(const char* fontFilename) {
48 FcConfig* config = FcConfigCreate();
49
50 // FontConfig may modify the passed path (make absolute or other).
51 FcConfigSetSysRoot(config, reinterpret_cast<const FcChar8*>(GetResourcePath("").c_str()));
52 // FontConfig will lexically compare paths against its version of the sysroot.
53 SkString fontFilePath(reinterpret_cast<const char*>(FcConfigGetSysRoot(config)));
54 fontFilePath += fontFilename;
55 FcConfigAppFontAddFile(config, reinterpret_cast<const FcChar8*>(fontFilePath.c_str()));
56
57 FcConfigBuildFonts(config);
58 return config;
59}
60
61} // namespace
62
63DEF_TEST(FontMgrFontConfig, reporter) {
64 FcConfig* config = build_fontconfig_with_fontfile("/fonts/Distortable.ttf");
65
67 sk_sp<SkTypeface> typeface(fontMgr->legacyMakeTypeface("Distortable", SkFontStyle()));
68 if (!typeface) {
69 ERRORF(reporter, "Could not find typeface. FcVersion: %d", FcGetVersion());
70 return;
71 }
72
73 SkBitmap bitmapStream;
74 bitmapStream.allocN32Pixels(64, 64);
75 SkCanvas canvasStream(bitmapStream);
76 canvasStream.drawColor(SK_ColorWHITE);
77
78 SkBitmap bitmapClone;
79 bitmapClone.allocN32Pixels(64, 64);
80 SkCanvas canvasClone(bitmapClone);
81 canvasStream.drawColor(SK_ColorWHITE);
82
84 paint.setColor(SK_ColorGRAY);
85
86 constexpr float kTextSize = 20;
87
88 std::unique_ptr<SkStreamAsset> distortableStream(
89 GetResourceAsStream("fonts/Distortable.ttf"));
90 if (!distortableStream) {
91 return;
92 }
93
94 SkPoint point = SkPoint::Make(20.0f, 20.0f);
95 SkFourByteTag tag = SkSetFourByteTag('w', 'g', 'h', 't');
96
97 for (int i = 0; i < 10; ++i) {
98 SkScalar styleValue =
99 SkDoubleToScalar(0.5 + i * ((2.0 - 0.5) / 10));
101 coordinates[] = {{tag, styleValue}};
103 position = {coordinates, std::size(coordinates)};
104
105 SkFont fontStream(
106 fontMgr->makeFromStream(distortableStream->duplicate(),
107 SkFontArguments().setVariationDesignPosition(position)),
108 kTextSize);
110
111 SkFont fontClone(
112 typeface->makeClone(SkFontArguments().setVariationDesignPosition(position)), kTextSize);
114
115 constexpr char text[] = "abc";
116
117 canvasStream.drawColor(SK_ColorWHITE);
118 canvasStream.drawString(text, point.fX, point.fY, fontStream, paint);
119
120 canvasClone.drawColor(SK_ColorWHITE);
121 canvasClone.drawString(text, point.fX, point.fY, fontClone, paint);
122
123 bool success = bitmap_compare(bitmapStream, bitmapClone);
124 REPORTER_ASSERT(reporter, success);
125 }
126}
static bool bitmap_compare(const SkBitmap &ref, const SkBitmap &test)
reporter
std::unique_ptr< SkStreamAsset > GetResourceAsStream(const char *resource, bool useFileStream)
Definition Resources.cpp:31
SkString GetResourcePath(const char *resource)
Definition Resources.cpp:23
uint32_t SkColor
Definition SkColor.h:37
constexpr SkColor SK_ColorGRAY
Definition SkColor.h:113
constexpr SkColor SK_ColorWHITE
Definition SkColor.h:122
SK_API sk_sp< SkFontMgr > SkFontMgr_New_FontConfig(FcConfig *fc)
#define SkDoubleToScalar(x)
Definition SkScalar.h:64
uint32_t SkFourByteTag
Definition SkTypes.h:166
static constexpr SkFourByteTag SkSetFourByteTag(char a, char b, char c, char d)
Definition SkTypes.h:167
#define DEF_TEST(name, reporter)
Definition Test.h:312
#define REPORTER_ASSERT(r, cond,...)
Definition Test.h:286
#define ERRORF(r,...)
Definition Test.h:293
SkColor getColor(int x, int y) const
Definition SkBitmap.h:874
void allocN32Pixels(int width, int height, bool isOpaque=false)
Definition SkBitmap.cpp:232
void drawColor(SkColor color, SkBlendMode mode=SkBlendMode::kSrcOver)
Definition SkCanvas.h:1182
void drawString(const char str[], SkScalar x, SkScalar y, const SkFont &font, const SkPaint &paint)
Definition SkCanvas.h:1803
sk_sp< SkTypeface > makeFromStream(std::unique_ptr< SkStreamAsset >, int ttcIndex=0) const
sk_sp< SkTypeface > legacyMakeTypeface(const char familyName[], SkFontStyle style) const
void setEdging(Edging edging)
Definition SkFont.cpp:121
@ kSubpixelAntiAlias
glyph positioned in pixel using transparency
const Paint & paint
sk_sp< SkFontMgr > fontMgr
Definition examples.cpp:32
float SkScalar
Definition extension.cpp:12
std::u16string text
double y
double x
float fX
x-axis value
static constexpr SkPoint Make(float x, float y)
float fY
y-axis value