• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/SkColor.h"
11 #include "include/core/SkFont.h"
12 #include "include/core/SkFontArguments.h"
13 #include "include/core/SkFontMgr.h"
14 #include "include/core/SkFontStyle.h"
15 #include "include/core/SkPaint.h"
16 #include "include/core/SkPoint.h"
17 #include "include/core/SkRefCnt.h"
18 #include "include/core/SkScalar.h"
19 #include "include/core/SkStream.h"
20 #include "include/core/SkString.h"
21 #include "include/core/SkTypeface.h"
22 #include "include/core/SkTypes.h"
23 #include "include/ports/SkFontMgr_fontconfig.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 
32 namespace {
33 
bitmap_compare(const SkBitmap & ref,const SkBitmap & test)34 bool 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 
build_fontconfig_with_fontfile(const char * fontFilename)47 FcConfig* 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 
DEF_TEST(FontMgrFontConfig,reporter)63 DEF_TEST(FontMgrFontConfig, reporter) {
64     FcConfig* config = build_fontconfig_with_fontfile("/fonts/Distortable.ttf");
65 
66     sk_sp<SkFontMgr> fontMgr(SkFontMgr_New_FontConfig(config));
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 
83     SkPaint paint;
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));
100         SkFontArguments::VariationPosition::Coordinate
101             coordinates[] = {{tag, styleValue}};
102         SkFontArguments::VariationPosition
103             position = {coordinates, std::size(coordinates)};
104 
105         SkFont fontStream(
106             fontMgr->makeFromStream(distortableStream->duplicate(),
107                                     SkFontArguments().setVariationDesignPosition(position)),
108             kTextSize);
109         fontStream.setEdging(SkFont::Edging::kSubpixelAntiAlias);
110 
111         SkFont fontClone(
112             typeface->makeClone(SkFontArguments().setVariationDesignPosition(position)), kTextSize);
113         fontClone.setEdging(SkFont::Edging::kSubpixelAntiAlias);
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 }
127