• 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/SkCanvas.h"
9 #include "include/core/SkFont.h"
10 #include "include/core/SkFontMgr.h"
11 #include "include/core/SkTypeface.h"
12 #include "include/ports/SkFontMgr_fontconfig.h"
13 #include "tests/Test.h"
14 #include "tools/Resources.h"
15 
16 #include <fontconfig/fontconfig.h>
17 
bitmap_compare(const SkBitmap & ref,const SkBitmap & test)18 static bool bitmap_compare(const SkBitmap& ref, const SkBitmap& test) {
19     for (int y = 0; y < test.height(); ++y) {
20         for (int x = 0; x < test.width(); ++x) {
21             SkColor testColor = test.getColor(x, y);
22             SkColor refColor = ref.getColor(x, y);
23             if (refColor != testColor) {
24                 return false;
25             }
26         }
27     }
28     return true;
29 }
30 
DEF_TEST(FontMgrFontConfig,reporter)31 DEF_TEST(FontMgrFontConfig, reporter) {
32     FcConfig* config = FcConfigCreate();
33 
34     // FontConfig may modify the passed path (make absolute or other).
35     FcConfigSetSysRoot(config, reinterpret_cast<const FcChar8*>(GetResourcePath("").c_str()));
36     // FontConfig will lexically compare paths against its version of the sysroot.
37     SkString distortablePath(reinterpret_cast<const char*>(FcConfigGetSysRoot(config)));
38     distortablePath += "/fonts/Distortable.ttf";
39     FcConfigAppFontAddFile(config, reinterpret_cast<const FcChar8*>(distortablePath.c_str()));
40 
41     FcConfigBuildFonts(config);
42 
43     sk_sp<SkFontMgr> fontMgr(SkFontMgr_New_FontConfig(config));
44     sk_sp<SkTypeface> typeface(fontMgr->legacyMakeTypeface("Distortable", SkFontStyle()));
45     if (!typeface) {
46         ERRORF(reporter, "Could not find typeface. FcVersion: %d", FcGetVersion());
47         return;
48     }
49 
50     SkBitmap bitmapStream;
51     bitmapStream.allocN32Pixels(64, 64);
52     SkCanvas canvasStream(bitmapStream);
53     canvasStream.drawColor(SK_ColorWHITE);
54 
55     SkBitmap bitmapClone;
56     bitmapClone.allocN32Pixels(64, 64);
57     SkCanvas canvasClone(bitmapClone);
58     canvasStream.drawColor(SK_ColorWHITE);
59 
60     SkPaint paint;
61     paint.setColor(SK_ColorGRAY);
62 
63     constexpr float kTextSize = 20;
64 
65     std::unique_ptr<SkStreamAsset> distortableStream(
66         GetResourceAsStream("fonts/Distortable.ttf"));
67     if (!distortableStream) {
68         return;
69     }
70 
71     SkPoint point = SkPoint::Make(20.0f, 20.0f);
72     SkFourByteTag tag = SkSetFourByteTag('w', 'g', 'h', 't');
73 
74     for (int i = 0; i < 10; ++i) {
75         SkScalar styleValue =
76             SkDoubleToScalar(0.5 + i * ((2.0 - 0.5) / 10));
77         SkFontArguments::VariationPosition::Coordinate
78             coordinates[] = {{tag, styleValue}};
79         SkFontArguments::VariationPosition
80             position = {coordinates, SK_ARRAY_COUNT(coordinates)};
81 
82         SkFont fontStream(
83             fontMgr->makeFromStream(distortableStream->duplicate(),
84                                     SkFontArguments().setVariationDesignPosition(position)),
85             kTextSize);
86         fontStream.setEdging(SkFont::Edging::kSubpixelAntiAlias);
87 
88         SkFont fontClone(
89             typeface->makeClone(SkFontArguments().setVariationDesignPosition(position)), kTextSize);
90         fontClone.setEdging(SkFont::Edging::kSubpixelAntiAlias);
91 
92         constexpr char text[] = "abc";
93 
94         canvasStream.drawColor(SK_ColorWHITE);
95         canvasStream.drawString(text, point.fX, point.fY, fontStream, paint);
96 
97         canvasClone.drawColor(SK_ColorWHITE);
98         canvasClone.drawString(text, point.fX, point.fY, fontClone, paint);
99 
100         bool success = bitmap_compare(bitmapStream, bitmapClone);
101         REPORTER_ASSERT(reporter, success);
102     }
103 }
104